]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
PR c++/88538 - braced-init-list in template-argument-list.
[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_CHAR16:
952 case RID_CHAR32:
953 case RID_WCHAR:
954 case RID_BOOL:
955 case RID_SHORT:
956 case RID_INT:
957 case RID_LONG:
958 case RID_SIGNED:
959 case RID_UNSIGNED:
960 case RID_FLOAT:
961 case RID_DOUBLE:
962 case RID_VOID:
963 /* GNU extensions. */
964 case RID_ATTRIBUTE:
965 case RID_TYPEOF:
966 /* C++0x extensions. */
967 case RID_DECLTYPE:
968 case RID_UNDERLYING_TYPE:
969 case RID_CONSTEXPR:
970 return true;
971
972 default:
973 if (keyword >= RID_FIRST_INT_N
974 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
975 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
976 return true;
977 return false;
978 }
979 }
980
981 /* Return true if the next token is a keyword for a decl-specifier. */
982
983 static bool
984 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
985 {
986 cp_token *token;
987
988 token = cp_lexer_peek_token (lexer);
989 return cp_keyword_starts_decl_specifier_p (token->keyword);
990 }
991
992 /* Returns TRUE iff the token T begins a decltype type. */
993
994 static bool
995 token_is_decltype (cp_token *t)
996 {
997 return (t->keyword == RID_DECLTYPE
998 || t->type == CPP_DECLTYPE);
999 }
1000
1001 /* Returns TRUE iff the next token begins a decltype type. */
1002
1003 static bool
1004 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1005 {
1006 cp_token *t = cp_lexer_peek_token (lexer);
1007 return token_is_decltype (t);
1008 }
1009
1010 /* Called when processing a token with tree_check_value; perform or defer the
1011 associated checks and return the value. */
1012
1013 static tree
1014 saved_checks_value (struct tree_check *check_value)
1015 {
1016 /* Perform any access checks that were deferred. */
1017 vec<deferred_access_check, va_gc> *checks;
1018 deferred_access_check *chk;
1019 checks = check_value->checks;
1020 if (checks)
1021 {
1022 int i;
1023 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1024 perform_or_defer_access_check (chk->binfo,
1025 chk->decl,
1026 chk->diag_decl, tf_warning_or_error);
1027 }
1028 /* Return the stored value. */
1029 return check_value->value;
1030 }
1031
1032 /* Return a pointer to the Nth token in the token stream. If N is 1,
1033 then this is precisely equivalent to cp_lexer_peek_token (except
1034 that it is not inline). One would like to disallow that case, but
1035 there is one case (cp_parser_nth_token_starts_template_id) where
1036 the caller passes a variable for N and it might be 1. */
1037
1038 static cp_token *
1039 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1040 {
1041 cp_token *token;
1042
1043 /* N is 1-based, not zero-based. */
1044 gcc_assert (n > 0);
1045
1046 if (cp_lexer_debugging_p (lexer))
1047 fprintf (cp_lexer_debug_stream,
1048 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1049
1050 --n;
1051 token = lexer->next_token;
1052 gcc_assert (!n || token != &eof_token);
1053 while (n != 0)
1054 {
1055 ++token;
1056 if (token == lexer->last_token)
1057 {
1058 token = &eof_token;
1059 break;
1060 }
1061
1062 if (!token->purged_p)
1063 --n;
1064 }
1065
1066 if (cp_lexer_debugging_p (lexer))
1067 {
1068 cp_lexer_print_token (cp_lexer_debug_stream, token);
1069 putc ('\n', cp_lexer_debug_stream);
1070 }
1071
1072 return token;
1073 }
1074
1075 /* Return the next token, and advance the lexer's next_token pointer
1076 to point to the next non-purged token. */
1077
1078 static cp_token *
1079 cp_lexer_consume_token (cp_lexer* lexer)
1080 {
1081 cp_token *token = lexer->next_token;
1082
1083 gcc_assert (token != &eof_token);
1084 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1085
1086 do
1087 {
1088 lexer->next_token++;
1089 if (lexer->next_token == lexer->last_token)
1090 {
1091 lexer->next_token = &eof_token;
1092 break;
1093 }
1094
1095 }
1096 while (lexer->next_token->purged_p);
1097
1098 cp_lexer_set_source_position_from_token (token);
1099
1100 /* Provide debugging output. */
1101 if (cp_lexer_debugging_p (lexer))
1102 {
1103 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1104 cp_lexer_print_token (cp_lexer_debug_stream, token);
1105 putc ('\n', cp_lexer_debug_stream);
1106 }
1107
1108 return token;
1109 }
1110
1111 /* Permanently remove the next token from the token stream, and
1112 advance the next_token pointer to refer to the next non-purged
1113 token. */
1114
1115 static void
1116 cp_lexer_purge_token (cp_lexer *lexer)
1117 {
1118 cp_token *tok = lexer->next_token;
1119
1120 gcc_assert (tok != &eof_token);
1121 tok->purged_p = true;
1122 tok->location = UNKNOWN_LOCATION;
1123 tok->u.value = NULL_TREE;
1124 tok->keyword = RID_MAX;
1125
1126 do
1127 {
1128 tok++;
1129 if (tok == lexer->last_token)
1130 {
1131 tok = &eof_token;
1132 break;
1133 }
1134 }
1135 while (tok->purged_p);
1136 lexer->next_token = tok;
1137 }
1138
1139 /* Permanently remove all tokens after TOK, up to, but not
1140 including, the token that will be returned next by
1141 cp_lexer_peek_token. */
1142
1143 static void
1144 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1145 {
1146 cp_token *peek = lexer->next_token;
1147
1148 if (peek == &eof_token)
1149 peek = lexer->last_token;
1150
1151 gcc_assert (tok < peek);
1152
1153 for ( tok += 1; tok != peek; tok += 1)
1154 {
1155 tok->purged_p = true;
1156 tok->location = UNKNOWN_LOCATION;
1157 tok->u.value = NULL_TREE;
1158 tok->keyword = RID_MAX;
1159 }
1160 }
1161
1162 /* Begin saving tokens. All tokens consumed after this point will be
1163 preserved. */
1164
1165 static void
1166 cp_lexer_save_tokens (cp_lexer* lexer)
1167 {
1168 /* Provide debugging output. */
1169 if (cp_lexer_debugging_p (lexer))
1170 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1171
1172 lexer->saved_tokens.safe_push (lexer->next_token);
1173 }
1174
1175 /* Commit to the portion of the token stream most recently saved. */
1176
1177 static void
1178 cp_lexer_commit_tokens (cp_lexer* lexer)
1179 {
1180 /* Provide debugging output. */
1181 if (cp_lexer_debugging_p (lexer))
1182 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1183
1184 lexer->saved_tokens.pop ();
1185 }
1186
1187 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1188 to the token stream. Stop saving tokens. */
1189
1190 static void
1191 cp_lexer_rollback_tokens (cp_lexer* lexer)
1192 {
1193 /* Provide debugging output. */
1194 if (cp_lexer_debugging_p (lexer))
1195 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1196
1197 lexer->next_token = lexer->saved_tokens.pop ();
1198 }
1199
1200 /* RAII wrapper around the above functions, with sanity checking. Creating
1201 a variable saves tokens, which are committed when the variable is
1202 destroyed unless they are explicitly rolled back by calling the rollback
1203 member function. */
1204
1205 struct saved_token_sentinel
1206 {
1207 cp_lexer *lexer;
1208 unsigned len;
1209 bool commit;
1210 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1211 {
1212 len = lexer->saved_tokens.length ();
1213 cp_lexer_save_tokens (lexer);
1214 }
1215 void rollback ()
1216 {
1217 cp_lexer_rollback_tokens (lexer);
1218 commit = false;
1219 }
1220 ~saved_token_sentinel()
1221 {
1222 if (commit)
1223 cp_lexer_commit_tokens (lexer);
1224 gcc_assert (lexer->saved_tokens.length () == len);
1225 }
1226 };
1227
1228 /* Print a representation of the TOKEN on the STREAM. */
1229
1230 static void
1231 cp_lexer_print_token (FILE * stream, cp_token *token)
1232 {
1233 /* We don't use cpp_type2name here because the parser defines
1234 a few tokens of its own. */
1235 static const char *const token_names[] = {
1236 /* cpplib-defined token types */
1237 #define OP(e, s) #e,
1238 #define TK(e, s) #e,
1239 TTYPE_TABLE
1240 #undef OP
1241 #undef TK
1242 /* C++ parser token types - see "Manifest constants", above. */
1243 "KEYWORD",
1244 "TEMPLATE_ID",
1245 "NESTED_NAME_SPECIFIER",
1246 };
1247
1248 /* For some tokens, print the associated data. */
1249 switch (token->type)
1250 {
1251 case CPP_KEYWORD:
1252 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1253 For example, `struct' is mapped to an INTEGER_CST. */
1254 if (!identifier_p (token->u.value))
1255 break;
1256 /* fall through */
1257 case CPP_NAME:
1258 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1259 break;
1260
1261 case CPP_STRING:
1262 case CPP_STRING16:
1263 case CPP_STRING32:
1264 case CPP_WSTRING:
1265 case CPP_UTF8STRING:
1266 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1267 break;
1268
1269 case CPP_NUMBER:
1270 print_generic_expr (stream, token->u.value);
1271 break;
1272
1273 default:
1274 /* If we have a name for the token, print it out. Otherwise, we
1275 simply give the numeric code. */
1276 if (token->type < ARRAY_SIZE(token_names))
1277 fputs (token_names[token->type], stream);
1278 else
1279 fprintf (stream, "[%d]", token->type);
1280 break;
1281 }
1282 }
1283
1284 DEBUG_FUNCTION void
1285 debug (cp_token &ref)
1286 {
1287 cp_lexer_print_token (stderr, &ref);
1288 fprintf (stderr, "\n");
1289 }
1290
1291 DEBUG_FUNCTION void
1292 debug (cp_token *ptr)
1293 {
1294 if (ptr)
1295 debug (*ptr);
1296 else
1297 fprintf (stderr, "<nil>\n");
1298 }
1299
1300
1301 /* Start emitting debugging information. */
1302
1303 static void
1304 cp_lexer_start_debugging (cp_lexer* lexer)
1305 {
1306 if (!LEXER_DEBUGGING_ENABLED_P)
1307 fatal_error (input_location,
1308 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1309
1310 lexer->debugging_p = true;
1311 cp_lexer_debug_stream = stderr;
1312 }
1313
1314 /* Stop emitting debugging information. */
1315
1316 static void
1317 cp_lexer_stop_debugging (cp_lexer* lexer)
1318 {
1319 if (!LEXER_DEBUGGING_ENABLED_P)
1320 fatal_error (input_location,
1321 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1322
1323 lexer->debugging_p = false;
1324 cp_lexer_debug_stream = NULL;
1325 }
1326
1327 /* Create a new cp_token_cache, representing a range of tokens. */
1328
1329 static cp_token_cache *
1330 cp_token_cache_new (cp_token *first, cp_token *last)
1331 {
1332 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1333 cache->first = first;
1334 cache->last = last;
1335 return cache;
1336 }
1337
1338 /* Diagnose if #pragma omp declare simd isn't followed immediately
1339 by function declaration or definition. */
1340
1341 static inline void
1342 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1343 {
1344 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1345 {
1346 error ("%<#pragma omp declare simd%> not immediately followed by "
1347 "function declaration or definition");
1348 parser->omp_declare_simd = NULL;
1349 }
1350 }
1351
1352 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1353 and put that into "omp declare simd" attribute. */
1354
1355 static inline void
1356 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1357 {
1358 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1359 {
1360 if (fndecl == error_mark_node)
1361 {
1362 parser->omp_declare_simd = NULL;
1363 return;
1364 }
1365 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1366 {
1367 cp_ensure_no_omp_declare_simd (parser);
1368 return;
1369 }
1370 }
1371 }
1372
1373 /* Diagnose if #pragma acc routine isn't followed immediately by function
1374 declaration or definition. */
1375
1376 static inline void
1377 cp_ensure_no_oacc_routine (cp_parser *parser)
1378 {
1379 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1380 {
1381 error_at (parser->oacc_routine->loc,
1382 "%<#pragma acc routine%> not immediately followed by "
1383 "function declaration or definition");
1384 parser->oacc_routine = NULL;
1385 }
1386 }
1387 \f
1388 /* Decl-specifiers. */
1389
1390 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1391
1392 static void
1393 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1394 {
1395 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1396 }
1397
1398 /* Declarators. */
1399
1400 /* Nothing other than the parser should be creating declarators;
1401 declarators are a semi-syntactic representation of C++ entities.
1402 Other parts of the front end that need to create entities (like
1403 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1404
1405 static cp_declarator *make_call_declarator
1406 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1407 static cp_declarator *make_array_declarator
1408 (cp_declarator *, tree);
1409 static cp_declarator *make_pointer_declarator
1410 (cp_cv_quals, cp_declarator *, tree);
1411 static cp_declarator *make_reference_declarator
1412 (cp_cv_quals, cp_declarator *, bool, tree);
1413 static cp_declarator *make_ptrmem_declarator
1414 (cp_cv_quals, tree, cp_declarator *, tree);
1415
1416 /* An erroneous declarator. */
1417 static cp_declarator *cp_error_declarator;
1418
1419 /* The obstack on which declarators and related data structures are
1420 allocated. */
1421 static struct obstack declarator_obstack;
1422
1423 /* Alloc BYTES from the declarator memory pool. */
1424
1425 static inline void *
1426 alloc_declarator (size_t bytes)
1427 {
1428 return obstack_alloc (&declarator_obstack, bytes);
1429 }
1430
1431 /* Allocate a declarator of the indicated KIND. Clear fields that are
1432 common to all declarators. */
1433
1434 static cp_declarator *
1435 make_declarator (cp_declarator_kind kind)
1436 {
1437 cp_declarator *declarator;
1438
1439 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1440 declarator->kind = kind;
1441 declarator->parenthesized = UNKNOWN_LOCATION;
1442 declarator->attributes = NULL_TREE;
1443 declarator->std_attributes = NULL_TREE;
1444 declarator->declarator = NULL;
1445 declarator->parameter_pack_p = false;
1446 declarator->id_loc = UNKNOWN_LOCATION;
1447
1448 return declarator;
1449 }
1450
1451 /* Make a declarator for a generalized identifier. If
1452 QUALIFYING_SCOPE is non-NULL, the identifier is
1453 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1454 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1455 is, if any. */
1456
1457 static cp_declarator *
1458 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1459 special_function_kind sfk, location_t id_location)
1460 {
1461 cp_declarator *declarator;
1462
1463 /* It is valid to write:
1464
1465 class C { void f(); };
1466 typedef C D;
1467 void D::f();
1468
1469 The standard is not clear about whether `typedef const C D' is
1470 legal; as of 2002-09-15 the committee is considering that
1471 question. EDG 3.0 allows that syntax. Therefore, we do as
1472 well. */
1473 if (qualifying_scope && TYPE_P (qualifying_scope))
1474 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1475
1476 gcc_assert (identifier_p (unqualified_name)
1477 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1478 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1479
1480 declarator = make_declarator (cdk_id);
1481 declarator->u.id.qualifying_scope = qualifying_scope;
1482 declarator->u.id.unqualified_name = unqualified_name;
1483 declarator->u.id.sfk = sfk;
1484 declarator->id_loc = id_location;
1485
1486 return declarator;
1487 }
1488
1489 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1490 of modifiers such as const or volatile to apply to the pointer
1491 type, represented as identifiers. ATTRIBUTES represent the attributes that
1492 appertain to the pointer or reference. */
1493
1494 cp_declarator *
1495 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1496 tree attributes)
1497 {
1498 cp_declarator *declarator;
1499
1500 declarator = make_declarator (cdk_pointer);
1501 declarator->declarator = target;
1502 declarator->u.pointer.qualifiers = cv_qualifiers;
1503 declarator->u.pointer.class_type = NULL_TREE;
1504 if (target)
1505 {
1506 declarator->id_loc = target->id_loc;
1507 declarator->parameter_pack_p = target->parameter_pack_p;
1508 target->parameter_pack_p = false;
1509 }
1510 else
1511 declarator->parameter_pack_p = false;
1512
1513 declarator->std_attributes = attributes;
1514
1515 return declarator;
1516 }
1517
1518 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1519 represent the attributes that appertain to the pointer or
1520 reference. */
1521
1522 cp_declarator *
1523 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1524 bool rvalue_ref, tree attributes)
1525 {
1526 cp_declarator *declarator;
1527
1528 declarator = make_declarator (cdk_reference);
1529 declarator->declarator = target;
1530 declarator->u.reference.qualifiers = cv_qualifiers;
1531 declarator->u.reference.rvalue_ref = rvalue_ref;
1532 if (target)
1533 {
1534 declarator->id_loc = target->id_loc;
1535 declarator->parameter_pack_p = target->parameter_pack_p;
1536 target->parameter_pack_p = false;
1537 }
1538 else
1539 declarator->parameter_pack_p = false;
1540
1541 declarator->std_attributes = attributes;
1542
1543 return declarator;
1544 }
1545
1546 /* Like make_pointer_declarator -- but for a pointer to a non-static
1547 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1548 appertain to the pointer or reference. */
1549
1550 cp_declarator *
1551 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1552 cp_declarator *pointee,
1553 tree attributes)
1554 {
1555 cp_declarator *declarator;
1556
1557 declarator = make_declarator (cdk_ptrmem);
1558 declarator->declarator = pointee;
1559 declarator->u.pointer.qualifiers = cv_qualifiers;
1560 declarator->u.pointer.class_type = class_type;
1561
1562 if (pointee)
1563 {
1564 declarator->parameter_pack_p = pointee->parameter_pack_p;
1565 pointee->parameter_pack_p = false;
1566 }
1567 else
1568 declarator->parameter_pack_p = false;
1569
1570 declarator->std_attributes = attributes;
1571
1572 return declarator;
1573 }
1574
1575 /* Make a declarator for the function given by TARGET, with the
1576 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1577 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1578 indicates what exceptions can be thrown. */
1579
1580 cp_declarator *
1581 make_call_declarator (cp_declarator *target,
1582 tree parms,
1583 cp_cv_quals cv_qualifiers,
1584 cp_virt_specifiers virt_specifiers,
1585 cp_ref_qualifier ref_qualifier,
1586 tree tx_qualifier,
1587 tree exception_specification,
1588 tree late_return_type,
1589 tree requires_clause)
1590 {
1591 cp_declarator *declarator;
1592
1593 declarator = make_declarator (cdk_function);
1594 declarator->declarator = target;
1595 declarator->u.function.parameters = parms;
1596 declarator->u.function.qualifiers = cv_qualifiers;
1597 declarator->u.function.virt_specifiers = virt_specifiers;
1598 declarator->u.function.ref_qualifier = ref_qualifier;
1599 declarator->u.function.tx_qualifier = tx_qualifier;
1600 declarator->u.function.exception_specification = exception_specification;
1601 declarator->u.function.late_return_type = late_return_type;
1602 declarator->u.function.requires_clause = requires_clause;
1603 if (target)
1604 {
1605 declarator->id_loc = target->id_loc;
1606 declarator->parameter_pack_p = target->parameter_pack_p;
1607 target->parameter_pack_p = false;
1608 }
1609 else
1610 declarator->parameter_pack_p = false;
1611
1612 return declarator;
1613 }
1614
1615 /* Make a declarator for an array of BOUNDS elements, each of which is
1616 defined by ELEMENT. */
1617
1618 cp_declarator *
1619 make_array_declarator (cp_declarator *element, tree bounds)
1620 {
1621 cp_declarator *declarator;
1622
1623 declarator = make_declarator (cdk_array);
1624 declarator->declarator = element;
1625 declarator->u.array.bounds = bounds;
1626 if (element)
1627 {
1628 declarator->id_loc = element->id_loc;
1629 declarator->parameter_pack_p = element->parameter_pack_p;
1630 element->parameter_pack_p = false;
1631 }
1632 else
1633 declarator->parameter_pack_p = false;
1634
1635 return declarator;
1636 }
1637
1638 /* Determine whether the declarator we've seen so far can be a
1639 parameter pack, when followed by an ellipsis. */
1640 static bool
1641 declarator_can_be_parameter_pack (cp_declarator *declarator)
1642 {
1643 if (declarator && declarator->parameter_pack_p)
1644 /* We already saw an ellipsis. */
1645 return false;
1646
1647 /* Search for a declarator name, or any other declarator that goes
1648 after the point where the ellipsis could appear in a parameter
1649 pack. If we find any of these, then this declarator can not be
1650 made into a parameter pack. */
1651 bool found = false;
1652 while (declarator && !found)
1653 {
1654 switch ((int)declarator->kind)
1655 {
1656 case cdk_id:
1657 case cdk_array:
1658 case cdk_decomp:
1659 found = true;
1660 break;
1661
1662 case cdk_error:
1663 return true;
1664
1665 default:
1666 declarator = declarator->declarator;
1667 break;
1668 }
1669 }
1670
1671 return !found;
1672 }
1673
1674 cp_parameter_declarator *no_parameters;
1675
1676 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1677 DECLARATOR and DEFAULT_ARGUMENT. */
1678
1679 cp_parameter_declarator *
1680 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1681 cp_declarator *declarator,
1682 tree default_argument,
1683 location_t loc,
1684 bool template_parameter_pack_p = false)
1685 {
1686 cp_parameter_declarator *parameter;
1687
1688 parameter = ((cp_parameter_declarator *)
1689 alloc_declarator (sizeof (cp_parameter_declarator)));
1690 parameter->next = NULL;
1691 if (decl_specifiers)
1692 parameter->decl_specifiers = *decl_specifiers;
1693 else
1694 clear_decl_specs (&parameter->decl_specifiers);
1695 parameter->declarator = declarator;
1696 parameter->default_argument = default_argument;
1697 parameter->template_parameter_pack_p = template_parameter_pack_p;
1698 parameter->loc = loc;
1699
1700 return parameter;
1701 }
1702
1703 /* Returns true iff DECLARATOR is a declaration for a function. */
1704
1705 static bool
1706 function_declarator_p (const cp_declarator *declarator)
1707 {
1708 while (declarator)
1709 {
1710 if (declarator->kind == cdk_function
1711 && declarator->declarator->kind == cdk_id)
1712 return true;
1713 if (declarator->kind == cdk_id
1714 || declarator->kind == cdk_decomp
1715 || declarator->kind == cdk_error)
1716 return false;
1717 declarator = declarator->declarator;
1718 }
1719 return false;
1720 }
1721
1722 /* The parser. */
1723
1724 /* Overview
1725 --------
1726
1727 A cp_parser parses the token stream as specified by the C++
1728 grammar. Its job is purely parsing, not semantic analysis. For
1729 example, the parser breaks the token stream into declarators,
1730 expressions, statements, and other similar syntactic constructs.
1731 It does not check that the types of the expressions on either side
1732 of an assignment-statement are compatible, or that a function is
1733 not declared with a parameter of type `void'.
1734
1735 The parser invokes routines elsewhere in the compiler to perform
1736 semantic analysis and to build up the abstract syntax tree for the
1737 code processed.
1738
1739 The parser (and the template instantiation code, which is, in a
1740 way, a close relative of parsing) are the only parts of the
1741 compiler that should be calling push_scope and pop_scope, or
1742 related functions. The parser (and template instantiation code)
1743 keeps track of what scope is presently active; everything else
1744 should simply honor that. (The code that generates static
1745 initializers may also need to set the scope, in order to check
1746 access control correctly when emitting the initializers.)
1747
1748 Methodology
1749 -----------
1750
1751 The parser is of the standard recursive-descent variety. Upcoming
1752 tokens in the token stream are examined in order to determine which
1753 production to use when parsing a non-terminal. Some C++ constructs
1754 require arbitrary look ahead to disambiguate. For example, it is
1755 impossible, in the general case, to tell whether a statement is an
1756 expression or declaration without scanning the entire statement.
1757 Therefore, the parser is capable of "parsing tentatively." When the
1758 parser is not sure what construct comes next, it enters this mode.
1759 Then, while we attempt to parse the construct, the parser queues up
1760 error messages, rather than issuing them immediately, and saves the
1761 tokens it consumes. If the construct is parsed successfully, the
1762 parser "commits", i.e., it issues any queued error messages and
1763 the tokens that were being preserved are permanently discarded.
1764 If, however, the construct is not parsed successfully, the parser
1765 rolls back its state completely so that it can resume parsing using
1766 a different alternative.
1767
1768 Future Improvements
1769 -------------------
1770
1771 The performance of the parser could probably be improved substantially.
1772 We could often eliminate the need to parse tentatively by looking ahead
1773 a little bit. In some places, this approach might not entirely eliminate
1774 the need to parse tentatively, but it might still speed up the average
1775 case. */
1776
1777 /* Flags that are passed to some parsing functions. These values can
1778 be bitwise-ored together. */
1779
1780 enum
1781 {
1782 /* No flags. */
1783 CP_PARSER_FLAGS_NONE = 0x0,
1784 /* The construct is optional. If it is not present, then no error
1785 should be issued. */
1786 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1787 /* When parsing a type-specifier, treat user-defined type-names
1788 as non-type identifiers. */
1789 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1790 /* When parsing a type-specifier, do not try to parse a class-specifier
1791 or enum-specifier. */
1792 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1793 /* When parsing a decl-specifier-seq, only allow type-specifier or
1794 constexpr. */
1795 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1796 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1797 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1798 /* When parsing a decl-specifier-seq, allow missing typename. */
1799 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20
1800 };
1801
1802 /* This type is used for parameters and variables which hold
1803 combinations of the above flags. */
1804 typedef int cp_parser_flags;
1805
1806 /* The different kinds of declarators we want to parse. */
1807
1808 enum cp_parser_declarator_kind
1809 {
1810 /* We want an abstract declarator. */
1811 CP_PARSER_DECLARATOR_ABSTRACT,
1812 /* We want a named declarator. */
1813 CP_PARSER_DECLARATOR_NAMED,
1814 /* We don't mind, but the name must be an unqualified-id. */
1815 CP_PARSER_DECLARATOR_EITHER
1816 };
1817
1818 /* The precedence values used to parse binary expressions. The minimum value
1819 of PREC must be 1, because zero is reserved to quickly discriminate
1820 binary operators from other tokens. */
1821
1822 enum cp_parser_prec
1823 {
1824 PREC_NOT_OPERATOR,
1825 PREC_LOGICAL_OR_EXPRESSION,
1826 PREC_LOGICAL_AND_EXPRESSION,
1827 PREC_INCLUSIVE_OR_EXPRESSION,
1828 PREC_EXCLUSIVE_OR_EXPRESSION,
1829 PREC_AND_EXPRESSION,
1830 PREC_EQUALITY_EXPRESSION,
1831 PREC_RELATIONAL_EXPRESSION,
1832 PREC_SHIFT_EXPRESSION,
1833 PREC_ADDITIVE_EXPRESSION,
1834 PREC_MULTIPLICATIVE_EXPRESSION,
1835 PREC_PM_EXPRESSION,
1836 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1837 };
1838
1839 /* A mapping from a token type to a corresponding tree node type, with a
1840 precedence value. */
1841
1842 struct cp_parser_binary_operations_map_node
1843 {
1844 /* The token type. */
1845 enum cpp_ttype token_type;
1846 /* The corresponding tree code. */
1847 enum tree_code tree_type;
1848 /* The precedence of this operator. */
1849 enum cp_parser_prec prec;
1850 };
1851
1852 struct cp_parser_expression_stack_entry
1853 {
1854 /* Left hand side of the binary operation we are currently
1855 parsing. */
1856 cp_expr lhs;
1857 /* Original tree code for left hand side, if it was a binary
1858 expression itself (used for -Wparentheses). */
1859 enum tree_code lhs_type;
1860 /* Tree code for the binary operation we are parsing. */
1861 enum tree_code tree_type;
1862 /* Precedence of the binary operation we are parsing. */
1863 enum cp_parser_prec prec;
1864 /* Location of the binary operation we are parsing. */
1865 location_t loc;
1866 };
1867
1868 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1869 entries because precedence levels on the stack are monotonically
1870 increasing. */
1871 typedef struct cp_parser_expression_stack_entry
1872 cp_parser_expression_stack[NUM_PREC_VALUES];
1873
1874 /* Prototypes. */
1875
1876 /* Constructors and destructors. */
1877
1878 static cp_parser_context *cp_parser_context_new
1879 (cp_parser_context *);
1880
1881 /* Class variables. */
1882
1883 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1884
1885 /* The operator-precedence table used by cp_parser_binary_expression.
1886 Transformed into an associative array (binops_by_token) by
1887 cp_parser_new. */
1888
1889 static const cp_parser_binary_operations_map_node binops[] = {
1890 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1891 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1892
1893 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1894 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1895 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1896
1897 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1898 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1899
1900 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1901 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1902
1903 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1904 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1905 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1906 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1907
1908 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1909 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1910
1911 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1912
1913 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1914
1915 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1916
1917 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1918
1919 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1920 };
1921
1922 /* The same as binops, but initialized by cp_parser_new so that
1923 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1924 for speed. */
1925 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1926
1927 /* Constructors and destructors. */
1928
1929 /* Construct a new context. The context below this one on the stack
1930 is given by NEXT. */
1931
1932 static cp_parser_context *
1933 cp_parser_context_new (cp_parser_context* next)
1934 {
1935 cp_parser_context *context;
1936
1937 /* Allocate the storage. */
1938 if (cp_parser_context_free_list != NULL)
1939 {
1940 /* Pull the first entry from the free list. */
1941 context = cp_parser_context_free_list;
1942 cp_parser_context_free_list = context->next;
1943 memset (context, 0, sizeof (*context));
1944 }
1945 else
1946 context = ggc_cleared_alloc<cp_parser_context> ();
1947
1948 /* No errors have occurred yet in this context. */
1949 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1950 /* If this is not the bottommost context, copy information that we
1951 need from the previous context. */
1952 if (next)
1953 {
1954 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1955 expression, then we are parsing one in this context, too. */
1956 context->object_type = next->object_type;
1957 /* Thread the stack. */
1958 context->next = next;
1959 }
1960
1961 return context;
1962 }
1963
1964 /* Managing the unparsed function queues. */
1965
1966 #define unparsed_funs_with_default_args \
1967 parser->unparsed_queues->last ().funs_with_default_args
1968 #define unparsed_funs_with_definitions \
1969 parser->unparsed_queues->last ().funs_with_definitions
1970 #define unparsed_nsdmis \
1971 parser->unparsed_queues->last ().nsdmis
1972 #define unparsed_classes \
1973 parser->unparsed_queues->last ().classes
1974
1975 static void
1976 push_unparsed_function_queues (cp_parser *parser)
1977 {
1978 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1979 vec_safe_push (parser->unparsed_queues, e);
1980 }
1981
1982 static void
1983 pop_unparsed_function_queues (cp_parser *parser)
1984 {
1985 release_tree_vector (unparsed_funs_with_definitions);
1986 parser->unparsed_queues->pop ();
1987 }
1988
1989 /* Prototypes. */
1990
1991 /* Constructors and destructors. */
1992
1993 static cp_parser *cp_parser_new
1994 (void);
1995
1996 /* Routines to parse various constructs.
1997
1998 Those that return `tree' will return the error_mark_node (rather
1999 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2000 Sometimes, they will return an ordinary node if error-recovery was
2001 attempted, even though a parse error occurred. So, to check
2002 whether or not a parse error occurred, you should always use
2003 cp_parser_error_occurred. If the construct is optional (indicated
2004 either by an `_opt' in the name of the function that does the
2005 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2006 the construct is not present. */
2007
2008 /* Lexical conventions [gram.lex] */
2009
2010 static cp_expr cp_parser_identifier
2011 (cp_parser *);
2012 static cp_expr cp_parser_string_literal
2013 (cp_parser *, bool, bool, bool);
2014 static cp_expr cp_parser_userdef_char_literal
2015 (cp_parser *);
2016 static tree cp_parser_userdef_string_literal
2017 (tree);
2018 static cp_expr cp_parser_userdef_numeric_literal
2019 (cp_parser *);
2020
2021 /* Basic concepts [gram.basic] */
2022
2023 static void cp_parser_translation_unit (cp_parser *);
2024
2025 /* Expressions [gram.expr] */
2026
2027 static cp_expr cp_parser_primary_expression
2028 (cp_parser *, bool, bool, bool, cp_id_kind *);
2029 static cp_expr cp_parser_id_expression
2030 (cp_parser *, bool, bool, bool *, bool, bool);
2031 static cp_expr cp_parser_unqualified_id
2032 (cp_parser *, bool, bool, bool, bool);
2033 static tree cp_parser_nested_name_specifier_opt
2034 (cp_parser *, bool, bool, bool, bool, bool = false);
2035 static tree cp_parser_nested_name_specifier
2036 (cp_parser *, bool, bool, bool, bool);
2037 static tree cp_parser_qualifying_entity
2038 (cp_parser *, bool, bool, bool, bool, bool);
2039 static cp_expr cp_parser_postfix_expression
2040 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2041 static tree cp_parser_postfix_open_square_expression
2042 (cp_parser *, tree, bool, bool);
2043 static tree cp_parser_postfix_dot_deref_expression
2044 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2045 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2046 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2047 bool = false);
2048 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2049 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2050 static void cp_parser_pseudo_destructor_name
2051 (cp_parser *, tree, tree *, tree *);
2052 static cp_expr cp_parser_unary_expression
2053 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2054 static enum tree_code cp_parser_unary_operator
2055 (cp_token *);
2056 static tree cp_parser_has_attribute_expression
2057 (cp_parser *);
2058 static tree cp_parser_new_expression
2059 (cp_parser *);
2060 static vec<tree, va_gc> *cp_parser_new_placement
2061 (cp_parser *);
2062 static tree cp_parser_new_type_id
2063 (cp_parser *, tree *);
2064 static cp_declarator *cp_parser_new_declarator_opt
2065 (cp_parser *);
2066 static cp_declarator *cp_parser_direct_new_declarator
2067 (cp_parser *);
2068 static vec<tree, va_gc> *cp_parser_new_initializer
2069 (cp_parser *);
2070 static tree cp_parser_delete_expression
2071 (cp_parser *);
2072 static cp_expr cp_parser_cast_expression
2073 (cp_parser *, bool, bool, bool, cp_id_kind *);
2074 static cp_expr cp_parser_binary_expression
2075 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2076 static tree cp_parser_question_colon_clause
2077 (cp_parser *, cp_expr);
2078 static cp_expr cp_parser_assignment_expression
2079 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2080 static enum tree_code cp_parser_assignment_operator_opt
2081 (cp_parser *);
2082 static cp_expr cp_parser_expression
2083 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2084 static cp_expr cp_parser_constant_expression
2085 (cp_parser *, bool = false, bool * = NULL, bool = false);
2086 static cp_expr cp_parser_builtin_offsetof
2087 (cp_parser *);
2088 static cp_expr cp_parser_lambda_expression
2089 (cp_parser *);
2090 static void cp_parser_lambda_introducer
2091 (cp_parser *, tree);
2092 static bool cp_parser_lambda_declarator_opt
2093 (cp_parser *, tree);
2094 static void cp_parser_lambda_body
2095 (cp_parser *, tree);
2096
2097 /* Statements [gram.stmt.stmt] */
2098
2099 static void cp_parser_statement
2100 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2101 static void cp_parser_label_for_labeled_statement
2102 (cp_parser *, tree);
2103 static tree cp_parser_expression_statement
2104 (cp_parser *, tree);
2105 static tree cp_parser_compound_statement
2106 (cp_parser *, tree, int, bool);
2107 static void cp_parser_statement_seq_opt
2108 (cp_parser *, tree);
2109 static tree cp_parser_selection_statement
2110 (cp_parser *, bool *, vec<tree> *);
2111 static tree cp_parser_condition
2112 (cp_parser *);
2113 static tree cp_parser_iteration_statement
2114 (cp_parser *, bool *, bool, unsigned short);
2115 static bool cp_parser_init_statement
2116 (cp_parser *, tree *decl);
2117 static tree cp_parser_for
2118 (cp_parser *, bool, unsigned short);
2119 static tree cp_parser_c_for
2120 (cp_parser *, tree, tree, bool, unsigned short);
2121 static tree cp_parser_range_for
2122 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2123 static void do_range_for_auto_deduction
2124 (tree, tree);
2125 static tree cp_parser_perform_range_for_lookup
2126 (tree, tree *, tree *);
2127 static tree cp_parser_range_for_member_function
2128 (tree, tree);
2129 static tree cp_parser_jump_statement
2130 (cp_parser *);
2131 static void cp_parser_declaration_statement
2132 (cp_parser *);
2133
2134 static tree cp_parser_implicitly_scoped_statement
2135 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2136 static void cp_parser_already_scoped_statement
2137 (cp_parser *, bool *, const token_indent_info &);
2138
2139 /* Declarations [gram.dcl.dcl] */
2140
2141 static void cp_parser_declaration_seq_opt
2142 (cp_parser *);
2143 static void cp_parser_declaration
2144 (cp_parser *);
2145 static void cp_parser_toplevel_declaration
2146 (cp_parser *);
2147 static void cp_parser_block_declaration
2148 (cp_parser *, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser *, bool, tree *);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2154 (cp_parser *);
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser *, cp_decl_specifier_seq *);
2157 static tree cp_parser_type_specifier
2158 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2159 int *, bool *);
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2162 static tree cp_parser_type_name
2163 (cp_parser *, bool);
2164 static tree cp_parser_nonclass_name
2165 (cp_parser* parser);
2166 static tree cp_parser_elaborated_type_specifier
2167 (cp_parser *, bool, bool);
2168 static tree cp_parser_enum_specifier
2169 (cp_parser *);
2170 static void cp_parser_enumerator_list
2171 (cp_parser *, tree);
2172 static void cp_parser_enumerator_definition
2173 (cp_parser *, tree);
2174 static tree cp_parser_namespace_name
2175 (cp_parser *);
2176 static void cp_parser_namespace_definition
2177 (cp_parser *);
2178 static void cp_parser_namespace_body
2179 (cp_parser *);
2180 static tree cp_parser_qualified_namespace_specifier
2181 (cp_parser *);
2182 static void cp_parser_namespace_alias_definition
2183 (cp_parser *);
2184 static bool cp_parser_using_declaration
2185 (cp_parser *, bool);
2186 static void cp_parser_using_directive
2187 (cp_parser *);
2188 static tree cp_parser_alias_declaration
2189 (cp_parser *);
2190 static void cp_parser_asm_definition
2191 (cp_parser *);
2192 static void cp_parser_linkage_specification
2193 (cp_parser *);
2194 static void cp_parser_static_assert
2195 (cp_parser *, bool);
2196 static tree cp_parser_decltype
2197 (cp_parser *);
2198 static tree cp_parser_decomposition_declaration
2199 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2200
2201 /* Declarators [gram.dcl.decl] */
2202
2203 static tree cp_parser_init_declarator
2204 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2205 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2206 location_t *, tree *);
2207 static cp_declarator *cp_parser_declarator
2208 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2209 bool, bool, bool);
2210 static cp_declarator *cp_parser_direct_declarator
2211 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2212 bool);
2213 static enum tree_code cp_parser_ptr_operator
2214 (cp_parser *, tree *, cp_cv_quals *, tree *);
2215 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2216 (cp_parser *);
2217 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2218 (cp_parser *);
2219 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2220 (cp_parser *);
2221 static tree cp_parser_tx_qualifier_opt
2222 (cp_parser *);
2223 static tree cp_parser_late_return_type_opt
2224 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2225 static tree cp_parser_declarator_id
2226 (cp_parser *, bool);
2227 static tree cp_parser_type_id
2228 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2229 static tree cp_parser_template_type_arg
2230 (cp_parser *);
2231 static tree cp_parser_trailing_type_id (cp_parser *);
2232 static tree cp_parser_type_id_1
2233 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2234 static void cp_parser_type_specifier_seq
2235 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2236 static tree cp_parser_parameter_declaration_clause
2237 (cp_parser *, cp_parser_flags);
2238 static tree cp_parser_parameter_declaration_list
2239 (cp_parser *, cp_parser_flags);
2240 static cp_parameter_declarator *cp_parser_parameter_declaration
2241 (cp_parser *, cp_parser_flags, bool, bool *);
2242 static tree cp_parser_default_argument
2243 (cp_parser *, bool);
2244 static void cp_parser_function_body
2245 (cp_parser *, bool);
2246 static tree cp_parser_initializer
2247 (cp_parser *, bool *, bool *, bool = false);
2248 static cp_expr cp_parser_initializer_clause
2249 (cp_parser *, bool *);
2250 static cp_expr cp_parser_braced_list
2251 (cp_parser*, bool*);
2252 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2253 (cp_parser *, bool *);
2254
2255 static void cp_parser_ctor_initializer_opt_and_function_body
2256 (cp_parser *, bool);
2257
2258 static tree cp_parser_late_parsing_omp_declare_simd
2259 (cp_parser *, tree);
2260
2261 static tree cp_parser_late_parsing_oacc_routine
2262 (cp_parser *, tree);
2263
2264 static tree synthesize_implicit_template_parm
2265 (cp_parser *, tree);
2266 static tree finish_fully_implicit_template
2267 (cp_parser *, tree);
2268 static void abort_fully_implicit_template
2269 (cp_parser *);
2270
2271 /* Classes [gram.class] */
2272
2273 static tree cp_parser_class_name
2274 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2275 static tree cp_parser_class_specifier
2276 (cp_parser *);
2277 static tree cp_parser_class_head
2278 (cp_parser *, bool *);
2279 static enum tag_types cp_parser_class_key
2280 (cp_parser *);
2281 static void cp_parser_type_parameter_key
2282 (cp_parser* parser);
2283 static void cp_parser_member_specification_opt
2284 (cp_parser *);
2285 static void cp_parser_member_declaration
2286 (cp_parser *);
2287 static tree cp_parser_pure_specifier
2288 (cp_parser *);
2289 static tree cp_parser_constant_initializer
2290 (cp_parser *);
2291
2292 /* Derived classes [gram.class.derived] */
2293
2294 static tree cp_parser_base_clause
2295 (cp_parser *);
2296 static tree cp_parser_base_specifier
2297 (cp_parser *);
2298
2299 /* Special member functions [gram.special] */
2300
2301 static tree cp_parser_conversion_function_id
2302 (cp_parser *);
2303 static tree cp_parser_conversion_type_id
2304 (cp_parser *);
2305 static cp_declarator *cp_parser_conversion_declarator_opt
2306 (cp_parser *);
2307 static void cp_parser_ctor_initializer_opt
2308 (cp_parser *);
2309 static void cp_parser_mem_initializer_list
2310 (cp_parser *);
2311 static tree cp_parser_mem_initializer
2312 (cp_parser *);
2313 static tree cp_parser_mem_initializer_id
2314 (cp_parser *);
2315
2316 /* Overloading [gram.over] */
2317
2318 static cp_expr cp_parser_operator_function_id
2319 (cp_parser *);
2320 static cp_expr cp_parser_operator
2321 (cp_parser *, location_t);
2322
2323 /* Templates [gram.temp] */
2324
2325 static void cp_parser_template_declaration
2326 (cp_parser *, bool);
2327 static tree cp_parser_template_parameter_list
2328 (cp_parser *);
2329 static tree cp_parser_template_parameter
2330 (cp_parser *, bool *, bool *);
2331 static tree cp_parser_type_parameter
2332 (cp_parser *, bool *);
2333 static tree cp_parser_template_id
2334 (cp_parser *, bool, bool, enum tag_types, bool);
2335 static tree cp_parser_template_name
2336 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2337 static tree cp_parser_template_argument_list
2338 (cp_parser *);
2339 static tree cp_parser_template_argument
2340 (cp_parser *);
2341 static void cp_parser_explicit_instantiation
2342 (cp_parser *);
2343 static void cp_parser_explicit_specialization
2344 (cp_parser *);
2345
2346 /* Exception handling [gram.exception] */
2347
2348 static tree cp_parser_try_block
2349 (cp_parser *);
2350 static void cp_parser_function_try_block
2351 (cp_parser *);
2352 static void cp_parser_handler_seq
2353 (cp_parser *);
2354 static void cp_parser_handler
2355 (cp_parser *);
2356 static tree cp_parser_exception_declaration
2357 (cp_parser *);
2358 static tree cp_parser_throw_expression
2359 (cp_parser *);
2360 static tree cp_parser_exception_specification_opt
2361 (cp_parser *);
2362 static tree cp_parser_type_id_list
2363 (cp_parser *);
2364
2365 /* GNU Extensions */
2366
2367 static tree cp_parser_asm_specification_opt
2368 (cp_parser *);
2369 static tree cp_parser_asm_operand_list
2370 (cp_parser *);
2371 static tree cp_parser_asm_clobber_list
2372 (cp_parser *);
2373 static tree cp_parser_asm_label_list
2374 (cp_parser *);
2375 static bool cp_next_tokens_can_be_attribute_p
2376 (cp_parser *);
2377 static bool cp_next_tokens_can_be_gnu_attribute_p
2378 (cp_parser *);
2379 static bool cp_next_tokens_can_be_std_attribute_p
2380 (cp_parser *);
2381 static bool cp_nth_tokens_can_be_std_attribute_p
2382 (cp_parser *, size_t);
2383 static bool cp_nth_tokens_can_be_gnu_attribute_p
2384 (cp_parser *, size_t);
2385 static bool cp_nth_tokens_can_be_attribute_p
2386 (cp_parser *, size_t);
2387 static tree cp_parser_attributes_opt
2388 (cp_parser *);
2389 static tree cp_parser_gnu_attributes_opt
2390 (cp_parser *);
2391 static tree cp_parser_gnu_attribute_list
2392 (cp_parser *, bool = false);
2393 static tree cp_parser_std_attribute
2394 (cp_parser *, tree);
2395 static tree cp_parser_std_attribute_spec
2396 (cp_parser *);
2397 static tree cp_parser_std_attribute_spec_seq
2398 (cp_parser *);
2399 static size_t cp_parser_skip_attributes_opt
2400 (cp_parser *, size_t);
2401 static bool cp_parser_extension_opt
2402 (cp_parser *, int *);
2403 static void cp_parser_label_declaration
2404 (cp_parser *);
2405
2406 /* Concept Extensions */
2407
2408 static tree cp_parser_requires_clause
2409 (cp_parser *);
2410 static tree cp_parser_requires_clause_opt
2411 (cp_parser *);
2412 static tree cp_parser_requires_expression
2413 (cp_parser *);
2414 static tree cp_parser_requirement_parameter_list
2415 (cp_parser *);
2416 static tree cp_parser_requirement_body
2417 (cp_parser *);
2418 static tree cp_parser_requirement_list
2419 (cp_parser *);
2420 static tree cp_parser_requirement
2421 (cp_parser *);
2422 static tree cp_parser_simple_requirement
2423 (cp_parser *);
2424 static tree cp_parser_compound_requirement
2425 (cp_parser *);
2426 static tree cp_parser_type_requirement
2427 (cp_parser *);
2428 static tree cp_parser_nested_requirement
2429 (cp_parser *);
2430
2431 /* Transactional Memory Extensions */
2432
2433 static tree cp_parser_transaction
2434 (cp_parser *, cp_token *);
2435 static tree cp_parser_transaction_expression
2436 (cp_parser *, enum rid);
2437 static void cp_parser_function_transaction
2438 (cp_parser *, enum rid);
2439 static tree cp_parser_transaction_cancel
2440 (cp_parser *);
2441
2442 enum pragma_context {
2443 pragma_external,
2444 pragma_member,
2445 pragma_objc_icode,
2446 pragma_stmt,
2447 pragma_compound
2448 };
2449 static bool cp_parser_pragma
2450 (cp_parser *, enum pragma_context, bool *);
2451
2452 /* Objective-C++ Productions */
2453
2454 static tree cp_parser_objc_message_receiver
2455 (cp_parser *);
2456 static tree cp_parser_objc_message_args
2457 (cp_parser *);
2458 static tree cp_parser_objc_message_expression
2459 (cp_parser *);
2460 static cp_expr cp_parser_objc_encode_expression
2461 (cp_parser *);
2462 static tree cp_parser_objc_defs_expression
2463 (cp_parser *);
2464 static tree cp_parser_objc_protocol_expression
2465 (cp_parser *);
2466 static tree cp_parser_objc_selector_expression
2467 (cp_parser *);
2468 static cp_expr cp_parser_objc_expression
2469 (cp_parser *);
2470 static bool cp_parser_objc_selector_p
2471 (enum cpp_ttype);
2472 static tree cp_parser_objc_selector
2473 (cp_parser *);
2474 static tree cp_parser_objc_protocol_refs_opt
2475 (cp_parser *);
2476 static void cp_parser_objc_declaration
2477 (cp_parser *, tree);
2478 static tree cp_parser_objc_statement
2479 (cp_parser *);
2480 static bool cp_parser_objc_valid_prefix_attributes
2481 (cp_parser *, tree *);
2482 static void cp_parser_objc_at_property_declaration
2483 (cp_parser *) ;
2484 static void cp_parser_objc_at_synthesize_declaration
2485 (cp_parser *) ;
2486 static void cp_parser_objc_at_dynamic_declaration
2487 (cp_parser *) ;
2488 static tree cp_parser_objc_struct_declaration
2489 (cp_parser *) ;
2490
2491 /* Utility Routines */
2492
2493 static cp_expr cp_parser_lookup_name
2494 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2495 static tree cp_parser_lookup_name_simple
2496 (cp_parser *, tree, location_t);
2497 static tree cp_parser_maybe_treat_template_as_class
2498 (tree, bool);
2499 static bool cp_parser_check_declarator_template_parameters
2500 (cp_parser *, cp_declarator *, location_t);
2501 static bool cp_parser_check_template_parameters
2502 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2503 static cp_expr cp_parser_simple_cast_expression
2504 (cp_parser *);
2505 static tree cp_parser_global_scope_opt
2506 (cp_parser *, bool);
2507 static bool cp_parser_constructor_declarator_p
2508 (cp_parser *, bool);
2509 static tree cp_parser_function_definition_from_specifiers_and_declarator
2510 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2511 static tree cp_parser_function_definition_after_declarator
2512 (cp_parser *, bool);
2513 static bool cp_parser_template_declaration_after_export
2514 (cp_parser *, bool);
2515 static void cp_parser_perform_template_parameter_access_checks
2516 (vec<deferred_access_check, va_gc> *);
2517 static tree cp_parser_single_declaration
2518 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2519 static cp_expr cp_parser_functional_cast
2520 (cp_parser *, tree);
2521 static tree cp_parser_save_member_function_body
2522 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2523 static tree cp_parser_save_nsdmi
2524 (cp_parser *);
2525 static tree cp_parser_enclosed_template_argument_list
2526 (cp_parser *);
2527 static void cp_parser_save_default_args
2528 (cp_parser *, tree);
2529 static void cp_parser_late_parsing_for_member
2530 (cp_parser *, tree);
2531 static tree cp_parser_late_parse_one_default_arg
2532 (cp_parser *, tree, tree, tree);
2533 static void cp_parser_late_parsing_nsdmi
2534 (cp_parser *, tree);
2535 static void cp_parser_late_parsing_default_args
2536 (cp_parser *, tree);
2537 static tree cp_parser_sizeof_operand
2538 (cp_parser *, enum rid);
2539 static cp_expr cp_parser_trait_expr
2540 (cp_parser *, enum rid);
2541 static bool cp_parser_declares_only_class_p
2542 (cp_parser *);
2543 static void cp_parser_set_storage_class
2544 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2545 static void cp_parser_set_decl_spec_type
2546 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2547 static void set_and_check_decl_spec_loc
2548 (cp_decl_specifier_seq *decl_specs,
2549 cp_decl_spec ds, cp_token *);
2550 static bool cp_parser_friend_p
2551 (const cp_decl_specifier_seq *);
2552 static void cp_parser_required_error
2553 (cp_parser *, required_token, bool, location_t);
2554 static cp_token *cp_parser_require
2555 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2556 static cp_token *cp_parser_require_keyword
2557 (cp_parser *, enum rid, required_token);
2558 static bool cp_parser_token_starts_function_definition_p
2559 (cp_token *);
2560 static bool cp_parser_next_token_starts_class_definition_p
2561 (cp_parser *);
2562 static bool cp_parser_next_token_ends_template_argument_p
2563 (cp_parser *);
2564 static bool cp_parser_nth_token_starts_template_argument_list_p
2565 (cp_parser *, size_t);
2566 static enum tag_types cp_parser_token_is_class_key
2567 (cp_token *);
2568 static enum tag_types cp_parser_token_is_type_parameter_key
2569 (cp_token *);
2570 static void cp_parser_check_class_key
2571 (enum tag_types, tree type);
2572 static void cp_parser_check_access_in_redeclaration
2573 (tree type, location_t location);
2574 static bool cp_parser_optional_template_keyword
2575 (cp_parser *);
2576 static void cp_parser_pre_parsed_nested_name_specifier
2577 (cp_parser *);
2578 static bool cp_parser_cache_group
2579 (cp_parser *, enum cpp_ttype, unsigned);
2580 static tree cp_parser_cache_defarg
2581 (cp_parser *parser, bool nsdmi);
2582 static void cp_parser_parse_tentatively
2583 (cp_parser *);
2584 static void cp_parser_commit_to_tentative_parse
2585 (cp_parser *);
2586 static void cp_parser_commit_to_topmost_tentative_parse
2587 (cp_parser *);
2588 static void cp_parser_abort_tentative_parse
2589 (cp_parser *);
2590 static bool cp_parser_parse_definitely
2591 (cp_parser *);
2592 static inline bool cp_parser_parsing_tentatively
2593 (cp_parser *);
2594 static bool cp_parser_uncommitted_to_tentative_parse_p
2595 (cp_parser *);
2596 static void cp_parser_error
2597 (cp_parser *, const char *);
2598 static void cp_parser_name_lookup_error
2599 (cp_parser *, tree, tree, name_lookup_error, location_t);
2600 static bool cp_parser_simulate_error
2601 (cp_parser *);
2602 static bool cp_parser_check_type_definition
2603 (cp_parser *);
2604 static void cp_parser_check_for_definition_in_return_type
2605 (cp_declarator *, tree, location_t type_location);
2606 static void cp_parser_check_for_invalid_template_id
2607 (cp_parser *, tree, enum tag_types, location_t location);
2608 static bool cp_parser_non_integral_constant_expression
2609 (cp_parser *, non_integral_constant);
2610 static void cp_parser_diagnose_invalid_type_name
2611 (cp_parser *, tree, location_t);
2612 static bool cp_parser_parse_and_diagnose_invalid_type_name
2613 (cp_parser *);
2614 static int cp_parser_skip_to_closing_parenthesis
2615 (cp_parser *, bool, bool, bool);
2616 static void cp_parser_skip_to_end_of_statement
2617 (cp_parser *);
2618 static void cp_parser_consume_semicolon_at_end_of_statement
2619 (cp_parser *);
2620 static void cp_parser_skip_to_end_of_block_or_statement
2621 (cp_parser *);
2622 static bool cp_parser_skip_to_closing_brace
2623 (cp_parser *);
2624 static void cp_parser_skip_to_end_of_template_parameter_list
2625 (cp_parser *);
2626 static void cp_parser_skip_to_pragma_eol
2627 (cp_parser*, cp_token *);
2628 static bool cp_parser_error_occurred
2629 (cp_parser *);
2630 static bool cp_parser_allow_gnu_extensions_p
2631 (cp_parser *);
2632 static bool cp_parser_is_pure_string_literal
2633 (cp_token *);
2634 static bool cp_parser_is_string_literal
2635 (cp_token *);
2636 static bool cp_parser_is_keyword
2637 (cp_token *, enum rid);
2638 static tree cp_parser_make_typename_type
2639 (cp_parser *, tree, location_t location);
2640 static cp_declarator * cp_parser_make_indirect_declarator
2641 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2642 static bool cp_parser_compound_literal_p
2643 (cp_parser *);
2644 static bool cp_parser_array_designator_p
2645 (cp_parser *);
2646 static bool cp_parser_init_statement_p
2647 (cp_parser *);
2648 static bool cp_parser_skip_to_closing_square_bracket
2649 (cp_parser *);
2650
2651 /* Concept-related syntactic transformations */
2652
2653 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2654 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2655
2656 // -------------------------------------------------------------------------- //
2657 // Unevaluated Operand Guard
2658 //
2659 // Implementation of an RAII helper for unevaluated operand parsing.
2660 cp_unevaluated::cp_unevaluated ()
2661 {
2662 ++cp_unevaluated_operand;
2663 ++c_inhibit_evaluation_warnings;
2664 }
2665
2666 cp_unevaluated::~cp_unevaluated ()
2667 {
2668 --c_inhibit_evaluation_warnings;
2669 --cp_unevaluated_operand;
2670 }
2671
2672 // -------------------------------------------------------------------------- //
2673 // Tentative Parsing
2674
2675 /* Returns nonzero if we are parsing tentatively. */
2676
2677 static inline bool
2678 cp_parser_parsing_tentatively (cp_parser* parser)
2679 {
2680 return parser->context->next != NULL;
2681 }
2682
2683 /* Returns nonzero if TOKEN is a string literal. */
2684
2685 static bool
2686 cp_parser_is_pure_string_literal (cp_token* token)
2687 {
2688 return (token->type == CPP_STRING ||
2689 token->type == CPP_STRING16 ||
2690 token->type == CPP_STRING32 ||
2691 token->type == CPP_WSTRING ||
2692 token->type == CPP_UTF8STRING);
2693 }
2694
2695 /* Returns nonzero if TOKEN is a string literal
2696 of a user-defined string literal. */
2697
2698 static bool
2699 cp_parser_is_string_literal (cp_token* token)
2700 {
2701 return (cp_parser_is_pure_string_literal (token) ||
2702 token->type == CPP_STRING_USERDEF ||
2703 token->type == CPP_STRING16_USERDEF ||
2704 token->type == CPP_STRING32_USERDEF ||
2705 token->type == CPP_WSTRING_USERDEF ||
2706 token->type == CPP_UTF8STRING_USERDEF);
2707 }
2708
2709 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2710
2711 static bool
2712 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2713 {
2714 return token->keyword == keyword;
2715 }
2716
2717 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2718 PRAGMA_NONE. */
2719
2720 static enum pragma_kind
2721 cp_parser_pragma_kind (cp_token *token)
2722 {
2723 if (token->type != CPP_PRAGMA)
2724 return PRAGMA_NONE;
2725 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2726 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2727 }
2728
2729 /* Helper function for cp_parser_error.
2730 Having peeked a token of kind TOK1_KIND that might signify
2731 a conflict marker, peek successor tokens to determine
2732 if we actually do have a conflict marker.
2733 Specifically, we consider a run of 7 '<', '=' or '>' characters
2734 at the start of a line as a conflict marker.
2735 These come through the lexer as three pairs and a single,
2736 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2737 If it returns true, *OUT_LOC is written to with the location/range
2738 of the marker. */
2739
2740 static bool
2741 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2742 location_t *out_loc)
2743 {
2744 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2745 if (token2->type != tok1_kind)
2746 return false;
2747 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2748 if (token3->type != tok1_kind)
2749 return false;
2750 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2751 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2752 return false;
2753
2754 /* It must be at the start of the line. */
2755 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2756 if (LOCATION_COLUMN (start_loc) != 1)
2757 return false;
2758
2759 /* We have a conflict marker. Construct a location of the form:
2760 <<<<<<<
2761 ^~~~~~~
2762 with start == caret, finishing at the end of the marker. */
2763 location_t finish_loc = get_finish (token4->location);
2764 *out_loc = make_location (start_loc, start_loc, finish_loc);
2765
2766 return true;
2767 }
2768
2769 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2770 RT_CLOSE_PAREN. */
2771
2772 static const char *
2773 get_matching_symbol (required_token token_desc)
2774 {
2775 switch (token_desc)
2776 {
2777 default:
2778 gcc_unreachable ();
2779 return "";
2780 case RT_CLOSE_BRACE:
2781 return "{";
2782 case RT_CLOSE_PAREN:
2783 return "(";
2784 }
2785 }
2786
2787 /* Attempt to convert TOKEN_DESC from a required_token to an
2788 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2789
2790 static enum cpp_ttype
2791 get_required_cpp_ttype (required_token token_desc)
2792 {
2793 switch (token_desc)
2794 {
2795 case RT_SEMICOLON:
2796 return CPP_SEMICOLON;
2797 case RT_OPEN_PAREN:
2798 return CPP_OPEN_PAREN;
2799 case RT_CLOSE_BRACE:
2800 return CPP_CLOSE_BRACE;
2801 case RT_OPEN_BRACE:
2802 return CPP_OPEN_BRACE;
2803 case RT_CLOSE_SQUARE:
2804 return CPP_CLOSE_SQUARE;
2805 case RT_OPEN_SQUARE:
2806 return CPP_OPEN_SQUARE;
2807 case RT_COMMA:
2808 return CPP_COMMA;
2809 case RT_COLON:
2810 return CPP_COLON;
2811 case RT_CLOSE_PAREN:
2812 return CPP_CLOSE_PAREN;
2813
2814 default:
2815 /* Use CPP_EOF as a "no completions possible" code. */
2816 return CPP_EOF;
2817 }
2818 }
2819
2820
2821 /* Subroutine of cp_parser_error and cp_parser_required_error.
2822
2823 Issue a diagnostic of the form
2824 FILE:LINE: MESSAGE before TOKEN
2825 where TOKEN is the next token in the input stream. MESSAGE
2826 (specified by the caller) is usually of the form "expected
2827 OTHER-TOKEN".
2828
2829 This bypasses the check for tentative passing, and potentially
2830 adds material needed by cp_parser_required_error.
2831
2832 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2833 suggesting insertion of the missing token.
2834
2835 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2836 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2837 location. */
2838
2839 static void
2840 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2841 required_token missing_token_desc,
2842 location_t matching_location)
2843 {
2844 cp_token *token = cp_lexer_peek_token (parser->lexer);
2845 /* This diagnostic makes more sense if it is tagged to the line
2846 of the token we just peeked at. */
2847 cp_lexer_set_source_position_from_token (token);
2848
2849 if (token->type == CPP_PRAGMA)
2850 {
2851 error_at (token->location,
2852 "%<#pragma%> is not allowed here");
2853 cp_parser_skip_to_pragma_eol (parser, token);
2854 return;
2855 }
2856
2857 /* If this is actually a conflict marker, report it as such. */
2858 if (token->type == CPP_LSHIFT
2859 || token->type == CPP_RSHIFT
2860 || token->type == CPP_EQ_EQ)
2861 {
2862 location_t loc;
2863 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2864 {
2865 error_at (loc, "version control conflict marker in file");
2866 expanded_location token_exploc = expand_location (token->location);
2867 /* Consume tokens until the end of the source line. */
2868 while (1)
2869 {
2870 cp_lexer_consume_token (parser->lexer);
2871 cp_token *next = cp_lexer_peek_token (parser->lexer);
2872 if (next == NULL)
2873 break;
2874 expanded_location next_exploc = expand_location (next->location);
2875 if (next_exploc.file != token_exploc.file)
2876 break;
2877 if (next_exploc.line != token_exploc.line)
2878 break;
2879 }
2880 return;
2881 }
2882 }
2883
2884 gcc_rich_location richloc (input_location);
2885
2886 bool added_matching_location = false;
2887
2888 if (missing_token_desc != RT_NONE)
2889 {
2890 /* Potentially supply a fix-it hint, suggesting to add the
2891 missing token immediately after the *previous* token.
2892 This may move the primary location within richloc. */
2893 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2894 location_t prev_token_loc
2895 = cp_lexer_previous_token (parser->lexer)->location;
2896 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2897
2898 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2899 Attempt to consolidate diagnostics by printing it as a
2900 secondary range within the main diagnostic. */
2901 if (matching_location != UNKNOWN_LOCATION)
2902 added_matching_location
2903 = richloc.add_location_if_nearby (matching_location);
2904 }
2905
2906 /* Actually emit the error. */
2907 c_parse_error (gmsgid,
2908 /* Because c_parser_error does not understand
2909 CPP_KEYWORD, keywords are treated like
2910 identifiers. */
2911 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2912 token->u.value, token->flags, &richloc);
2913
2914 if (missing_token_desc != RT_NONE)
2915 {
2916 /* If we weren't able to consolidate matching_location, then
2917 print it as a secondary diagnostic. */
2918 if (matching_location != UNKNOWN_LOCATION
2919 && !added_matching_location)
2920 inform (matching_location, "to match this %qs",
2921 get_matching_symbol (missing_token_desc));
2922 }
2923 }
2924
2925 /* If not parsing tentatively, issue a diagnostic of the form
2926 FILE:LINE: MESSAGE before TOKEN
2927 where TOKEN is the next token in the input stream. MESSAGE
2928 (specified by the caller) is usually of the form "expected
2929 OTHER-TOKEN". */
2930
2931 static void
2932 cp_parser_error (cp_parser* parser, const char* gmsgid)
2933 {
2934 if (!cp_parser_simulate_error (parser))
2935 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2936 }
2937
2938 /* Issue an error about name-lookup failing. NAME is the
2939 IDENTIFIER_NODE DECL is the result of
2940 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2941 the thing that we hoped to find. */
2942
2943 static void
2944 cp_parser_name_lookup_error (cp_parser* parser,
2945 tree name,
2946 tree decl,
2947 name_lookup_error desired,
2948 location_t location)
2949 {
2950 /* If name lookup completely failed, tell the user that NAME was not
2951 declared. */
2952 if (decl == error_mark_node)
2953 {
2954 if (parser->scope && parser->scope != global_namespace)
2955 error_at (location, "%<%E::%E%> has not been declared",
2956 parser->scope, name);
2957 else if (parser->scope == global_namespace)
2958 error_at (location, "%<::%E%> has not been declared", name);
2959 else if (parser->object_scope
2960 && !CLASS_TYPE_P (parser->object_scope))
2961 error_at (location, "request for member %qE in non-class type %qT",
2962 name, parser->object_scope);
2963 else if (parser->object_scope)
2964 error_at (location, "%<%T::%E%> has not been declared",
2965 parser->object_scope, name);
2966 else
2967 error_at (location, "%qE has not been declared", name);
2968 }
2969 else if (parser->scope && parser->scope != global_namespace)
2970 {
2971 switch (desired)
2972 {
2973 case NLE_TYPE:
2974 error_at (location, "%<%E::%E%> is not a type",
2975 parser->scope, name);
2976 break;
2977 case NLE_CXX98:
2978 error_at (location, "%<%E::%E%> is not a class or namespace",
2979 parser->scope, name);
2980 break;
2981 case NLE_NOT_CXX98:
2982 error_at (location,
2983 "%<%E::%E%> is not a class, namespace, or enumeration",
2984 parser->scope, name);
2985 break;
2986 default:
2987 gcc_unreachable ();
2988
2989 }
2990 }
2991 else if (parser->scope == global_namespace)
2992 {
2993 switch (desired)
2994 {
2995 case NLE_TYPE:
2996 error_at (location, "%<::%E%> is not a type", name);
2997 break;
2998 case NLE_CXX98:
2999 error_at (location, "%<::%E%> is not a class or namespace", name);
3000 break;
3001 case NLE_NOT_CXX98:
3002 error_at (location,
3003 "%<::%E%> is not a class, namespace, or enumeration",
3004 name);
3005 break;
3006 default:
3007 gcc_unreachable ();
3008 }
3009 }
3010 else
3011 {
3012 switch (desired)
3013 {
3014 case NLE_TYPE:
3015 error_at (location, "%qE is not a type", name);
3016 break;
3017 case NLE_CXX98:
3018 error_at (location, "%qE is not a class or namespace", name);
3019 break;
3020 case NLE_NOT_CXX98:
3021 error_at (location,
3022 "%qE is not a class, namespace, or enumeration", name);
3023 break;
3024 default:
3025 gcc_unreachable ();
3026 }
3027 }
3028 }
3029
3030 /* If we are parsing tentatively, remember that an error has occurred
3031 during this tentative parse. Returns true if the error was
3032 simulated; false if a message should be issued by the caller. */
3033
3034 static bool
3035 cp_parser_simulate_error (cp_parser* parser)
3036 {
3037 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3038 {
3039 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3040 return true;
3041 }
3042 return false;
3043 }
3044
3045 /* This function is called when a type is defined. If type
3046 definitions are forbidden at this point, an error message is
3047 issued. */
3048
3049 static bool
3050 cp_parser_check_type_definition (cp_parser* parser)
3051 {
3052 /* If types are forbidden here, issue a message. */
3053 if (parser->type_definition_forbidden_message)
3054 {
3055 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3056 in the message need to be interpreted. */
3057 error (parser->type_definition_forbidden_message);
3058 return false;
3059 }
3060 return true;
3061 }
3062
3063 /* This function is called when the DECLARATOR is processed. The TYPE
3064 was a type defined in the decl-specifiers. If it is invalid to
3065 define a type in the decl-specifiers for DECLARATOR, an error is
3066 issued. TYPE_LOCATION is the location of TYPE and is used
3067 for error reporting. */
3068
3069 static void
3070 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3071 tree type, location_t type_location)
3072 {
3073 /* [dcl.fct] forbids type definitions in return types.
3074 Unfortunately, it's not easy to know whether or not we are
3075 processing a return type until after the fact. */
3076 while (declarator
3077 && (declarator->kind == cdk_pointer
3078 || declarator->kind == cdk_reference
3079 || declarator->kind == cdk_ptrmem))
3080 declarator = declarator->declarator;
3081 if (declarator
3082 && declarator->kind == cdk_function)
3083 {
3084 error_at (type_location,
3085 "new types may not be defined in a return type");
3086 inform (type_location,
3087 "(perhaps a semicolon is missing after the definition of %qT)",
3088 type);
3089 }
3090 }
3091
3092 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3093 "<" in any valid C++ program. If the next token is indeed "<",
3094 issue a message warning the user about what appears to be an
3095 invalid attempt to form a template-id. LOCATION is the location
3096 of the type-specifier (TYPE) */
3097
3098 static void
3099 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3100 tree type,
3101 enum tag_types tag_type,
3102 location_t location)
3103 {
3104 cp_token_position start = 0;
3105
3106 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3107 {
3108 if (TREE_CODE (type) == TYPE_DECL)
3109 type = TREE_TYPE (type);
3110 if (TYPE_P (type) && !template_placeholder_p (type))
3111 error_at (location, "%qT is not a template", type);
3112 else if (identifier_p (type))
3113 {
3114 if (tag_type != none_type)
3115 error_at (location, "%qE is not a class template", type);
3116 else
3117 error_at (location, "%qE is not a template", type);
3118 }
3119 else
3120 error_at (location, "invalid template-id");
3121 /* Remember the location of the invalid "<". */
3122 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3123 start = cp_lexer_token_position (parser->lexer, true);
3124 /* Consume the "<". */
3125 cp_lexer_consume_token (parser->lexer);
3126 /* Parse the template arguments. */
3127 cp_parser_enclosed_template_argument_list (parser);
3128 /* Permanently remove the invalid template arguments so that
3129 this error message is not issued again. */
3130 if (start)
3131 cp_lexer_purge_tokens_after (parser->lexer, start);
3132 }
3133 }
3134
3135 /* If parsing an integral constant-expression, issue an error message
3136 about the fact that THING appeared and return true. Otherwise,
3137 return false. In either case, set
3138 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3139
3140 static bool
3141 cp_parser_non_integral_constant_expression (cp_parser *parser,
3142 non_integral_constant thing)
3143 {
3144 parser->non_integral_constant_expression_p = true;
3145 if (parser->integral_constant_expression_p)
3146 {
3147 if (!parser->allow_non_integral_constant_expression_p)
3148 {
3149 const char *msg = NULL;
3150 switch (thing)
3151 {
3152 case NIC_FLOAT:
3153 pedwarn (input_location, OPT_Wpedantic,
3154 "ISO C++ forbids using a floating-point literal "
3155 "in a constant-expression");
3156 return true;
3157 case NIC_CAST:
3158 error ("a cast to a type other than an integral or "
3159 "enumeration type cannot appear in a "
3160 "constant-expression");
3161 return true;
3162 case NIC_TYPEID:
3163 error ("%<typeid%> operator "
3164 "cannot appear in a constant-expression");
3165 return true;
3166 case NIC_NCC:
3167 error ("non-constant compound literals "
3168 "cannot appear in a constant-expression");
3169 return true;
3170 case NIC_FUNC_CALL:
3171 error ("a function call "
3172 "cannot appear in a constant-expression");
3173 return true;
3174 case NIC_INC:
3175 error ("an increment "
3176 "cannot appear in a constant-expression");
3177 return true;
3178 case NIC_DEC:
3179 error ("an decrement "
3180 "cannot appear in a constant-expression");
3181 return true;
3182 case NIC_ARRAY_REF:
3183 error ("an array reference "
3184 "cannot appear in a constant-expression");
3185 return true;
3186 case NIC_ADDR_LABEL:
3187 error ("the address of a label "
3188 "cannot appear in a constant-expression");
3189 return true;
3190 case NIC_OVERLOADED:
3191 error ("calls to overloaded operators "
3192 "cannot appear in a constant-expression");
3193 return true;
3194 case NIC_ASSIGNMENT:
3195 error ("an assignment cannot appear in a constant-expression");
3196 return true;
3197 case NIC_COMMA:
3198 error ("a comma operator "
3199 "cannot appear in a constant-expression");
3200 return true;
3201 case NIC_CONSTRUCTOR:
3202 error ("a call to a constructor "
3203 "cannot appear in a constant-expression");
3204 return true;
3205 case NIC_TRANSACTION:
3206 error ("a transaction expression "
3207 "cannot appear in a constant-expression");
3208 return true;
3209 case NIC_THIS:
3210 msg = "this";
3211 break;
3212 case NIC_FUNC_NAME:
3213 msg = "__FUNCTION__";
3214 break;
3215 case NIC_PRETTY_FUNC:
3216 msg = "__PRETTY_FUNCTION__";
3217 break;
3218 case NIC_C99_FUNC:
3219 msg = "__func__";
3220 break;
3221 case NIC_VA_ARG:
3222 msg = "va_arg";
3223 break;
3224 case NIC_ARROW:
3225 msg = "->";
3226 break;
3227 case NIC_POINT:
3228 msg = ".";
3229 break;
3230 case NIC_STAR:
3231 msg = "*";
3232 break;
3233 case NIC_ADDR:
3234 msg = "&";
3235 break;
3236 case NIC_PREINCREMENT:
3237 msg = "++";
3238 break;
3239 case NIC_PREDECREMENT:
3240 msg = "--";
3241 break;
3242 case NIC_NEW:
3243 msg = "new";
3244 break;
3245 case NIC_DEL:
3246 msg = "delete";
3247 break;
3248 default:
3249 gcc_unreachable ();
3250 }
3251 if (msg)
3252 error ("%qs cannot appear in a constant-expression", msg);
3253 return true;
3254 }
3255 }
3256 return false;
3257 }
3258
3259 /* Emit a diagnostic for an invalid type name. This function commits
3260 to the current active tentative parse, if any. (Otherwise, the
3261 problematic construct might be encountered again later, resulting
3262 in duplicate error messages.) LOCATION is the location of ID. */
3263
3264 static void
3265 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3266 location_t location)
3267 {
3268 tree decl, ambiguous_decls;
3269 cp_parser_commit_to_tentative_parse (parser);
3270 /* Try to lookup the identifier. */
3271 decl = cp_parser_lookup_name (parser, id, none_type,
3272 /*is_template=*/false,
3273 /*is_namespace=*/false,
3274 /*check_dependency=*/true,
3275 &ambiguous_decls, location);
3276 if (ambiguous_decls)
3277 /* If the lookup was ambiguous, an error will already have
3278 been issued. */
3279 return;
3280 /* If the lookup found a template-name, it means that the user forgot
3281 to specify an argument list. Emit a useful error message. */
3282 if (DECL_TYPE_TEMPLATE_P (decl))
3283 {
3284 auto_diagnostic_group d;
3285 error_at (location,
3286 "invalid use of template-name %qE without an argument list",
3287 decl);
3288 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3289 inform (location, "class template argument deduction is only available "
3290 "with -std=c++17 or -std=gnu++17");
3291 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3292 }
3293 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3294 error_at (location, "invalid use of destructor %qD as a type", id);
3295 else if (TREE_CODE (decl) == TYPE_DECL)
3296 /* Something like 'unsigned A a;' */
3297 error_at (location, "invalid combination of multiple type-specifiers");
3298 else if (!parser->scope)
3299 {
3300 /* Issue an error message. */
3301 auto_diagnostic_group d;
3302 name_hint hint;
3303 if (TREE_CODE (id) == IDENTIFIER_NODE)
3304 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3305 if (const char *suggestion = hint.suggestion ())
3306 {
3307 gcc_rich_location richloc (location);
3308 richloc.add_fixit_replace (suggestion);
3309 error_at (&richloc,
3310 "%qE does not name a type; did you mean %qs?",
3311 id, suggestion);
3312 }
3313 else
3314 error_at (location, "%qE does not name a type", id);
3315 /* If we're in a template class, it's possible that the user was
3316 referring to a type from a base class. For example:
3317
3318 template <typename T> struct A { typedef T X; };
3319 template <typename T> struct B : public A<T> { X x; };
3320
3321 The user should have said "typename A<T>::X". */
3322 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3323 inform (location, "C++11 %<constexpr%> only available with "
3324 "-std=c++11 or -std=gnu++11");
3325 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3326 inform (location, "C++11 %<noexcept%> only available with "
3327 "-std=c++11 or -std=gnu++11");
3328 else if (cxx_dialect < cxx11
3329 && TREE_CODE (id) == IDENTIFIER_NODE
3330 && id_equal (id, "thread_local"))
3331 inform (location, "C++11 %<thread_local%> only available with "
3332 "-std=c++11 or -std=gnu++11");
3333 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3334 inform (location, "%<concept%> only available with -fconcepts");
3335 else if (processing_template_decl && current_class_type
3336 && TYPE_BINFO (current_class_type))
3337 {
3338 tree b;
3339
3340 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3341 b;
3342 b = TREE_CHAIN (b))
3343 {
3344 tree base_type = BINFO_TYPE (b);
3345 if (CLASS_TYPE_P (base_type)
3346 && dependent_type_p (base_type))
3347 {
3348 tree field;
3349 /* Go from a particular instantiation of the
3350 template (which will have an empty TYPE_FIELDs),
3351 to the main version. */
3352 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3353 for (field = TYPE_FIELDS (base_type);
3354 field;
3355 field = DECL_CHAIN (field))
3356 if (TREE_CODE (field) == TYPE_DECL
3357 && DECL_NAME (field) == id)
3358 {
3359 inform (location,
3360 "(perhaps %<typename %T::%E%> was intended)",
3361 BINFO_TYPE (b), id);
3362 break;
3363 }
3364 if (field)
3365 break;
3366 }
3367 }
3368 }
3369 }
3370 /* Here we diagnose qualified-ids where the scope is actually correct,
3371 but the identifier does not resolve to a valid type name. */
3372 else if (parser->scope != error_mark_node)
3373 {
3374 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3375 {
3376 auto_diagnostic_group d;
3377 name_hint hint;
3378 if (decl == error_mark_node)
3379 hint = suggest_alternative_in_explicit_scope (location, id,
3380 parser->scope);
3381 const char *suggestion = hint.suggestion ();
3382 gcc_rich_location richloc (location_of (id));
3383 if (suggestion)
3384 richloc.add_fixit_replace (suggestion);
3385 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3386 {
3387 if (suggestion)
3388 error_at (&richloc,
3389 "%qE in namespace %qE does not name a template"
3390 " type; did you mean %qs?",
3391 id, parser->scope, suggestion);
3392 else
3393 error_at (&richloc,
3394 "%qE in namespace %qE does not name a template type",
3395 id, parser->scope);
3396 }
3397 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3398 {
3399 if (suggestion)
3400 error_at (&richloc,
3401 "%qE in namespace %qE does not name a template"
3402 " type; did you mean %qs?",
3403 TREE_OPERAND (id, 0), parser->scope, suggestion);
3404 else
3405 error_at (&richloc,
3406 "%qE in namespace %qE does not name a template"
3407 " type",
3408 TREE_OPERAND (id, 0), parser->scope);
3409 }
3410 else
3411 {
3412 if (suggestion)
3413 error_at (&richloc,
3414 "%qE in namespace %qE does not name a type"
3415 "; did you mean %qs?",
3416 id, parser->scope, suggestion);
3417 else
3418 error_at (&richloc,
3419 "%qE in namespace %qE does not name a type",
3420 id, parser->scope);
3421 }
3422 if (DECL_P (decl))
3423 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3424 }
3425 else if (CLASS_TYPE_P (parser->scope)
3426 && constructor_name_p (id, parser->scope))
3427 {
3428 /* A<T>::A<T>() */
3429 auto_diagnostic_group d;
3430 error_at (location, "%<%T::%E%> names the constructor, not"
3431 " the type", parser->scope, id);
3432 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3433 error_at (location, "and %qT has no template constructors",
3434 parser->scope);
3435 }
3436 else if (TYPE_P (parser->scope)
3437 && dependent_scope_p (parser->scope))
3438 {
3439 gcc_rich_location richloc (location);
3440 richloc.add_fixit_insert_before ("typename ");
3441 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3442 error_at (&richloc,
3443 "need %<typename%> before %<%T::%D::%E%> because "
3444 "%<%T::%D%> is a dependent scope",
3445 TYPE_CONTEXT (parser->scope),
3446 TYPENAME_TYPE_FULLNAME (parser->scope),
3447 id,
3448 TYPE_CONTEXT (parser->scope),
3449 TYPENAME_TYPE_FULLNAME (parser->scope));
3450 else
3451 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3452 "%qT is a dependent scope",
3453 parser->scope, id, parser->scope);
3454 }
3455 else if (TYPE_P (parser->scope))
3456 {
3457 auto_diagnostic_group d;
3458 if (!COMPLETE_TYPE_P (parser->scope))
3459 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3460 parser->scope);
3461 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3462 error_at (location_of (id),
3463 "%qE in %q#T does not name a template type",
3464 id, parser->scope);
3465 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3466 error_at (location_of (id),
3467 "%qE in %q#T does not name a template type",
3468 TREE_OPERAND (id, 0), parser->scope);
3469 else
3470 error_at (location_of (id),
3471 "%qE in %q#T does not name a type",
3472 id, parser->scope);
3473 if (DECL_P (decl))
3474 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3475 }
3476 else
3477 gcc_unreachable ();
3478 }
3479 }
3480
3481 /* Check for a common situation where a type-name should be present,
3482 but is not, and issue a sensible error message. Returns true if an
3483 invalid type-name was detected.
3484
3485 The situation handled by this function are variable declarations of the
3486 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3487 Usually, `ID' should name a type, but if we got here it means that it
3488 does not. We try to emit the best possible error message depending on
3489 how exactly the id-expression looks like. */
3490
3491 static bool
3492 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3493 {
3494 tree id;
3495 cp_token *token = cp_lexer_peek_token (parser->lexer);
3496
3497 /* Avoid duplicate error about ambiguous lookup. */
3498 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3499 {
3500 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3501 if (next->type == CPP_NAME && next->error_reported)
3502 goto out;
3503 }
3504
3505 cp_parser_parse_tentatively (parser);
3506 id = cp_parser_id_expression (parser,
3507 /*template_keyword_p=*/false,
3508 /*check_dependency_p=*/true,
3509 /*template_p=*/NULL,
3510 /*declarator_p=*/false,
3511 /*optional_p=*/false);
3512 /* If the next token is a (, this is a function with no explicit return
3513 type, i.e. constructor, destructor or conversion op. */
3514 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3515 || TREE_CODE (id) == TYPE_DECL)
3516 {
3517 cp_parser_abort_tentative_parse (parser);
3518 return false;
3519 }
3520 if (!cp_parser_parse_definitely (parser))
3521 return false;
3522
3523 /* Emit a diagnostic for the invalid type. */
3524 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3525 out:
3526 /* If we aren't in the middle of a declarator (i.e. in a
3527 parameter-declaration-clause), skip to the end of the declaration;
3528 there's no point in trying to process it. */
3529 if (!parser->in_declarator_p)
3530 cp_parser_skip_to_end_of_block_or_statement (parser);
3531 return true;
3532 }
3533
3534 /* Consume tokens up to, and including, the next non-nested closing `)'.
3535 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3536 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3537 found an unnested token of that type. */
3538
3539 static int
3540 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3541 bool recovering,
3542 cpp_ttype or_ttype,
3543 bool consume_paren)
3544 {
3545 unsigned paren_depth = 0;
3546 unsigned brace_depth = 0;
3547 unsigned square_depth = 0;
3548 unsigned condop_depth = 0;
3549
3550 if (recovering && or_ttype == CPP_EOF
3551 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3552 return 0;
3553
3554 while (true)
3555 {
3556 cp_token * token = cp_lexer_peek_token (parser->lexer);
3557
3558 /* Have we found what we're looking for before the closing paren? */
3559 if (token->type == or_ttype && or_ttype != CPP_EOF
3560 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3561 return -1;
3562
3563 switch (token->type)
3564 {
3565 case CPP_PRAGMA_EOL:
3566 if (!parser->lexer->in_pragma)
3567 break;
3568 /* FALLTHRU */
3569 case CPP_EOF:
3570 /* If we've run out of tokens, then there is no closing `)'. */
3571 return 0;
3572
3573 /* This is good for lambda expression capture-lists. */
3574 case CPP_OPEN_SQUARE:
3575 ++square_depth;
3576 break;
3577 case CPP_CLOSE_SQUARE:
3578 if (!square_depth--)
3579 return 0;
3580 break;
3581
3582 case CPP_SEMICOLON:
3583 /* This matches the processing in skip_to_end_of_statement. */
3584 if (!brace_depth)
3585 return 0;
3586 break;
3587
3588 case CPP_OPEN_BRACE:
3589 ++brace_depth;
3590 break;
3591 case CPP_CLOSE_BRACE:
3592 if (!brace_depth--)
3593 return 0;
3594 break;
3595
3596 case CPP_OPEN_PAREN:
3597 if (!brace_depth)
3598 ++paren_depth;
3599 break;
3600
3601 case CPP_CLOSE_PAREN:
3602 if (!brace_depth && !paren_depth--)
3603 {
3604 if (consume_paren)
3605 cp_lexer_consume_token (parser->lexer);
3606 return 1;
3607 }
3608 break;
3609
3610 case CPP_QUERY:
3611 if (!brace_depth && !paren_depth && !square_depth)
3612 ++condop_depth;
3613 break;
3614
3615 case CPP_COLON:
3616 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3617 condop_depth--;
3618 break;
3619
3620 default:
3621 break;
3622 }
3623
3624 /* Consume the token. */
3625 cp_lexer_consume_token (parser->lexer);
3626 }
3627 }
3628
3629 /* Consume tokens up to, and including, the next non-nested closing `)'.
3630 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3631 are doing error recovery. Returns -1 if OR_COMMA is true and we
3632 found an unnested token of that type. */
3633
3634 static int
3635 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3636 bool recovering,
3637 bool or_comma,
3638 bool consume_paren)
3639 {
3640 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3641 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3642 ttype, consume_paren);
3643 }
3644
3645 /* Consume tokens until we reach the end of the current statement.
3646 Normally, that will be just before consuming a `;'. However, if a
3647 non-nested `}' comes first, then we stop before consuming that. */
3648
3649 static void
3650 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3651 {
3652 unsigned nesting_depth = 0;
3653
3654 /* Unwind generic function template scope if necessary. */
3655 if (parser->fully_implicit_function_template_p)
3656 abort_fully_implicit_template (parser);
3657
3658 while (true)
3659 {
3660 cp_token *token = cp_lexer_peek_token (parser->lexer);
3661
3662 switch (token->type)
3663 {
3664 case CPP_PRAGMA_EOL:
3665 if (!parser->lexer->in_pragma)
3666 break;
3667 /* FALLTHRU */
3668 case CPP_EOF:
3669 /* If we've run out of tokens, stop. */
3670 return;
3671
3672 case CPP_SEMICOLON:
3673 /* If the next token is a `;', we have reached the end of the
3674 statement. */
3675 if (!nesting_depth)
3676 return;
3677 break;
3678
3679 case CPP_CLOSE_BRACE:
3680 /* If this is a non-nested '}', stop before consuming it.
3681 That way, when confronted with something like:
3682
3683 { 3 + }
3684
3685 we stop before consuming the closing '}', even though we
3686 have not yet reached a `;'. */
3687 if (nesting_depth == 0)
3688 return;
3689
3690 /* If it is the closing '}' for a block that we have
3691 scanned, stop -- but only after consuming the token.
3692 That way given:
3693
3694 void f g () { ... }
3695 typedef int I;
3696
3697 we will stop after the body of the erroneously declared
3698 function, but before consuming the following `typedef'
3699 declaration. */
3700 if (--nesting_depth == 0)
3701 {
3702 cp_lexer_consume_token (parser->lexer);
3703 return;
3704 }
3705 break;
3706
3707 case CPP_OPEN_BRACE:
3708 ++nesting_depth;
3709 break;
3710
3711 default:
3712 break;
3713 }
3714
3715 /* Consume the token. */
3716 cp_lexer_consume_token (parser->lexer);
3717 }
3718 }
3719
3720 /* This function is called at the end of a statement or declaration.
3721 If the next token is a semicolon, it is consumed; otherwise, error
3722 recovery is attempted. */
3723
3724 static void
3725 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3726 {
3727 /* Look for the trailing `;'. */
3728 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3729 {
3730 /* If there is additional (erroneous) input, skip to the end of
3731 the statement. */
3732 cp_parser_skip_to_end_of_statement (parser);
3733 /* If the next token is now a `;', consume it. */
3734 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3735 cp_lexer_consume_token (parser->lexer);
3736 }
3737 }
3738
3739 /* Skip tokens until we have consumed an entire block, or until we
3740 have consumed a non-nested `;'. */
3741
3742 static void
3743 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3744 {
3745 int nesting_depth = 0;
3746
3747 /* Unwind generic function template scope if necessary. */
3748 if (parser->fully_implicit_function_template_p)
3749 abort_fully_implicit_template (parser);
3750
3751 while (nesting_depth >= 0)
3752 {
3753 cp_token *token = cp_lexer_peek_token (parser->lexer);
3754
3755 switch (token->type)
3756 {
3757 case CPP_PRAGMA_EOL:
3758 if (!parser->lexer->in_pragma)
3759 break;
3760 /* FALLTHRU */
3761 case CPP_EOF:
3762 /* If we've run out of tokens, stop. */
3763 return;
3764
3765 case CPP_SEMICOLON:
3766 /* Stop if this is an unnested ';'. */
3767 if (!nesting_depth)
3768 nesting_depth = -1;
3769 break;
3770
3771 case CPP_CLOSE_BRACE:
3772 /* Stop if this is an unnested '}', or closes the outermost
3773 nesting level. */
3774 nesting_depth--;
3775 if (nesting_depth < 0)
3776 return;
3777 if (!nesting_depth)
3778 nesting_depth = -1;
3779 break;
3780
3781 case CPP_OPEN_BRACE:
3782 /* Nest. */
3783 nesting_depth++;
3784 break;
3785
3786 default:
3787 break;
3788 }
3789
3790 /* Consume the token. */
3791 cp_lexer_consume_token (parser->lexer);
3792 }
3793 }
3794
3795 /* Skip tokens until a non-nested closing curly brace is the next
3796 token, or there are no more tokens. Return true in the first case,
3797 false otherwise. */
3798
3799 static bool
3800 cp_parser_skip_to_closing_brace (cp_parser *parser)
3801 {
3802 unsigned nesting_depth = 0;
3803
3804 while (true)
3805 {
3806 cp_token *token = cp_lexer_peek_token (parser->lexer);
3807
3808 switch (token->type)
3809 {
3810 case CPP_PRAGMA_EOL:
3811 if (!parser->lexer->in_pragma)
3812 break;
3813 /* FALLTHRU */
3814 case CPP_EOF:
3815 /* If we've run out of tokens, stop. */
3816 return false;
3817
3818 case CPP_CLOSE_BRACE:
3819 /* If the next token is a non-nested `}', then we have reached
3820 the end of the current block. */
3821 if (nesting_depth-- == 0)
3822 return true;
3823 break;
3824
3825 case CPP_OPEN_BRACE:
3826 /* If it the next token is a `{', then we are entering a new
3827 block. Consume the entire block. */
3828 ++nesting_depth;
3829 break;
3830
3831 default:
3832 break;
3833 }
3834
3835 /* Consume the token. */
3836 cp_lexer_consume_token (parser->lexer);
3837 }
3838 }
3839
3840 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3841 parameter is the PRAGMA token, allowing us to purge the entire pragma
3842 sequence. */
3843
3844 static void
3845 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3846 {
3847 cp_token *token;
3848
3849 parser->lexer->in_pragma = false;
3850
3851 do
3852 token = cp_lexer_consume_token (parser->lexer);
3853 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3854
3855 /* Ensure that the pragma is not parsed again. */
3856 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3857 }
3858
3859 /* Require pragma end of line, resyncing with it as necessary. The
3860 arguments are as for cp_parser_skip_to_pragma_eol. */
3861
3862 static void
3863 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3864 {
3865 parser->lexer->in_pragma = false;
3866 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3867 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3868 }
3869
3870 /* This is a simple wrapper around make_typename_type. When the id is
3871 an unresolved identifier node, we can provide a superior diagnostic
3872 using cp_parser_diagnose_invalid_type_name. */
3873
3874 static tree
3875 cp_parser_make_typename_type (cp_parser *parser, tree id,
3876 location_t id_location)
3877 {
3878 tree result;
3879 if (identifier_p (id))
3880 {
3881 result = make_typename_type (parser->scope, id, typename_type,
3882 /*complain=*/tf_none);
3883 if (result == error_mark_node)
3884 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3885 return result;
3886 }
3887 return make_typename_type (parser->scope, id, typename_type, tf_error);
3888 }
3889
3890 /* This is a wrapper around the
3891 make_{pointer,ptrmem,reference}_declarator functions that decides
3892 which one to call based on the CODE and CLASS_TYPE arguments. The
3893 CODE argument should be one of the values returned by
3894 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3895 appertain to the pointer or reference. */
3896
3897 static cp_declarator *
3898 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3899 cp_cv_quals cv_qualifiers,
3900 cp_declarator *target,
3901 tree attributes)
3902 {
3903 if (code == ERROR_MARK || target == cp_error_declarator)
3904 return cp_error_declarator;
3905
3906 if (code == INDIRECT_REF)
3907 if (class_type == NULL_TREE)
3908 return make_pointer_declarator (cv_qualifiers, target, attributes);
3909 else
3910 return make_ptrmem_declarator (cv_qualifiers, class_type,
3911 target, attributes);
3912 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3913 return make_reference_declarator (cv_qualifiers, target,
3914 false, attributes);
3915 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3916 return make_reference_declarator (cv_qualifiers, target,
3917 true, attributes);
3918 gcc_unreachable ();
3919 }
3920
3921 /* Create a new C++ parser. */
3922
3923 static cp_parser *
3924 cp_parser_new (void)
3925 {
3926 cp_parser *parser;
3927 cp_lexer *lexer;
3928 unsigned i;
3929
3930 /* cp_lexer_new_main is called before doing GC allocation because
3931 cp_lexer_new_main might load a PCH file. */
3932 lexer = cp_lexer_new_main ();
3933
3934 /* Initialize the binops_by_token so that we can get the tree
3935 directly from the token. */
3936 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3937 binops_by_token[binops[i].token_type] = binops[i];
3938
3939 parser = ggc_cleared_alloc<cp_parser> ();
3940 parser->lexer = lexer;
3941 parser->context = cp_parser_context_new (NULL);
3942
3943 /* For now, we always accept GNU extensions. */
3944 parser->allow_gnu_extensions_p = 1;
3945
3946 /* The `>' token is a greater-than operator, not the end of a
3947 template-id. */
3948 parser->greater_than_is_operator_p = true;
3949
3950 parser->default_arg_ok_p = true;
3951
3952 /* We are not parsing a constant-expression. */
3953 parser->integral_constant_expression_p = false;
3954 parser->allow_non_integral_constant_expression_p = false;
3955 parser->non_integral_constant_expression_p = false;
3956
3957 /* Local variable names are not forbidden. */
3958 parser->local_variables_forbidden_p = 0;
3959
3960 /* We are not processing an `extern "C"' declaration. */
3961 parser->in_unbraced_linkage_specification_p = false;
3962
3963 /* We are not processing a declarator. */
3964 parser->in_declarator_p = false;
3965
3966 /* We are not processing a template-argument-list. */
3967 parser->in_template_argument_list_p = false;
3968
3969 /* We are not in an iteration statement. */
3970 parser->in_statement = 0;
3971
3972 /* We are not in a switch statement. */
3973 parser->in_switch_statement_p = false;
3974
3975 /* We are not parsing a type-id inside an expression. */
3976 parser->in_type_id_in_expr_p = false;
3977
3978 /* String literals should be translated to the execution character set. */
3979 parser->translate_strings_p = true;
3980
3981 /* We are not parsing a function body. */
3982 parser->in_function_body = false;
3983
3984 /* We can correct until told otherwise. */
3985 parser->colon_corrects_to_scope_p = true;
3986
3987 /* The unparsed function queue is empty. */
3988 push_unparsed_function_queues (parser);
3989
3990 /* There are no classes being defined. */
3991 parser->num_classes_being_defined = 0;
3992
3993 /* No template parameters apply. */
3994 parser->num_template_parameter_lists = 0;
3995
3996 /* Special parsing data structures. */
3997 parser->omp_declare_simd = NULL;
3998 parser->oacc_routine = NULL;
3999
4000 /* Not declaring an implicit function template. */
4001 parser->auto_is_implicit_function_template_parm_p = false;
4002 parser->fully_implicit_function_template_p = false;
4003 parser->implicit_template_parms = 0;
4004 parser->implicit_template_scope = 0;
4005
4006 /* Allow constrained-type-specifiers. */
4007 parser->prevent_constrained_type_specifiers = 0;
4008
4009 /* We haven't yet seen an 'extern "C"'. */
4010 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4011
4012 return parser;
4013 }
4014
4015 /* Create a cp_lexer structure which will emit the tokens in CACHE
4016 and push it onto the parser's lexer stack. This is used for delayed
4017 parsing of in-class method bodies and default arguments, and should
4018 not be confused with tentative parsing. */
4019 static void
4020 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4021 {
4022 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4023 lexer->next = parser->lexer;
4024 parser->lexer = lexer;
4025
4026 /* Move the current source position to that of the first token in the
4027 new lexer. */
4028 cp_lexer_set_source_position_from_token (lexer->next_token);
4029 }
4030
4031 /* Pop the top lexer off the parser stack. This is never used for the
4032 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4033 static void
4034 cp_parser_pop_lexer (cp_parser *parser)
4035 {
4036 cp_lexer *lexer = parser->lexer;
4037 parser->lexer = lexer->next;
4038 cp_lexer_destroy (lexer);
4039
4040 /* Put the current source position back where it was before this
4041 lexer was pushed. */
4042 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4043 }
4044
4045 /* Lexical conventions [gram.lex] */
4046
4047 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4048 identifier. */
4049
4050 static cp_expr
4051 cp_parser_identifier (cp_parser* parser)
4052 {
4053 cp_token *token;
4054
4055 /* Look for the identifier. */
4056 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4057 /* Return the value. */
4058 if (token)
4059 return cp_expr (token->u.value, token->location);
4060 else
4061 return error_mark_node;
4062 }
4063
4064 /* Parse a sequence of adjacent string constants. Returns a
4065 TREE_STRING representing the combined, nul-terminated string
4066 constant. If TRANSLATE is true, translate the string to the
4067 execution character set. If WIDE_OK is true, a wide string is
4068 invalid here.
4069
4070 C++98 [lex.string] says that if a narrow string literal token is
4071 adjacent to a wide string literal token, the behavior is undefined.
4072 However, C99 6.4.5p4 says that this results in a wide string literal.
4073 We follow C99 here, for consistency with the C front end.
4074
4075 This code is largely lifted from lex_string() in c-lex.c.
4076
4077 FUTURE: ObjC++ will need to handle @-strings here. */
4078 static cp_expr
4079 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4080 bool lookup_udlit = true)
4081 {
4082 tree value;
4083 size_t count;
4084 struct obstack str_ob;
4085 struct obstack loc_ob;
4086 cpp_string str, istr, *strs;
4087 cp_token *tok;
4088 enum cpp_ttype type, curr_type;
4089 int have_suffix_p = 0;
4090 tree string_tree;
4091 tree suffix_id = NULL_TREE;
4092 bool curr_tok_is_userdef_p = false;
4093
4094 tok = cp_lexer_peek_token (parser->lexer);
4095 if (!cp_parser_is_string_literal (tok))
4096 {
4097 cp_parser_error (parser, "expected string-literal");
4098 return error_mark_node;
4099 }
4100
4101 location_t loc = tok->location;
4102
4103 if (cpp_userdef_string_p (tok->type))
4104 {
4105 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4106 curr_type = cpp_userdef_string_remove_type (tok->type);
4107 curr_tok_is_userdef_p = true;
4108 }
4109 else
4110 {
4111 string_tree = tok->u.value;
4112 curr_type = tok->type;
4113 }
4114 type = curr_type;
4115
4116 /* Try to avoid the overhead of creating and destroying an obstack
4117 for the common case of just one string. */
4118 if (!cp_parser_is_string_literal
4119 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4120 {
4121 cp_lexer_consume_token (parser->lexer);
4122
4123 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4124 str.len = TREE_STRING_LENGTH (string_tree);
4125 count = 1;
4126
4127 if (curr_tok_is_userdef_p)
4128 {
4129 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4130 have_suffix_p = 1;
4131 curr_type = cpp_userdef_string_remove_type (tok->type);
4132 }
4133 else
4134 curr_type = tok->type;
4135
4136 strs = &str;
4137 }
4138 else
4139 {
4140 location_t last_tok_loc = tok->location;
4141 gcc_obstack_init (&str_ob);
4142 gcc_obstack_init (&loc_ob);
4143 count = 0;
4144
4145 do
4146 {
4147 cp_lexer_consume_token (parser->lexer);
4148 count++;
4149 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4150 str.len = TREE_STRING_LENGTH (string_tree);
4151
4152 if (curr_tok_is_userdef_p)
4153 {
4154 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4155 if (have_suffix_p == 0)
4156 {
4157 suffix_id = curr_suffix_id;
4158 have_suffix_p = 1;
4159 }
4160 else if (have_suffix_p == 1
4161 && curr_suffix_id != suffix_id)
4162 {
4163 error ("inconsistent user-defined literal suffixes"
4164 " %qD and %qD in string literal",
4165 suffix_id, curr_suffix_id);
4166 have_suffix_p = -1;
4167 }
4168 curr_type = cpp_userdef_string_remove_type (tok->type);
4169 }
4170 else
4171 curr_type = tok->type;
4172
4173 if (type != curr_type)
4174 {
4175 if (type == CPP_STRING)
4176 type = curr_type;
4177 else if (curr_type != CPP_STRING)
4178 {
4179 rich_location rich_loc (line_table, tok->location);
4180 rich_loc.add_range (last_tok_loc);
4181 error_at (&rich_loc,
4182 "unsupported non-standard concatenation "
4183 "of string literals");
4184 }
4185 }
4186
4187 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4188 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4189
4190 last_tok_loc = tok->location;
4191
4192 tok = cp_lexer_peek_token (parser->lexer);
4193 if (cpp_userdef_string_p (tok->type))
4194 {
4195 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4196 curr_type = cpp_userdef_string_remove_type (tok->type);
4197 curr_tok_is_userdef_p = true;
4198 }
4199 else
4200 {
4201 string_tree = tok->u.value;
4202 curr_type = tok->type;
4203 curr_tok_is_userdef_p = false;
4204 }
4205 }
4206 while (cp_parser_is_string_literal (tok));
4207
4208 /* A string literal built by concatenation has its caret=start at
4209 the start of the initial string, and its finish at the finish of
4210 the final string literal. */
4211 loc = make_location (loc, loc, get_finish (last_tok_loc));
4212
4213 strs = (cpp_string *) obstack_finish (&str_ob);
4214 }
4215
4216 if (type != CPP_STRING && !wide_ok)
4217 {
4218 cp_parser_error (parser, "a wide string is invalid in this context");
4219 type = CPP_STRING;
4220 }
4221
4222 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4223 (parse_in, strs, count, &istr, type))
4224 {
4225 value = build_string (istr.len, (const char *)istr.text);
4226 free (CONST_CAST (unsigned char *, istr.text));
4227 if (count > 1)
4228 {
4229 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4230 gcc_assert (g_string_concat_db);
4231 g_string_concat_db->record_string_concatenation (count, locs);
4232 }
4233
4234 switch (type)
4235 {
4236 default:
4237 case CPP_STRING:
4238 case CPP_UTF8STRING:
4239 TREE_TYPE (value) = char_array_type_node;
4240 break;
4241 case CPP_STRING16:
4242 TREE_TYPE (value) = char16_array_type_node;
4243 break;
4244 case CPP_STRING32:
4245 TREE_TYPE (value) = char32_array_type_node;
4246 break;
4247 case CPP_WSTRING:
4248 TREE_TYPE (value) = wchar_array_type_node;
4249 break;
4250 }
4251
4252 value = fix_string_type (value);
4253
4254 if (have_suffix_p)
4255 {
4256 tree literal = build_userdef_literal (suffix_id, value,
4257 OT_NONE, NULL_TREE);
4258 if (lookup_udlit)
4259 value = cp_parser_userdef_string_literal (literal);
4260 else
4261 value = literal;
4262 }
4263 }
4264 else
4265 /* cpp_interpret_string has issued an error. */
4266 value = error_mark_node;
4267
4268 if (count > 1)
4269 {
4270 obstack_free (&str_ob, 0);
4271 obstack_free (&loc_ob, 0);
4272 }
4273
4274 return cp_expr (value, loc);
4275 }
4276
4277 /* Look up a literal operator with the name and the exact arguments. */
4278
4279 static tree
4280 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4281 {
4282 tree decl = lookup_name (name);
4283 if (!decl || !is_overloaded_fn (decl))
4284 return error_mark_node;
4285
4286 for (lkp_iterator iter (decl); iter; ++iter)
4287 {
4288 tree fn = *iter;
4289
4290 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4291 {
4292 unsigned int ix;
4293 bool found = true;
4294
4295 for (ix = 0;
4296 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4297 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4298 {
4299 tree tparm = TREE_VALUE (parmtypes);
4300 tree targ = TREE_TYPE ((*args)[ix]);
4301 bool ptr = TYPE_PTR_P (tparm);
4302 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4303 if ((ptr || arr || !same_type_p (tparm, targ))
4304 && (!ptr || !arr
4305 || !same_type_p (TREE_TYPE (tparm),
4306 TREE_TYPE (targ))))
4307 found = false;
4308 }
4309
4310 if (found
4311 && ix == vec_safe_length (args)
4312 /* May be this should be sufficient_parms_p instead,
4313 depending on how exactly should user-defined literals
4314 work in presence of default arguments on the literal
4315 operator parameters. */
4316 && parmtypes == void_list_node)
4317 return decl;
4318 }
4319 }
4320
4321 return error_mark_node;
4322 }
4323
4324 /* Parse a user-defined char constant. Returns a call to a user-defined
4325 literal operator taking the character as an argument. */
4326
4327 static cp_expr
4328 cp_parser_userdef_char_literal (cp_parser *parser)
4329 {
4330 cp_token *token = cp_lexer_consume_token (parser->lexer);
4331 tree literal = token->u.value;
4332 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4333 tree value = USERDEF_LITERAL_VALUE (literal);
4334 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4335 tree decl, result;
4336
4337 /* Build up a call to the user-defined operator */
4338 /* Lookup the name we got back from the id-expression. */
4339 vec<tree, va_gc> *args = make_tree_vector ();
4340 vec_safe_push (args, value);
4341 decl = lookup_literal_operator (name, args);
4342 if (!decl || decl == error_mark_node)
4343 {
4344 error ("unable to find character literal operator %qD with %qT argument",
4345 name, TREE_TYPE (value));
4346 release_tree_vector (args);
4347 return error_mark_node;
4348 }
4349 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4350 release_tree_vector (args);
4351 return result;
4352 }
4353
4354 /* A subroutine of cp_parser_userdef_numeric_literal to
4355 create a char... template parameter pack from a string node. */
4356
4357 static tree
4358 make_char_string_pack (tree value)
4359 {
4360 tree charvec;
4361 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4362 const char *str = TREE_STRING_POINTER (value);
4363 int i, len = TREE_STRING_LENGTH (value) - 1;
4364 tree argvec = make_tree_vec (1);
4365
4366 /* Fill in CHARVEC with all of the parameters. */
4367 charvec = make_tree_vec (len);
4368 for (i = 0; i < len; ++i)
4369 {
4370 unsigned char s[3] = { '\'', str[i], '\'' };
4371 cpp_string in = { 3, s };
4372 cpp_string out = { 0, 0 };
4373 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4374 return NULL_TREE;
4375 gcc_assert (out.len == 2);
4376 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4377 out.text[0]);
4378 }
4379
4380 /* Build the argument packs. */
4381 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4382
4383 TREE_VEC_ELT (argvec, 0) = argpack;
4384
4385 return argvec;
4386 }
4387
4388 /* A subroutine of cp_parser_userdef_numeric_literal to
4389 create a char... template parameter pack from a string node. */
4390
4391 static tree
4392 make_string_pack (tree value)
4393 {
4394 tree charvec;
4395 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4396 const unsigned char *str
4397 = (const unsigned char *) TREE_STRING_POINTER (value);
4398 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4399 int len = TREE_STRING_LENGTH (value) / sz - 1;
4400 tree argvec = make_tree_vec (2);
4401
4402 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4403 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4404
4405 /* First template parm is character type. */
4406 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4407
4408 /* Fill in CHARVEC with all of the parameters. */
4409 charvec = make_tree_vec (len);
4410 for (int i = 0; i < len; ++i)
4411 TREE_VEC_ELT (charvec, i)
4412 = double_int_to_tree (str_char_type_node,
4413 double_int::from_buffer (str + i * sz, sz));
4414
4415 /* Build the argument packs. */
4416 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4417
4418 TREE_VEC_ELT (argvec, 1) = argpack;
4419
4420 return argvec;
4421 }
4422
4423 /* Parse a user-defined numeric constant. returns a call to a user-defined
4424 literal operator. */
4425
4426 static cp_expr
4427 cp_parser_userdef_numeric_literal (cp_parser *parser)
4428 {
4429 cp_token *token = cp_lexer_consume_token (parser->lexer);
4430 tree literal = token->u.value;
4431 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4432 tree value = USERDEF_LITERAL_VALUE (literal);
4433 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4434 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4435 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4436 tree decl, result;
4437 vec<tree, va_gc> *args;
4438
4439 /* Look for a literal operator taking the exact type of numeric argument
4440 as the literal value. */
4441 args = make_tree_vector ();
4442 vec_safe_push (args, value);
4443 decl = lookup_literal_operator (name, args);
4444 if (decl && decl != error_mark_node)
4445 {
4446 result = finish_call_expr (decl, &args, false, true,
4447 tf_warning_or_error);
4448
4449 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4450 {
4451 warning_at (token->location, OPT_Woverflow,
4452 "integer literal exceeds range of %qT type",
4453 long_long_unsigned_type_node);
4454 }
4455 else
4456 {
4457 if (overflow > 0)
4458 warning_at (token->location, OPT_Woverflow,
4459 "floating literal exceeds range of %qT type",
4460 long_double_type_node);
4461 else if (overflow < 0)
4462 warning_at (token->location, OPT_Woverflow,
4463 "floating literal truncated to zero");
4464 }
4465
4466 release_tree_vector (args);
4467 return result;
4468 }
4469 release_tree_vector (args);
4470
4471 /* If the numeric argument didn't work, look for a raw literal
4472 operator taking a const char* argument consisting of the number
4473 in string format. */
4474 args = make_tree_vector ();
4475 vec_safe_push (args, num_string);
4476 decl = lookup_literal_operator (name, args);
4477 if (decl && decl != error_mark_node)
4478 {
4479 result = finish_call_expr (decl, &args, false, true,
4480 tf_warning_or_error);
4481 release_tree_vector (args);
4482 return result;
4483 }
4484 release_tree_vector (args);
4485
4486 /* If the raw literal didn't work, look for a non-type template
4487 function with parameter pack char.... Call the function with
4488 template parameter characters representing the number. */
4489 args = make_tree_vector ();
4490 decl = lookup_literal_operator (name, args);
4491 if (decl && decl != error_mark_node)
4492 {
4493 tree tmpl_args = make_char_string_pack (num_string);
4494 if (tmpl_args == NULL_TREE)
4495 {
4496 error ("failed to translate literal to execution character set %qT",
4497 num_string);
4498 return error_mark_node;
4499 }
4500 decl = lookup_template_function (decl, tmpl_args);
4501 result = finish_call_expr (decl, &args, false, true,
4502 tf_warning_or_error);
4503 release_tree_vector (args);
4504 return result;
4505 }
4506
4507 release_tree_vector (args);
4508
4509 /* In C++14 the standard library defines complex number suffixes that
4510 conflict with GNU extensions. Prefer them if <complex> is #included. */
4511 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4512 bool i14 = (cxx_dialect > cxx11
4513 && (id_equal (suffix_id, "i")
4514 || id_equal (suffix_id, "if")
4515 || id_equal (suffix_id, "il")));
4516 diagnostic_t kind = DK_ERROR;
4517 int opt = 0;
4518
4519 if (i14 && ext)
4520 {
4521 tree cxlit = lookup_qualified_name (std_node,
4522 get_identifier ("complex_literals"),
4523 0, false, false);
4524 if (cxlit == error_mark_node)
4525 {
4526 /* No <complex>, so pedwarn and use GNU semantics. */
4527 kind = DK_PEDWARN;
4528 opt = OPT_Wpedantic;
4529 }
4530 }
4531
4532 bool complained
4533 = emit_diagnostic (kind, input_location, opt,
4534 "unable to find numeric literal operator %qD", name);
4535
4536 if (!complained)
4537 /* Don't inform either. */;
4538 else if (i14)
4539 {
4540 inform (token->location, "add %<using namespace std::complex_literals%> "
4541 "(from <complex>) to enable the C++14 user-defined literal "
4542 "suffixes");
4543 if (ext)
4544 inform (token->location, "or use %<j%> instead of %<i%> for the "
4545 "GNU built-in suffix");
4546 }
4547 else if (!ext)
4548 inform (token->location, "use -fext-numeric-literals "
4549 "to enable more built-in suffixes");
4550
4551 if (kind == DK_ERROR)
4552 value = error_mark_node;
4553 else
4554 {
4555 /* Use the built-in semantics. */
4556 tree type;
4557 if (id_equal (suffix_id, "i"))
4558 {
4559 if (TREE_CODE (value) == INTEGER_CST)
4560 type = integer_type_node;
4561 else
4562 type = double_type_node;
4563 }
4564 else if (id_equal (suffix_id, "if"))
4565 type = float_type_node;
4566 else /* if (id_equal (suffix_id, "il")) */
4567 type = long_double_type_node;
4568
4569 value = build_complex (build_complex_type (type),
4570 fold_convert (type, integer_zero_node),
4571 fold_convert (type, value));
4572 }
4573
4574 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4575 /* Avoid repeated diagnostics. */
4576 token->u.value = value;
4577 return value;
4578 }
4579
4580 /* Parse a user-defined string constant. Returns a call to a user-defined
4581 literal operator taking a character pointer and the length of the string
4582 as arguments. */
4583
4584 static tree
4585 cp_parser_userdef_string_literal (tree literal)
4586 {
4587 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4588 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4589 tree value = USERDEF_LITERAL_VALUE (literal);
4590 int len = TREE_STRING_LENGTH (value)
4591 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4592 tree decl;
4593
4594 /* Build up a call to the user-defined operator. */
4595 /* Lookup the name we got back from the id-expression. */
4596 releasing_vec rargs;
4597 vec<tree, va_gc> *&args = rargs.get_ref();
4598 vec_safe_push (args, value);
4599 vec_safe_push (args, build_int_cst (size_type_node, len));
4600 decl = lookup_literal_operator (name, args);
4601
4602 if (decl && decl != error_mark_node)
4603 return finish_call_expr (decl, &args, false, true,
4604 tf_warning_or_error);
4605
4606 /* Look for a suitable template function, either (C++20) with a single
4607 parameter of class type, or (N3599) with typename parameter CharT and
4608 parameter pack CharT... */
4609 args->truncate (0);
4610 decl = lookup_literal_operator (name, args);
4611 if (decl && decl != error_mark_node)
4612 {
4613 /* Use resolve_nondeduced_context to try to choose one form of template
4614 or the other. */
4615 tree tmpl_args = make_tree_vec (1);
4616 TREE_VEC_ELT (tmpl_args, 0) = value;
4617 decl = lookup_template_function (decl, tmpl_args);
4618 tree res = resolve_nondeduced_context (decl, tf_none);
4619 if (DECL_P (res))
4620 decl = res;
4621 else
4622 {
4623 TREE_OPERAND (decl, 1) = make_string_pack (value);
4624 res = resolve_nondeduced_context (decl, tf_none);
4625 if (DECL_P (res))
4626 decl = res;
4627 }
4628 if (!DECL_P (decl) && cxx_dialect > cxx17)
4629 TREE_OPERAND (decl, 1) = tmpl_args;
4630 return finish_call_expr (decl, &args, false, true,
4631 tf_warning_or_error);
4632 }
4633
4634 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4635 name, TREE_TYPE (value), size_type_node);
4636 return error_mark_node;
4637 }
4638
4639
4640 /* Basic concepts [gram.basic] */
4641
4642 /* Parse a translation-unit.
4643
4644 translation-unit:
4645 declaration-seq [opt] */
4646
4647 static void
4648 cp_parser_translation_unit (cp_parser* parser)
4649 {
4650 gcc_checking_assert (!cp_error_declarator);
4651
4652 /* Create the declarator obstack. */
4653 gcc_obstack_init (&declarator_obstack);
4654 /* Create the error declarator. */
4655 cp_error_declarator = make_declarator (cdk_error);
4656 /* Create the empty parameter list. */
4657 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4658 UNKNOWN_LOCATION);
4659 /* Remember where the base of the declarator obstack lies. */
4660 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4661
4662 bool implicit_extern_c = false;
4663
4664 for (;;)
4665 {
4666 cp_token *token = cp_lexer_peek_token (parser->lexer);
4667
4668 /* If we're entering or exiting a region that's implicitly
4669 extern "C", modify the lang context appropriately. */
4670 if (implicit_extern_c
4671 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4672 {
4673 implicit_extern_c = !implicit_extern_c;
4674 if (implicit_extern_c)
4675 push_lang_context (lang_name_c);
4676 else
4677 pop_lang_context ();
4678 }
4679
4680 if (token->type == CPP_EOF)
4681 break;
4682
4683 if (token->type == CPP_CLOSE_BRACE)
4684 {
4685 cp_parser_error (parser, "expected declaration");
4686 cp_lexer_consume_token (parser->lexer);
4687 /* If the next token is now a `;', consume it. */
4688 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4689 cp_lexer_consume_token (parser->lexer);
4690 }
4691 else
4692 cp_parser_toplevel_declaration (parser);
4693 }
4694
4695 /* Get rid of the token array; we don't need it any more. */
4696 cp_lexer_destroy (parser->lexer);
4697 parser->lexer = NULL;
4698
4699 /* The EOF should have reset this. */
4700 gcc_checking_assert (!implicit_extern_c);
4701
4702 /* Make sure the declarator obstack was fully cleaned up. */
4703 gcc_assert (obstack_next_free (&declarator_obstack)
4704 == declarator_obstack_base);
4705 }
4706
4707 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4708 decltype context. */
4709
4710 static inline tsubst_flags_t
4711 complain_flags (bool decltype_p)
4712 {
4713 tsubst_flags_t complain = tf_warning_or_error;
4714 if (decltype_p)
4715 complain |= tf_decltype;
4716 return complain;
4717 }
4718
4719 /* We're about to parse a collection of statements. If we're currently
4720 parsing tentatively, set up a firewall so that any nested
4721 cp_parser_commit_to_tentative_parse won't affect the current context. */
4722
4723 static cp_token_position
4724 cp_parser_start_tentative_firewall (cp_parser *parser)
4725 {
4726 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4727 return 0;
4728
4729 cp_parser_parse_tentatively (parser);
4730 cp_parser_commit_to_topmost_tentative_parse (parser);
4731 return cp_lexer_token_position (parser->lexer, false);
4732 }
4733
4734 /* We've finished parsing the collection of statements. Wrap up the
4735 firewall and replace the relevant tokens with the parsed form. */
4736
4737 static void
4738 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4739 tree expr)
4740 {
4741 if (!start)
4742 return;
4743
4744 /* Finish the firewall level. */
4745 cp_parser_parse_definitely (parser);
4746 /* And remember the result of the parse for when we try again. */
4747 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4748 token->type = CPP_PREPARSED_EXPR;
4749 token->u.value = expr;
4750 token->keyword = RID_MAX;
4751 cp_lexer_purge_tokens_after (parser->lexer, start);
4752 }
4753
4754 /* Like the above functions, but let the user modify the tokens. Used by
4755 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4756 later parses, so it makes sense to localize the effects of
4757 cp_parser_commit_to_tentative_parse. */
4758
4759 struct tentative_firewall
4760 {
4761 cp_parser *parser;
4762 bool set;
4763
4764 tentative_firewall (cp_parser *p): parser(p)
4765 {
4766 /* If we're currently parsing tentatively, start a committed level as a
4767 firewall and then an inner tentative parse. */
4768 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4769 {
4770 cp_parser_parse_tentatively (parser);
4771 cp_parser_commit_to_topmost_tentative_parse (parser);
4772 cp_parser_parse_tentatively (parser);
4773 }
4774 }
4775
4776 ~tentative_firewall()
4777 {
4778 if (set)
4779 {
4780 /* Finish the inner tentative parse and the firewall, propagating any
4781 uncommitted error state to the outer tentative parse. */
4782 bool err = cp_parser_error_occurred (parser);
4783 cp_parser_parse_definitely (parser);
4784 cp_parser_parse_definitely (parser);
4785 if (err)
4786 cp_parser_simulate_error (parser);
4787 }
4788 }
4789 };
4790
4791 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4792 This class is for tracking such a matching pair of symbols.
4793 In particular, it tracks the location of the first token,
4794 so that if the second token is missing, we can highlight the
4795 location of the first token when notifying the user about the
4796 problem. */
4797
4798 template <typename traits_t>
4799 class token_pair
4800 {
4801 public:
4802 /* token_pair's ctor. */
4803 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4804
4805 /* If the next token is the opening symbol for this pair, consume it and
4806 return true.
4807 Otherwise, issue an error and return false.
4808 In either case, record the location of the opening token. */
4809
4810 bool require_open (cp_parser *parser)
4811 {
4812 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4813 return cp_parser_require (parser, traits_t::open_token_type,
4814 traits_t::required_token_open);
4815 }
4816
4817 /* Consume the next token from PARSER, recording its location as
4818 that of the opening token within the pair. */
4819
4820 cp_token * consume_open (cp_parser *parser)
4821 {
4822 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4823 gcc_assert (tok->type == traits_t::open_token_type);
4824 m_open_loc = tok->location;
4825 return tok;
4826 }
4827
4828 /* If the next token is the closing symbol for this pair, consume it
4829 and return it.
4830 Otherwise, issue an error, highlighting the location of the
4831 corresponding opening token, and return NULL. */
4832
4833 cp_token *require_close (cp_parser *parser) const
4834 {
4835 return cp_parser_require (parser, traits_t::close_token_type,
4836 traits_t::required_token_close,
4837 m_open_loc);
4838 }
4839
4840 private:
4841 location_t m_open_loc;
4842 };
4843
4844 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4845
4846 struct matching_paren_traits
4847 {
4848 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4849 static const enum required_token required_token_open = RT_OPEN_PAREN;
4850 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4851 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4852 };
4853
4854 /* "matching_parens" is a token_pair<T> class for tracking matching
4855 pairs of parentheses. */
4856
4857 typedef token_pair<matching_paren_traits> matching_parens;
4858
4859 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4860
4861 struct matching_brace_traits
4862 {
4863 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4864 static const enum required_token required_token_open = RT_OPEN_BRACE;
4865 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4866 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4867 };
4868
4869 /* "matching_braces" is a token_pair<T> class for tracking matching
4870 pairs of braces. */
4871
4872 typedef token_pair<matching_brace_traits> matching_braces;
4873
4874
4875 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4876 enclosing parentheses. */
4877
4878 static cp_expr
4879 cp_parser_statement_expr (cp_parser *parser)
4880 {
4881 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4882
4883 /* Consume the '('. */
4884 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4885 matching_parens parens;
4886 parens.consume_open (parser);
4887 /* Start the statement-expression. */
4888 tree expr = begin_stmt_expr ();
4889 /* Parse the compound-statement. */
4890 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4891 /* Finish up. */
4892 expr = finish_stmt_expr (expr, false);
4893 /* Consume the ')'. */
4894 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4895 if (!parens.require_close (parser))
4896 cp_parser_skip_to_end_of_statement (parser);
4897
4898 cp_parser_end_tentative_firewall (parser, start, expr);
4899 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4900 return cp_expr (expr, combined_loc);
4901 }
4902
4903 /* Expressions [gram.expr] */
4904
4905 /* Parse a fold-operator.
4906
4907 fold-operator:
4908 - * / % ^ & | = < > << >>
4909 = -= *= /= %= ^= &= |= <<= >>=
4910 == != <= >= && || , .* ->*
4911
4912 This returns the tree code corresponding to the matched operator
4913 as an int. When the current token matches a compound assignment
4914 opertor, the resulting tree code is the negative value of the
4915 non-assignment operator. */
4916
4917 static int
4918 cp_parser_fold_operator (cp_token *token)
4919 {
4920 switch (token->type)
4921 {
4922 case CPP_PLUS: return PLUS_EXPR;
4923 case CPP_MINUS: return MINUS_EXPR;
4924 case CPP_MULT: return MULT_EXPR;
4925 case CPP_DIV: return TRUNC_DIV_EXPR;
4926 case CPP_MOD: return TRUNC_MOD_EXPR;
4927 case CPP_XOR: return BIT_XOR_EXPR;
4928 case CPP_AND: return BIT_AND_EXPR;
4929 case CPP_OR: return BIT_IOR_EXPR;
4930 case CPP_LSHIFT: return LSHIFT_EXPR;
4931 case CPP_RSHIFT: return RSHIFT_EXPR;
4932
4933 case CPP_EQ: return -NOP_EXPR;
4934 case CPP_PLUS_EQ: return -PLUS_EXPR;
4935 case CPP_MINUS_EQ: return -MINUS_EXPR;
4936 case CPP_MULT_EQ: return -MULT_EXPR;
4937 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4938 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4939 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4940 case CPP_AND_EQ: return -BIT_AND_EXPR;
4941 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4942 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4943 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4944
4945 case CPP_EQ_EQ: return EQ_EXPR;
4946 case CPP_NOT_EQ: return NE_EXPR;
4947 case CPP_LESS: return LT_EXPR;
4948 case CPP_GREATER: return GT_EXPR;
4949 case CPP_LESS_EQ: return LE_EXPR;
4950 case CPP_GREATER_EQ: return GE_EXPR;
4951
4952 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4953 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4954
4955 case CPP_COMMA: return COMPOUND_EXPR;
4956
4957 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4958 case CPP_DEREF_STAR: return MEMBER_REF;
4959
4960 default: return ERROR_MARK;
4961 }
4962 }
4963
4964 /* Returns true if CODE indicates a binary expression, which is not allowed in
4965 the LHS of a fold-expression. More codes will need to be added to use this
4966 function in other contexts. */
4967
4968 static bool
4969 is_binary_op (tree_code code)
4970 {
4971 switch (code)
4972 {
4973 case PLUS_EXPR:
4974 case POINTER_PLUS_EXPR:
4975 case MINUS_EXPR:
4976 case MULT_EXPR:
4977 case TRUNC_DIV_EXPR:
4978 case TRUNC_MOD_EXPR:
4979 case BIT_XOR_EXPR:
4980 case BIT_AND_EXPR:
4981 case BIT_IOR_EXPR:
4982 case LSHIFT_EXPR:
4983 case RSHIFT_EXPR:
4984
4985 case MODOP_EXPR:
4986
4987 case EQ_EXPR:
4988 case NE_EXPR:
4989 case LE_EXPR:
4990 case GE_EXPR:
4991 case LT_EXPR:
4992 case GT_EXPR:
4993
4994 case TRUTH_ANDIF_EXPR:
4995 case TRUTH_ORIF_EXPR:
4996
4997 case COMPOUND_EXPR:
4998
4999 case DOTSTAR_EXPR:
5000 case MEMBER_REF:
5001 return true;
5002
5003 default:
5004 return false;
5005 }
5006 }
5007
5008 /* If the next token is a suitable fold operator, consume it and return as
5009 the function above. */
5010
5011 static int
5012 cp_parser_fold_operator (cp_parser *parser)
5013 {
5014 cp_token* token = cp_lexer_peek_token (parser->lexer);
5015 int code = cp_parser_fold_operator (token);
5016 if (code != ERROR_MARK)
5017 cp_lexer_consume_token (parser->lexer);
5018 return code;
5019 }
5020
5021 /* Parse a fold-expression.
5022
5023 fold-expression:
5024 ( ... folding-operator cast-expression)
5025 ( cast-expression folding-operator ... )
5026 ( cast-expression folding operator ... folding-operator cast-expression)
5027
5028 Note that the '(' and ')' are matched in primary expression. */
5029
5030 static cp_expr
5031 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5032 {
5033 cp_id_kind pidk;
5034
5035 // Left fold.
5036 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5037 {
5038 cp_lexer_consume_token (parser->lexer);
5039 int op = cp_parser_fold_operator (parser);
5040 if (op == ERROR_MARK)
5041 {
5042 cp_parser_error (parser, "expected binary operator");
5043 return error_mark_node;
5044 }
5045
5046 tree expr = cp_parser_cast_expression (parser, false, false,
5047 false, &pidk);
5048 if (expr == error_mark_node)
5049 return error_mark_node;
5050 return finish_left_unary_fold_expr (expr, op);
5051 }
5052
5053 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5054 int op = cp_parser_fold_operator (parser);
5055 if (op == ERROR_MARK)
5056 {
5057 cp_parser_error (parser, "expected binary operator");
5058 return error_mark_node;
5059 }
5060
5061 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5062 {
5063 cp_parser_error (parser, "expected ...");
5064 return error_mark_node;
5065 }
5066 cp_lexer_consume_token (parser->lexer);
5067
5068 /* The operands of a fold-expression are cast-expressions, so binary or
5069 conditional expressions are not allowed. We check this here to avoid
5070 tentative parsing. */
5071 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5072 /* OK, the expression was parenthesized. */;
5073 else if (is_binary_op (TREE_CODE (expr1)))
5074 error_at (location_of (expr1),
5075 "binary expression in operand of fold-expression");
5076 else if (TREE_CODE (expr1) == COND_EXPR
5077 || (REFERENCE_REF_P (expr1)
5078 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5079 error_at (location_of (expr1),
5080 "conditional expression in operand of fold-expression");
5081
5082 // Right fold.
5083 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5084 return finish_right_unary_fold_expr (expr1, op);
5085
5086 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5087 {
5088 cp_parser_error (parser, "mismatched operator in fold-expression");
5089 return error_mark_node;
5090 }
5091 cp_lexer_consume_token (parser->lexer);
5092
5093 // Binary left or right fold.
5094 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5095 if (expr2 == error_mark_node)
5096 return error_mark_node;
5097 return finish_binary_fold_expr (expr1, expr2, op);
5098 }
5099
5100 /* Parse a primary-expression.
5101
5102 primary-expression:
5103 literal
5104 this
5105 ( expression )
5106 id-expression
5107 lambda-expression (C++11)
5108
5109 GNU Extensions:
5110
5111 primary-expression:
5112 ( compound-statement )
5113 __builtin_va_arg ( assignment-expression , type-id )
5114 __builtin_offsetof ( type-id , offsetof-expression )
5115
5116 C++ Extensions:
5117 __has_nothrow_assign ( type-id )
5118 __has_nothrow_constructor ( type-id )
5119 __has_nothrow_copy ( type-id )
5120 __has_trivial_assign ( type-id )
5121 __has_trivial_constructor ( type-id )
5122 __has_trivial_copy ( type-id )
5123 __has_trivial_destructor ( type-id )
5124 __has_virtual_destructor ( type-id )
5125 __is_abstract ( type-id )
5126 __is_base_of ( type-id , type-id )
5127 __is_class ( type-id )
5128 __is_empty ( type-id )
5129 __is_enum ( type-id )
5130 __is_final ( type-id )
5131 __is_literal_type ( type-id )
5132 __is_pod ( type-id )
5133 __is_polymorphic ( type-id )
5134 __is_std_layout ( type-id )
5135 __is_trivial ( type-id )
5136 __is_union ( type-id )
5137
5138 Objective-C++ Extension:
5139
5140 primary-expression:
5141 objc-expression
5142
5143 literal:
5144 __null
5145
5146 ADDRESS_P is true iff this expression was immediately preceded by
5147 "&" and therefore might denote a pointer-to-member. CAST_P is true
5148 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5149 true iff this expression is a template argument.
5150
5151 Returns a representation of the expression. Upon return, *IDK
5152 indicates what kind of id-expression (if any) was present. */
5153
5154 static cp_expr
5155 cp_parser_primary_expression (cp_parser *parser,
5156 bool address_p,
5157 bool cast_p,
5158 bool template_arg_p,
5159 bool decltype_p,
5160 cp_id_kind *idk)
5161 {
5162 cp_token *token = NULL;
5163
5164 /* Assume the primary expression is not an id-expression. */
5165 *idk = CP_ID_KIND_NONE;
5166
5167 /* Peek at the next token. */
5168 token = cp_lexer_peek_token (parser->lexer);
5169 switch ((int) token->type)
5170 {
5171 /* literal:
5172 integer-literal
5173 character-literal
5174 floating-literal
5175 string-literal
5176 boolean-literal
5177 pointer-literal
5178 user-defined-literal */
5179 case CPP_CHAR:
5180 case CPP_CHAR16:
5181 case CPP_CHAR32:
5182 case CPP_WCHAR:
5183 case CPP_UTF8CHAR:
5184 case CPP_NUMBER:
5185 case CPP_PREPARSED_EXPR:
5186 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5187 return cp_parser_userdef_numeric_literal (parser);
5188 token = cp_lexer_consume_token (parser->lexer);
5189 if (TREE_CODE (token->u.value) == FIXED_CST)
5190 {
5191 error_at (token->location,
5192 "fixed-point types not supported in C++");
5193 return error_mark_node;
5194 }
5195 /* Floating-point literals are only allowed in an integral
5196 constant expression if they are cast to an integral or
5197 enumeration type. */
5198 if (TREE_CODE (token->u.value) == REAL_CST
5199 && parser->integral_constant_expression_p
5200 && pedantic)
5201 {
5202 /* CAST_P will be set even in invalid code like "int(2.7 +
5203 ...)". Therefore, we have to check that the next token
5204 is sure to end the cast. */
5205 if (cast_p)
5206 {
5207 cp_token *next_token;
5208
5209 next_token = cp_lexer_peek_token (parser->lexer);
5210 if (/* The comma at the end of an
5211 enumerator-definition. */
5212 next_token->type != CPP_COMMA
5213 /* The curly brace at the end of an enum-specifier. */
5214 && next_token->type != CPP_CLOSE_BRACE
5215 /* The end of a statement. */
5216 && next_token->type != CPP_SEMICOLON
5217 /* The end of the cast-expression. */
5218 && next_token->type != CPP_CLOSE_PAREN
5219 /* The end of an array bound. */
5220 && next_token->type != CPP_CLOSE_SQUARE
5221 /* The closing ">" in a template-argument-list. */
5222 && (next_token->type != CPP_GREATER
5223 || parser->greater_than_is_operator_p)
5224 /* C++0x only: A ">>" treated like two ">" tokens,
5225 in a template-argument-list. */
5226 && (next_token->type != CPP_RSHIFT
5227 || (cxx_dialect == cxx98)
5228 || parser->greater_than_is_operator_p))
5229 cast_p = false;
5230 }
5231
5232 /* If we are within a cast, then the constraint that the
5233 cast is to an integral or enumeration type will be
5234 checked at that point. If we are not within a cast, then
5235 this code is invalid. */
5236 if (!cast_p)
5237 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5238 }
5239 return (cp_expr (token->u.value, token->location)
5240 .maybe_add_location_wrapper ());
5241
5242 case CPP_CHAR_USERDEF:
5243 case CPP_CHAR16_USERDEF:
5244 case CPP_CHAR32_USERDEF:
5245 case CPP_WCHAR_USERDEF:
5246 case CPP_UTF8CHAR_USERDEF:
5247 return cp_parser_userdef_char_literal (parser);
5248
5249 case CPP_STRING:
5250 case CPP_STRING16:
5251 case CPP_STRING32:
5252 case CPP_WSTRING:
5253 case CPP_UTF8STRING:
5254 case CPP_STRING_USERDEF:
5255 case CPP_STRING16_USERDEF:
5256 case CPP_STRING32_USERDEF:
5257 case CPP_WSTRING_USERDEF:
5258 case CPP_UTF8STRING_USERDEF:
5259 /* ??? Should wide strings be allowed when parser->translate_strings_p
5260 is false (i.e. in attributes)? If not, we can kill the third
5261 argument to cp_parser_string_literal. */
5262 return (cp_parser_string_literal (parser,
5263 parser->translate_strings_p,
5264 true)
5265 .maybe_add_location_wrapper ());
5266
5267 case CPP_OPEN_PAREN:
5268 /* If we see `( { ' then we are looking at the beginning of
5269 a GNU statement-expression. */
5270 if (cp_parser_allow_gnu_extensions_p (parser)
5271 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5272 {
5273 /* Statement-expressions are not allowed by the standard. */
5274 pedwarn (token->location, OPT_Wpedantic,
5275 "ISO C++ forbids braced-groups within expressions");
5276
5277 /* And they're not allowed outside of a function-body; you
5278 cannot, for example, write:
5279
5280 int i = ({ int j = 3; j + 1; });
5281
5282 at class or namespace scope. */
5283 if (!parser->in_function_body
5284 || parser->in_template_argument_list_p)
5285 {
5286 error_at (token->location,
5287 "statement-expressions are not allowed outside "
5288 "functions nor in template-argument lists");
5289 cp_parser_skip_to_end_of_block_or_statement (parser);
5290 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5291 cp_lexer_consume_token (parser->lexer);
5292 return error_mark_node;
5293 }
5294 else
5295 return cp_parser_statement_expr (parser);
5296 }
5297 /* Otherwise it's a normal parenthesized expression. */
5298 {
5299 cp_expr expr;
5300 bool saved_greater_than_is_operator_p;
5301
5302 location_t open_paren_loc = token->location;
5303
5304 /* Consume the `('. */
5305 matching_parens parens;
5306 parens.consume_open (parser);
5307 /* Within a parenthesized expression, a `>' token is always
5308 the greater-than operator. */
5309 saved_greater_than_is_operator_p
5310 = parser->greater_than_is_operator_p;
5311 parser->greater_than_is_operator_p = true;
5312
5313 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5314 /* Left fold expression. */
5315 expr = NULL_TREE;
5316 else
5317 /* Parse the parenthesized expression. */
5318 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5319
5320 token = cp_lexer_peek_token (parser->lexer);
5321 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5322 {
5323 expr = cp_parser_fold_expression (parser, expr);
5324 if (expr != error_mark_node
5325 && cxx_dialect < cxx17
5326 && !in_system_header_at (input_location))
5327 pedwarn (input_location, 0, "fold-expressions only available "
5328 "with -std=c++17 or -std=gnu++17");
5329 }
5330 else
5331 /* Let the front end know that this expression was
5332 enclosed in parentheses. This matters in case, for
5333 example, the expression is of the form `A::B', since
5334 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5335 not. */
5336 expr = finish_parenthesized_expr (expr);
5337
5338 /* DR 705: Wrapping an unqualified name in parentheses
5339 suppresses arg-dependent lookup. We want to pass back
5340 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5341 (c++/37862), but none of the others. */
5342 if (*idk != CP_ID_KIND_QUALIFIED)
5343 *idk = CP_ID_KIND_NONE;
5344
5345 /* The `>' token might be the end of a template-id or
5346 template-parameter-list now. */
5347 parser->greater_than_is_operator_p
5348 = saved_greater_than_is_operator_p;
5349
5350 /* Consume the `)'. */
5351 token = cp_lexer_peek_token (parser->lexer);
5352 location_t close_paren_loc = token->location;
5353 expr.set_range (open_paren_loc, close_paren_loc);
5354 if (!parens.require_close (parser)
5355 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5356 cp_parser_skip_to_end_of_statement (parser);
5357
5358 return expr;
5359 }
5360
5361 case CPP_OPEN_SQUARE:
5362 {
5363 if (c_dialect_objc ())
5364 {
5365 /* We might have an Objective-C++ message. */
5366 cp_parser_parse_tentatively (parser);
5367 tree msg = cp_parser_objc_message_expression (parser);
5368 /* If that works out, we're done ... */
5369 if (cp_parser_parse_definitely (parser))
5370 return msg;
5371 /* ... else, fall though to see if it's a lambda. */
5372 }
5373 cp_expr lam = cp_parser_lambda_expression (parser);
5374 /* Don't warn about a failed tentative parse. */
5375 if (cp_parser_error_occurred (parser))
5376 return error_mark_node;
5377 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5378 return lam;
5379 }
5380
5381 case CPP_OBJC_STRING:
5382 if (c_dialect_objc ())
5383 /* We have an Objective-C++ string literal. */
5384 return cp_parser_objc_expression (parser);
5385 cp_parser_error (parser, "expected primary-expression");
5386 return error_mark_node;
5387
5388 case CPP_KEYWORD:
5389 switch (token->keyword)
5390 {
5391 /* These two are the boolean literals. */
5392 case RID_TRUE:
5393 cp_lexer_consume_token (parser->lexer);
5394 return cp_expr (boolean_true_node, token->location);
5395 case RID_FALSE:
5396 cp_lexer_consume_token (parser->lexer);
5397 return cp_expr (boolean_false_node, token->location);
5398
5399 /* The `__null' literal. */
5400 case RID_NULL:
5401 cp_lexer_consume_token (parser->lexer);
5402 return cp_expr (null_node, token->location);
5403
5404 /* The `nullptr' literal. */
5405 case RID_NULLPTR:
5406 cp_lexer_consume_token (parser->lexer);
5407 return cp_expr (nullptr_node, token->location);
5408
5409 /* Recognize the `this' keyword. */
5410 case RID_THIS:
5411 cp_lexer_consume_token (parser->lexer);
5412 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5413 {
5414 error_at (token->location,
5415 "%<this%> may not be used in this context");
5416 return error_mark_node;
5417 }
5418 /* Pointers cannot appear in constant-expressions. */
5419 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5420 return error_mark_node;
5421 return cp_expr (finish_this_expr (), token->location);
5422
5423 /* The `operator' keyword can be the beginning of an
5424 id-expression. */
5425 case RID_OPERATOR:
5426 goto id_expression;
5427
5428 case RID_FUNCTION_NAME:
5429 case RID_PRETTY_FUNCTION_NAME:
5430 case RID_C99_FUNCTION_NAME:
5431 {
5432 non_integral_constant name;
5433
5434 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5435 __func__ are the names of variables -- but they are
5436 treated specially. Therefore, they are handled here,
5437 rather than relying on the generic id-expression logic
5438 below. Grammatically, these names are id-expressions.
5439
5440 Consume the token. */
5441 token = cp_lexer_consume_token (parser->lexer);
5442
5443 switch (token->keyword)
5444 {
5445 case RID_FUNCTION_NAME:
5446 name = NIC_FUNC_NAME;
5447 break;
5448 case RID_PRETTY_FUNCTION_NAME:
5449 name = NIC_PRETTY_FUNC;
5450 break;
5451 case RID_C99_FUNCTION_NAME:
5452 name = NIC_C99_FUNC;
5453 break;
5454 default:
5455 gcc_unreachable ();
5456 }
5457
5458 if (cp_parser_non_integral_constant_expression (parser, name))
5459 return error_mark_node;
5460
5461 /* Look up the name. */
5462 return finish_fname (token->u.value);
5463 }
5464
5465 case RID_VA_ARG:
5466 {
5467 tree expression;
5468 tree type;
5469 location_t type_location;
5470 location_t start_loc
5471 = cp_lexer_peek_token (parser->lexer)->location;
5472 /* The `__builtin_va_arg' construct is used to handle
5473 `va_arg'. Consume the `__builtin_va_arg' token. */
5474 cp_lexer_consume_token (parser->lexer);
5475 /* Look for the opening `('. */
5476 matching_parens parens;
5477 parens.require_open (parser);
5478 /* Now, parse the assignment-expression. */
5479 expression = cp_parser_assignment_expression (parser);
5480 /* Look for the `,'. */
5481 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5482 type_location = cp_lexer_peek_token (parser->lexer)->location;
5483 /* Parse the type-id. */
5484 {
5485 type_id_in_expr_sentinel s (parser);
5486 type = cp_parser_type_id (parser);
5487 }
5488 /* Look for the closing `)'. */
5489 location_t finish_loc
5490 = cp_lexer_peek_token (parser->lexer)->location;
5491 parens.require_close (parser);
5492 /* Using `va_arg' in a constant-expression is not
5493 allowed. */
5494 if (cp_parser_non_integral_constant_expression (parser,
5495 NIC_VA_ARG))
5496 return error_mark_node;
5497 /* Construct a location of the form:
5498 __builtin_va_arg (v, int)
5499 ~~~~~~~~~~~~~~~~~~~~~^~~~
5500 with the caret at the type, ranging from the start of the
5501 "__builtin_va_arg" token to the close paren. */
5502 location_t combined_loc
5503 = make_location (type_location, start_loc, finish_loc);
5504 return build_x_va_arg (combined_loc, expression, type);
5505 }
5506
5507 case RID_OFFSETOF:
5508 return cp_parser_builtin_offsetof (parser);
5509
5510 case RID_HAS_NOTHROW_ASSIGN:
5511 case RID_HAS_NOTHROW_CONSTRUCTOR:
5512 case RID_HAS_NOTHROW_COPY:
5513 case RID_HAS_TRIVIAL_ASSIGN:
5514 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5515 case RID_HAS_TRIVIAL_COPY:
5516 case RID_HAS_TRIVIAL_DESTRUCTOR:
5517 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5518 case RID_HAS_VIRTUAL_DESTRUCTOR:
5519 case RID_IS_ABSTRACT:
5520 case RID_IS_AGGREGATE:
5521 case RID_IS_BASE_OF:
5522 case RID_IS_CLASS:
5523 case RID_IS_EMPTY:
5524 case RID_IS_ENUM:
5525 case RID_IS_FINAL:
5526 case RID_IS_LITERAL_TYPE:
5527 case RID_IS_POD:
5528 case RID_IS_POLYMORPHIC:
5529 case RID_IS_SAME_AS:
5530 case RID_IS_STD_LAYOUT:
5531 case RID_IS_TRIVIAL:
5532 case RID_IS_TRIVIALLY_ASSIGNABLE:
5533 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5534 case RID_IS_TRIVIALLY_COPYABLE:
5535 case RID_IS_UNION:
5536 case RID_IS_ASSIGNABLE:
5537 case RID_IS_CONSTRUCTIBLE:
5538 return cp_parser_trait_expr (parser, token->keyword);
5539
5540 // C++ concepts
5541 case RID_REQUIRES:
5542 return cp_parser_requires_expression (parser);
5543
5544 /* Objective-C++ expressions. */
5545 case RID_AT_ENCODE:
5546 case RID_AT_PROTOCOL:
5547 case RID_AT_SELECTOR:
5548 return cp_parser_objc_expression (parser);
5549
5550 case RID_TEMPLATE:
5551 if (parser->in_function_body
5552 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5553 == CPP_LESS))
5554 {
5555 error_at (token->location,
5556 "a template declaration cannot appear at block scope");
5557 cp_parser_skip_to_end_of_block_or_statement (parser);
5558 return error_mark_node;
5559 }
5560 /* FALLTHRU */
5561 default:
5562 cp_parser_error (parser, "expected primary-expression");
5563 return error_mark_node;
5564 }
5565
5566 /* An id-expression can start with either an identifier, a
5567 `::' as the beginning of a qualified-id, or the "operator"
5568 keyword. */
5569 case CPP_NAME:
5570 case CPP_SCOPE:
5571 case CPP_TEMPLATE_ID:
5572 case CPP_NESTED_NAME_SPECIFIER:
5573 {
5574 id_expression:
5575 cp_expr id_expression;
5576 cp_expr decl;
5577 const char *error_msg;
5578 bool template_p;
5579 bool done;
5580 cp_token *id_expr_token;
5581
5582 /* Parse the id-expression. */
5583 id_expression
5584 = cp_parser_id_expression (parser,
5585 /*template_keyword_p=*/false,
5586 /*check_dependency_p=*/true,
5587 &template_p,
5588 /*declarator_p=*/false,
5589 /*optional_p=*/false);
5590 if (id_expression == error_mark_node)
5591 return error_mark_node;
5592 id_expr_token = token;
5593 token = cp_lexer_peek_token (parser->lexer);
5594 done = (token->type != CPP_OPEN_SQUARE
5595 && token->type != CPP_OPEN_PAREN
5596 && token->type != CPP_DOT
5597 && token->type != CPP_DEREF
5598 && token->type != CPP_PLUS_PLUS
5599 && token->type != CPP_MINUS_MINUS);
5600 /* If we have a template-id, then no further lookup is
5601 required. If the template-id was for a template-class, we
5602 will sometimes have a TYPE_DECL at this point. */
5603 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5604 || TREE_CODE (id_expression) == TYPE_DECL)
5605 decl = id_expression;
5606 /* Look up the name. */
5607 else
5608 {
5609 tree ambiguous_decls;
5610
5611 /* If we already know that this lookup is ambiguous, then
5612 we've already issued an error message; there's no reason
5613 to check again. */
5614 if (id_expr_token->type == CPP_NAME
5615 && id_expr_token->error_reported)
5616 {
5617 cp_parser_simulate_error (parser);
5618 return error_mark_node;
5619 }
5620
5621 decl = cp_parser_lookup_name (parser, id_expression,
5622 none_type,
5623 template_p,
5624 /*is_namespace=*/false,
5625 /*check_dependency=*/true,
5626 &ambiguous_decls,
5627 id_expression.get_location ());
5628 /* If the lookup was ambiguous, an error will already have
5629 been issued. */
5630 if (ambiguous_decls)
5631 return error_mark_node;
5632
5633 /* In Objective-C++, we may have an Objective-C 2.0
5634 dot-syntax for classes here. */
5635 if (c_dialect_objc ()
5636 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5637 && TREE_CODE (decl) == TYPE_DECL
5638 && objc_is_class_name (decl))
5639 {
5640 tree component;
5641 cp_lexer_consume_token (parser->lexer);
5642 component = cp_parser_identifier (parser);
5643 if (component == error_mark_node)
5644 return error_mark_node;
5645
5646 tree result = objc_build_class_component_ref (id_expression,
5647 component);
5648 /* Build a location of the form:
5649 expr.component
5650 ~~~~~^~~~~~~~~
5651 with caret at the start of the component name (at
5652 input_location), ranging from the start of the id_expression
5653 to the end of the component name. */
5654 location_t combined_loc
5655 = make_location (input_location, id_expression.get_start (),
5656 get_finish (input_location));
5657 protected_set_expr_location (result, combined_loc);
5658 return result;
5659 }
5660
5661 /* In Objective-C++, an instance variable (ivar) may be preferred
5662 to whatever cp_parser_lookup_name() found.
5663 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5664 rest of c-family, we have to do a little extra work to preserve
5665 any location information in cp_expr "decl". Given that
5666 objc_lookup_ivar is implemented in "c-family" and "objc", we
5667 have a trip through the pure "tree" type, rather than cp_expr.
5668 Naively copying it back to "decl" would implicitly give the
5669 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5670 store an EXPR_LOCATION. Hence we only update "decl" (and
5671 hence its location_t) if we get back a different tree node. */
5672 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5673 id_expression);
5674 if (decl_tree != decl.get_value ())
5675 decl = cp_expr (decl_tree);
5676
5677 /* If name lookup gives us a SCOPE_REF, then the
5678 qualifying scope was dependent. */
5679 if (TREE_CODE (decl) == SCOPE_REF)
5680 {
5681 /* At this point, we do not know if DECL is a valid
5682 integral constant expression. We assume that it is
5683 in fact such an expression, so that code like:
5684
5685 template <int N> struct A {
5686 int a[B<N>::i];
5687 };
5688
5689 is accepted. At template-instantiation time, we
5690 will check that B<N>::i is actually a constant. */
5691 return decl;
5692 }
5693 /* Check to see if DECL is a local variable in a context
5694 where that is forbidden. */
5695 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
5696 && local_variable_p (decl))
5697 {
5698 error_at (id_expression.get_location (),
5699 "local variable %qD may not appear in this context",
5700 decl.get_value ());
5701 return error_mark_node;
5702 }
5703 }
5704
5705 decl = (finish_id_expression
5706 (id_expression, decl, parser->scope,
5707 idk,
5708 parser->integral_constant_expression_p,
5709 parser->allow_non_integral_constant_expression_p,
5710 &parser->non_integral_constant_expression_p,
5711 template_p, done, address_p,
5712 template_arg_p,
5713 &error_msg,
5714 id_expression.get_location ()));
5715 if (error_msg)
5716 cp_parser_error (parser, error_msg);
5717 /* Build a location for an id-expression of the form:
5718 ::ns::id
5719 ~~~~~~^~
5720 or:
5721 id
5722 ^~
5723 i.e. from the start of the first token to the end of the final
5724 token, with the caret at the start of the unqualified-id. */
5725 location_t caret_loc = get_pure_location (id_expression.get_location ());
5726 location_t start_loc = get_start (id_expr_token->location);
5727 location_t finish_loc = get_finish (id_expression.get_location ());
5728 location_t combined_loc
5729 = make_location (caret_loc, start_loc, finish_loc);
5730
5731 decl.set_location (combined_loc);
5732 return decl;
5733 }
5734
5735 /* Anything else is an error. */
5736 default:
5737 cp_parser_error (parser, "expected primary-expression");
5738 return error_mark_node;
5739 }
5740 }
5741
5742 static inline cp_expr
5743 cp_parser_primary_expression (cp_parser *parser,
5744 bool address_p,
5745 bool cast_p,
5746 bool template_arg_p,
5747 cp_id_kind *idk)
5748 {
5749 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5750 /*decltype*/false, idk);
5751 }
5752
5753 /* Parse an id-expression.
5754
5755 id-expression:
5756 unqualified-id
5757 qualified-id
5758
5759 qualified-id:
5760 :: [opt] nested-name-specifier template [opt] unqualified-id
5761 :: identifier
5762 :: operator-function-id
5763 :: template-id
5764
5765 Return a representation of the unqualified portion of the
5766 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5767 a `::' or nested-name-specifier.
5768
5769 Often, if the id-expression was a qualified-id, the caller will
5770 want to make a SCOPE_REF to represent the qualified-id. This
5771 function does not do this in order to avoid wastefully creating
5772 SCOPE_REFs when they are not required.
5773
5774 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5775 `template' keyword.
5776
5777 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5778 uninstantiated templates.
5779
5780 If *TEMPLATE_P is non-NULL, it is set to true iff the
5781 `template' keyword is used to explicitly indicate that the entity
5782 named is a template.
5783
5784 If DECLARATOR_P is true, the id-expression is appearing as part of
5785 a declarator, rather than as part of an expression. */
5786
5787 static cp_expr
5788 cp_parser_id_expression (cp_parser *parser,
5789 bool template_keyword_p,
5790 bool check_dependency_p,
5791 bool *template_p,
5792 bool declarator_p,
5793 bool optional_p)
5794 {
5795 bool global_scope_p;
5796 bool nested_name_specifier_p;
5797
5798 /* Assume the `template' keyword was not used. */
5799 if (template_p)
5800 *template_p = template_keyword_p;
5801
5802 /* Look for the optional `::' operator. */
5803 global_scope_p
5804 = (!template_keyword_p
5805 && (cp_parser_global_scope_opt (parser,
5806 /*current_scope_valid_p=*/false)
5807 != NULL_TREE));
5808
5809 /* Look for the optional nested-name-specifier. */
5810 nested_name_specifier_p
5811 = (cp_parser_nested_name_specifier_opt (parser,
5812 /*typename_keyword_p=*/false,
5813 check_dependency_p,
5814 /*type_p=*/false,
5815 declarator_p,
5816 template_keyword_p)
5817 != NULL_TREE);
5818
5819 /* If there is a nested-name-specifier, then we are looking at
5820 the first qualified-id production. */
5821 if (nested_name_specifier_p)
5822 {
5823 tree saved_scope;
5824 tree saved_object_scope;
5825 tree saved_qualifying_scope;
5826 cp_expr unqualified_id;
5827 bool is_template;
5828
5829 /* See if the next token is the `template' keyword. */
5830 if (!template_p)
5831 template_p = &is_template;
5832 *template_p = cp_parser_optional_template_keyword (parser);
5833 /* Name lookup we do during the processing of the
5834 unqualified-id might obliterate SCOPE. */
5835 saved_scope = parser->scope;
5836 saved_object_scope = parser->object_scope;
5837 saved_qualifying_scope = parser->qualifying_scope;
5838 /* Process the final unqualified-id. */
5839 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5840 check_dependency_p,
5841 declarator_p,
5842 /*optional_p=*/false);
5843 /* Restore the SAVED_SCOPE for our caller. */
5844 parser->scope = saved_scope;
5845 parser->object_scope = saved_object_scope;
5846 parser->qualifying_scope = saved_qualifying_scope;
5847
5848 return unqualified_id;
5849 }
5850 /* Otherwise, if we are in global scope, then we are looking at one
5851 of the other qualified-id productions. */
5852 else if (global_scope_p)
5853 {
5854 cp_token *token;
5855 tree id;
5856
5857 /* Peek at the next token. */
5858 token = cp_lexer_peek_token (parser->lexer);
5859
5860 /* If it's an identifier, and the next token is not a "<", then
5861 we can avoid the template-id case. This is an optimization
5862 for this common case. */
5863 if (token->type == CPP_NAME
5864 && !cp_parser_nth_token_starts_template_argument_list_p
5865 (parser, 2))
5866 return cp_parser_identifier (parser);
5867
5868 cp_parser_parse_tentatively (parser);
5869 /* Try a template-id. */
5870 id = cp_parser_template_id (parser,
5871 /*template_keyword_p=*/false,
5872 /*check_dependency_p=*/true,
5873 none_type,
5874 declarator_p);
5875 /* If that worked, we're done. */
5876 if (cp_parser_parse_definitely (parser))
5877 return id;
5878
5879 /* Peek at the next token. (Changes in the token buffer may
5880 have invalidated the pointer obtained above.) */
5881 token = cp_lexer_peek_token (parser->lexer);
5882
5883 switch (token->type)
5884 {
5885 case CPP_NAME:
5886 return cp_parser_identifier (parser);
5887
5888 case CPP_KEYWORD:
5889 if (token->keyword == RID_OPERATOR)
5890 return cp_parser_operator_function_id (parser);
5891 /* Fall through. */
5892
5893 default:
5894 cp_parser_error (parser, "expected id-expression");
5895 return error_mark_node;
5896 }
5897 }
5898 else
5899 return cp_parser_unqualified_id (parser, template_keyword_p,
5900 /*check_dependency_p=*/true,
5901 declarator_p,
5902 optional_p);
5903 }
5904
5905 /* Parse an unqualified-id.
5906
5907 unqualified-id:
5908 identifier
5909 operator-function-id
5910 conversion-function-id
5911 ~ class-name
5912 template-id
5913
5914 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5915 keyword, in a construct like `A::template ...'.
5916
5917 Returns a representation of unqualified-id. For the `identifier'
5918 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5919 production a BIT_NOT_EXPR is returned; the operand of the
5920 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5921 other productions, see the documentation accompanying the
5922 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5923 names are looked up in uninstantiated templates. If DECLARATOR_P
5924 is true, the unqualified-id is appearing as part of a declarator,
5925 rather than as part of an expression. */
5926
5927 static cp_expr
5928 cp_parser_unqualified_id (cp_parser* parser,
5929 bool template_keyword_p,
5930 bool check_dependency_p,
5931 bool declarator_p,
5932 bool optional_p)
5933 {
5934 cp_token *token;
5935
5936 /* Peek at the next token. */
5937 token = cp_lexer_peek_token (parser->lexer);
5938
5939 switch ((int) token->type)
5940 {
5941 case CPP_NAME:
5942 {
5943 tree id;
5944
5945 /* We don't know yet whether or not this will be a
5946 template-id. */
5947 cp_parser_parse_tentatively (parser);
5948 /* Try a template-id. */
5949 id = cp_parser_template_id (parser, template_keyword_p,
5950 check_dependency_p,
5951 none_type,
5952 declarator_p);
5953 /* If it worked, we're done. */
5954 if (cp_parser_parse_definitely (parser))
5955 return id;
5956 /* Otherwise, it's an ordinary identifier. */
5957 return cp_parser_identifier (parser);
5958 }
5959
5960 case CPP_TEMPLATE_ID:
5961 return cp_parser_template_id (parser, template_keyword_p,
5962 check_dependency_p,
5963 none_type,
5964 declarator_p);
5965
5966 case CPP_COMPL:
5967 {
5968 tree type_decl;
5969 tree qualifying_scope;
5970 tree object_scope;
5971 tree scope;
5972 bool done;
5973
5974 /* Consume the `~' token. */
5975 cp_lexer_consume_token (parser->lexer);
5976 /* Parse the class-name. The standard, as written, seems to
5977 say that:
5978
5979 template <typename T> struct S { ~S (); };
5980 template <typename T> S<T>::~S() {}
5981
5982 is invalid, since `~' must be followed by a class-name, but
5983 `S<T>' is dependent, and so not known to be a class.
5984 That's not right; we need to look in uninstantiated
5985 templates. A further complication arises from:
5986
5987 template <typename T> void f(T t) {
5988 t.T::~T();
5989 }
5990
5991 Here, it is not possible to look up `T' in the scope of `T'
5992 itself. We must look in both the current scope, and the
5993 scope of the containing complete expression.
5994
5995 Yet another issue is:
5996
5997 struct S {
5998 int S;
5999 ~S();
6000 };
6001
6002 S::~S() {}
6003
6004 The standard does not seem to say that the `S' in `~S'
6005 should refer to the type `S' and not the data member
6006 `S::S'. */
6007
6008 /* DR 244 says that we look up the name after the "~" in the
6009 same scope as we looked up the qualifying name. That idea
6010 isn't fully worked out; it's more complicated than that. */
6011 scope = parser->scope;
6012 object_scope = parser->object_scope;
6013 qualifying_scope = parser->qualifying_scope;
6014
6015 /* Check for invalid scopes. */
6016 if (scope == error_mark_node)
6017 {
6018 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6019 cp_lexer_consume_token (parser->lexer);
6020 return error_mark_node;
6021 }
6022 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6023 {
6024 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6025 error_at (token->location,
6026 "scope %qT before %<~%> is not a class-name",
6027 scope);
6028 cp_parser_simulate_error (parser);
6029 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6030 cp_lexer_consume_token (parser->lexer);
6031 return error_mark_node;
6032 }
6033 gcc_assert (!scope || TYPE_P (scope));
6034
6035 /* If the name is of the form "X::~X" it's OK even if X is a
6036 typedef. */
6037 token = cp_lexer_peek_token (parser->lexer);
6038 if (scope
6039 && token->type == CPP_NAME
6040 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6041 != CPP_LESS)
6042 && (token->u.value == TYPE_IDENTIFIER (scope)
6043 || (CLASS_TYPE_P (scope)
6044 && constructor_name_p (token->u.value, scope))))
6045 {
6046 cp_lexer_consume_token (parser->lexer);
6047 return build_nt (BIT_NOT_EXPR, scope);
6048 }
6049
6050 /* ~auto means the destructor of whatever the object is. */
6051 if (cp_parser_is_keyword (token, RID_AUTO))
6052 {
6053 if (cxx_dialect < cxx14)
6054 pedwarn (input_location, 0,
6055 "%<~auto%> only available with "
6056 "-std=c++14 or -std=gnu++14");
6057 cp_lexer_consume_token (parser->lexer);
6058 return build_nt (BIT_NOT_EXPR, make_auto ());
6059 }
6060
6061 /* If there was an explicit qualification (S::~T), first look
6062 in the scope given by the qualification (i.e., S).
6063
6064 Note: in the calls to cp_parser_class_name below we pass
6065 typename_type so that lookup finds the injected-class-name
6066 rather than the constructor. */
6067 done = false;
6068 type_decl = NULL_TREE;
6069 if (scope)
6070 {
6071 cp_parser_parse_tentatively (parser);
6072 type_decl = cp_parser_class_name (parser,
6073 /*typename_keyword_p=*/false,
6074 /*template_keyword_p=*/false,
6075 typename_type,
6076 /*check_dependency=*/false,
6077 /*class_head_p=*/false,
6078 declarator_p);
6079 if (cp_parser_parse_definitely (parser))
6080 done = true;
6081 }
6082 /* In "N::S::~S", look in "N" as well. */
6083 if (!done && scope && qualifying_scope)
6084 {
6085 cp_parser_parse_tentatively (parser);
6086 parser->scope = qualifying_scope;
6087 parser->object_scope = NULL_TREE;
6088 parser->qualifying_scope = NULL_TREE;
6089 type_decl
6090 = cp_parser_class_name (parser,
6091 /*typename_keyword_p=*/false,
6092 /*template_keyword_p=*/false,
6093 typename_type,
6094 /*check_dependency=*/false,
6095 /*class_head_p=*/false,
6096 declarator_p);
6097 if (cp_parser_parse_definitely (parser))
6098 done = true;
6099 }
6100 /* In "p->S::~T", look in the scope given by "*p" as well. */
6101 else if (!done && object_scope)
6102 {
6103 cp_parser_parse_tentatively (parser);
6104 parser->scope = object_scope;
6105 parser->object_scope = NULL_TREE;
6106 parser->qualifying_scope = NULL_TREE;
6107 type_decl
6108 = cp_parser_class_name (parser,
6109 /*typename_keyword_p=*/false,
6110 /*template_keyword_p=*/false,
6111 typename_type,
6112 /*check_dependency=*/false,
6113 /*class_head_p=*/false,
6114 declarator_p);
6115 if (cp_parser_parse_definitely (parser))
6116 done = true;
6117 }
6118 /* Look in the surrounding context. */
6119 if (!done)
6120 {
6121 parser->scope = NULL_TREE;
6122 parser->object_scope = NULL_TREE;
6123 parser->qualifying_scope = NULL_TREE;
6124 if (processing_template_decl)
6125 cp_parser_parse_tentatively (parser);
6126 type_decl
6127 = cp_parser_class_name (parser,
6128 /*typename_keyword_p=*/false,
6129 /*template_keyword_p=*/false,
6130 typename_type,
6131 /*check_dependency=*/false,
6132 /*class_head_p=*/false,
6133 declarator_p);
6134 if (processing_template_decl
6135 && ! cp_parser_parse_definitely (parser))
6136 {
6137 /* We couldn't find a type with this name. If we're parsing
6138 tentatively, fail and try something else. */
6139 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6140 {
6141 cp_parser_simulate_error (parser);
6142 return error_mark_node;
6143 }
6144 /* Otherwise, accept it and check for a match at instantiation
6145 time. */
6146 type_decl = cp_parser_identifier (parser);
6147 if (type_decl != error_mark_node)
6148 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6149 return type_decl;
6150 }
6151 }
6152 /* If an error occurred, assume that the name of the
6153 destructor is the same as the name of the qualifying
6154 class. That allows us to keep parsing after running
6155 into ill-formed destructor names. */
6156 if (type_decl == error_mark_node && scope)
6157 return build_nt (BIT_NOT_EXPR, scope);
6158 else if (type_decl == error_mark_node)
6159 return error_mark_node;
6160
6161 /* Check that destructor name and scope match. */
6162 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6163 {
6164 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6165 error_at (token->location,
6166 "declaration of %<~%T%> as member of %qT",
6167 type_decl, scope);
6168 cp_parser_simulate_error (parser);
6169 return error_mark_node;
6170 }
6171
6172 /* [class.dtor]
6173
6174 A typedef-name that names a class shall not be used as the
6175 identifier in the declarator for a destructor declaration. */
6176 if (declarator_p
6177 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6178 && !DECL_SELF_REFERENCE_P (type_decl)
6179 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6180 error_at (token->location,
6181 "typedef-name %qD used as destructor declarator",
6182 type_decl);
6183
6184 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6185 }
6186
6187 case CPP_KEYWORD:
6188 if (token->keyword == RID_OPERATOR)
6189 {
6190 cp_expr id;
6191
6192 /* This could be a template-id, so we try that first. */
6193 cp_parser_parse_tentatively (parser);
6194 /* Try a template-id. */
6195 id = cp_parser_template_id (parser, template_keyword_p,
6196 /*check_dependency_p=*/true,
6197 none_type,
6198 declarator_p);
6199 /* If that worked, we're done. */
6200 if (cp_parser_parse_definitely (parser))
6201 return id;
6202 /* We still don't know whether we're looking at an
6203 operator-function-id or a conversion-function-id. */
6204 cp_parser_parse_tentatively (parser);
6205 /* Try an operator-function-id. */
6206 id = cp_parser_operator_function_id (parser);
6207 /* If that didn't work, try a conversion-function-id. */
6208 if (!cp_parser_parse_definitely (parser))
6209 id = cp_parser_conversion_function_id (parser);
6210
6211 return id;
6212 }
6213 /* Fall through. */
6214
6215 default:
6216 if (optional_p)
6217 return NULL_TREE;
6218 cp_parser_error (parser, "expected unqualified-id");
6219 return error_mark_node;
6220 }
6221 }
6222
6223 /* Parse an (optional) nested-name-specifier.
6224
6225 nested-name-specifier: [C++98]
6226 class-or-namespace-name :: nested-name-specifier [opt]
6227 class-or-namespace-name :: template nested-name-specifier [opt]
6228
6229 nested-name-specifier: [C++0x]
6230 type-name ::
6231 namespace-name ::
6232 nested-name-specifier identifier ::
6233 nested-name-specifier template [opt] simple-template-id ::
6234
6235 PARSER->SCOPE should be set appropriately before this function is
6236 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6237 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6238 in name lookups.
6239
6240 Sets PARSER->SCOPE to the class (TYPE) or namespace
6241 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6242 it unchanged if there is no nested-name-specifier. Returns the new
6243 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6244
6245 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6246 part of a declaration and/or decl-specifier. */
6247
6248 static tree
6249 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6250 bool typename_keyword_p,
6251 bool check_dependency_p,
6252 bool type_p,
6253 bool is_declaration,
6254 bool template_keyword_p /* = false */)
6255 {
6256 bool success = false;
6257 cp_token_position start = 0;
6258 cp_token *token;
6259
6260 /* Remember where the nested-name-specifier starts. */
6261 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6262 {
6263 start = cp_lexer_token_position (parser->lexer, false);
6264 push_deferring_access_checks (dk_deferred);
6265 }
6266
6267 while (true)
6268 {
6269 tree new_scope;
6270 tree old_scope;
6271 tree saved_qualifying_scope;
6272
6273 /* Spot cases that cannot be the beginning of a
6274 nested-name-specifier. */
6275 token = cp_lexer_peek_token (parser->lexer);
6276
6277 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6278 the already parsed nested-name-specifier. */
6279 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6280 {
6281 /* Grab the nested-name-specifier and continue the loop. */
6282 cp_parser_pre_parsed_nested_name_specifier (parser);
6283 /* If we originally encountered this nested-name-specifier
6284 with IS_DECLARATION set to false, we will not have
6285 resolved TYPENAME_TYPEs, so we must do so here. */
6286 if (is_declaration
6287 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6288 {
6289 new_scope = resolve_typename_type (parser->scope,
6290 /*only_current_p=*/false);
6291 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6292 parser->scope = new_scope;
6293 }
6294 success = true;
6295 continue;
6296 }
6297
6298 /* Spot cases that cannot be the beginning of a
6299 nested-name-specifier. On the second and subsequent times
6300 through the loop, we look for the `template' keyword. */
6301 if (success && token->keyword == RID_TEMPLATE)
6302 ;
6303 /* A template-id can start a nested-name-specifier. */
6304 else if (token->type == CPP_TEMPLATE_ID)
6305 ;
6306 /* DR 743: decltype can be used in a nested-name-specifier. */
6307 else if (token_is_decltype (token))
6308 ;
6309 else
6310 {
6311 /* If the next token is not an identifier, then it is
6312 definitely not a type-name or namespace-name. */
6313 if (token->type != CPP_NAME)
6314 break;
6315 /* If the following token is neither a `<' (to begin a
6316 template-id), nor a `::', then we are not looking at a
6317 nested-name-specifier. */
6318 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6319
6320 if (token->type == CPP_COLON
6321 && parser->colon_corrects_to_scope_p
6322 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6323 {
6324 gcc_rich_location richloc (token->location);
6325 richloc.add_fixit_replace ("::");
6326 error_at (&richloc,
6327 "found %<:%> in nested-name-specifier, "
6328 "expected %<::%>");
6329 token->type = CPP_SCOPE;
6330 }
6331
6332 if (token->type != CPP_SCOPE
6333 && !cp_parser_nth_token_starts_template_argument_list_p
6334 (parser, 2))
6335 break;
6336 }
6337
6338 /* The nested-name-specifier is optional, so we parse
6339 tentatively. */
6340 cp_parser_parse_tentatively (parser);
6341
6342 /* Look for the optional `template' keyword, if this isn't the
6343 first time through the loop. */
6344 if (success)
6345 template_keyword_p = cp_parser_optional_template_keyword (parser);
6346
6347 /* Save the old scope since the name lookup we are about to do
6348 might destroy it. */
6349 old_scope = parser->scope;
6350 saved_qualifying_scope = parser->qualifying_scope;
6351 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6352 look up names in "X<T>::I" in order to determine that "Y" is
6353 a template. So, if we have a typename at this point, we make
6354 an effort to look through it. */
6355 if (is_declaration
6356 && !typename_keyword_p
6357 && parser->scope
6358 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6359 parser->scope = resolve_typename_type (parser->scope,
6360 /*only_current_p=*/false);
6361 /* Parse the qualifying entity. */
6362 new_scope
6363 = cp_parser_qualifying_entity (parser,
6364 typename_keyword_p,
6365 template_keyword_p,
6366 check_dependency_p,
6367 type_p,
6368 is_declaration);
6369 /* Look for the `::' token. */
6370 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6371
6372 /* If we found what we wanted, we keep going; otherwise, we're
6373 done. */
6374 if (!cp_parser_parse_definitely (parser))
6375 {
6376 bool error_p = false;
6377
6378 /* Restore the OLD_SCOPE since it was valid before the
6379 failed attempt at finding the last
6380 class-or-namespace-name. */
6381 parser->scope = old_scope;
6382 parser->qualifying_scope = saved_qualifying_scope;
6383
6384 /* If the next token is a decltype, and the one after that is a
6385 `::', then the decltype has failed to resolve to a class or
6386 enumeration type. Give this error even when parsing
6387 tentatively since it can't possibly be valid--and we're going
6388 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6389 won't get another chance.*/
6390 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6391 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6392 == CPP_SCOPE))
6393 {
6394 token = cp_lexer_consume_token (parser->lexer);
6395 error_at (token->location, "decltype evaluates to %qT, "
6396 "which is not a class or enumeration type",
6397 token->u.tree_check_value->value);
6398 parser->scope = error_mark_node;
6399 error_p = true;
6400 /* As below. */
6401 success = true;
6402 cp_lexer_consume_token (parser->lexer);
6403 }
6404
6405 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6406 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6407 {
6408 /* If we have a non-type template-id followed by ::, it can't
6409 possibly be valid. */
6410 token = cp_lexer_peek_token (parser->lexer);
6411 tree tid = token->u.tree_check_value->value;
6412 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6413 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6414 {
6415 tree tmpl = NULL_TREE;
6416 if (is_overloaded_fn (tid))
6417 {
6418 tree fns = get_fns (tid);
6419 if (OVL_SINGLE_P (fns))
6420 tmpl = OVL_FIRST (fns);
6421 error_at (token->location, "function template-id %qD "
6422 "in nested-name-specifier", tid);
6423 }
6424 else
6425 {
6426 /* Variable template. */
6427 tmpl = TREE_OPERAND (tid, 0);
6428 gcc_assert (variable_template_p (tmpl));
6429 error_at (token->location, "variable template-id %qD "
6430 "in nested-name-specifier", tid);
6431 }
6432 if (tmpl)
6433 inform (DECL_SOURCE_LOCATION (tmpl),
6434 "%qD declared here", tmpl);
6435
6436 parser->scope = error_mark_node;
6437 error_p = true;
6438 /* As below. */
6439 success = true;
6440 cp_lexer_consume_token (parser->lexer);
6441 cp_lexer_consume_token (parser->lexer);
6442 }
6443 }
6444
6445 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6446 break;
6447 /* If the next token is an identifier, and the one after
6448 that is a `::', then any valid interpretation would have
6449 found a class-or-namespace-name. */
6450 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6451 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6452 == CPP_SCOPE)
6453 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6454 != CPP_COMPL))
6455 {
6456 token = cp_lexer_consume_token (parser->lexer);
6457 if (!error_p)
6458 {
6459 if (!token->error_reported)
6460 {
6461 tree decl;
6462 tree ambiguous_decls;
6463
6464 decl = cp_parser_lookup_name (parser, token->u.value,
6465 none_type,
6466 /*is_template=*/false,
6467 /*is_namespace=*/false,
6468 /*check_dependency=*/true,
6469 &ambiguous_decls,
6470 token->location);
6471 if (TREE_CODE (decl) == TEMPLATE_DECL)
6472 error_at (token->location,
6473 "%qD used without template arguments",
6474 decl);
6475 else if (ambiguous_decls)
6476 {
6477 // cp_parser_lookup_name has the same diagnostic,
6478 // thus make sure to emit it at most once.
6479 if (cp_parser_uncommitted_to_tentative_parse_p
6480 (parser))
6481 {
6482 error_at (token->location,
6483 "reference to %qD is ambiguous",
6484 token->u.value);
6485 print_candidates (ambiguous_decls);
6486 }
6487 decl = error_mark_node;
6488 }
6489 else
6490 {
6491 if (cxx_dialect != cxx98)
6492 cp_parser_name_lookup_error
6493 (parser, token->u.value, decl, NLE_NOT_CXX98,
6494 token->location);
6495 else
6496 cp_parser_name_lookup_error
6497 (parser, token->u.value, decl, NLE_CXX98,
6498 token->location);
6499 }
6500 }
6501 parser->scope = error_mark_node;
6502 error_p = true;
6503 /* Treat this as a successful nested-name-specifier
6504 due to:
6505
6506 [basic.lookup.qual]
6507
6508 If the name found is not a class-name (clause
6509 _class_) or namespace-name (_namespace.def_), the
6510 program is ill-formed. */
6511 success = true;
6512 }
6513 cp_lexer_consume_token (parser->lexer);
6514 }
6515 break;
6516 }
6517 /* We've found one valid nested-name-specifier. */
6518 success = true;
6519 /* Name lookup always gives us a DECL. */
6520 if (TREE_CODE (new_scope) == TYPE_DECL)
6521 new_scope = TREE_TYPE (new_scope);
6522 /* Uses of "template" must be followed by actual templates. */
6523 if (template_keyword_p
6524 && !(CLASS_TYPE_P (new_scope)
6525 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6526 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6527 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6528 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6529 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6530 == TEMPLATE_ID_EXPR)))
6531 permerror (input_location, TYPE_P (new_scope)
6532 ? G_("%qT is not a template")
6533 : G_("%qD is not a template"),
6534 new_scope);
6535 /* If it is a class scope, try to complete it; we are about to
6536 be looking up names inside the class. */
6537 if (TYPE_P (new_scope)
6538 /* Since checking types for dependency can be expensive,
6539 avoid doing it if the type is already complete. */
6540 && !COMPLETE_TYPE_P (new_scope)
6541 /* Do not try to complete dependent types. */
6542 && !dependent_type_p (new_scope))
6543 {
6544 new_scope = complete_type (new_scope);
6545 /* If it is a typedef to current class, use the current
6546 class instead, as the typedef won't have any names inside
6547 it yet. */
6548 if (!COMPLETE_TYPE_P (new_scope)
6549 && currently_open_class (new_scope))
6550 new_scope = TYPE_MAIN_VARIANT (new_scope);
6551 }
6552 /* Make sure we look in the right scope the next time through
6553 the loop. */
6554 parser->scope = new_scope;
6555 }
6556
6557 /* If parsing tentatively, replace the sequence of tokens that makes
6558 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6559 token. That way, should we re-parse the token stream, we will
6560 not have to repeat the effort required to do the parse, nor will
6561 we issue duplicate error messages. */
6562 if (success && start)
6563 {
6564 cp_token *token;
6565
6566 token = cp_lexer_token_at (parser->lexer, start);
6567 /* Reset the contents of the START token. */
6568 token->type = CPP_NESTED_NAME_SPECIFIER;
6569 /* Retrieve any deferred checks. Do not pop this access checks yet
6570 so the memory will not be reclaimed during token replacing below. */
6571 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6572 token->u.tree_check_value->value = parser->scope;
6573 token->u.tree_check_value->checks = get_deferred_access_checks ();
6574 token->u.tree_check_value->qualifying_scope =
6575 parser->qualifying_scope;
6576 token->keyword = RID_MAX;
6577
6578 /* Purge all subsequent tokens. */
6579 cp_lexer_purge_tokens_after (parser->lexer, start);
6580 }
6581
6582 if (start)
6583 pop_to_parent_deferring_access_checks ();
6584
6585 return success ? parser->scope : NULL_TREE;
6586 }
6587
6588 /* Parse a nested-name-specifier. See
6589 cp_parser_nested_name_specifier_opt for details. This function
6590 behaves identically, except that it will an issue an error if no
6591 nested-name-specifier is present. */
6592
6593 static tree
6594 cp_parser_nested_name_specifier (cp_parser *parser,
6595 bool typename_keyword_p,
6596 bool check_dependency_p,
6597 bool type_p,
6598 bool is_declaration)
6599 {
6600 tree scope;
6601
6602 /* Look for the nested-name-specifier. */
6603 scope = cp_parser_nested_name_specifier_opt (parser,
6604 typename_keyword_p,
6605 check_dependency_p,
6606 type_p,
6607 is_declaration);
6608 /* If it was not present, issue an error message. */
6609 if (!scope)
6610 {
6611 cp_parser_error (parser, "expected nested-name-specifier");
6612 parser->scope = NULL_TREE;
6613 }
6614
6615 return scope;
6616 }
6617
6618 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6619 this is either a class-name or a namespace-name (which corresponds
6620 to the class-or-namespace-name production in the grammar). For
6621 C++0x, it can also be a type-name that refers to an enumeration
6622 type or a simple-template-id.
6623
6624 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6625 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6626 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6627 TYPE_P is TRUE iff the next name should be taken as a class-name,
6628 even the same name is declared to be another entity in the same
6629 scope.
6630
6631 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6632 specified by the class-or-namespace-name. If neither is found the
6633 ERROR_MARK_NODE is returned. */
6634
6635 static tree
6636 cp_parser_qualifying_entity (cp_parser *parser,
6637 bool typename_keyword_p,
6638 bool template_keyword_p,
6639 bool check_dependency_p,
6640 bool type_p,
6641 bool is_declaration)
6642 {
6643 tree saved_scope;
6644 tree saved_qualifying_scope;
6645 tree saved_object_scope;
6646 tree scope;
6647 bool only_class_p;
6648 bool successful_parse_p;
6649
6650 /* DR 743: decltype can appear in a nested-name-specifier. */
6651 if (cp_lexer_next_token_is_decltype (parser->lexer))
6652 {
6653 scope = cp_parser_decltype (parser);
6654 if (TREE_CODE (scope) != ENUMERAL_TYPE
6655 && !MAYBE_CLASS_TYPE_P (scope))
6656 {
6657 cp_parser_simulate_error (parser);
6658 return error_mark_node;
6659 }
6660 if (TYPE_NAME (scope))
6661 scope = TYPE_NAME (scope);
6662 return scope;
6663 }
6664
6665 /* Before we try to parse the class-name, we must save away the
6666 current PARSER->SCOPE since cp_parser_class_name will destroy
6667 it. */
6668 saved_scope = parser->scope;
6669 saved_qualifying_scope = parser->qualifying_scope;
6670 saved_object_scope = parser->object_scope;
6671 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6672 there is no need to look for a namespace-name. */
6673 only_class_p = template_keyword_p
6674 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6675 if (!only_class_p)
6676 cp_parser_parse_tentatively (parser);
6677 scope = cp_parser_class_name (parser,
6678 typename_keyword_p,
6679 template_keyword_p,
6680 type_p ? class_type : none_type,
6681 check_dependency_p,
6682 /*class_head_p=*/false,
6683 is_declaration,
6684 /*enum_ok=*/cxx_dialect > cxx98);
6685 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6686 /* If that didn't work, try for a namespace-name. */
6687 if (!only_class_p && !successful_parse_p)
6688 {
6689 /* Restore the saved scope. */
6690 parser->scope = saved_scope;
6691 parser->qualifying_scope = saved_qualifying_scope;
6692 parser->object_scope = saved_object_scope;
6693 /* If we are not looking at an identifier followed by the scope
6694 resolution operator, then this is not part of a
6695 nested-name-specifier. (Note that this function is only used
6696 to parse the components of a nested-name-specifier.) */
6697 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6698 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6699 return error_mark_node;
6700 scope = cp_parser_namespace_name (parser);
6701 }
6702
6703 return scope;
6704 }
6705
6706 /* Return true if we are looking at a compound-literal, false otherwise. */
6707
6708 static bool
6709 cp_parser_compound_literal_p (cp_parser *parser)
6710 {
6711 cp_lexer_save_tokens (parser->lexer);
6712
6713 /* Skip tokens until the next token is a closing parenthesis.
6714 If we find the closing `)', and the next token is a `{', then
6715 we are looking at a compound-literal. */
6716 bool compound_literal_p
6717 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6718 /*consume_paren=*/true)
6719 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6720
6721 /* Roll back the tokens we skipped. */
6722 cp_lexer_rollback_tokens (parser->lexer);
6723
6724 return compound_literal_p;
6725 }
6726
6727 /* Return true if EXPR is the integer constant zero or a complex constant
6728 of zero, without any folding, but ignoring location wrappers. */
6729
6730 bool
6731 literal_integer_zerop (const_tree expr)
6732 {
6733 return (location_wrapper_p (expr)
6734 && integer_zerop (TREE_OPERAND (expr, 0)));
6735 }
6736
6737 /* Parse a postfix-expression.
6738
6739 postfix-expression:
6740 primary-expression
6741 postfix-expression [ expression ]
6742 postfix-expression ( expression-list [opt] )
6743 simple-type-specifier ( expression-list [opt] )
6744 typename :: [opt] nested-name-specifier identifier
6745 ( expression-list [opt] )
6746 typename :: [opt] nested-name-specifier template [opt] template-id
6747 ( expression-list [opt] )
6748 postfix-expression . template [opt] id-expression
6749 postfix-expression -> template [opt] id-expression
6750 postfix-expression . pseudo-destructor-name
6751 postfix-expression -> pseudo-destructor-name
6752 postfix-expression ++
6753 postfix-expression --
6754 dynamic_cast < type-id > ( expression )
6755 static_cast < type-id > ( expression )
6756 reinterpret_cast < type-id > ( expression )
6757 const_cast < type-id > ( expression )
6758 typeid ( expression )
6759 typeid ( type-id )
6760
6761 GNU Extension:
6762
6763 postfix-expression:
6764 ( type-id ) { initializer-list , [opt] }
6765
6766 This extension is a GNU version of the C99 compound-literal
6767 construct. (The C99 grammar uses `type-name' instead of `type-id',
6768 but they are essentially the same concept.)
6769
6770 If ADDRESS_P is true, the postfix expression is the operand of the
6771 `&' operator. CAST_P is true if this expression is the target of a
6772 cast.
6773
6774 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6775 class member access expressions [expr.ref].
6776
6777 Returns a representation of the expression. */
6778
6779 static cp_expr
6780 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6781 bool member_access_only_p, bool decltype_p,
6782 cp_id_kind * pidk_return)
6783 {
6784 cp_token *token;
6785 location_t loc;
6786 enum rid keyword;
6787 cp_id_kind idk = CP_ID_KIND_NONE;
6788 cp_expr postfix_expression = NULL_TREE;
6789 bool is_member_access = false;
6790
6791 /* Peek at the next token. */
6792 token = cp_lexer_peek_token (parser->lexer);
6793 loc = token->location;
6794 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6795
6796 /* Some of the productions are determined by keywords. */
6797 keyword = token->keyword;
6798 switch (keyword)
6799 {
6800 case RID_DYNCAST:
6801 case RID_STATCAST:
6802 case RID_REINTCAST:
6803 case RID_CONSTCAST:
6804 {
6805 tree type;
6806 cp_expr expression;
6807 const char *saved_message;
6808 bool saved_in_type_id_in_expr_p;
6809
6810 /* All of these can be handled in the same way from the point
6811 of view of parsing. Begin by consuming the token
6812 identifying the cast. */
6813 cp_lexer_consume_token (parser->lexer);
6814
6815 /* New types cannot be defined in the cast. */
6816 saved_message = parser->type_definition_forbidden_message;
6817 parser->type_definition_forbidden_message
6818 = G_("types may not be defined in casts");
6819
6820 /* Look for the opening `<'. */
6821 cp_parser_require (parser, CPP_LESS, RT_LESS);
6822 /* Parse the type to which we are casting. */
6823 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6824 parser->in_type_id_in_expr_p = true;
6825 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
6826 NULL);
6827 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6828 /* Look for the closing `>'. */
6829 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6830 /* Restore the old message. */
6831 parser->type_definition_forbidden_message = saved_message;
6832
6833 bool saved_greater_than_is_operator_p
6834 = parser->greater_than_is_operator_p;
6835 parser->greater_than_is_operator_p = true;
6836
6837 /* And the expression which is being cast. */
6838 matching_parens parens;
6839 parens.require_open (parser);
6840 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6841 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6842 RT_CLOSE_PAREN);
6843 location_t end_loc = close_paren ?
6844 close_paren->location : UNKNOWN_LOCATION;
6845
6846 parser->greater_than_is_operator_p
6847 = saved_greater_than_is_operator_p;
6848
6849 /* Only type conversions to integral or enumeration types
6850 can be used in constant-expressions. */
6851 if (!cast_valid_in_integral_constant_expression_p (type)
6852 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6853 {
6854 postfix_expression = error_mark_node;
6855 break;
6856 }
6857
6858 switch (keyword)
6859 {
6860 case RID_DYNCAST:
6861 postfix_expression
6862 = build_dynamic_cast (type, expression, tf_warning_or_error);
6863 break;
6864 case RID_STATCAST:
6865 postfix_expression
6866 = build_static_cast (type, expression, tf_warning_or_error);
6867 break;
6868 case RID_REINTCAST:
6869 postfix_expression
6870 = build_reinterpret_cast (type, expression,
6871 tf_warning_or_error);
6872 break;
6873 case RID_CONSTCAST:
6874 postfix_expression
6875 = build_const_cast (type, expression, tf_warning_or_error);
6876 break;
6877 default:
6878 gcc_unreachable ();
6879 }
6880
6881 /* Construct a location e.g. :
6882 reinterpret_cast <int *> (expr)
6883 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6884 ranging from the start of the "*_cast" token to the final closing
6885 paren, with the caret at the start. */
6886 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6887 postfix_expression.set_location (cp_cast_loc);
6888 }
6889 break;
6890
6891 case RID_TYPEID:
6892 {
6893 tree type;
6894 const char *saved_message;
6895 bool saved_in_type_id_in_expr_p;
6896
6897 /* Consume the `typeid' token. */
6898 cp_lexer_consume_token (parser->lexer);
6899 /* Look for the `(' token. */
6900 matching_parens parens;
6901 parens.require_open (parser);
6902 /* Types cannot be defined in a `typeid' expression. */
6903 saved_message = parser->type_definition_forbidden_message;
6904 parser->type_definition_forbidden_message
6905 = G_("types may not be defined in a %<typeid%> expression");
6906 /* We can't be sure yet whether we're looking at a type-id or an
6907 expression. */
6908 cp_parser_parse_tentatively (parser);
6909 /* Try a type-id first. */
6910 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6911 parser->in_type_id_in_expr_p = true;
6912 type = cp_parser_type_id (parser);
6913 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6914 /* Look for the `)' token. Otherwise, we can't be sure that
6915 we're not looking at an expression: consider `typeid (int
6916 (3))', for example. */
6917 cp_token *close_paren = parens.require_close (parser);
6918 /* If all went well, simply lookup the type-id. */
6919 if (cp_parser_parse_definitely (parser))
6920 postfix_expression = get_typeid (type, tf_warning_or_error);
6921 /* Otherwise, fall back to the expression variant. */
6922 else
6923 {
6924 tree expression;
6925
6926 /* Look for an expression. */
6927 expression = cp_parser_expression (parser, & idk);
6928 /* Compute its typeid. */
6929 postfix_expression = build_typeid (expression, tf_warning_or_error);
6930 /* Look for the `)' token. */
6931 close_paren = parens.require_close (parser);
6932 }
6933 /* Restore the saved message. */
6934 parser->type_definition_forbidden_message = saved_message;
6935 /* `typeid' may not appear in an integral constant expression. */
6936 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6937 postfix_expression = error_mark_node;
6938
6939 /* Construct a location e.g. :
6940 typeid (expr)
6941 ^~~~~~~~~~~~~
6942 ranging from the start of the "typeid" token to the final closing
6943 paren, with the caret at the start. */
6944 if (close_paren)
6945 {
6946 location_t typeid_loc
6947 = make_location (start_loc, start_loc, close_paren->location);
6948 postfix_expression.set_location (typeid_loc);
6949 postfix_expression.maybe_add_location_wrapper ();
6950 }
6951 }
6952 break;
6953
6954 case RID_TYPENAME:
6955 {
6956 tree type;
6957 /* The syntax permitted here is the same permitted for an
6958 elaborated-type-specifier. */
6959 ++parser->prevent_constrained_type_specifiers;
6960 type = cp_parser_elaborated_type_specifier (parser,
6961 /*is_friend=*/false,
6962 /*is_declaration=*/false);
6963 --parser->prevent_constrained_type_specifiers;
6964 postfix_expression = cp_parser_functional_cast (parser, type);
6965 }
6966 break;
6967
6968 case RID_ADDRESSOF:
6969 case RID_BUILTIN_SHUFFLE:
6970 case RID_BUILTIN_LAUNDER:
6971 {
6972 vec<tree, va_gc> *vec;
6973 unsigned int i;
6974 tree p;
6975
6976 cp_lexer_consume_token (parser->lexer);
6977 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6978 /*cast_p=*/false, /*allow_expansion_p=*/true,
6979 /*non_constant_p=*/NULL);
6980 if (vec == NULL)
6981 {
6982 postfix_expression = error_mark_node;
6983 break;
6984 }
6985
6986 FOR_EACH_VEC_ELT (*vec, i, p)
6987 mark_exp_read (p);
6988
6989 switch (keyword)
6990 {
6991 case RID_ADDRESSOF:
6992 if (vec->length () == 1)
6993 postfix_expression
6994 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6995 else
6996 {
6997 error_at (loc, "wrong number of arguments to "
6998 "%<__builtin_addressof%>");
6999 postfix_expression = error_mark_node;
7000 }
7001 break;
7002
7003 case RID_BUILTIN_LAUNDER:
7004 if (vec->length () == 1)
7005 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7006 tf_warning_or_error);
7007 else
7008 {
7009 error_at (loc, "wrong number of arguments to "
7010 "%<__builtin_launder%>");
7011 postfix_expression = error_mark_node;
7012 }
7013 break;
7014
7015 case RID_BUILTIN_SHUFFLE:
7016 if (vec->length () == 2)
7017 postfix_expression
7018 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7019 (*vec)[1], tf_warning_or_error);
7020 else if (vec->length () == 3)
7021 postfix_expression
7022 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7023 (*vec)[2], tf_warning_or_error);
7024 else
7025 {
7026 error_at (loc, "wrong number of arguments to "
7027 "%<__builtin_shuffle%>");
7028 postfix_expression = error_mark_node;
7029 }
7030 break;
7031
7032 default:
7033 gcc_unreachable ();
7034 }
7035 break;
7036 }
7037
7038 case RID_BUILTIN_CONVERTVECTOR:
7039 {
7040 tree expression;
7041 tree type;
7042 /* Consume the `__builtin_convertvector' token. */
7043 cp_lexer_consume_token (parser->lexer);
7044 /* Look for the opening `('. */
7045 matching_parens parens;
7046 parens.require_open (parser);
7047 /* Now, parse the assignment-expression. */
7048 expression = cp_parser_assignment_expression (parser);
7049 /* Look for the `,'. */
7050 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7051 location_t type_location
7052 = cp_lexer_peek_token (parser->lexer)->location;
7053 /* Parse the type-id. */
7054 {
7055 type_id_in_expr_sentinel s (parser);
7056 type = cp_parser_type_id (parser);
7057 }
7058 /* Look for the closing `)'. */
7059 parens.require_close (parser);
7060 return cp_build_vec_convert (expression, type_location, type,
7061 tf_warning_or_error);
7062 }
7063
7064 default:
7065 {
7066 tree type;
7067
7068 /* If the next thing is a simple-type-specifier, we may be
7069 looking at a functional cast. We could also be looking at
7070 an id-expression. So, we try the functional cast, and if
7071 that doesn't work we fall back to the primary-expression. */
7072 cp_parser_parse_tentatively (parser);
7073 /* Look for the simple-type-specifier. */
7074 ++parser->prevent_constrained_type_specifiers;
7075 type = cp_parser_simple_type_specifier (parser,
7076 /*decl_specs=*/NULL,
7077 CP_PARSER_FLAGS_NONE);
7078 --parser->prevent_constrained_type_specifiers;
7079 /* Parse the cast itself. */
7080 if (!cp_parser_error_occurred (parser))
7081 postfix_expression
7082 = cp_parser_functional_cast (parser, type);
7083 /* If that worked, we're done. */
7084 if (cp_parser_parse_definitely (parser))
7085 break;
7086
7087 /* If the functional-cast didn't work out, try a
7088 compound-literal. */
7089 if (cp_parser_allow_gnu_extensions_p (parser)
7090 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7091 {
7092 cp_expr initializer = NULL_TREE;
7093
7094 cp_parser_parse_tentatively (parser);
7095
7096 matching_parens parens;
7097 parens.consume_open (parser);
7098
7099 /* Avoid calling cp_parser_type_id pointlessly, see comment
7100 in cp_parser_cast_expression about c++/29234. */
7101 if (!cp_parser_compound_literal_p (parser))
7102 cp_parser_simulate_error (parser);
7103 else
7104 {
7105 /* Parse the type. */
7106 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7107 parser->in_type_id_in_expr_p = true;
7108 type = cp_parser_type_id (parser);
7109 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7110 parens.require_close (parser);
7111 }
7112
7113 /* If things aren't going well, there's no need to
7114 keep going. */
7115 if (!cp_parser_error_occurred (parser))
7116 {
7117 bool non_constant_p;
7118 /* Parse the brace-enclosed initializer list. */
7119 initializer = cp_parser_braced_list (parser,
7120 &non_constant_p);
7121 }
7122 /* If that worked, we're definitely looking at a
7123 compound-literal expression. */
7124 if (cp_parser_parse_definitely (parser))
7125 {
7126 /* Warn the user that a compound literal is not
7127 allowed in standard C++. */
7128 pedwarn (input_location, OPT_Wpedantic,
7129 "ISO C++ forbids compound-literals");
7130 /* For simplicity, we disallow compound literals in
7131 constant-expressions. We could
7132 allow compound literals of integer type, whose
7133 initializer was a constant, in constant
7134 expressions. Permitting that usage, as a further
7135 extension, would not change the meaning of any
7136 currently accepted programs. (Of course, as
7137 compound literals are not part of ISO C++, the
7138 standard has nothing to say.) */
7139 if (cp_parser_non_integral_constant_expression (parser,
7140 NIC_NCC))
7141 {
7142 postfix_expression = error_mark_node;
7143 break;
7144 }
7145 /* Form the representation of the compound-literal. */
7146 postfix_expression
7147 = finish_compound_literal (type, initializer,
7148 tf_warning_or_error, fcl_c99);
7149 postfix_expression.set_location (initializer.get_location ());
7150 break;
7151 }
7152 }
7153
7154 /* It must be a primary-expression. */
7155 postfix_expression
7156 = cp_parser_primary_expression (parser, address_p, cast_p,
7157 /*template_arg_p=*/false,
7158 decltype_p,
7159 &idk);
7160 }
7161 break;
7162 }
7163
7164 /* Note that we don't need to worry about calling build_cplus_new on a
7165 class-valued CALL_EXPR in decltype when it isn't the end of the
7166 postfix-expression; unary_complex_lvalue will take care of that for
7167 all these cases. */
7168
7169 /* Keep looping until the postfix-expression is complete. */
7170 while (true)
7171 {
7172 if (idk == CP_ID_KIND_UNQUALIFIED
7173 && identifier_p (postfix_expression)
7174 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7175 /* It is not a Koenig lookup function call. */
7176 postfix_expression
7177 = unqualified_name_lookup_error (postfix_expression);
7178
7179 /* Peek at the next token. */
7180 token = cp_lexer_peek_token (parser->lexer);
7181
7182 switch (token->type)
7183 {
7184 case CPP_OPEN_SQUARE:
7185 if (cp_next_tokens_can_be_std_attribute_p (parser))
7186 {
7187 cp_parser_error (parser,
7188 "two consecutive %<[%> shall "
7189 "only introduce an attribute");
7190 return error_mark_node;
7191 }
7192 postfix_expression
7193 = cp_parser_postfix_open_square_expression (parser,
7194 postfix_expression,
7195 false,
7196 decltype_p);
7197 postfix_expression.set_range (start_loc,
7198 postfix_expression.get_location ());
7199
7200 idk = CP_ID_KIND_NONE;
7201 is_member_access = false;
7202 break;
7203
7204 case CPP_OPEN_PAREN:
7205 /* postfix-expression ( expression-list [opt] ) */
7206 {
7207 bool koenig_p;
7208 bool is_builtin_constant_p;
7209 bool saved_integral_constant_expression_p = false;
7210 bool saved_non_integral_constant_expression_p = false;
7211 tsubst_flags_t complain = complain_flags (decltype_p);
7212 vec<tree, va_gc> *args;
7213 location_t close_paren_loc = UNKNOWN_LOCATION;
7214
7215 is_member_access = false;
7216
7217 tree stripped_expression
7218 = tree_strip_any_location_wrapper (postfix_expression);
7219 is_builtin_constant_p
7220 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7221 if (is_builtin_constant_p)
7222 {
7223 /* The whole point of __builtin_constant_p is to allow
7224 non-constant expressions to appear as arguments. */
7225 saved_integral_constant_expression_p
7226 = parser->integral_constant_expression_p;
7227 saved_non_integral_constant_expression_p
7228 = parser->non_integral_constant_expression_p;
7229 parser->integral_constant_expression_p = false;
7230 }
7231 args = (cp_parser_parenthesized_expression_list
7232 (parser, non_attr,
7233 /*cast_p=*/false, /*allow_expansion_p=*/true,
7234 /*non_constant_p=*/NULL,
7235 /*close_paren_loc=*/&close_paren_loc,
7236 /*wrap_locations_p=*/true));
7237 if (is_builtin_constant_p)
7238 {
7239 parser->integral_constant_expression_p
7240 = saved_integral_constant_expression_p;
7241 parser->non_integral_constant_expression_p
7242 = saved_non_integral_constant_expression_p;
7243 }
7244
7245 if (args == NULL)
7246 {
7247 postfix_expression = error_mark_node;
7248 break;
7249 }
7250
7251 /* Function calls are not permitted in
7252 constant-expressions. */
7253 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7254 && cp_parser_non_integral_constant_expression (parser,
7255 NIC_FUNC_CALL))
7256 {
7257 postfix_expression = error_mark_node;
7258 release_tree_vector (args);
7259 break;
7260 }
7261
7262 koenig_p = false;
7263 if (idk == CP_ID_KIND_UNQUALIFIED
7264 || idk == CP_ID_KIND_TEMPLATE_ID)
7265 {
7266 if (identifier_p (postfix_expression)
7267 /* In C++2A, we may need to perform ADL for a template
7268 name. */
7269 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7270 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7271 {
7272 if (!args->is_empty ())
7273 {
7274 koenig_p = true;
7275 if (!any_type_dependent_arguments_p (args))
7276 postfix_expression
7277 = perform_koenig_lookup (postfix_expression, args,
7278 complain);
7279 }
7280 else
7281 postfix_expression
7282 = unqualified_fn_lookup_error (postfix_expression);
7283 }
7284 /* We do not perform argument-dependent lookup if
7285 normal lookup finds a non-function, in accordance
7286 with the expected resolution of DR 218. */
7287 else if (!args->is_empty ()
7288 && is_overloaded_fn (postfix_expression))
7289 {
7290 /* We only need to look at the first function,
7291 because all the fns share the attribute we're
7292 concerned with (all member fns or all local
7293 fns). */
7294 tree fn = get_first_fn (postfix_expression);
7295 fn = STRIP_TEMPLATE (fn);
7296
7297 /* Do not do argument dependent lookup if regular
7298 lookup finds a member function or a block-scope
7299 function declaration. [basic.lookup.argdep]/3 */
7300 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7301 || DECL_FUNCTION_MEMBER_P (fn)
7302 || DECL_LOCAL_FUNCTION_P (fn)))
7303 {
7304 koenig_p = true;
7305 if (!any_type_dependent_arguments_p (args))
7306 postfix_expression
7307 = perform_koenig_lookup (postfix_expression, args,
7308 complain);
7309 }
7310 }
7311 }
7312
7313 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7314 {
7315 tree instance = TREE_OPERAND (postfix_expression, 0);
7316 tree fn = TREE_OPERAND (postfix_expression, 1);
7317
7318 if (processing_template_decl
7319 && (type_dependent_object_expression_p (instance)
7320 || (!BASELINK_P (fn)
7321 && TREE_CODE (fn) != FIELD_DECL)
7322 || type_dependent_expression_p (fn)
7323 || any_type_dependent_arguments_p (args)))
7324 {
7325 maybe_generic_this_capture (instance, fn);
7326 postfix_expression
7327 = build_min_nt_call_vec (postfix_expression, args);
7328 release_tree_vector (args);
7329 break;
7330 }
7331
7332 if (BASELINK_P (fn))
7333 {
7334 postfix_expression
7335 = (build_new_method_call
7336 (instance, fn, &args, NULL_TREE,
7337 (idk == CP_ID_KIND_QUALIFIED
7338 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7339 : LOOKUP_NORMAL),
7340 /*fn_p=*/NULL,
7341 complain));
7342 }
7343 else
7344 postfix_expression
7345 = finish_call_expr (postfix_expression, &args,
7346 /*disallow_virtual=*/false,
7347 /*koenig_p=*/false,
7348 complain);
7349 }
7350 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7351 || TREE_CODE (postfix_expression) == MEMBER_REF
7352 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7353 postfix_expression = (build_offset_ref_call_from_tree
7354 (postfix_expression, &args,
7355 complain));
7356 else if (idk == CP_ID_KIND_QUALIFIED)
7357 /* A call to a static class member, or a namespace-scope
7358 function. */
7359 postfix_expression
7360 = finish_call_expr (postfix_expression, &args,
7361 /*disallow_virtual=*/true,
7362 koenig_p,
7363 complain);
7364 else
7365 /* All other function calls. */
7366 postfix_expression
7367 = finish_call_expr (postfix_expression, &args,
7368 /*disallow_virtual=*/false,
7369 koenig_p,
7370 complain);
7371
7372 if (close_paren_loc != UNKNOWN_LOCATION)
7373 {
7374 location_t combined_loc = make_location (token->location,
7375 start_loc,
7376 close_paren_loc);
7377 postfix_expression.set_location (combined_loc);
7378 }
7379
7380 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7381 idk = CP_ID_KIND_NONE;
7382
7383 release_tree_vector (args);
7384 }
7385 break;
7386
7387 case CPP_DOT:
7388 case CPP_DEREF:
7389 /* postfix-expression . template [opt] id-expression
7390 postfix-expression . pseudo-destructor-name
7391 postfix-expression -> template [opt] id-expression
7392 postfix-expression -> pseudo-destructor-name */
7393
7394 /* Consume the `.' or `->' operator. */
7395 cp_lexer_consume_token (parser->lexer);
7396
7397 postfix_expression
7398 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7399 postfix_expression,
7400 false, &idk, loc);
7401
7402 is_member_access = true;
7403 break;
7404
7405 case CPP_PLUS_PLUS:
7406 /* postfix-expression ++ */
7407 /* Consume the `++' token. */
7408 cp_lexer_consume_token (parser->lexer);
7409 /* Generate a representation for the complete expression. */
7410 postfix_expression
7411 = finish_increment_expr (postfix_expression,
7412 POSTINCREMENT_EXPR);
7413 /* Increments may not appear in constant-expressions. */
7414 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7415 postfix_expression = error_mark_node;
7416 idk = CP_ID_KIND_NONE;
7417 is_member_access = false;
7418 break;
7419
7420 case CPP_MINUS_MINUS:
7421 /* postfix-expression -- */
7422 /* Consume the `--' token. */
7423 cp_lexer_consume_token (parser->lexer);
7424 /* Generate a representation for the complete expression. */
7425 postfix_expression
7426 = finish_increment_expr (postfix_expression,
7427 POSTDECREMENT_EXPR);
7428 /* Decrements may not appear in constant-expressions. */
7429 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7430 postfix_expression = error_mark_node;
7431 idk = CP_ID_KIND_NONE;
7432 is_member_access = false;
7433 break;
7434
7435 default:
7436 if (pidk_return != NULL)
7437 * pidk_return = idk;
7438 if (member_access_only_p)
7439 return is_member_access
7440 ? postfix_expression
7441 : cp_expr (error_mark_node);
7442 else
7443 return postfix_expression;
7444 }
7445 }
7446
7447 /* We should never get here. */
7448 gcc_unreachable ();
7449 return error_mark_node;
7450 }
7451
7452 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7453 by cp_parser_builtin_offsetof. We're looking for
7454
7455 postfix-expression [ expression ]
7456 postfix-expression [ braced-init-list ] (C++11)
7457
7458 FOR_OFFSETOF is set if we're being called in that context, which
7459 changes how we deal with integer constant expressions. */
7460
7461 static tree
7462 cp_parser_postfix_open_square_expression (cp_parser *parser,
7463 tree postfix_expression,
7464 bool for_offsetof,
7465 bool decltype_p)
7466 {
7467 tree index = NULL_TREE;
7468 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7469 bool saved_greater_than_is_operator_p;
7470
7471 /* Consume the `[' token. */
7472 cp_lexer_consume_token (parser->lexer);
7473
7474 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7475 parser->greater_than_is_operator_p = true;
7476
7477 /* Parse the index expression. */
7478 /* ??? For offsetof, there is a question of what to allow here. If
7479 offsetof is not being used in an integral constant expression context,
7480 then we *could* get the right answer by computing the value at runtime.
7481 If we are in an integral constant expression context, then we might
7482 could accept any constant expression; hard to say without analysis.
7483 Rather than open the barn door too wide right away, allow only integer
7484 constant expressions here. */
7485 if (for_offsetof)
7486 index = cp_parser_constant_expression (parser);
7487 else
7488 {
7489 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7490 {
7491 bool expr_nonconst_p;
7492 cp_lexer_set_source_position (parser->lexer);
7493 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7494 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7495 }
7496 else
7497 index = cp_parser_expression (parser);
7498 }
7499
7500 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7501
7502 /* Look for the closing `]'. */
7503 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7504
7505 /* Build the ARRAY_REF. */
7506 postfix_expression = grok_array_decl (loc, postfix_expression,
7507 index, decltype_p);
7508
7509 /* When not doing offsetof, array references are not permitted in
7510 constant-expressions. */
7511 if (!for_offsetof
7512 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7513 postfix_expression = error_mark_node;
7514
7515 return postfix_expression;
7516 }
7517
7518 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7519 dereference of incomplete type, returns true if error_mark_node should
7520 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7521 and *DEPENDENT_P. */
7522
7523 bool
7524 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7525 bool *dependent_p)
7526 {
7527 /* In a template, be permissive by treating an object expression
7528 of incomplete type as dependent (after a pedwarn). */
7529 diagnostic_t kind = (processing_template_decl
7530 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7531
7532 switch (TREE_CODE (*postfix_expression))
7533 {
7534 case CAST_EXPR:
7535 case REINTERPRET_CAST_EXPR:
7536 case CONST_CAST_EXPR:
7537 case STATIC_CAST_EXPR:
7538 case DYNAMIC_CAST_EXPR:
7539 case IMPLICIT_CONV_EXPR:
7540 case VIEW_CONVERT_EXPR:
7541 case NON_LVALUE_EXPR:
7542 kind = DK_ERROR;
7543 break;
7544 case OVERLOAD:
7545 /* Don't emit any diagnostic for OVERLOADs. */
7546 kind = DK_IGNORED;
7547 break;
7548 default:
7549 /* Avoid clobbering e.g. DECLs. */
7550 if (!EXPR_P (*postfix_expression))
7551 kind = DK_ERROR;
7552 break;
7553 }
7554
7555 if (kind == DK_IGNORED)
7556 return false;
7557
7558 location_t exploc = location_of (*postfix_expression);
7559 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7560 if (!MAYBE_CLASS_TYPE_P (*scope))
7561 return true;
7562 if (kind == DK_ERROR)
7563 *scope = *postfix_expression = error_mark_node;
7564 else if (processing_template_decl)
7565 {
7566 *dependent_p = true;
7567 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7568 }
7569 return false;
7570 }
7571
7572 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7573 by cp_parser_builtin_offsetof. We're looking for
7574
7575 postfix-expression . template [opt] id-expression
7576 postfix-expression . pseudo-destructor-name
7577 postfix-expression -> template [opt] id-expression
7578 postfix-expression -> pseudo-destructor-name
7579
7580 FOR_OFFSETOF is set if we're being called in that context. That sorta
7581 limits what of the above we'll actually accept, but nevermind.
7582 TOKEN_TYPE is the "." or "->" token, which will already have been
7583 removed from the stream. */
7584
7585 static tree
7586 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7587 enum cpp_ttype token_type,
7588 cp_expr postfix_expression,
7589 bool for_offsetof, cp_id_kind *idk,
7590 location_t location)
7591 {
7592 tree name;
7593 bool dependent_p;
7594 bool pseudo_destructor_p;
7595 tree scope = NULL_TREE;
7596 location_t start_loc = postfix_expression.get_start ();
7597
7598 /* If this is a `->' operator, dereference the pointer. */
7599 if (token_type == CPP_DEREF)
7600 postfix_expression = build_x_arrow (location, postfix_expression,
7601 tf_warning_or_error);
7602 /* Check to see whether or not the expression is type-dependent and
7603 not the current instantiation. */
7604 dependent_p = type_dependent_object_expression_p (postfix_expression);
7605 /* The identifier following the `->' or `.' is not qualified. */
7606 parser->scope = NULL_TREE;
7607 parser->qualifying_scope = NULL_TREE;
7608 parser->object_scope = NULL_TREE;
7609 *idk = CP_ID_KIND_NONE;
7610
7611 /* Enter the scope corresponding to the type of the object
7612 given by the POSTFIX_EXPRESSION. */
7613 if (!dependent_p)
7614 {
7615 scope = TREE_TYPE (postfix_expression);
7616 /* According to the standard, no expression should ever have
7617 reference type. Unfortunately, we do not currently match
7618 the standard in this respect in that our internal representation
7619 of an expression may have reference type even when the standard
7620 says it does not. Therefore, we have to manually obtain the
7621 underlying type here. */
7622 scope = non_reference (scope);
7623 /* The type of the POSTFIX_EXPRESSION must be complete. */
7624 /* Unlike the object expression in other contexts, *this is not
7625 required to be of complete type for purposes of class member
7626 access (5.2.5) outside the member function body. */
7627 if (postfix_expression != current_class_ref
7628 && scope != error_mark_node
7629 && !currently_open_class (scope))
7630 {
7631 scope = complete_type (scope);
7632 if (!COMPLETE_TYPE_P (scope)
7633 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7634 &dependent_p))
7635 return error_mark_node;
7636 }
7637
7638 if (!dependent_p)
7639 {
7640 /* Let the name lookup machinery know that we are processing a
7641 class member access expression. */
7642 parser->context->object_type = scope;
7643 /* If something went wrong, we want to be able to discern that case,
7644 as opposed to the case where there was no SCOPE due to the type
7645 of expression being dependent. */
7646 if (!scope)
7647 scope = error_mark_node;
7648 /* If the SCOPE was erroneous, make the various semantic analysis
7649 functions exit quickly -- and without issuing additional error
7650 messages. */
7651 if (scope == error_mark_node)
7652 postfix_expression = error_mark_node;
7653 }
7654 }
7655
7656 if (dependent_p)
7657 /* Tell cp_parser_lookup_name that there was an object, even though it's
7658 type-dependent. */
7659 parser->context->object_type = unknown_type_node;
7660
7661 /* Assume this expression is not a pseudo-destructor access. */
7662 pseudo_destructor_p = false;
7663
7664 /* If the SCOPE is a scalar type, then, if this is a valid program,
7665 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7666 is type dependent, it can be pseudo-destructor-name or something else.
7667 Try to parse it as pseudo-destructor-name first. */
7668 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7669 {
7670 tree s;
7671 tree type;
7672
7673 cp_parser_parse_tentatively (parser);
7674 /* Parse the pseudo-destructor-name. */
7675 s = NULL_TREE;
7676 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7677 &s, &type);
7678 if (dependent_p
7679 && (cp_parser_error_occurred (parser)
7680 || !SCALAR_TYPE_P (type)))
7681 cp_parser_abort_tentative_parse (parser);
7682 else if (cp_parser_parse_definitely (parser))
7683 {
7684 pseudo_destructor_p = true;
7685 postfix_expression
7686 = finish_pseudo_destructor_expr (postfix_expression,
7687 s, type, location);
7688 }
7689 }
7690
7691 if (!pseudo_destructor_p)
7692 {
7693 /* If the SCOPE is not a scalar type, we are looking at an
7694 ordinary class member access expression, rather than a
7695 pseudo-destructor-name. */
7696 bool template_p;
7697 cp_token *token = cp_lexer_peek_token (parser->lexer);
7698 /* Parse the id-expression. */
7699 name = (cp_parser_id_expression
7700 (parser,
7701 cp_parser_optional_template_keyword (parser),
7702 /*check_dependency_p=*/true,
7703 &template_p,
7704 /*declarator_p=*/false,
7705 /*optional_p=*/false));
7706 /* In general, build a SCOPE_REF if the member name is qualified.
7707 However, if the name was not dependent and has already been
7708 resolved; there is no need to build the SCOPE_REF. For example;
7709
7710 struct X { void f(); };
7711 template <typename T> void f(T* t) { t->X::f(); }
7712
7713 Even though "t" is dependent, "X::f" is not and has been resolved
7714 to a BASELINK; there is no need to include scope information. */
7715
7716 /* But we do need to remember that there was an explicit scope for
7717 virtual function calls. */
7718 if (parser->scope)
7719 *idk = CP_ID_KIND_QUALIFIED;
7720
7721 /* If the name is a template-id that names a type, we will get a
7722 TYPE_DECL here. That is invalid code. */
7723 if (TREE_CODE (name) == TYPE_DECL)
7724 {
7725 error_at (token->location, "invalid use of %qD", name);
7726 postfix_expression = error_mark_node;
7727 }
7728 else
7729 {
7730 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7731 {
7732 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7733 {
7734 error_at (token->location, "%<%D::%D%> is not a class member",
7735 parser->scope, name);
7736 postfix_expression = error_mark_node;
7737 }
7738 else
7739 name = build_qualified_name (/*type=*/NULL_TREE,
7740 parser->scope,
7741 name,
7742 template_p);
7743 parser->scope = NULL_TREE;
7744 parser->qualifying_scope = NULL_TREE;
7745 parser->object_scope = NULL_TREE;
7746 }
7747 if (parser->scope && name && BASELINK_P (name))
7748 adjust_result_of_qualified_name_lookup
7749 (name, parser->scope, scope);
7750 postfix_expression
7751 = finish_class_member_access_expr (postfix_expression, name,
7752 template_p,
7753 tf_warning_or_error);
7754 /* Build a location e.g.:
7755 ptr->access_expr
7756 ~~~^~~~~~~~~~~~~
7757 where the caret is at the deref token, ranging from
7758 the start of postfix_expression to the end of the access expr. */
7759 location_t end_loc
7760 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7761 location_t combined_loc
7762 = make_location (input_location, start_loc, end_loc);
7763 protected_set_expr_location (postfix_expression, combined_loc);
7764 }
7765 }
7766
7767 /* We no longer need to look up names in the scope of the object on
7768 the left-hand side of the `.' or `->' operator. */
7769 parser->context->object_type = NULL_TREE;
7770
7771 /* Outside of offsetof, these operators may not appear in
7772 constant-expressions. */
7773 if (!for_offsetof
7774 && (cp_parser_non_integral_constant_expression
7775 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7776 postfix_expression = error_mark_node;
7777
7778 return postfix_expression;
7779 }
7780
7781 /* Parse a parenthesized expression-list.
7782
7783 expression-list:
7784 assignment-expression
7785 expression-list, assignment-expression
7786
7787 attribute-list:
7788 expression-list
7789 identifier
7790 identifier, expression-list
7791
7792 CAST_P is true if this expression is the target of a cast.
7793
7794 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7795 argument pack.
7796
7797 WRAP_LOCATIONS_P is true if expressions within this list for which
7798 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7799 their source locations.
7800
7801 Returns a vector of trees. Each element is a representation of an
7802 assignment-expression. NULL is returned if the ( and or ) are
7803 missing. An empty, but allocated, vector is returned on no
7804 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7805 if we are parsing an attribute list for an attribute that wants a
7806 plain identifier argument, normal_attr for an attribute that wants
7807 an expression, or non_attr if we aren't parsing an attribute list. If
7808 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7809 not all of the expressions in the list were constant.
7810 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7811 will be written to with the location of the closing parenthesis. If
7812 an error occurs, it may or may not be written to. */
7813
7814 static vec<tree, va_gc> *
7815 cp_parser_parenthesized_expression_list (cp_parser* parser,
7816 int is_attribute_list,
7817 bool cast_p,
7818 bool allow_expansion_p,
7819 bool *non_constant_p,
7820 location_t *close_paren_loc,
7821 bool wrap_locations_p)
7822 {
7823 vec<tree, va_gc> *expression_list;
7824 bool fold_expr_p = is_attribute_list != non_attr;
7825 tree identifier = NULL_TREE;
7826 bool saved_greater_than_is_operator_p;
7827
7828 /* Assume all the expressions will be constant. */
7829 if (non_constant_p)
7830 *non_constant_p = false;
7831
7832 matching_parens parens;
7833 if (!parens.require_open (parser))
7834 return NULL;
7835
7836 expression_list = make_tree_vector ();
7837
7838 /* Within a parenthesized expression, a `>' token is always
7839 the greater-than operator. */
7840 saved_greater_than_is_operator_p
7841 = parser->greater_than_is_operator_p;
7842 parser->greater_than_is_operator_p = true;
7843
7844 cp_expr expr (NULL_TREE);
7845
7846 /* Consume expressions until there are no more. */
7847 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7848 while (true)
7849 {
7850 /* At the beginning of attribute lists, check to see if the
7851 next token is an identifier. */
7852 if (is_attribute_list == id_attr
7853 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7854 {
7855 cp_token *token;
7856
7857 /* Consume the identifier. */
7858 token = cp_lexer_consume_token (parser->lexer);
7859 /* Save the identifier. */
7860 identifier = token->u.value;
7861 }
7862 else
7863 {
7864 bool expr_non_constant_p;
7865
7866 /* Parse the next assignment-expression. */
7867 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7868 {
7869 /* A braced-init-list. */
7870 cp_lexer_set_source_position (parser->lexer);
7871 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7872 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7873 if (non_constant_p && expr_non_constant_p)
7874 *non_constant_p = true;
7875 }
7876 else if (non_constant_p)
7877 {
7878 expr = (cp_parser_constant_expression
7879 (parser, /*allow_non_constant_p=*/true,
7880 &expr_non_constant_p));
7881 if (expr_non_constant_p)
7882 *non_constant_p = true;
7883 }
7884 else
7885 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7886 cast_p);
7887
7888 if (fold_expr_p)
7889 expr = instantiate_non_dependent_expr (expr);
7890
7891 /* If we have an ellipsis, then this is an expression
7892 expansion. */
7893 if (allow_expansion_p
7894 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7895 {
7896 /* Consume the `...'. */
7897 cp_lexer_consume_token (parser->lexer);
7898
7899 /* Build the argument pack. */
7900 expr = make_pack_expansion (expr);
7901 }
7902
7903 if (wrap_locations_p)
7904 expr.maybe_add_location_wrapper ();
7905
7906 /* Add it to the list. We add error_mark_node
7907 expressions to the list, so that we can still tell if
7908 the correct form for a parenthesized expression-list
7909 is found. That gives better errors. */
7910 vec_safe_push (expression_list, expr.get_value ());
7911
7912 if (expr == error_mark_node)
7913 goto skip_comma;
7914 }
7915
7916 /* After the first item, attribute lists look the same as
7917 expression lists. */
7918 is_attribute_list = non_attr;
7919
7920 get_comma:;
7921 /* If the next token isn't a `,', then we are done. */
7922 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7923 break;
7924
7925 /* Otherwise, consume the `,' and keep going. */
7926 cp_lexer_consume_token (parser->lexer);
7927 }
7928
7929 if (close_paren_loc)
7930 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7931
7932 if (!parens.require_close (parser))
7933 {
7934 int ending;
7935
7936 skip_comma:;
7937 /* We try and resync to an unnested comma, as that will give the
7938 user better diagnostics. */
7939 ending = cp_parser_skip_to_closing_parenthesis (parser,
7940 /*recovering=*/true,
7941 /*or_comma=*/true,
7942 /*consume_paren=*/true);
7943 if (ending < 0)
7944 goto get_comma;
7945 if (!ending)
7946 {
7947 parser->greater_than_is_operator_p
7948 = saved_greater_than_is_operator_p;
7949 return NULL;
7950 }
7951 }
7952
7953 parser->greater_than_is_operator_p
7954 = saved_greater_than_is_operator_p;
7955
7956 if (identifier)
7957 vec_safe_insert (expression_list, 0, identifier);
7958
7959 return expression_list;
7960 }
7961
7962 /* Parse a pseudo-destructor-name.
7963
7964 pseudo-destructor-name:
7965 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7966 :: [opt] nested-name-specifier template template-id :: ~ type-name
7967 :: [opt] nested-name-specifier [opt] ~ type-name
7968
7969 If either of the first two productions is used, sets *SCOPE to the
7970 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7971 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7972 or ERROR_MARK_NODE if the parse fails. */
7973
7974 static void
7975 cp_parser_pseudo_destructor_name (cp_parser* parser,
7976 tree object,
7977 tree* scope,
7978 tree* type)
7979 {
7980 bool nested_name_specifier_p;
7981
7982 /* Handle ~auto. */
7983 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7984 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7985 && !type_dependent_expression_p (object))
7986 {
7987 if (cxx_dialect < cxx14)
7988 pedwarn (input_location, 0,
7989 "%<~auto%> only available with "
7990 "-std=c++14 or -std=gnu++14");
7991 cp_lexer_consume_token (parser->lexer);
7992 cp_lexer_consume_token (parser->lexer);
7993 *scope = NULL_TREE;
7994 *type = TREE_TYPE (object);
7995 return;
7996 }
7997
7998 /* Assume that things will not work out. */
7999 *type = error_mark_node;
8000
8001 /* Look for the optional `::' operator. */
8002 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8003 /* Look for the optional nested-name-specifier. */
8004 nested_name_specifier_p
8005 = (cp_parser_nested_name_specifier_opt (parser,
8006 /*typename_keyword_p=*/false,
8007 /*check_dependency_p=*/true,
8008 /*type_p=*/false,
8009 /*is_declaration=*/false)
8010 != NULL_TREE);
8011 /* Now, if we saw a nested-name-specifier, we might be doing the
8012 second production. */
8013 if (nested_name_specifier_p
8014 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8015 {
8016 /* Consume the `template' keyword. */
8017 cp_lexer_consume_token (parser->lexer);
8018 /* Parse the template-id. */
8019 cp_parser_template_id (parser,
8020 /*template_keyword_p=*/true,
8021 /*check_dependency_p=*/false,
8022 class_type,
8023 /*is_declaration=*/true);
8024 /* Look for the `::' token. */
8025 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8026 }
8027 /* If the next token is not a `~', then there might be some
8028 additional qualification. */
8029 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8030 {
8031 /* At this point, we're looking for "type-name :: ~". The type-name
8032 must not be a class-name, since this is a pseudo-destructor. So,
8033 it must be either an enum-name, or a typedef-name -- both of which
8034 are just identifiers. So, we peek ahead to check that the "::"
8035 and "~" tokens are present; if they are not, then we can avoid
8036 calling type_name. */
8037 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8038 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8039 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8040 {
8041 cp_parser_error (parser, "non-scalar type");
8042 return;
8043 }
8044
8045 /* Look for the type-name. */
8046 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8047 if (*scope == error_mark_node)
8048 return;
8049
8050 /* Look for the `::' token. */
8051 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8052 }
8053 else
8054 *scope = NULL_TREE;
8055
8056 /* Look for the `~'. */
8057 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8058
8059 /* Once we see the ~, this has to be a pseudo-destructor. */
8060 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8061 cp_parser_commit_to_topmost_tentative_parse (parser);
8062
8063 /* Look for the type-name again. We are not responsible for
8064 checking that it matches the first type-name. */
8065 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8066 }
8067
8068 /* Parse a unary-expression.
8069
8070 unary-expression:
8071 postfix-expression
8072 ++ cast-expression
8073 -- cast-expression
8074 unary-operator cast-expression
8075 sizeof unary-expression
8076 sizeof ( type-id )
8077 alignof ( type-id ) [C++0x]
8078 new-expression
8079 delete-expression
8080
8081 GNU Extensions:
8082
8083 unary-expression:
8084 __extension__ cast-expression
8085 __alignof__ unary-expression
8086 __alignof__ ( type-id )
8087 alignof unary-expression [C++0x]
8088 __real__ cast-expression
8089 __imag__ cast-expression
8090 && identifier
8091 sizeof ( type-id ) { initializer-list , [opt] }
8092 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8093 __alignof__ ( type-id ) { initializer-list , [opt] }
8094
8095 ADDRESS_P is true iff the unary-expression is appearing as the
8096 operand of the `&' operator. CAST_P is true if this expression is
8097 the target of a cast.
8098
8099 Returns a representation of the expression. */
8100
8101 static cp_expr
8102 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8103 bool address_p, bool cast_p, bool decltype_p)
8104 {
8105 cp_token *token;
8106 enum tree_code unary_operator;
8107
8108 /* Peek at the next token. */
8109 token = cp_lexer_peek_token (parser->lexer);
8110 /* Some keywords give away the kind of expression. */
8111 if (token->type == CPP_KEYWORD)
8112 {
8113 enum rid keyword = token->keyword;
8114
8115 switch (keyword)
8116 {
8117 case RID_ALIGNOF:
8118 case RID_SIZEOF:
8119 {
8120 tree operand, ret;
8121 enum tree_code op;
8122 location_t start_loc = token->location;
8123
8124 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8125 bool std_alignof = id_equal (token->u.value, "alignof");
8126
8127 /* Consume the token. */
8128 cp_lexer_consume_token (parser->lexer);
8129 /* Parse the operand. */
8130 operand = cp_parser_sizeof_operand (parser, keyword);
8131
8132 if (TYPE_P (operand))
8133 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8134 true);
8135 else
8136 {
8137 /* ISO C++ defines alignof only with types, not with
8138 expressions. So pedwarn if alignof is used with a non-
8139 type expression. However, __alignof__ is ok. */
8140 if (std_alignof)
8141 pedwarn (token->location, OPT_Wpedantic,
8142 "ISO C++ does not allow %<alignof%> "
8143 "with a non-type");
8144
8145 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8146 }
8147 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8148 SIZEOF_EXPR with the original operand. */
8149 if (op == SIZEOF_EXPR && ret != error_mark_node)
8150 {
8151 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8152 {
8153 if (!processing_template_decl && TYPE_P (operand))
8154 {
8155 ret = build_min (SIZEOF_EXPR, size_type_node,
8156 build1 (NOP_EXPR, operand,
8157 error_mark_node));
8158 SIZEOF_EXPR_TYPE_P (ret) = 1;
8159 }
8160 else
8161 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8162 TREE_SIDE_EFFECTS (ret) = 0;
8163 TREE_READONLY (ret) = 1;
8164 }
8165 }
8166
8167 /* Construct a location e.g. :
8168 alignof (expr)
8169 ^~~~~~~~~~~~~~
8170 with start == caret at the start of the "alignof"/"sizeof"
8171 token, with the endpoint at the final closing paren. */
8172 location_t finish_loc
8173 = cp_lexer_previous_token (parser->lexer)->location;
8174 location_t compound_loc
8175 = make_location (start_loc, start_loc, finish_loc);
8176
8177 cp_expr ret_expr (ret);
8178 ret_expr.set_location (compound_loc);
8179 ret_expr = ret_expr.maybe_add_location_wrapper ();
8180 return ret_expr;
8181 }
8182
8183 case RID_BUILTIN_HAS_ATTRIBUTE:
8184 return cp_parser_has_attribute_expression (parser);
8185
8186 case RID_NEW:
8187 return cp_parser_new_expression (parser);
8188
8189 case RID_DELETE:
8190 return cp_parser_delete_expression (parser);
8191
8192 case RID_EXTENSION:
8193 {
8194 /* The saved value of the PEDANTIC flag. */
8195 int saved_pedantic;
8196 tree expr;
8197
8198 /* Save away the PEDANTIC flag. */
8199 cp_parser_extension_opt (parser, &saved_pedantic);
8200 /* Parse the cast-expression. */
8201 expr = cp_parser_simple_cast_expression (parser);
8202 /* Restore the PEDANTIC flag. */
8203 pedantic = saved_pedantic;
8204
8205 return expr;
8206 }
8207
8208 case RID_REALPART:
8209 case RID_IMAGPART:
8210 {
8211 tree expression;
8212
8213 /* Consume the `__real__' or `__imag__' token. */
8214 cp_lexer_consume_token (parser->lexer);
8215 /* Parse the cast-expression. */
8216 expression = cp_parser_simple_cast_expression (parser);
8217 /* Create the complete representation. */
8218 return build_x_unary_op (token->location,
8219 (keyword == RID_REALPART
8220 ? REALPART_EXPR : IMAGPART_EXPR),
8221 expression,
8222 tf_warning_or_error);
8223 }
8224 break;
8225
8226 case RID_TRANSACTION_ATOMIC:
8227 case RID_TRANSACTION_RELAXED:
8228 return cp_parser_transaction_expression (parser, keyword);
8229
8230 case RID_NOEXCEPT:
8231 {
8232 tree expr;
8233 const char *saved_message;
8234 bool saved_integral_constant_expression_p;
8235 bool saved_non_integral_constant_expression_p;
8236 bool saved_greater_than_is_operator_p;
8237
8238 location_t start_loc = token->location;
8239
8240 cp_lexer_consume_token (parser->lexer);
8241 matching_parens parens;
8242 parens.require_open (parser);
8243
8244 saved_message = parser->type_definition_forbidden_message;
8245 parser->type_definition_forbidden_message
8246 = G_("types may not be defined in %<noexcept%> expressions");
8247
8248 saved_integral_constant_expression_p
8249 = parser->integral_constant_expression_p;
8250 saved_non_integral_constant_expression_p
8251 = parser->non_integral_constant_expression_p;
8252 parser->integral_constant_expression_p = false;
8253
8254 saved_greater_than_is_operator_p
8255 = parser->greater_than_is_operator_p;
8256 parser->greater_than_is_operator_p = true;
8257
8258 ++cp_unevaluated_operand;
8259 ++c_inhibit_evaluation_warnings;
8260 ++cp_noexcept_operand;
8261 expr = cp_parser_expression (parser);
8262 --cp_noexcept_operand;
8263 --c_inhibit_evaluation_warnings;
8264 --cp_unevaluated_operand;
8265
8266 parser->greater_than_is_operator_p
8267 = saved_greater_than_is_operator_p;
8268
8269 parser->integral_constant_expression_p
8270 = saved_integral_constant_expression_p;
8271 parser->non_integral_constant_expression_p
8272 = saved_non_integral_constant_expression_p;
8273
8274 parser->type_definition_forbidden_message = saved_message;
8275
8276 location_t finish_loc
8277 = cp_lexer_peek_token (parser->lexer)->location;
8278 parens.require_close (parser);
8279
8280 /* Construct a location of the form:
8281 noexcept (expr)
8282 ^~~~~~~~~~~~~~~
8283 with start == caret, finishing at the close-paren. */
8284 location_t noexcept_loc
8285 = make_location (start_loc, start_loc, finish_loc);
8286
8287 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8288 noexcept_loc);
8289 }
8290
8291 default:
8292 break;
8293 }
8294 }
8295
8296 /* Look for the `:: new' and `:: delete', which also signal the
8297 beginning of a new-expression, or delete-expression,
8298 respectively. If the next token is `::', then it might be one of
8299 these. */
8300 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8301 {
8302 enum rid keyword;
8303
8304 /* See if the token after the `::' is one of the keywords in
8305 which we're interested. */
8306 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8307 /* If it's `new', we have a new-expression. */
8308 if (keyword == RID_NEW)
8309 return cp_parser_new_expression (parser);
8310 /* Similarly, for `delete'. */
8311 else if (keyword == RID_DELETE)
8312 return cp_parser_delete_expression (parser);
8313 }
8314
8315 /* Look for a unary operator. */
8316 unary_operator = cp_parser_unary_operator (token);
8317 /* The `++' and `--' operators can be handled similarly, even though
8318 they are not technically unary-operators in the grammar. */
8319 if (unary_operator == ERROR_MARK)
8320 {
8321 if (token->type == CPP_PLUS_PLUS)
8322 unary_operator = PREINCREMENT_EXPR;
8323 else if (token->type == CPP_MINUS_MINUS)
8324 unary_operator = PREDECREMENT_EXPR;
8325 /* Handle the GNU address-of-label extension. */
8326 else if (cp_parser_allow_gnu_extensions_p (parser)
8327 && token->type == CPP_AND_AND)
8328 {
8329 tree identifier;
8330 tree expression;
8331 location_t start_loc = token->location;
8332
8333 /* Consume the '&&' token. */
8334 cp_lexer_consume_token (parser->lexer);
8335 /* Look for the identifier. */
8336 location_t finish_loc
8337 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8338 identifier = cp_parser_identifier (parser);
8339 /* Construct a location of the form:
8340 &&label
8341 ^~~~~~~
8342 with caret==start at the "&&", finish at the end of the label. */
8343 location_t combined_loc
8344 = make_location (start_loc, start_loc, finish_loc);
8345 /* Create an expression representing the address. */
8346 expression = finish_label_address_expr (identifier, combined_loc);
8347 if (cp_parser_non_integral_constant_expression (parser,
8348 NIC_ADDR_LABEL))
8349 expression = error_mark_node;
8350 return expression;
8351 }
8352 }
8353 if (unary_operator != ERROR_MARK)
8354 {
8355 cp_expr cast_expression;
8356 cp_expr expression = error_mark_node;
8357 non_integral_constant non_constant_p = NIC_NONE;
8358 location_t loc = token->location;
8359 tsubst_flags_t complain = complain_flags (decltype_p);
8360
8361 /* Consume the operator token. */
8362 token = cp_lexer_consume_token (parser->lexer);
8363 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8364
8365 /* Parse the cast-expression. */
8366 cast_expression
8367 = cp_parser_cast_expression (parser,
8368 unary_operator == ADDR_EXPR,
8369 /*cast_p=*/false,
8370 /*decltype*/false,
8371 pidk);
8372
8373 /* Make a location:
8374 OP_TOKEN CAST_EXPRESSION
8375 ^~~~~~~~~~~~~~~~~~~~~~~~~
8376 with start==caret at the operator token, and
8377 extending to the end of the cast_expression. */
8378 loc = make_location (loc, loc, cast_expression.get_finish ());
8379
8380 /* Now, build an appropriate representation. */
8381 switch (unary_operator)
8382 {
8383 case INDIRECT_REF:
8384 non_constant_p = NIC_STAR;
8385 expression = build_x_indirect_ref (loc, cast_expression,
8386 RO_UNARY_STAR,
8387 complain);
8388 /* TODO: build_x_indirect_ref does not always honor the
8389 location, so ensure it is set. */
8390 expression.set_location (loc);
8391 break;
8392
8393 case ADDR_EXPR:
8394 non_constant_p = NIC_ADDR;
8395 /* Fall through. */
8396 case BIT_NOT_EXPR:
8397 expression = build_x_unary_op (loc, unary_operator,
8398 cast_expression,
8399 complain);
8400 /* TODO: build_x_unary_op does not always honor the location,
8401 so ensure it is set. */
8402 expression.set_location (loc);
8403 break;
8404
8405 case PREINCREMENT_EXPR:
8406 case PREDECREMENT_EXPR:
8407 non_constant_p = unary_operator == PREINCREMENT_EXPR
8408 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8409 /* Fall through. */
8410 case NEGATE_EXPR:
8411 /* Immediately fold negation of a constant, unless the constant is 0
8412 (since -0 == 0) or it would overflow. */
8413 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8414 {
8415 tree stripped_expr
8416 = tree_strip_any_location_wrapper (cast_expression);
8417 if (CONSTANT_CLASS_P (stripped_expr)
8418 && !integer_zerop (stripped_expr)
8419 && !TREE_OVERFLOW (stripped_expr))
8420 {
8421 tree folded = fold_build1 (unary_operator,
8422 TREE_TYPE (stripped_expr),
8423 stripped_expr);
8424 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8425 {
8426 expression = maybe_wrap_with_location (folded, loc);
8427 break;
8428 }
8429 }
8430 }
8431 /* Fall through. */
8432 case UNARY_PLUS_EXPR:
8433 case TRUTH_NOT_EXPR:
8434 expression = finish_unary_op_expr (loc, unary_operator,
8435 cast_expression, complain);
8436 break;
8437
8438 default:
8439 gcc_unreachable ();
8440 }
8441
8442 if (non_constant_p != NIC_NONE
8443 && cp_parser_non_integral_constant_expression (parser,
8444 non_constant_p))
8445 expression = error_mark_node;
8446
8447 return expression;
8448 }
8449
8450 return cp_parser_postfix_expression (parser, address_p, cast_p,
8451 /*member_access_only_p=*/false,
8452 decltype_p,
8453 pidk);
8454 }
8455
8456 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8457 unary-operator, the corresponding tree code is returned. */
8458
8459 static enum tree_code
8460 cp_parser_unary_operator (cp_token* token)
8461 {
8462 switch (token->type)
8463 {
8464 case CPP_MULT:
8465 return INDIRECT_REF;
8466
8467 case CPP_AND:
8468 return ADDR_EXPR;
8469
8470 case CPP_PLUS:
8471 return UNARY_PLUS_EXPR;
8472
8473 case CPP_MINUS:
8474 return NEGATE_EXPR;
8475
8476 case CPP_NOT:
8477 return TRUTH_NOT_EXPR;
8478
8479 case CPP_COMPL:
8480 return BIT_NOT_EXPR;
8481
8482 default:
8483 return ERROR_MARK;
8484 }
8485 }
8486
8487 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8488 Returns a representation of the expression. */
8489
8490 static tree
8491 cp_parser_has_attribute_expression (cp_parser *parser)
8492 {
8493 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8494
8495 /* Consume the __builtin_has_attribute token. */
8496 cp_lexer_consume_token (parser->lexer);
8497
8498 matching_parens parens;
8499 if (!parens.require_open (parser))
8500 return error_mark_node;
8501
8502 /* Types cannot be defined in a `sizeof' expression. Save away the
8503 old message. */
8504 const char *saved_message = parser->type_definition_forbidden_message;
8505 /* And create the new one. */
8506 const int kwd = RID_BUILTIN_HAS_ATTRIBUTE;
8507 char *tmp = concat ("types may not be defined in %<",
8508 IDENTIFIER_POINTER (ridpointers[kwd]),
8509 "%> expressions", NULL);
8510 parser->type_definition_forbidden_message = tmp;
8511
8512 /* The restrictions on constant-expressions do not apply inside
8513 sizeof expressions. */
8514 bool saved_integral_constant_expression_p
8515 = parser->integral_constant_expression_p;
8516 bool saved_non_integral_constant_expression_p
8517 = parser->non_integral_constant_expression_p;
8518 parser->integral_constant_expression_p = false;
8519
8520 /* Do not actually evaluate the expression. */
8521 ++cp_unevaluated_operand;
8522 ++c_inhibit_evaluation_warnings;
8523
8524 tree oper = NULL_TREE;
8525
8526 /* We can't be sure yet whether we're looking at a type-id or an
8527 expression. */
8528 cp_parser_parse_tentatively (parser);
8529
8530 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8531 parser->in_type_id_in_expr_p = true;
8532 /* Look for the type-id. */
8533 oper = cp_parser_type_id (parser);
8534 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8535
8536 cp_parser_parse_definitely (parser);
8537
8538 /* If the type-id production did not work out, then we must be
8539 looking at the unary-expression production. */
8540 if (!oper || oper == error_mark_node)
8541 oper = cp_parser_unary_expression (parser);
8542
8543 STRIP_ANY_LOCATION_WRAPPER (oper);
8544
8545 /* Go back to evaluating expressions. */
8546 --cp_unevaluated_operand;
8547 --c_inhibit_evaluation_warnings;
8548
8549 /* Free the message we created. */
8550 free (tmp);
8551 /* And restore the old one. */
8552 parser->type_definition_forbidden_message = saved_message;
8553 parser->integral_constant_expression_p
8554 = saved_integral_constant_expression_p;
8555 parser->non_integral_constant_expression_p
8556 = saved_non_integral_constant_expression_p;
8557
8558 /* Consume the comma if it's there. */
8559 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
8560 {
8561 cp_parser_skip_to_closing_parenthesis (parser, false, false,
8562 /*consume_paren=*/true);
8563 return error_mark_node;
8564 }
8565
8566 /* Parse the attribute specification. */
8567 bool ret = false;
8568 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
8569 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
8570 {
8571 if (oper != error_mark_node)
8572 {
8573 /* Fold constant expressions used in attributes first. */
8574 cp_check_const_attributes (attr);
8575
8576 /* Finally, see if OPER has been declared with ATTR. */
8577 ret = has_attribute (atloc, oper, attr, default_conversion);
8578 }
8579
8580 parens.require_close (parser);
8581 }
8582 else
8583 {
8584 error_at (atloc, "expected identifier");
8585 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8586 }
8587
8588 /* Construct a location e.g. :
8589 __builtin_has_attribute (oper, attr)
8590 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8591 with start == caret at the start of the built-in token,
8592 and with the endpoint at the final closing paren. */
8593 location_t finish_loc
8594 = cp_lexer_previous_token (parser->lexer)->location;
8595 location_t compound_loc
8596 = make_location (start_loc, start_loc, finish_loc);
8597
8598 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
8599 ret_expr.set_location (compound_loc);
8600 ret_expr = ret_expr.maybe_add_location_wrapper ();
8601 return ret_expr;
8602 }
8603
8604 /* Parse a new-expression.
8605
8606 new-expression:
8607 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8608 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8609
8610 Returns a representation of the expression. */
8611
8612 static tree
8613 cp_parser_new_expression (cp_parser* parser)
8614 {
8615 bool global_scope_p;
8616 vec<tree, va_gc> *placement;
8617 tree type;
8618 vec<tree, va_gc> *initializer;
8619 tree nelts = NULL_TREE;
8620 tree ret;
8621
8622 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8623
8624 /* Look for the optional `::' operator. */
8625 global_scope_p
8626 = (cp_parser_global_scope_opt (parser,
8627 /*current_scope_valid_p=*/false)
8628 != NULL_TREE);
8629 /* Look for the `new' operator. */
8630 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8631 /* There's no easy way to tell a new-placement from the
8632 `( type-id )' construct. */
8633 cp_parser_parse_tentatively (parser);
8634 /* Look for a new-placement. */
8635 placement = cp_parser_new_placement (parser);
8636 /* If that didn't work out, there's no new-placement. */
8637 if (!cp_parser_parse_definitely (parser))
8638 {
8639 if (placement != NULL)
8640 release_tree_vector (placement);
8641 placement = NULL;
8642 }
8643
8644 /* If the next token is a `(', then we have a parenthesized
8645 type-id. */
8646 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8647 {
8648 cp_token *token;
8649 const char *saved_message = parser->type_definition_forbidden_message;
8650
8651 /* Consume the `('. */
8652 matching_parens parens;
8653 parens.consume_open (parser);
8654
8655 /* Parse the type-id. */
8656 parser->type_definition_forbidden_message
8657 = G_("types may not be defined in a new-expression");
8658 {
8659 type_id_in_expr_sentinel s (parser);
8660 type = cp_parser_type_id (parser);
8661 }
8662 parser->type_definition_forbidden_message = saved_message;
8663
8664 /* Look for the closing `)'. */
8665 parens.require_close (parser);
8666 token = cp_lexer_peek_token (parser->lexer);
8667 /* There should not be a direct-new-declarator in this production,
8668 but GCC used to allowed this, so we check and emit a sensible error
8669 message for this case. */
8670 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8671 {
8672 error_at (token->location,
8673 "array bound forbidden after parenthesized type-id");
8674 inform (token->location,
8675 "try removing the parentheses around the type-id");
8676 cp_parser_direct_new_declarator (parser);
8677 }
8678 }
8679 /* Otherwise, there must be a new-type-id. */
8680 else
8681 type = cp_parser_new_type_id (parser, &nelts);
8682
8683 /* If the next token is a `(' or '{', then we have a new-initializer. */
8684 cp_token *token = cp_lexer_peek_token (parser->lexer);
8685 if (token->type == CPP_OPEN_PAREN
8686 || token->type == CPP_OPEN_BRACE)
8687 initializer = cp_parser_new_initializer (parser);
8688 else
8689 initializer = NULL;
8690
8691 /* A new-expression may not appear in an integral constant
8692 expression. */
8693 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8694 ret = error_mark_node;
8695 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8696 of a new-type-id or type-id of a new-expression, the new-expression shall
8697 contain a new-initializer of the form ( assignment-expression )".
8698 Additionally, consistently with the spirit of DR 1467, we want to accept
8699 'new auto { 2 }' too. */
8700 else if ((ret = type_uses_auto (type))
8701 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8702 && (vec_safe_length (initializer) != 1
8703 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8704 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8705 {
8706 error_at (token->location,
8707 "initialization of new-expression for type %<auto%> "
8708 "requires exactly one element");
8709 ret = error_mark_node;
8710 }
8711 else
8712 {
8713 /* Construct a location e.g.:
8714 ptr = new int[100]
8715 ^~~~~~~~~~~~
8716 with caret == start at the start of the "new" token, and the end
8717 at the end of the final token we consumed. */
8718 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8719 location_t end_loc = get_finish (end_tok->location);
8720 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8721
8722 /* Create a representation of the new-expression. */
8723 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8724 tf_warning_or_error);
8725 protected_set_expr_location (ret, combined_loc);
8726 }
8727
8728 if (placement != NULL)
8729 release_tree_vector (placement);
8730 if (initializer != NULL)
8731 release_tree_vector (initializer);
8732
8733 return ret;
8734 }
8735
8736 /* Parse a new-placement.
8737
8738 new-placement:
8739 ( expression-list )
8740
8741 Returns the same representation as for an expression-list. */
8742
8743 static vec<tree, va_gc> *
8744 cp_parser_new_placement (cp_parser* parser)
8745 {
8746 vec<tree, va_gc> *expression_list;
8747
8748 /* Parse the expression-list. */
8749 expression_list = (cp_parser_parenthesized_expression_list
8750 (parser, non_attr, /*cast_p=*/false,
8751 /*allow_expansion_p=*/true,
8752 /*non_constant_p=*/NULL));
8753
8754 if (expression_list && expression_list->is_empty ())
8755 error ("expected expression-list or type-id");
8756
8757 return expression_list;
8758 }
8759
8760 /* Parse a new-type-id.
8761
8762 new-type-id:
8763 type-specifier-seq new-declarator [opt]
8764
8765 Returns the TYPE allocated. If the new-type-id indicates an array
8766 type, *NELTS is set to the number of elements in the last array
8767 bound; the TYPE will not include the last array bound. */
8768
8769 static tree
8770 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8771 {
8772 cp_decl_specifier_seq type_specifier_seq;
8773 cp_declarator *new_declarator;
8774 cp_declarator *declarator;
8775 cp_declarator *outer_declarator;
8776 const char *saved_message;
8777
8778 /* The type-specifier sequence must not contain type definitions.
8779 (It cannot contain declarations of new types either, but if they
8780 are not definitions we will catch that because they are not
8781 complete.) */
8782 saved_message = parser->type_definition_forbidden_message;
8783 parser->type_definition_forbidden_message
8784 = G_("types may not be defined in a new-type-id");
8785 /* Parse the type-specifier-seq. */
8786 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
8787 /*is_declaration=*/false,
8788 /*is_trailing_return=*/false,
8789 &type_specifier_seq);
8790 /* Restore the old message. */
8791 parser->type_definition_forbidden_message = saved_message;
8792
8793 if (type_specifier_seq.type == error_mark_node)
8794 return error_mark_node;
8795
8796 /* Parse the new-declarator. */
8797 new_declarator = cp_parser_new_declarator_opt (parser);
8798
8799 /* Determine the number of elements in the last array dimension, if
8800 any. */
8801 *nelts = NULL_TREE;
8802 /* Skip down to the last array dimension. */
8803 declarator = new_declarator;
8804 outer_declarator = NULL;
8805 while (declarator && (declarator->kind == cdk_pointer
8806 || declarator->kind == cdk_ptrmem))
8807 {
8808 outer_declarator = declarator;
8809 declarator = declarator->declarator;
8810 }
8811 while (declarator
8812 && declarator->kind == cdk_array
8813 && declarator->declarator
8814 && declarator->declarator->kind == cdk_array)
8815 {
8816 outer_declarator = declarator;
8817 declarator = declarator->declarator;
8818 }
8819
8820 if (declarator && declarator->kind == cdk_array)
8821 {
8822 *nelts = declarator->u.array.bounds;
8823 if (*nelts == error_mark_node)
8824 *nelts = integer_one_node;
8825
8826 if (outer_declarator)
8827 outer_declarator->declarator = declarator->declarator;
8828 else
8829 new_declarator = NULL;
8830 }
8831
8832 return groktypename (&type_specifier_seq, new_declarator, false);
8833 }
8834
8835 /* Parse an (optional) new-declarator.
8836
8837 new-declarator:
8838 ptr-operator new-declarator [opt]
8839 direct-new-declarator
8840
8841 Returns the declarator. */
8842
8843 static cp_declarator *
8844 cp_parser_new_declarator_opt (cp_parser* parser)
8845 {
8846 enum tree_code code;
8847 tree type, std_attributes = NULL_TREE;
8848 cp_cv_quals cv_quals;
8849
8850 /* We don't know if there's a ptr-operator next, or not. */
8851 cp_parser_parse_tentatively (parser);
8852 /* Look for a ptr-operator. */
8853 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8854 /* If that worked, look for more new-declarators. */
8855 if (cp_parser_parse_definitely (parser))
8856 {
8857 cp_declarator *declarator;
8858
8859 /* Parse another optional declarator. */
8860 declarator = cp_parser_new_declarator_opt (parser);
8861
8862 declarator = cp_parser_make_indirect_declarator
8863 (code, type, cv_quals, declarator, std_attributes);
8864
8865 return declarator;
8866 }
8867
8868 /* If the next token is a `[', there is a direct-new-declarator. */
8869 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8870 return cp_parser_direct_new_declarator (parser);
8871
8872 return NULL;
8873 }
8874
8875 /* Parse a direct-new-declarator.
8876
8877 direct-new-declarator:
8878 [ expression ]
8879 direct-new-declarator [constant-expression]
8880
8881 */
8882
8883 static cp_declarator *
8884 cp_parser_direct_new_declarator (cp_parser* parser)
8885 {
8886 cp_declarator *declarator = NULL;
8887
8888 while (true)
8889 {
8890 tree expression;
8891 cp_token *token;
8892
8893 /* Look for the opening `['. */
8894 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8895
8896 token = cp_lexer_peek_token (parser->lexer);
8897 expression = cp_parser_expression (parser);
8898 /* The standard requires that the expression have integral
8899 type. DR 74 adds enumeration types. We believe that the
8900 real intent is that these expressions be handled like the
8901 expression in a `switch' condition, which also allows
8902 classes with a single conversion to integral or
8903 enumeration type. */
8904 if (!processing_template_decl)
8905 {
8906 expression
8907 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8908 expression,
8909 /*complain=*/true);
8910 if (!expression)
8911 {
8912 error_at (token->location,
8913 "expression in new-declarator must have integral "
8914 "or enumeration type");
8915 expression = error_mark_node;
8916 }
8917 }
8918
8919 /* Look for the closing `]'. */
8920 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8921
8922 /* Add this bound to the declarator. */
8923 declarator = make_array_declarator (declarator, expression);
8924
8925 /* If the next token is not a `[', then there are no more
8926 bounds. */
8927 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8928 break;
8929 }
8930
8931 return declarator;
8932 }
8933
8934 /* Parse a new-initializer.
8935
8936 new-initializer:
8937 ( expression-list [opt] )
8938 braced-init-list
8939
8940 Returns a representation of the expression-list. */
8941
8942 static vec<tree, va_gc> *
8943 cp_parser_new_initializer (cp_parser* parser)
8944 {
8945 vec<tree, va_gc> *expression_list;
8946
8947 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8948 {
8949 tree t;
8950 bool expr_non_constant_p;
8951 cp_lexer_set_source_position (parser->lexer);
8952 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8953 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8954 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8955 expression_list = make_tree_vector_single (t);
8956 }
8957 else
8958 expression_list = (cp_parser_parenthesized_expression_list
8959 (parser, non_attr, /*cast_p=*/false,
8960 /*allow_expansion_p=*/true,
8961 /*non_constant_p=*/NULL));
8962
8963 return expression_list;
8964 }
8965
8966 /* Parse a delete-expression.
8967
8968 delete-expression:
8969 :: [opt] delete cast-expression
8970 :: [opt] delete [ ] cast-expression
8971
8972 Returns a representation of the expression. */
8973
8974 static tree
8975 cp_parser_delete_expression (cp_parser* parser)
8976 {
8977 bool global_scope_p;
8978 bool array_p;
8979 tree expression;
8980
8981 /* Look for the optional `::' operator. */
8982 global_scope_p
8983 = (cp_parser_global_scope_opt (parser,
8984 /*current_scope_valid_p=*/false)
8985 != NULL_TREE);
8986 /* Look for the `delete' keyword. */
8987 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8988 /* See if the array syntax is in use. */
8989 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8990 {
8991 /* Consume the `[' token. */
8992 cp_lexer_consume_token (parser->lexer);
8993 /* Look for the `]' token. */
8994 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8995 /* Remember that this is the `[]' construct. */
8996 array_p = true;
8997 }
8998 else
8999 array_p = false;
9000
9001 /* Parse the cast-expression. */
9002 expression = cp_parser_simple_cast_expression (parser);
9003
9004 /* A delete-expression may not appear in an integral constant
9005 expression. */
9006 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9007 return error_mark_node;
9008
9009 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
9010 tf_warning_or_error);
9011 }
9012
9013 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9014 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9015 0 otherwise. */
9016
9017 static int
9018 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9019 {
9020 cp_token *token = cp_lexer_peek_token (parser->lexer);
9021 switch (token->type)
9022 {
9023 case CPP_COMMA:
9024 case CPP_SEMICOLON:
9025 case CPP_QUERY:
9026 case CPP_COLON:
9027 case CPP_CLOSE_SQUARE:
9028 case CPP_CLOSE_PAREN:
9029 case CPP_CLOSE_BRACE:
9030 case CPP_OPEN_BRACE:
9031 case CPP_DOT:
9032 case CPP_DOT_STAR:
9033 case CPP_DEREF:
9034 case CPP_DEREF_STAR:
9035 case CPP_DIV:
9036 case CPP_MOD:
9037 case CPP_LSHIFT:
9038 case CPP_RSHIFT:
9039 case CPP_LESS:
9040 case CPP_GREATER:
9041 case CPP_LESS_EQ:
9042 case CPP_GREATER_EQ:
9043 case CPP_EQ_EQ:
9044 case CPP_NOT_EQ:
9045 case CPP_EQ:
9046 case CPP_MULT_EQ:
9047 case CPP_DIV_EQ:
9048 case CPP_MOD_EQ:
9049 case CPP_PLUS_EQ:
9050 case CPP_MINUS_EQ:
9051 case CPP_RSHIFT_EQ:
9052 case CPP_LSHIFT_EQ:
9053 case CPP_AND_EQ:
9054 case CPP_XOR_EQ:
9055 case CPP_OR_EQ:
9056 case CPP_XOR:
9057 case CPP_OR:
9058 case CPP_OR_OR:
9059 case CPP_EOF:
9060 case CPP_ELLIPSIS:
9061 return 0;
9062
9063 case CPP_OPEN_PAREN:
9064 /* In ((type ()) () the last () isn't a valid cast-expression,
9065 so the whole must be parsed as postfix-expression. */
9066 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9067 != CPP_CLOSE_PAREN;
9068
9069 case CPP_OPEN_SQUARE:
9070 /* '[' may start a primary-expression in obj-c++ and in C++11,
9071 as a lambda-expression, eg, '(void)[]{}'. */
9072 if (cxx_dialect >= cxx11)
9073 return -1;
9074 return c_dialect_objc ();
9075
9076 case CPP_PLUS_PLUS:
9077 case CPP_MINUS_MINUS:
9078 /* '++' and '--' may or may not start a cast-expression:
9079
9080 struct T { void operator++(int); };
9081 void f() { (T())++; }
9082
9083 vs
9084
9085 int a;
9086 (int)++a; */
9087 return -1;
9088
9089 default:
9090 return 1;
9091 }
9092 }
9093
9094 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9095 in the order: const_cast, static_cast, reinterpret_cast.
9096
9097 Don't suggest dynamic_cast.
9098
9099 Return the first legal cast kind found, or NULL otherwise. */
9100
9101 static const char *
9102 get_cast_suggestion (tree dst_type, tree orig_expr)
9103 {
9104 tree trial;
9105
9106 /* Reuse the parser logic by attempting to build the various kinds of
9107 cast, with "complain" disabled.
9108 Identify the first such cast that is valid. */
9109
9110 /* Don't attempt to run such logic within template processing. */
9111 if (processing_template_decl)
9112 return NULL;
9113
9114 /* First try const_cast. */
9115 trial = build_const_cast (dst_type, orig_expr, tf_none);
9116 if (trial != error_mark_node)
9117 return "const_cast";
9118
9119 /* If that fails, try static_cast. */
9120 trial = build_static_cast (dst_type, orig_expr, tf_none);
9121 if (trial != error_mark_node)
9122 return "static_cast";
9123
9124 /* Finally, try reinterpret_cast. */
9125 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
9126 if (trial != error_mark_node)
9127 return "reinterpret_cast";
9128
9129 /* No such cast possible. */
9130 return NULL;
9131 }
9132
9133 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9134 suggesting how to convert a C-style cast of the form:
9135
9136 (DST_TYPE)ORIG_EXPR
9137
9138 to a C++-style cast.
9139
9140 The primary range of RICHLOC is asssumed to be that of the original
9141 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9142 of the parens in the C-style cast. */
9143
9144 static void
9145 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9146 location_t close_paren_loc, tree orig_expr,
9147 tree dst_type)
9148 {
9149 /* This function is non-trivial, so bail out now if the warning isn't
9150 going to be emitted. */
9151 if (!warn_old_style_cast)
9152 return;
9153
9154 /* Try to find a legal C++ cast, trying them in order:
9155 const_cast, static_cast, reinterpret_cast. */
9156 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9157 if (!cast_suggestion)
9158 return;
9159
9160 /* Replace the open paren with "CAST_SUGGESTION<". */
9161 pretty_printer pp;
9162 pp_printf (&pp, "%s<", cast_suggestion);
9163 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9164
9165 /* Replace the close paren with "> (". */
9166 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9167
9168 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9169 rich_loc->add_fixit_insert_after (")");
9170 }
9171
9172
9173 /* Parse a cast-expression.
9174
9175 cast-expression:
9176 unary-expression
9177 ( type-id ) cast-expression
9178
9179 ADDRESS_P is true iff the unary-expression is appearing as the
9180 operand of the `&' operator. CAST_P is true if this expression is
9181 the target of a cast.
9182
9183 Returns a representation of the expression. */
9184
9185 static cp_expr
9186 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9187 bool decltype_p, cp_id_kind * pidk)
9188 {
9189 /* If it's a `(', then we might be looking at a cast. */
9190 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9191 {
9192 tree type = NULL_TREE;
9193 cp_expr expr (NULL_TREE);
9194 int cast_expression = 0;
9195 const char *saved_message;
9196
9197 /* There's no way to know yet whether or not this is a cast.
9198 For example, `(int (3))' is a unary-expression, while `(int)
9199 3' is a cast. So, we resort to parsing tentatively. */
9200 cp_parser_parse_tentatively (parser);
9201 /* Types may not be defined in a cast. */
9202 saved_message = parser->type_definition_forbidden_message;
9203 parser->type_definition_forbidden_message
9204 = G_("types may not be defined in casts");
9205 /* Consume the `('. */
9206 matching_parens parens;
9207 cp_token *open_paren = parens.consume_open (parser);
9208 location_t open_paren_loc = open_paren->location;
9209 location_t close_paren_loc = UNKNOWN_LOCATION;
9210
9211 /* A very tricky bit is that `(struct S) { 3 }' is a
9212 compound-literal (which we permit in C++ as an extension).
9213 But, that construct is not a cast-expression -- it is a
9214 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9215 is legal; if the compound-literal were a cast-expression,
9216 you'd need an extra set of parentheses.) But, if we parse
9217 the type-id, and it happens to be a class-specifier, then we
9218 will commit to the parse at that point, because we cannot
9219 undo the action that is done when creating a new class. So,
9220 then we cannot back up and do a postfix-expression.
9221
9222 Another tricky case is the following (c++/29234):
9223
9224 struct S { void operator () (); };
9225
9226 void foo ()
9227 {
9228 ( S()() );
9229 }
9230
9231 As a type-id we parse the parenthesized S()() as a function
9232 returning a function, groktypename complains and we cannot
9233 back up in this case either.
9234
9235 Therefore, we scan ahead to the closing `)', and check to see
9236 if the tokens after the `)' can start a cast-expression. Otherwise
9237 we are dealing with an unary-expression, a postfix-expression
9238 or something else.
9239
9240 Yet another tricky case, in C++11, is the following (c++/54891):
9241
9242 (void)[]{};
9243
9244 The issue is that usually, besides the case of lambda-expressions,
9245 the parenthesized type-id cannot be followed by '[', and, eg, we
9246 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9247 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9248 we don't commit, we try a cast-expression, then an unary-expression.
9249
9250 Save tokens so that we can put them back. */
9251 cp_lexer_save_tokens (parser->lexer);
9252
9253 /* We may be looking at a cast-expression. */
9254 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9255 /*consume_paren=*/true))
9256 cast_expression
9257 = cp_parser_tokens_start_cast_expression (parser);
9258
9259 /* Roll back the tokens we skipped. */
9260 cp_lexer_rollback_tokens (parser->lexer);
9261 /* If we aren't looking at a cast-expression, simulate an error so
9262 that the call to cp_parser_error_occurred below returns true. */
9263 if (!cast_expression)
9264 cp_parser_simulate_error (parser);
9265 else
9266 {
9267 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9268 parser->in_type_id_in_expr_p = true;
9269 /* Look for the type-id. */
9270 type = cp_parser_type_id (parser);
9271 /* Look for the closing `)'. */
9272 cp_token *close_paren = parens.require_close (parser);
9273 if (close_paren)
9274 close_paren_loc = close_paren->location;
9275 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9276 }
9277
9278 /* Restore the saved message. */
9279 parser->type_definition_forbidden_message = saved_message;
9280
9281 /* At this point this can only be either a cast or a
9282 parenthesized ctor such as `(T ())' that looks like a cast to
9283 function returning T. */
9284 if (!cp_parser_error_occurred (parser))
9285 {
9286 /* Only commit if the cast-expression doesn't start with
9287 '++', '--', or '[' in C++11. */
9288 if (cast_expression > 0)
9289 cp_parser_commit_to_topmost_tentative_parse (parser);
9290
9291 expr = cp_parser_cast_expression (parser,
9292 /*address_p=*/false,
9293 /*cast_p=*/true,
9294 /*decltype_p=*/false,
9295 pidk);
9296
9297 if (cp_parser_parse_definitely (parser))
9298 {
9299 /* Warn about old-style casts, if so requested. */
9300 if (warn_old_style_cast
9301 && !in_system_header_at (input_location)
9302 && !VOID_TYPE_P (type)
9303 && current_lang_name != lang_name_c)
9304 {
9305 gcc_rich_location rich_loc (input_location);
9306 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9307 expr, type);
9308 warning_at (&rich_loc, OPT_Wold_style_cast,
9309 "use of old-style cast to %q#T", type);
9310 }
9311
9312 /* Only type conversions to integral or enumeration types
9313 can be used in constant-expressions. */
9314 if (!cast_valid_in_integral_constant_expression_p (type)
9315 && cp_parser_non_integral_constant_expression (parser,
9316 NIC_CAST))
9317 return error_mark_node;
9318
9319 /* Perform the cast. */
9320 /* Make a location:
9321 (TYPE) EXPR
9322 ^~~~~~~~~~~
9323 with start==caret at the open paren, extending to the
9324 end of "expr". */
9325 location_t cast_loc = make_location (open_paren_loc,
9326 open_paren_loc,
9327 expr.get_finish ());
9328 expr = build_c_cast (cast_loc, type, expr);
9329 return expr;
9330 }
9331 }
9332 else
9333 cp_parser_abort_tentative_parse (parser);
9334 }
9335
9336 /* If we get here, then it's not a cast, so it must be a
9337 unary-expression. */
9338 return cp_parser_unary_expression (parser, pidk, address_p,
9339 cast_p, decltype_p);
9340 }
9341
9342 /* Parse a binary expression of the general form:
9343
9344 pm-expression:
9345 cast-expression
9346 pm-expression .* cast-expression
9347 pm-expression ->* cast-expression
9348
9349 multiplicative-expression:
9350 pm-expression
9351 multiplicative-expression * pm-expression
9352 multiplicative-expression / pm-expression
9353 multiplicative-expression % pm-expression
9354
9355 additive-expression:
9356 multiplicative-expression
9357 additive-expression + multiplicative-expression
9358 additive-expression - multiplicative-expression
9359
9360 shift-expression:
9361 additive-expression
9362 shift-expression << additive-expression
9363 shift-expression >> additive-expression
9364
9365 relational-expression:
9366 shift-expression
9367 relational-expression < shift-expression
9368 relational-expression > shift-expression
9369 relational-expression <= shift-expression
9370 relational-expression >= shift-expression
9371
9372 GNU Extension:
9373
9374 relational-expression:
9375 relational-expression <? shift-expression
9376 relational-expression >? shift-expression
9377
9378 equality-expression:
9379 relational-expression
9380 equality-expression == relational-expression
9381 equality-expression != relational-expression
9382
9383 and-expression:
9384 equality-expression
9385 and-expression & equality-expression
9386
9387 exclusive-or-expression:
9388 and-expression
9389 exclusive-or-expression ^ and-expression
9390
9391 inclusive-or-expression:
9392 exclusive-or-expression
9393 inclusive-or-expression | exclusive-or-expression
9394
9395 logical-and-expression:
9396 inclusive-or-expression
9397 logical-and-expression && inclusive-or-expression
9398
9399 logical-or-expression:
9400 logical-and-expression
9401 logical-or-expression || logical-and-expression
9402
9403 All these are implemented with a single function like:
9404
9405 binary-expression:
9406 simple-cast-expression
9407 binary-expression <token> binary-expression
9408
9409 CAST_P is true if this expression is the target of a cast.
9410
9411 The binops_by_token map is used to get the tree codes for each <token> type.
9412 binary-expressions are associated according to a precedence table. */
9413
9414 #define TOKEN_PRECEDENCE(token) \
9415 (((token->type == CPP_GREATER \
9416 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9417 && !parser->greater_than_is_operator_p) \
9418 ? PREC_NOT_OPERATOR \
9419 : binops_by_token[token->type].prec)
9420
9421 static cp_expr
9422 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9423 bool no_toplevel_fold_p,
9424 bool decltype_p,
9425 enum cp_parser_prec prec,
9426 cp_id_kind * pidk)
9427 {
9428 cp_parser_expression_stack stack;
9429 cp_parser_expression_stack_entry *sp = &stack[0];
9430 cp_parser_expression_stack_entry current;
9431 cp_expr rhs;
9432 cp_token *token;
9433 enum tree_code rhs_type;
9434 enum cp_parser_prec new_prec, lookahead_prec;
9435 tree overload;
9436
9437 /* Parse the first expression. */
9438 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9439 ? TRUTH_NOT_EXPR : ERROR_MARK);
9440 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9441 cast_p, decltype_p, pidk);
9442 current.prec = prec;
9443
9444 if (cp_parser_error_occurred (parser))
9445 return error_mark_node;
9446
9447 for (;;)
9448 {
9449 /* Get an operator token. */
9450 token = cp_lexer_peek_token (parser->lexer);
9451
9452 if (warn_cxx11_compat
9453 && token->type == CPP_RSHIFT
9454 && !parser->greater_than_is_operator_p)
9455 {
9456 if (warning_at (token->location, OPT_Wc__11_compat,
9457 "%<>>%> operator is treated"
9458 " as two right angle brackets in C++11"))
9459 inform (token->location,
9460 "suggest parentheses around %<>>%> expression");
9461 }
9462
9463 new_prec = TOKEN_PRECEDENCE (token);
9464 if (new_prec != PREC_NOT_OPERATOR
9465 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9466 /* This is a fold-expression; handle it later. */
9467 new_prec = PREC_NOT_OPERATOR;
9468
9469 /* Popping an entry off the stack means we completed a subexpression:
9470 - either we found a token which is not an operator (`>' where it is not
9471 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9472 will happen repeatedly;
9473 - or, we found an operator which has lower priority. This is the case
9474 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9475 parsing `3 * 4'. */
9476 if (new_prec <= current.prec)
9477 {
9478 if (sp == stack)
9479 break;
9480 else
9481 goto pop;
9482 }
9483
9484 get_rhs:
9485 current.tree_type = binops_by_token[token->type].tree_type;
9486 current.loc = token->location;
9487
9488 /* We used the operator token. */
9489 cp_lexer_consume_token (parser->lexer);
9490
9491 /* For "false && x" or "true || x", x will never be executed;
9492 disable warnings while evaluating it. */
9493 if (current.tree_type == TRUTH_ANDIF_EXPR)
9494 c_inhibit_evaluation_warnings +=
9495 cp_fully_fold (current.lhs) == truthvalue_false_node;
9496 else if (current.tree_type == TRUTH_ORIF_EXPR)
9497 c_inhibit_evaluation_warnings +=
9498 cp_fully_fold (current.lhs) == truthvalue_true_node;
9499
9500 /* Extract another operand. It may be the RHS of this expression
9501 or the LHS of a new, higher priority expression. */
9502 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9503 ? TRUTH_NOT_EXPR : ERROR_MARK);
9504 rhs = cp_parser_simple_cast_expression (parser);
9505
9506 /* Get another operator token. Look up its precedence to avoid
9507 building a useless (immediately popped) stack entry for common
9508 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9509 token = cp_lexer_peek_token (parser->lexer);
9510 lookahead_prec = TOKEN_PRECEDENCE (token);
9511 if (lookahead_prec != PREC_NOT_OPERATOR
9512 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9513 lookahead_prec = PREC_NOT_OPERATOR;
9514 if (lookahead_prec > new_prec)
9515 {
9516 /* ... and prepare to parse the RHS of the new, higher priority
9517 expression. Since precedence levels on the stack are
9518 monotonically increasing, we do not have to care about
9519 stack overflows. */
9520 *sp = current;
9521 ++sp;
9522 current.lhs = rhs;
9523 current.lhs_type = rhs_type;
9524 current.prec = new_prec;
9525 new_prec = lookahead_prec;
9526 goto get_rhs;
9527
9528 pop:
9529 lookahead_prec = new_prec;
9530 /* If the stack is not empty, we have parsed into LHS the right side
9531 (`4' in the example above) of an expression we had suspended.
9532 We can use the information on the stack to recover the LHS (`3')
9533 from the stack together with the tree code (`MULT_EXPR'), and
9534 the precedence of the higher level subexpression
9535 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9536 which will be used to actually build the additive expression. */
9537 rhs = current.lhs;
9538 rhs_type = current.lhs_type;
9539 --sp;
9540 current = *sp;
9541 }
9542
9543 /* Undo the disabling of warnings done above. */
9544 if (current.tree_type == TRUTH_ANDIF_EXPR)
9545 c_inhibit_evaluation_warnings -=
9546 cp_fully_fold (current.lhs) == truthvalue_false_node;
9547 else if (current.tree_type == TRUTH_ORIF_EXPR)
9548 c_inhibit_evaluation_warnings -=
9549 cp_fully_fold (current.lhs) == truthvalue_true_node;
9550
9551 if (warn_logical_not_paren
9552 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9553 && current.lhs_type == TRUTH_NOT_EXPR
9554 /* Avoid warning for !!x == y. */
9555 && (TREE_CODE (current.lhs) != NE_EXPR
9556 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9557 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9558 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9559 /* Avoid warning for !b == y where b is boolean. */
9560 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9561 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9562 != BOOLEAN_TYPE))))
9563 /* Avoid warning for !!b == y where b is boolean. */
9564 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
9565 || TREE_TYPE (current.lhs) == NULL_TREE
9566 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9567 warn_logical_not_parentheses (current.loc, current.tree_type,
9568 current.lhs, maybe_constant_value (rhs));
9569
9570 overload = NULL;
9571
9572 location_t combined_loc = make_location (current.loc,
9573 current.lhs.get_start (),
9574 rhs.get_finish ());
9575
9576 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9577 ERROR_MARK for everything that is not a binary expression.
9578 This makes warn_about_parentheses miss some warnings that
9579 involve unary operators. For unary expressions we should
9580 pass the correct tree_code unless the unary expression was
9581 surrounded by parentheses.
9582 */
9583 if (no_toplevel_fold_p
9584 && lookahead_prec <= current.prec
9585 && sp == stack)
9586 {
9587 if (current.lhs == error_mark_node || rhs == error_mark_node)
9588 current.lhs = error_mark_node;
9589 else
9590 {
9591 current.lhs
9592 = build_min (current.tree_type,
9593 TREE_CODE_CLASS (current.tree_type)
9594 == tcc_comparison
9595 ? boolean_type_node : TREE_TYPE (current.lhs),
9596 current.lhs.get_value (), rhs.get_value ());
9597 SET_EXPR_LOCATION (current.lhs, combined_loc);
9598 }
9599 }
9600 else
9601 {
9602 op_location_t op_loc (current.loc, combined_loc);
9603 current.lhs = build_x_binary_op (op_loc, current.tree_type,
9604 current.lhs, current.lhs_type,
9605 rhs, rhs_type, &overload,
9606 complain_flags (decltype_p));
9607 /* TODO: build_x_binary_op doesn't always honor the location. */
9608 current.lhs.set_location (combined_loc);
9609 }
9610 current.lhs_type = current.tree_type;
9611
9612 /* If the binary operator required the use of an overloaded operator,
9613 then this expression cannot be an integral constant-expression.
9614 An overloaded operator can be used even if both operands are
9615 otherwise permissible in an integral constant-expression if at
9616 least one of the operands is of enumeration type. */
9617
9618 if (overload
9619 && cp_parser_non_integral_constant_expression (parser,
9620 NIC_OVERLOADED))
9621 return error_mark_node;
9622 }
9623
9624 return current.lhs;
9625 }
9626
9627 static cp_expr
9628 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9629 bool no_toplevel_fold_p,
9630 enum cp_parser_prec prec,
9631 cp_id_kind * pidk)
9632 {
9633 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9634 /*decltype*/false, prec, pidk);
9635 }
9636
9637 /* Parse the `? expression : assignment-expression' part of a
9638 conditional-expression. The LOGICAL_OR_EXPR is the
9639 logical-or-expression that started the conditional-expression.
9640 Returns a representation of the entire conditional-expression.
9641
9642 This routine is used by cp_parser_assignment_expression.
9643
9644 ? expression : assignment-expression
9645
9646 GNU Extensions:
9647
9648 ? : assignment-expression */
9649
9650 static tree
9651 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9652 {
9653 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9654 cp_expr assignment_expr;
9655 struct cp_token *token;
9656 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9657
9658 /* Consume the `?' token. */
9659 cp_lexer_consume_token (parser->lexer);
9660 token = cp_lexer_peek_token (parser->lexer);
9661 if (cp_parser_allow_gnu_extensions_p (parser)
9662 && token->type == CPP_COLON)
9663 {
9664 pedwarn (token->location, OPT_Wpedantic,
9665 "ISO C++ does not allow ?: with omitted middle operand");
9666 /* Implicit true clause. */
9667 expr = NULL_TREE;
9668 c_inhibit_evaluation_warnings +=
9669 folded_logical_or_expr == truthvalue_true_node;
9670 warn_for_omitted_condop (token->location, logical_or_expr);
9671 }
9672 else
9673 {
9674 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9675 parser->colon_corrects_to_scope_p = false;
9676 /* Parse the expression. */
9677 c_inhibit_evaluation_warnings +=
9678 folded_logical_or_expr == truthvalue_false_node;
9679 expr = cp_parser_expression (parser);
9680 c_inhibit_evaluation_warnings +=
9681 ((folded_logical_or_expr == truthvalue_true_node)
9682 - (folded_logical_or_expr == truthvalue_false_node));
9683 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9684 }
9685
9686 /* The next token should be a `:'. */
9687 cp_parser_require (parser, CPP_COLON, RT_COLON);
9688 /* Parse the assignment-expression. */
9689 assignment_expr = cp_parser_assignment_expression (parser);
9690 c_inhibit_evaluation_warnings -=
9691 folded_logical_or_expr == truthvalue_true_node;
9692
9693 /* Make a location:
9694 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9695 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9696 with the caret at the "?", ranging from the start of
9697 the logical_or_expr to the end of the assignment_expr. */
9698 loc = make_location (loc,
9699 logical_or_expr.get_start (),
9700 assignment_expr.get_finish ());
9701
9702 /* Build the conditional-expression. */
9703 return build_x_conditional_expr (loc, logical_or_expr,
9704 expr,
9705 assignment_expr,
9706 tf_warning_or_error);
9707 }
9708
9709 /* Parse an assignment-expression.
9710
9711 assignment-expression:
9712 conditional-expression
9713 logical-or-expression assignment-operator assignment_expression
9714 throw-expression
9715
9716 CAST_P is true if this expression is the target of a cast.
9717 DECLTYPE_P is true if this expression is the operand of decltype.
9718
9719 Returns a representation for the expression. */
9720
9721 static cp_expr
9722 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9723 bool cast_p, bool decltype_p)
9724 {
9725 cp_expr expr;
9726
9727 /* If the next token is the `throw' keyword, then we're looking at
9728 a throw-expression. */
9729 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9730 expr = cp_parser_throw_expression (parser);
9731 /* Otherwise, it must be that we are looking at a
9732 logical-or-expression. */
9733 else
9734 {
9735 /* Parse the binary expressions (logical-or-expression). */
9736 expr = cp_parser_binary_expression (parser, cast_p, false,
9737 decltype_p,
9738 PREC_NOT_OPERATOR, pidk);
9739 /* If the next token is a `?' then we're actually looking at a
9740 conditional-expression. */
9741 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9742 return cp_parser_question_colon_clause (parser, expr);
9743 else
9744 {
9745 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9746
9747 /* If it's an assignment-operator, we're using the second
9748 production. */
9749 enum tree_code assignment_operator
9750 = cp_parser_assignment_operator_opt (parser);
9751 if (assignment_operator != ERROR_MARK)
9752 {
9753 bool non_constant_p;
9754
9755 /* Parse the right-hand side of the assignment. */
9756 cp_expr rhs = cp_parser_initializer_clause (parser,
9757 &non_constant_p);
9758
9759 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9760 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9761
9762 /* An assignment may not appear in a
9763 constant-expression. */
9764 if (cp_parser_non_integral_constant_expression (parser,
9765 NIC_ASSIGNMENT))
9766 return error_mark_node;
9767 /* Build the assignment expression. Its default
9768 location:
9769 LHS = RHS
9770 ~~~~^~~~~
9771 is the location of the '=' token as the
9772 caret, ranging from the start of the lhs to the
9773 end of the rhs. */
9774 loc = make_location (loc,
9775 expr.get_start (),
9776 rhs.get_finish ());
9777 expr = build_x_modify_expr (loc, expr,
9778 assignment_operator,
9779 rhs,
9780 complain_flags (decltype_p));
9781 /* TODO: build_x_modify_expr doesn't honor the location,
9782 so we must set it here. */
9783 expr.set_location (loc);
9784 }
9785 }
9786 }
9787
9788 return expr;
9789 }
9790
9791 /* Parse an (optional) assignment-operator.
9792
9793 assignment-operator: one of
9794 = *= /= %= += -= >>= <<= &= ^= |=
9795
9796 GNU Extension:
9797
9798 assignment-operator: one of
9799 <?= >?=
9800
9801 If the next token is an assignment operator, the corresponding tree
9802 code is returned, and the token is consumed. For example, for
9803 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9804 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9805 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9806 operator, ERROR_MARK is returned. */
9807
9808 static enum tree_code
9809 cp_parser_assignment_operator_opt (cp_parser* parser)
9810 {
9811 enum tree_code op;
9812 cp_token *token;
9813
9814 /* Peek at the next token. */
9815 token = cp_lexer_peek_token (parser->lexer);
9816
9817 switch (token->type)
9818 {
9819 case CPP_EQ:
9820 op = NOP_EXPR;
9821 break;
9822
9823 case CPP_MULT_EQ:
9824 op = MULT_EXPR;
9825 break;
9826
9827 case CPP_DIV_EQ:
9828 op = TRUNC_DIV_EXPR;
9829 break;
9830
9831 case CPP_MOD_EQ:
9832 op = TRUNC_MOD_EXPR;
9833 break;
9834
9835 case CPP_PLUS_EQ:
9836 op = PLUS_EXPR;
9837 break;
9838
9839 case CPP_MINUS_EQ:
9840 op = MINUS_EXPR;
9841 break;
9842
9843 case CPP_RSHIFT_EQ:
9844 op = RSHIFT_EXPR;
9845 break;
9846
9847 case CPP_LSHIFT_EQ:
9848 op = LSHIFT_EXPR;
9849 break;
9850
9851 case CPP_AND_EQ:
9852 op = BIT_AND_EXPR;
9853 break;
9854
9855 case CPP_XOR_EQ:
9856 op = BIT_XOR_EXPR;
9857 break;
9858
9859 case CPP_OR_EQ:
9860 op = BIT_IOR_EXPR;
9861 break;
9862
9863 default:
9864 /* Nothing else is an assignment operator. */
9865 op = ERROR_MARK;
9866 }
9867
9868 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9869 if (op != ERROR_MARK
9870 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9871 op = ERROR_MARK;
9872
9873 /* If it was an assignment operator, consume it. */
9874 if (op != ERROR_MARK)
9875 cp_lexer_consume_token (parser->lexer);
9876
9877 return op;
9878 }
9879
9880 /* Parse an expression.
9881
9882 expression:
9883 assignment-expression
9884 expression , assignment-expression
9885
9886 CAST_P is true if this expression is the target of a cast.
9887 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9888 except possibly parenthesized or on the RHS of a comma (N3276).
9889
9890 Returns a representation of the expression. */
9891
9892 static cp_expr
9893 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9894 bool cast_p, bool decltype_p)
9895 {
9896 cp_expr expression = NULL_TREE;
9897 location_t loc = UNKNOWN_LOCATION;
9898
9899 while (true)
9900 {
9901 cp_expr assignment_expression;
9902
9903 /* Parse the next assignment-expression. */
9904 assignment_expression
9905 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9906
9907 /* We don't create a temporary for a call that is the immediate operand
9908 of decltype or on the RHS of a comma. But when we see a comma, we
9909 need to create a temporary for a call on the LHS. */
9910 if (decltype_p && !processing_template_decl
9911 && TREE_CODE (assignment_expression) == CALL_EXPR
9912 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9913 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9914 assignment_expression
9915 = build_cplus_new (TREE_TYPE (assignment_expression),
9916 assignment_expression, tf_warning_or_error);
9917
9918 /* If this is the first assignment-expression, we can just
9919 save it away. */
9920 if (!expression)
9921 expression = assignment_expression;
9922 else
9923 {
9924 /* Create a location with caret at the comma, ranging
9925 from the start of the LHS to the end of the RHS. */
9926 loc = make_location (loc,
9927 expression.get_start (),
9928 assignment_expression.get_finish ());
9929 expression = build_x_compound_expr (loc, expression,
9930 assignment_expression,
9931 complain_flags (decltype_p));
9932 expression.set_location (loc);
9933 }
9934 /* If the next token is not a comma, or we're in a fold-expression, then
9935 we are done with the expression. */
9936 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9937 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9938 break;
9939 /* Consume the `,'. */
9940 loc = cp_lexer_peek_token (parser->lexer)->location;
9941 cp_lexer_consume_token (parser->lexer);
9942 /* A comma operator cannot appear in a constant-expression. */
9943 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9944 expression = error_mark_node;
9945 }
9946
9947 return expression;
9948 }
9949
9950 /* Parse a constant-expression.
9951
9952 constant-expression:
9953 conditional-expression
9954
9955 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9956 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9957 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9958 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9959 only parse a conditional-expression, otherwise parse an
9960 assignment-expression. See below for rationale. */
9961
9962 static cp_expr
9963 cp_parser_constant_expression (cp_parser* parser,
9964 bool allow_non_constant_p,
9965 bool *non_constant_p,
9966 bool strict_p)
9967 {
9968 bool saved_integral_constant_expression_p;
9969 bool saved_allow_non_integral_constant_expression_p;
9970 bool saved_non_integral_constant_expression_p;
9971 cp_expr expression;
9972
9973 /* It might seem that we could simply parse the
9974 conditional-expression, and then check to see if it were
9975 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9976 one that the compiler can figure out is constant, possibly after
9977 doing some simplifications or optimizations. The standard has a
9978 precise definition of constant-expression, and we must honor
9979 that, even though it is somewhat more restrictive.
9980
9981 For example:
9982
9983 int i[(2, 3)];
9984
9985 is not a legal declaration, because `(2, 3)' is not a
9986 constant-expression. The `,' operator is forbidden in a
9987 constant-expression. However, GCC's constant-folding machinery
9988 will fold this operation to an INTEGER_CST for `3'. */
9989
9990 /* Save the old settings. */
9991 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9992 saved_allow_non_integral_constant_expression_p
9993 = parser->allow_non_integral_constant_expression_p;
9994 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9995 /* We are now parsing a constant-expression. */
9996 parser->integral_constant_expression_p = true;
9997 parser->allow_non_integral_constant_expression_p
9998 = (allow_non_constant_p || cxx_dialect >= cxx11);
9999 parser->non_integral_constant_expression_p = false;
10000 /* Although the grammar says "conditional-expression", when not STRICT_P,
10001 we parse an "assignment-expression", which also permits
10002 "throw-expression" and the use of assignment operators. In the case
10003 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10004 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10005 actually essential that we look for an assignment-expression.
10006 For example, cp_parser_initializer_clauses uses this function to
10007 determine whether a particular assignment-expression is in fact
10008 constant. */
10009 if (strict_p)
10010 {
10011 /* Parse the binary expressions (logical-or-expression). */
10012 expression = cp_parser_binary_expression (parser, false, false, false,
10013 PREC_NOT_OPERATOR, NULL);
10014 /* If the next token is a `?' then we're actually looking at
10015 a conditional-expression; otherwise we're done. */
10016 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10017 expression = cp_parser_question_colon_clause (parser, expression);
10018 }
10019 else
10020 expression = cp_parser_assignment_expression (parser);
10021 /* Restore the old settings. */
10022 parser->integral_constant_expression_p
10023 = saved_integral_constant_expression_p;
10024 parser->allow_non_integral_constant_expression_p
10025 = saved_allow_non_integral_constant_expression_p;
10026 if (cxx_dialect >= cxx11)
10027 {
10028 /* Require an rvalue constant expression here; that's what our
10029 callers expect. Reference constant expressions are handled
10030 separately in e.g. cp_parser_template_argument. */
10031 tree decay = expression;
10032 if (TREE_TYPE (expression)
10033 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10034 decay = build_address (expression);
10035 bool is_const = potential_rvalue_constant_expression (decay);
10036 parser->non_integral_constant_expression_p = !is_const;
10037 if (!is_const && !allow_non_constant_p)
10038 require_potential_rvalue_constant_expression (decay);
10039 }
10040 if (allow_non_constant_p)
10041 *non_constant_p = parser->non_integral_constant_expression_p;
10042 parser->non_integral_constant_expression_p
10043 = saved_non_integral_constant_expression_p;
10044
10045 return expression;
10046 }
10047
10048 /* Parse __builtin_offsetof.
10049
10050 offsetof-expression:
10051 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10052
10053 offsetof-member-designator:
10054 id-expression
10055 | offsetof-member-designator "." id-expression
10056 | offsetof-member-designator "[" expression "]"
10057 | offsetof-member-designator "->" id-expression */
10058
10059 static cp_expr
10060 cp_parser_builtin_offsetof (cp_parser *parser)
10061 {
10062 int save_ice_p, save_non_ice_p;
10063 tree type;
10064 cp_expr expr;
10065 cp_id_kind dummy;
10066 cp_token *token;
10067 location_t finish_loc;
10068
10069 /* We're about to accept non-integral-constant things, but will
10070 definitely yield an integral constant expression. Save and
10071 restore these values around our local parsing. */
10072 save_ice_p = parser->integral_constant_expression_p;
10073 save_non_ice_p = parser->non_integral_constant_expression_p;
10074
10075 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10076
10077 /* Consume the "__builtin_offsetof" token. */
10078 cp_lexer_consume_token (parser->lexer);
10079 /* Consume the opening `('. */
10080 matching_parens parens;
10081 parens.require_open (parser);
10082 /* Parse the type-id. */
10083 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10084 {
10085 const char *saved_message = parser->type_definition_forbidden_message;
10086 parser->type_definition_forbidden_message
10087 = G_("types may not be defined within __builtin_offsetof");
10088 type = cp_parser_type_id (parser);
10089 parser->type_definition_forbidden_message = saved_message;
10090 }
10091 /* Look for the `,'. */
10092 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10093 token = cp_lexer_peek_token (parser->lexer);
10094
10095 /* Build the (type *)null that begins the traditional offsetof macro. */
10096 tree object_ptr
10097 = build_static_cast (build_pointer_type (type), null_pointer_node,
10098 tf_warning_or_error);
10099
10100 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10101 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10102 true, &dummy, token->location);
10103 while (true)
10104 {
10105 token = cp_lexer_peek_token (parser->lexer);
10106 switch (token->type)
10107 {
10108 case CPP_OPEN_SQUARE:
10109 /* offsetof-member-designator "[" expression "]" */
10110 expr = cp_parser_postfix_open_square_expression (parser, expr,
10111 true, false);
10112 break;
10113
10114 case CPP_DEREF:
10115 /* offsetof-member-designator "->" identifier */
10116 expr = grok_array_decl (token->location, expr,
10117 integer_zero_node, false);
10118 /* FALLTHRU */
10119
10120 case CPP_DOT:
10121 /* offsetof-member-designator "." identifier */
10122 cp_lexer_consume_token (parser->lexer);
10123 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10124 expr, true, &dummy,
10125 token->location);
10126 break;
10127
10128 case CPP_CLOSE_PAREN:
10129 /* Consume the ")" token. */
10130 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10131 cp_lexer_consume_token (parser->lexer);
10132 goto success;
10133
10134 default:
10135 /* Error. We know the following require will fail, but
10136 that gives the proper error message. */
10137 parens.require_close (parser);
10138 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10139 expr = error_mark_node;
10140 goto failure;
10141 }
10142 }
10143
10144 success:
10145 /* Make a location of the form:
10146 __builtin_offsetof (struct s, f)
10147 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10148 with caret at the type-id, ranging from the start of the
10149 "_builtin_offsetof" token to the close paren. */
10150 loc = make_location (loc, start_loc, finish_loc);
10151 /* The result will be an INTEGER_CST, so we need to explicitly
10152 preserve the location. */
10153 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10154
10155 failure:
10156 parser->integral_constant_expression_p = save_ice_p;
10157 parser->non_integral_constant_expression_p = save_non_ice_p;
10158
10159 expr = expr.maybe_add_location_wrapper ();
10160 return expr;
10161 }
10162
10163 /* Parse a trait expression.
10164
10165 Returns a representation of the expression, the underlying type
10166 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10167
10168 static cp_expr
10169 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10170 {
10171 cp_trait_kind kind;
10172 tree type1, type2 = NULL_TREE;
10173 bool binary = false;
10174 bool variadic = false;
10175
10176 switch (keyword)
10177 {
10178 case RID_HAS_NOTHROW_ASSIGN:
10179 kind = CPTK_HAS_NOTHROW_ASSIGN;
10180 break;
10181 case RID_HAS_NOTHROW_CONSTRUCTOR:
10182 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10183 break;
10184 case RID_HAS_NOTHROW_COPY:
10185 kind = CPTK_HAS_NOTHROW_COPY;
10186 break;
10187 case RID_HAS_TRIVIAL_ASSIGN:
10188 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10189 break;
10190 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10191 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10192 break;
10193 case RID_HAS_TRIVIAL_COPY:
10194 kind = CPTK_HAS_TRIVIAL_COPY;
10195 break;
10196 case RID_HAS_TRIVIAL_DESTRUCTOR:
10197 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10198 break;
10199 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10200 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10201 break;
10202 case RID_HAS_VIRTUAL_DESTRUCTOR:
10203 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10204 break;
10205 case RID_IS_ABSTRACT:
10206 kind = CPTK_IS_ABSTRACT;
10207 break;
10208 case RID_IS_AGGREGATE:
10209 kind = CPTK_IS_AGGREGATE;
10210 break;
10211 case RID_IS_BASE_OF:
10212 kind = CPTK_IS_BASE_OF;
10213 binary = true;
10214 break;
10215 case RID_IS_CLASS:
10216 kind = CPTK_IS_CLASS;
10217 break;
10218 case RID_IS_EMPTY:
10219 kind = CPTK_IS_EMPTY;
10220 break;
10221 case RID_IS_ENUM:
10222 kind = CPTK_IS_ENUM;
10223 break;
10224 case RID_IS_FINAL:
10225 kind = CPTK_IS_FINAL;
10226 break;
10227 case RID_IS_LITERAL_TYPE:
10228 kind = CPTK_IS_LITERAL_TYPE;
10229 break;
10230 case RID_IS_POD:
10231 kind = CPTK_IS_POD;
10232 break;
10233 case RID_IS_POLYMORPHIC:
10234 kind = CPTK_IS_POLYMORPHIC;
10235 break;
10236 case RID_IS_SAME_AS:
10237 kind = CPTK_IS_SAME_AS;
10238 binary = true;
10239 break;
10240 case RID_IS_STD_LAYOUT:
10241 kind = CPTK_IS_STD_LAYOUT;
10242 break;
10243 case RID_IS_TRIVIAL:
10244 kind = CPTK_IS_TRIVIAL;
10245 break;
10246 case RID_IS_TRIVIALLY_ASSIGNABLE:
10247 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10248 binary = true;
10249 break;
10250 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10251 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10252 variadic = true;
10253 break;
10254 case RID_IS_TRIVIALLY_COPYABLE:
10255 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10256 break;
10257 case RID_IS_UNION:
10258 kind = CPTK_IS_UNION;
10259 break;
10260 case RID_UNDERLYING_TYPE:
10261 kind = CPTK_UNDERLYING_TYPE;
10262 break;
10263 case RID_BASES:
10264 kind = CPTK_BASES;
10265 break;
10266 case RID_DIRECT_BASES:
10267 kind = CPTK_DIRECT_BASES;
10268 break;
10269 case RID_IS_ASSIGNABLE:
10270 kind = CPTK_IS_ASSIGNABLE;
10271 binary = true;
10272 break;
10273 case RID_IS_CONSTRUCTIBLE:
10274 kind = CPTK_IS_CONSTRUCTIBLE;
10275 variadic = true;
10276 break;
10277 default:
10278 gcc_unreachable ();
10279 }
10280
10281 /* Get location of initial token. */
10282 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10283
10284 /* Consume the token. */
10285 cp_lexer_consume_token (parser->lexer);
10286
10287 matching_parens parens;
10288 parens.require_open (parser);
10289
10290 {
10291 type_id_in_expr_sentinel s (parser);
10292 type1 = cp_parser_type_id (parser);
10293 }
10294
10295 if (type1 == error_mark_node)
10296 return error_mark_node;
10297
10298 if (binary)
10299 {
10300 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10301
10302 {
10303 type_id_in_expr_sentinel s (parser);
10304 type2 = cp_parser_type_id (parser);
10305 }
10306
10307 if (type2 == error_mark_node)
10308 return error_mark_node;
10309 }
10310 else if (variadic)
10311 {
10312 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10313 {
10314 cp_lexer_consume_token (parser->lexer);
10315 tree elt = cp_parser_type_id (parser);
10316 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10317 {
10318 cp_lexer_consume_token (parser->lexer);
10319 elt = make_pack_expansion (elt);
10320 }
10321 if (elt == error_mark_node)
10322 return error_mark_node;
10323 type2 = tree_cons (NULL_TREE, elt, type2);
10324 }
10325 }
10326
10327 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10328 parens.require_close (parser);
10329
10330 /* Construct a location of the form:
10331 __is_trivially_copyable(_Tp)
10332 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10333 with start == caret, finishing at the close-paren. */
10334 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10335
10336 /* Complete the trait expression, which may mean either processing
10337 the trait expr now or saving it for template instantiation. */
10338 switch (kind)
10339 {
10340 case CPTK_UNDERLYING_TYPE:
10341 return cp_expr (finish_underlying_type (type1), trait_loc);
10342 case CPTK_BASES:
10343 return cp_expr (finish_bases (type1, false), trait_loc);
10344 case CPTK_DIRECT_BASES:
10345 return cp_expr (finish_bases (type1, true), trait_loc);
10346 default:
10347 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10348 }
10349 }
10350
10351 /* Parse a lambda expression.
10352
10353 lambda-expression:
10354 lambda-introducer lambda-declarator [opt] compound-statement
10355
10356 Returns a representation of the expression. */
10357
10358 static cp_expr
10359 cp_parser_lambda_expression (cp_parser* parser)
10360 {
10361 tree lambda_expr = build_lambda_expr ();
10362 tree type;
10363 bool ok = true;
10364 cp_token *token = cp_lexer_peek_token (parser->lexer);
10365 cp_token_position start = 0;
10366
10367 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10368
10369 if (cxx_dialect >= cxx2a)
10370 /* C++20 allows lambdas in unevaluated context. */;
10371 else if (cp_unevaluated_operand)
10372 {
10373 if (!token->error_reported)
10374 {
10375 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10376 "lambda-expression in unevaluated context"
10377 " only available with -std=c++2a or -std=gnu++2a");
10378 token->error_reported = true;
10379 }
10380 ok = false;
10381 }
10382 else if (parser->in_template_argument_list_p)
10383 {
10384 if (!token->error_reported)
10385 {
10386 error_at (token->location, "lambda-expression in template-argument"
10387 " only available with -std=c++2a or -std=gnu++2a");
10388 token->error_reported = true;
10389 }
10390 ok = false;
10391 }
10392
10393 /* We may be in the middle of deferred access check. Disable
10394 it now. */
10395 push_deferring_access_checks (dk_no_deferred);
10396
10397 cp_parser_lambda_introducer (parser, lambda_expr);
10398 if (cp_parser_error_occurred (parser))
10399 return error_mark_node;
10400
10401 type = begin_lambda_type (lambda_expr);
10402 if (type == error_mark_node)
10403 return error_mark_node;
10404
10405 record_lambda_scope (lambda_expr);
10406
10407 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10408 determine_visibility (TYPE_NAME (type));
10409
10410 /* Now that we've started the type, add the capture fields for any
10411 explicit captures. */
10412 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10413
10414 {
10415 /* Inside the class, surrounding template-parameter-lists do not apply. */
10416 unsigned int saved_num_template_parameter_lists
10417 = parser->num_template_parameter_lists;
10418 unsigned char in_statement = parser->in_statement;
10419 bool in_switch_statement_p = parser->in_switch_statement_p;
10420 bool fully_implicit_function_template_p
10421 = parser->fully_implicit_function_template_p;
10422 tree implicit_template_parms = parser->implicit_template_parms;
10423 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10424 bool auto_is_implicit_function_template_parm_p
10425 = parser->auto_is_implicit_function_template_parm_p;
10426
10427 parser->num_template_parameter_lists = 0;
10428 parser->in_statement = 0;
10429 parser->in_switch_statement_p = false;
10430 parser->fully_implicit_function_template_p = false;
10431 parser->implicit_template_parms = 0;
10432 parser->implicit_template_scope = 0;
10433 parser->auto_is_implicit_function_template_parm_p = false;
10434
10435 /* By virtue of defining a local class, a lambda expression has access to
10436 the private variables of enclosing classes. */
10437
10438 if (cp_parser_start_tentative_firewall (parser))
10439 start = token;
10440
10441 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10442
10443 if (ok && cp_parser_error_occurred (parser))
10444 ok = false;
10445
10446 if (ok)
10447 {
10448 cp_parser_lambda_body (parser, lambda_expr);
10449 }
10450 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10451 {
10452 if (cp_parser_skip_to_closing_brace (parser))
10453 cp_lexer_consume_token (parser->lexer);
10454 }
10455
10456 /* The capture list was built up in reverse order; fix that now. */
10457 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10458 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10459
10460 if (ok)
10461 maybe_add_lambda_conv_op (type);
10462
10463 type = finish_struct (type, /*attributes=*/NULL_TREE);
10464
10465 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10466 parser->in_statement = in_statement;
10467 parser->in_switch_statement_p = in_switch_statement_p;
10468 parser->fully_implicit_function_template_p
10469 = fully_implicit_function_template_p;
10470 parser->implicit_template_parms = implicit_template_parms;
10471 parser->implicit_template_scope = implicit_template_scope;
10472 parser->auto_is_implicit_function_template_parm_p
10473 = auto_is_implicit_function_template_parm_p;
10474 }
10475
10476 /* This field is only used during parsing of the lambda. */
10477 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10478
10479 /* This lambda shouldn't have any proxies left at this point. */
10480 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10481 /* And now that we're done, push proxies for an enclosing lambda. */
10482 insert_pending_capture_proxies ();
10483
10484 /* Update the lambda expression to a range. */
10485 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10486 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10487 token->location,
10488 end_tok->location);
10489
10490 if (ok)
10491 lambda_expr = build_lambda_object (lambda_expr);
10492 else
10493 lambda_expr = error_mark_node;
10494
10495 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10496
10497 pop_deferring_access_checks ();
10498
10499 return lambda_expr;
10500 }
10501
10502 /* Parse the beginning of a lambda expression.
10503
10504 lambda-introducer:
10505 [ lambda-capture [opt] ]
10506
10507 LAMBDA_EXPR is the current representation of the lambda expression. */
10508
10509 static void
10510 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10511 {
10512 /* Need commas after the first capture. */
10513 bool first = true;
10514
10515 /* Eat the leading `['. */
10516 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10517
10518 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10519 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10520 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10521 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10522 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10523 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10524
10525 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10526 {
10527 cp_lexer_consume_token (parser->lexer);
10528 first = false;
10529
10530 if (!(at_function_scope_p () || parsing_nsdmi ()))
10531 error ("non-local lambda expression cannot have a capture-default");
10532 }
10533
10534 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10535 {
10536 cp_token* capture_token;
10537 tree capture_id;
10538 tree capture_init_expr;
10539 cp_id_kind idk = CP_ID_KIND_NONE;
10540 bool explicit_init_p = false;
10541
10542 enum capture_kind_type
10543 {
10544 BY_COPY,
10545 BY_REFERENCE
10546 };
10547 enum capture_kind_type capture_kind = BY_COPY;
10548
10549 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10550 {
10551 error ("expected end of capture-list");
10552 return;
10553 }
10554
10555 if (first)
10556 first = false;
10557 else
10558 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10559
10560 /* Possibly capture `this'. */
10561 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10562 {
10563 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10564 if (cxx_dialect < cxx2a
10565 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10566 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10567 "with by-copy capture default");
10568 cp_lexer_consume_token (parser->lexer);
10569 add_capture (lambda_expr,
10570 /*id=*/this_identifier,
10571 /*initializer=*/finish_this_expr (),
10572 /*by_reference_p=*/true,
10573 explicit_init_p);
10574 continue;
10575 }
10576
10577 /* Possibly capture `*this'. */
10578 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10579 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10580 {
10581 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10582 if (cxx_dialect < cxx17)
10583 pedwarn (loc, 0, "%<*this%> capture only available with "
10584 "-std=c++17 or -std=gnu++17");
10585 cp_lexer_consume_token (parser->lexer);
10586 cp_lexer_consume_token (parser->lexer);
10587 add_capture (lambda_expr,
10588 /*id=*/this_identifier,
10589 /*initializer=*/finish_this_expr (),
10590 /*by_reference_p=*/false,
10591 explicit_init_p);
10592 continue;
10593 }
10594
10595 bool init_pack_expansion = false;
10596 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10597 {
10598 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10599 if (cxx_dialect < cxx2a)
10600 pedwarn (loc, 0, "pack init-capture only available with "
10601 "-std=c++2a or -std=gnu++2a");
10602 cp_lexer_consume_token (parser->lexer);
10603 init_pack_expansion = true;
10604 }
10605
10606 /* Remember whether we want to capture as a reference or not. */
10607 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10608 {
10609 capture_kind = BY_REFERENCE;
10610 cp_lexer_consume_token (parser->lexer);
10611 }
10612
10613 /* Get the identifier. */
10614 capture_token = cp_lexer_peek_token (parser->lexer);
10615 capture_id = cp_parser_identifier (parser);
10616
10617 if (capture_id == error_mark_node)
10618 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10619 delimiters, but I modified this to stop on unnested ']' as well. It
10620 was already changed to stop on unnested '}', so the
10621 "closing_parenthesis" name is no more misleading with my change. */
10622 {
10623 cp_parser_skip_to_closing_parenthesis (parser,
10624 /*recovering=*/true,
10625 /*or_comma=*/true,
10626 /*consume_paren=*/true);
10627 break;
10628 }
10629
10630 /* Find the initializer for this capture. */
10631 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10632 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10633 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10634 {
10635 bool direct, non_constant;
10636 /* An explicit initializer exists. */
10637 if (cxx_dialect < cxx14)
10638 pedwarn (input_location, 0,
10639 "lambda capture initializers "
10640 "only available with -std=c++14 or -std=gnu++14");
10641 capture_init_expr = cp_parser_initializer (parser, &direct,
10642 &non_constant, true);
10643 explicit_init_p = true;
10644 if (capture_init_expr == NULL_TREE)
10645 {
10646 error ("empty initializer for lambda init-capture");
10647 capture_init_expr = error_mark_node;
10648 }
10649 if (init_pack_expansion)
10650 capture_init_expr = make_pack_expansion (capture_init_expr);
10651 }
10652 else
10653 {
10654 const char* error_msg;
10655
10656 /* Turn the identifier into an id-expression. */
10657 capture_init_expr
10658 = cp_parser_lookup_name_simple (parser, capture_id,
10659 capture_token->location);
10660
10661 if (capture_init_expr == error_mark_node)
10662 {
10663 unqualified_name_lookup_error (capture_id);
10664 continue;
10665 }
10666 else if (!VAR_P (capture_init_expr)
10667 && TREE_CODE (capture_init_expr) != PARM_DECL)
10668 {
10669 error_at (capture_token->location,
10670 "capture of non-variable %qE",
10671 capture_init_expr);
10672 if (DECL_P (capture_init_expr))
10673 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10674 "%q#D declared here", capture_init_expr);
10675 continue;
10676 }
10677 if (VAR_P (capture_init_expr)
10678 && decl_storage_duration (capture_init_expr) != dk_auto)
10679 {
10680 if (pedwarn (capture_token->location, 0, "capture of variable "
10681 "%qD with non-automatic storage duration",
10682 capture_init_expr))
10683 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10684 "%q#D declared here", capture_init_expr);
10685 continue;
10686 }
10687
10688 capture_init_expr
10689 = finish_id_expression
10690 (capture_id,
10691 capture_init_expr,
10692 parser->scope,
10693 &idk,
10694 /*integral_constant_expression_p=*/false,
10695 /*allow_non_integral_constant_expression_p=*/false,
10696 /*non_integral_constant_expression_p=*/NULL,
10697 /*template_p=*/false,
10698 /*done=*/true,
10699 /*address_p=*/false,
10700 /*template_arg_p=*/false,
10701 &error_msg,
10702 capture_token->location);
10703
10704 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10705 {
10706 cp_lexer_consume_token (parser->lexer);
10707 capture_init_expr = make_pack_expansion (capture_init_expr);
10708 }
10709 }
10710
10711 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10712 && !explicit_init_p)
10713 {
10714 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10715 && capture_kind == BY_COPY)
10716 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10717 "of %qD redundant with by-copy capture default",
10718 capture_id);
10719 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10720 && capture_kind == BY_REFERENCE)
10721 pedwarn (capture_token->location, 0, "explicit by-reference "
10722 "capture of %qD redundant with by-reference capture "
10723 "default", capture_id);
10724 }
10725
10726 add_capture (lambda_expr,
10727 capture_id,
10728 capture_init_expr,
10729 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10730 explicit_init_p);
10731
10732 /* If there is any qualification still in effect, clear it
10733 now; we will be starting fresh with the next capture. */
10734 parser->scope = NULL_TREE;
10735 parser->qualifying_scope = NULL_TREE;
10736 parser->object_scope = NULL_TREE;
10737 }
10738
10739 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10740 }
10741
10742 /* Parse the (optional) middle of a lambda expression.
10743
10744 lambda-declarator:
10745 < template-parameter-list [opt] >
10746 ( parameter-declaration-clause [opt] )
10747 attribute-specifier [opt]
10748 decl-specifier-seq [opt]
10749 exception-specification [opt]
10750 lambda-return-type-clause [opt]
10751
10752 LAMBDA_EXPR is the current representation of the lambda expression. */
10753
10754 static bool
10755 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10756 {
10757 /* 5.1.1.4 of the standard says:
10758 If a lambda-expression does not include a lambda-declarator, it is as if
10759 the lambda-declarator were ().
10760 This means an empty parameter list, no attributes, and no exception
10761 specification. */
10762 tree param_list = void_list_node;
10763 tree attributes = NULL_TREE;
10764 tree exception_spec = NULL_TREE;
10765 tree template_param_list = NULL_TREE;
10766 tree tx_qual = NULL_TREE;
10767 tree return_type = NULL_TREE;
10768 cp_decl_specifier_seq lambda_specs;
10769 clear_decl_specs (&lambda_specs);
10770
10771 /* The template-parameter-list is optional, but must begin with
10772 an opening angle if present. */
10773 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10774 {
10775 if (cxx_dialect < cxx14)
10776 pedwarn (parser->lexer->next_token->location, 0,
10777 "lambda templates are only available with "
10778 "-std=c++14 or -std=gnu++14");
10779 else if (cxx_dialect < cxx2a)
10780 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10781 "lambda templates are only available with "
10782 "-std=c++2a or -std=gnu++2a");
10783
10784 cp_lexer_consume_token (parser->lexer);
10785
10786 template_param_list = cp_parser_template_parameter_list (parser);
10787
10788 cp_parser_skip_to_end_of_template_parameter_list (parser);
10789
10790 /* We just processed one more parameter list. */
10791 ++parser->num_template_parameter_lists;
10792 }
10793
10794 /* The parameter-declaration-clause is optional (unless
10795 template-parameter-list was given), but must begin with an
10796 opening parenthesis if present. */
10797 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10798 {
10799 matching_parens parens;
10800 parens.consume_open (parser);
10801
10802 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10803
10804 /* Parse parameters. */
10805 param_list
10806 = cp_parser_parameter_declaration_clause
10807 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
10808
10809 /* Default arguments shall not be specified in the
10810 parameter-declaration-clause of a lambda-declarator. */
10811 if (cxx_dialect < cxx14)
10812 for (tree t = param_list; t; t = TREE_CHAIN (t))
10813 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10814 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10815 "default argument specified for lambda parameter");
10816
10817 parens.require_close (parser);
10818
10819 /* In the decl-specifier-seq of the lambda-declarator, each
10820 decl-specifier shall either be mutable or constexpr. */
10821 int declares_class_or_enum;
10822 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10823 cp_parser_decl_specifier_seq (parser,
10824 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10825 &lambda_specs, &declares_class_or_enum);
10826 if (lambda_specs.storage_class == sc_mutable)
10827 {
10828 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10829 if (lambda_specs.conflicting_specifiers_p)
10830 error_at (lambda_specs.locations[ds_storage_class],
10831 "duplicate %<mutable%>");
10832 }
10833
10834 tx_qual = cp_parser_tx_qualifier_opt (parser);
10835
10836 /* Parse optional exception specification. */
10837 exception_spec = cp_parser_exception_specification_opt (parser);
10838
10839 attributes = cp_parser_std_attribute_spec_seq (parser);
10840
10841 /* Parse optional trailing return type. */
10842 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10843 {
10844 cp_lexer_consume_token (parser->lexer);
10845 return_type = cp_parser_trailing_type_id (parser);
10846 }
10847
10848 /* The function parameters must be in scope all the way until after the
10849 trailing-return-type in case of decltype. */
10850 pop_bindings_and_leave_scope ();
10851 }
10852 else if (template_param_list != NULL_TREE) // generate diagnostic
10853 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10854
10855 /* Create the function call operator.
10856
10857 Messing with declarators like this is no uglier than building up the
10858 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10859 other code. */
10860 {
10861 cp_decl_specifier_seq return_type_specs;
10862 cp_declarator* declarator;
10863 tree fco;
10864 int quals;
10865 void *p;
10866
10867 clear_decl_specs (&return_type_specs);
10868 return_type_specs.type = make_auto ();
10869
10870 if (lambda_specs.locations[ds_constexpr])
10871 {
10872 if (cxx_dialect >= cxx17)
10873 return_type_specs.locations[ds_constexpr]
10874 = lambda_specs.locations[ds_constexpr];
10875 else
10876 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10877 "lambda only available with -std=c++17 or -std=gnu++17");
10878 }
10879
10880 p = obstack_alloc (&declarator_obstack, 0);
10881
10882 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
10883 LAMBDA_EXPR_LOCATION (lambda_expr));
10884
10885 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10886 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10887 declarator = make_call_declarator (declarator, param_list, quals,
10888 VIRT_SPEC_UNSPECIFIED,
10889 REF_QUAL_NONE,
10890 tx_qual,
10891 exception_spec,
10892 return_type,
10893 /*requires_clause*/NULL_TREE);
10894 declarator->std_attributes = attributes;
10895
10896 fco = grokmethod (&return_type_specs,
10897 declarator,
10898 NULL_TREE);
10899 if (fco != error_mark_node)
10900 {
10901 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10902 DECL_ARTIFICIAL (fco) = 1;
10903 /* Give the object parameter a different name. */
10904 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10905 DECL_LAMBDA_FUNCTION (fco) = 1;
10906 }
10907 if (template_param_list)
10908 {
10909 fco = finish_member_template_decl (fco);
10910 finish_template_decl (template_param_list);
10911 --parser->num_template_parameter_lists;
10912 }
10913 else if (parser->fully_implicit_function_template_p)
10914 fco = finish_fully_implicit_template (parser, fco);
10915
10916 finish_member_declaration (fco);
10917
10918 obstack_free (&declarator_obstack, p);
10919
10920 return (fco != error_mark_node);
10921 }
10922 }
10923
10924 /* Parse the body of a lambda expression, which is simply
10925
10926 compound-statement
10927
10928 but which requires special handling.
10929 LAMBDA_EXPR is the current representation of the lambda expression. */
10930
10931 static void
10932 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10933 {
10934 bool nested = (current_function_decl != NULL_TREE);
10935 unsigned char local_variables_forbidden_p
10936 = parser->local_variables_forbidden_p;
10937 bool in_function_body = parser->in_function_body;
10938
10939 /* The body of a lambda-expression is not a subexpression of the enclosing
10940 expression. */
10941 cp_evaluated ev;
10942
10943 if (nested)
10944 push_function_context ();
10945 else
10946 /* Still increment function_depth so that we don't GC in the
10947 middle of an expression. */
10948 ++function_depth;
10949
10950 vec<tree> omp_privatization_save;
10951 save_omp_privatization_clauses (omp_privatization_save);
10952 /* Clear this in case we're in the middle of a default argument. */
10953 parser->local_variables_forbidden_p = 0;
10954 parser->in_function_body = true;
10955
10956 {
10957 local_specialization_stack s (lss_copy);
10958 tree fco = lambda_function (lambda_expr);
10959 tree body = start_lambda_function (fco, lambda_expr);
10960 matching_braces braces;
10961
10962 if (braces.require_open (parser))
10963 {
10964 tree compound_stmt = begin_compound_stmt (0);
10965
10966 /* Originally C++11 required us to peek for 'return expr'; and
10967 process it specially here to deduce the return type. N3638
10968 removed the need for that. */
10969
10970 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10971 cp_parser_label_declaration (parser);
10972 cp_parser_statement_seq_opt (parser, NULL_TREE);
10973 braces.require_close (parser);
10974
10975 finish_compound_stmt (compound_stmt);
10976 }
10977
10978 finish_lambda_function (body);
10979 }
10980
10981 restore_omp_privatization_clauses (omp_privatization_save);
10982 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10983 parser->in_function_body = in_function_body;
10984 if (nested)
10985 pop_function_context();
10986 else
10987 --function_depth;
10988 }
10989
10990 /* Statements [gram.stmt.stmt] */
10991
10992 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10993
10994 static void
10995 add_debug_begin_stmt (location_t loc)
10996 {
10997 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10998 return;
10999 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11000 /* A concept is never expanded normally. */
11001 return;
11002
11003 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11004 SET_EXPR_LOCATION (stmt, loc);
11005 add_stmt (stmt);
11006 }
11007
11008 /* Parse a statement.
11009
11010 statement:
11011 labeled-statement
11012 expression-statement
11013 compound-statement
11014 selection-statement
11015 iteration-statement
11016 jump-statement
11017 declaration-statement
11018 try-block
11019
11020 C++11:
11021
11022 statement:
11023 labeled-statement
11024 attribute-specifier-seq (opt) expression-statement
11025 attribute-specifier-seq (opt) compound-statement
11026 attribute-specifier-seq (opt) selection-statement
11027 attribute-specifier-seq (opt) iteration-statement
11028 attribute-specifier-seq (opt) jump-statement
11029 declaration-statement
11030 attribute-specifier-seq (opt) try-block
11031
11032 init-statement:
11033 expression-statement
11034 simple-declaration
11035
11036 TM Extension:
11037
11038 statement:
11039 atomic-statement
11040
11041 IN_COMPOUND is true when the statement is nested inside a
11042 cp_parser_compound_statement; this matters for certain pragmas.
11043
11044 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11045 is a (possibly labeled) if statement which is not enclosed in braces
11046 and has an else clause. This is used to implement -Wparentheses.
11047
11048 CHAIN is a vector of if-else-if conditions. */
11049
11050 static void
11051 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
11052 bool in_compound, bool *if_p, vec<tree> *chain,
11053 location_t *loc_after_labels)
11054 {
11055 tree statement, std_attrs = NULL_TREE;
11056 cp_token *token;
11057 location_t statement_location, attrs_location;
11058
11059 restart:
11060 if (if_p != NULL)
11061 *if_p = false;
11062 /* There is no statement yet. */
11063 statement = NULL_TREE;
11064
11065 saved_token_sentinel saved_tokens (parser->lexer);
11066 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
11067 if (c_dialect_objc ())
11068 /* In obj-c++, seeing '[[' might be the either the beginning of
11069 c++11 attributes, or a nested objc-message-expression. So
11070 let's parse the c++11 attributes tentatively. */
11071 cp_parser_parse_tentatively (parser);
11072 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11073 if (c_dialect_objc ())
11074 {
11075 if (!cp_parser_parse_definitely (parser))
11076 std_attrs = NULL_TREE;
11077 }
11078
11079 /* Peek at the next token. */
11080 token = cp_lexer_peek_token (parser->lexer);
11081 /* Remember the location of the first token in the statement. */
11082 cp_token *statement_token = token;
11083 statement_location = token->location;
11084 add_debug_begin_stmt (statement_location);
11085 /* If this is a keyword, then that will often determine what kind of
11086 statement we have. */
11087 if (token->type == CPP_KEYWORD)
11088 {
11089 enum rid keyword = token->keyword;
11090
11091 switch (keyword)
11092 {
11093 case RID_CASE:
11094 case RID_DEFAULT:
11095 /* Looks like a labeled-statement with a case label.
11096 Parse the label, and then use tail recursion to parse
11097 the statement. */
11098 cp_parser_label_for_labeled_statement (parser, std_attrs);
11099 in_compound = false;
11100 goto restart;
11101
11102 case RID_IF:
11103 case RID_SWITCH:
11104 std_attrs = process_stmt_hotness_attribute (std_attrs);
11105 statement = cp_parser_selection_statement (parser, if_p, chain);
11106 break;
11107
11108 case RID_WHILE:
11109 case RID_DO:
11110 case RID_FOR:
11111 std_attrs = process_stmt_hotness_attribute (std_attrs);
11112 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11113 break;
11114
11115 case RID_BREAK:
11116 case RID_CONTINUE:
11117 case RID_RETURN:
11118 case RID_GOTO:
11119 std_attrs = process_stmt_hotness_attribute (std_attrs);
11120 statement = cp_parser_jump_statement (parser);
11121 break;
11122
11123 /* Objective-C++ exception-handling constructs. */
11124 case RID_AT_TRY:
11125 case RID_AT_CATCH:
11126 case RID_AT_FINALLY:
11127 case RID_AT_SYNCHRONIZED:
11128 case RID_AT_THROW:
11129 std_attrs = process_stmt_hotness_attribute (std_attrs);
11130 statement = cp_parser_objc_statement (parser);
11131 break;
11132
11133 case RID_TRY:
11134 std_attrs = process_stmt_hotness_attribute (std_attrs);
11135 statement = cp_parser_try_block (parser);
11136 break;
11137
11138 case RID_NAMESPACE:
11139 /* This must be a namespace alias definition. */
11140 if (std_attrs != NULL_TREE)
11141 {
11142 /* Attributes should be parsed as part of the the
11143 declaration, so let's un-parse them. */
11144 saved_tokens.rollback();
11145 std_attrs = NULL_TREE;
11146 }
11147 cp_parser_declaration_statement (parser);
11148 return;
11149
11150 case RID_TRANSACTION_ATOMIC:
11151 case RID_TRANSACTION_RELAXED:
11152 case RID_SYNCHRONIZED:
11153 case RID_ATOMIC_NOEXCEPT:
11154 case RID_ATOMIC_CANCEL:
11155 std_attrs = process_stmt_hotness_attribute (std_attrs);
11156 statement = cp_parser_transaction (parser, token);
11157 break;
11158 case RID_TRANSACTION_CANCEL:
11159 std_attrs = process_stmt_hotness_attribute (std_attrs);
11160 statement = cp_parser_transaction_cancel (parser);
11161 break;
11162
11163 default:
11164 /* It might be a keyword like `int' that can start a
11165 declaration-statement. */
11166 break;
11167 }
11168 }
11169 else if (token->type == CPP_NAME)
11170 {
11171 /* If the next token is a `:', then we are looking at a
11172 labeled-statement. */
11173 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11174 if (token->type == CPP_COLON)
11175 {
11176 /* Looks like a labeled-statement with an ordinary label.
11177 Parse the label, and then use tail recursion to parse
11178 the statement. */
11179
11180 cp_parser_label_for_labeled_statement (parser, std_attrs);
11181 in_compound = false;
11182 goto restart;
11183 }
11184 }
11185 /* Anything that starts with a `{' must be a compound-statement. */
11186 else if (token->type == CPP_OPEN_BRACE)
11187 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11188 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11189 a statement all its own. */
11190 else if (token->type == CPP_PRAGMA)
11191 {
11192 /* Only certain OpenMP pragmas are attached to statements, and thus
11193 are considered statements themselves. All others are not. In
11194 the context of a compound, accept the pragma as a "statement" and
11195 return so that we can check for a close brace. Otherwise we
11196 require a real statement and must go back and read one. */
11197 if (in_compound)
11198 cp_parser_pragma (parser, pragma_compound, if_p);
11199 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11200 goto restart;
11201 return;
11202 }
11203 else if (token->type == CPP_EOF)
11204 {
11205 cp_parser_error (parser, "expected statement");
11206 return;
11207 }
11208
11209 /* Everything else must be a declaration-statement or an
11210 expression-statement. Try for the declaration-statement
11211 first, unless we are looking at a `;', in which case we know that
11212 we have an expression-statement. */
11213 if (!statement)
11214 {
11215 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11216 {
11217 if (std_attrs != NULL_TREE)
11218 /* Attributes should be parsed as part of the declaration,
11219 so let's un-parse them. */
11220 saved_tokens.rollback();
11221
11222 cp_parser_parse_tentatively (parser);
11223 /* Try to parse the declaration-statement. */
11224 cp_parser_declaration_statement (parser);
11225 /* If that worked, we're done. */
11226 if (cp_parser_parse_definitely (parser))
11227 return;
11228 /* It didn't work, restore the post-attribute position. */
11229 if (std_attrs)
11230 cp_lexer_set_token_position (parser->lexer, statement_token);
11231 }
11232 /* All preceding labels have been parsed at this point. */
11233 if (loc_after_labels != NULL)
11234 *loc_after_labels = statement_location;
11235
11236 std_attrs = process_stmt_hotness_attribute (std_attrs);
11237
11238 /* Look for an expression-statement instead. */
11239 statement = cp_parser_expression_statement (parser, in_statement_expr);
11240
11241 /* Handle [[fallthrough]];. */
11242 if (attribute_fallthrough_p (std_attrs))
11243 {
11244 /* The next token after the fallthrough attribute is ';'. */
11245 if (statement == NULL_TREE)
11246 {
11247 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11248 statement = build_call_expr_internal_loc (statement_location,
11249 IFN_FALLTHROUGH,
11250 void_type_node, 0);
11251 finish_expr_stmt (statement);
11252 }
11253 else
11254 warning_at (statement_location, OPT_Wattributes,
11255 "%<fallthrough%> attribute not followed by %<;%>");
11256 std_attrs = NULL_TREE;
11257 }
11258 }
11259
11260 /* Set the line number for the statement. */
11261 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11262 SET_EXPR_LOCATION (statement, statement_location);
11263
11264 /* Allow "[[fallthrough]];", but warn otherwise. */
11265 if (std_attrs != NULL_TREE)
11266 warning_at (attrs_location,
11267 OPT_Wattributes,
11268 "attributes at the beginning of statement are ignored");
11269 }
11270
11271 /* Append ATTR to attribute list ATTRS. */
11272
11273 static tree
11274 attr_chainon (tree attrs, tree attr)
11275 {
11276 if (attrs == error_mark_node)
11277 return error_mark_node;
11278 if (attr == error_mark_node)
11279 return error_mark_node;
11280 return chainon (attrs, attr);
11281 }
11282
11283 /* Parse the label for a labeled-statement, i.e.
11284
11285 identifier :
11286 case constant-expression :
11287 default :
11288
11289 GNU Extension:
11290 case constant-expression ... constant-expression : statement
11291
11292 When a label is parsed without errors, the label is added to the
11293 parse tree by the finish_* functions, so this function doesn't
11294 have to return the label. */
11295
11296 static void
11297 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11298 {
11299 cp_token *token;
11300 tree label = NULL_TREE;
11301 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11302
11303 /* The next token should be an identifier. */
11304 token = cp_lexer_peek_token (parser->lexer);
11305 if (token->type != CPP_NAME
11306 && token->type != CPP_KEYWORD)
11307 {
11308 cp_parser_error (parser, "expected labeled-statement");
11309 return;
11310 }
11311
11312 /* Remember whether this case or a user-defined label is allowed to fall
11313 through to. */
11314 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11315
11316 parser->colon_corrects_to_scope_p = false;
11317 switch (token->keyword)
11318 {
11319 case RID_CASE:
11320 {
11321 tree expr, expr_hi;
11322 cp_token *ellipsis;
11323
11324 /* Consume the `case' token. */
11325 cp_lexer_consume_token (parser->lexer);
11326 /* Parse the constant-expression. */
11327 expr = cp_parser_constant_expression (parser);
11328 if (check_for_bare_parameter_packs (expr))
11329 expr = error_mark_node;
11330
11331 ellipsis = cp_lexer_peek_token (parser->lexer);
11332 if (ellipsis->type == CPP_ELLIPSIS)
11333 {
11334 /* Consume the `...' token. */
11335 cp_lexer_consume_token (parser->lexer);
11336 expr_hi = cp_parser_constant_expression (parser);
11337 if (check_for_bare_parameter_packs (expr_hi))
11338 expr_hi = error_mark_node;
11339
11340 /* We don't need to emit warnings here, as the common code
11341 will do this for us. */
11342 }
11343 else
11344 expr_hi = NULL_TREE;
11345
11346 if (parser->in_switch_statement_p)
11347 {
11348 tree l = finish_case_label (token->location, expr, expr_hi);
11349 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11350 {
11351 label = CASE_LABEL (l);
11352 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11353 }
11354 }
11355 else
11356 error_at (token->location,
11357 "case label %qE not within a switch statement",
11358 expr);
11359 }
11360 break;
11361
11362 case RID_DEFAULT:
11363 /* Consume the `default' token. */
11364 cp_lexer_consume_token (parser->lexer);
11365
11366 if (parser->in_switch_statement_p)
11367 {
11368 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11369 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11370 {
11371 label = CASE_LABEL (l);
11372 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11373 }
11374 }
11375 else
11376 error_at (token->location, "case label not within a switch statement");
11377 break;
11378
11379 default:
11380 /* Anything else must be an ordinary label. */
11381 label = finish_label_stmt (cp_parser_identifier (parser));
11382 if (label && TREE_CODE (label) == LABEL_DECL)
11383 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11384 break;
11385 }
11386
11387 /* Require the `:' token. */
11388 cp_parser_require (parser, CPP_COLON, RT_COLON);
11389
11390 /* An ordinary label may optionally be followed by attributes.
11391 However, this is only permitted if the attributes are then
11392 followed by a semicolon. This is because, for backward
11393 compatibility, when parsing
11394 lab: __attribute__ ((unused)) int i;
11395 we want the attribute to attach to "i", not "lab". */
11396 if (label != NULL_TREE
11397 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11398 {
11399 tree attrs;
11400 cp_parser_parse_tentatively (parser);
11401 attrs = cp_parser_gnu_attributes_opt (parser);
11402 if (attrs == NULL_TREE
11403 /* And fallthrough always binds to the expression-statement. */
11404 || attribute_fallthrough_p (attrs)
11405 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11406 cp_parser_abort_tentative_parse (parser);
11407 else if (!cp_parser_parse_definitely (parser))
11408 ;
11409 else
11410 attributes = attr_chainon (attributes, attrs);
11411 }
11412
11413 if (attributes != NULL_TREE)
11414 cplus_decl_attributes (&label, attributes, 0);
11415
11416 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11417 }
11418
11419 /* Parse an expression-statement.
11420
11421 expression-statement:
11422 expression [opt] ;
11423
11424 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11425 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11426 indicates whether this expression-statement is part of an
11427 expression statement. */
11428
11429 static tree
11430 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11431 {
11432 tree statement = NULL_TREE;
11433 cp_token *token = cp_lexer_peek_token (parser->lexer);
11434 location_t loc = token->location;
11435
11436 /* There might be attribute fallthrough. */
11437 tree attr = cp_parser_gnu_attributes_opt (parser);
11438
11439 /* If the next token is a ';', then there is no expression
11440 statement. */
11441 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11442 {
11443 statement = cp_parser_expression (parser);
11444 if (statement == error_mark_node
11445 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11446 {
11447 cp_parser_skip_to_end_of_block_or_statement (parser);
11448 return error_mark_node;
11449 }
11450 }
11451
11452 /* Handle [[fallthrough]];. */
11453 if (attribute_fallthrough_p (attr))
11454 {
11455 /* The next token after the fallthrough attribute is ';'. */
11456 if (statement == NULL_TREE)
11457 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11458 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11459 void_type_node, 0);
11460 else
11461 warning_at (loc, OPT_Wattributes,
11462 "%<fallthrough%> attribute not followed by %<;%>");
11463 attr = NULL_TREE;
11464 }
11465
11466 /* Allow "[[fallthrough]];", but warn otherwise. */
11467 if (attr != NULL_TREE)
11468 warning_at (loc, OPT_Wattributes,
11469 "attributes at the beginning of statement are ignored");
11470
11471 /* Give a helpful message for "A<T>::type t;" and the like. */
11472 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11473 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11474 {
11475 if (TREE_CODE (statement) == SCOPE_REF)
11476 error_at (token->location, "need %<typename%> before %qE because "
11477 "%qT is a dependent scope",
11478 statement, TREE_OPERAND (statement, 0));
11479 else if (is_overloaded_fn (statement)
11480 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11481 {
11482 /* A::A a; */
11483 tree fn = get_first_fn (statement);
11484 error_at (token->location,
11485 "%<%T::%D%> names the constructor, not the type",
11486 DECL_CONTEXT (fn), DECL_NAME (fn));
11487 }
11488 }
11489
11490 /* Consume the final `;'. */
11491 cp_parser_consume_semicolon_at_end_of_statement (parser);
11492
11493 if (in_statement_expr
11494 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11495 /* This is the final expression statement of a statement
11496 expression. */
11497 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11498 else if (statement)
11499 statement = finish_expr_stmt (statement);
11500
11501 return statement;
11502 }
11503
11504 /* Parse a compound-statement.
11505
11506 compound-statement:
11507 { statement-seq [opt] }
11508
11509 GNU extension:
11510
11511 compound-statement:
11512 { label-declaration-seq [opt] statement-seq [opt] }
11513
11514 label-declaration-seq:
11515 label-declaration
11516 label-declaration-seq label-declaration
11517
11518 Returns a tree representing the statement. */
11519
11520 static tree
11521 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11522 int bcs_flags, bool function_body)
11523 {
11524 tree compound_stmt;
11525 matching_braces braces;
11526
11527 /* Consume the `{'. */
11528 if (!braces.require_open (parser))
11529 return error_mark_node;
11530 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11531 && !function_body && cxx_dialect < cxx14)
11532 pedwarn (input_location, OPT_Wpedantic,
11533 "compound-statement in %<constexpr%> function");
11534 /* Begin the compound-statement. */
11535 compound_stmt = begin_compound_stmt (bcs_flags);
11536 /* If the next keyword is `__label__' we have a label declaration. */
11537 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11538 cp_parser_label_declaration (parser);
11539 /* Parse an (optional) statement-seq. */
11540 cp_parser_statement_seq_opt (parser, in_statement_expr);
11541 /* Finish the compound-statement. */
11542 finish_compound_stmt (compound_stmt);
11543 /* Consume the `}'. */
11544 braces.require_close (parser);
11545
11546 return compound_stmt;
11547 }
11548
11549 /* Parse an (optional) statement-seq.
11550
11551 statement-seq:
11552 statement
11553 statement-seq [opt] statement */
11554
11555 static void
11556 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11557 {
11558 /* Scan statements until there aren't any more. */
11559 while (true)
11560 {
11561 cp_token *token = cp_lexer_peek_token (parser->lexer);
11562
11563 /* If we are looking at a `}', then we have run out of
11564 statements; the same is true if we have reached the end
11565 of file, or have stumbled upon a stray '@end'. */
11566 if (token->type == CPP_CLOSE_BRACE
11567 || token->type == CPP_EOF
11568 || token->type == CPP_PRAGMA_EOL
11569 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11570 break;
11571
11572 /* If we are in a compound statement and find 'else' then
11573 something went wrong. */
11574 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11575 {
11576 if (parser->in_statement & IN_IF_STMT)
11577 break;
11578 else
11579 {
11580 token = cp_lexer_consume_token (parser->lexer);
11581 error_at (token->location, "%<else%> without a previous %<if%>");
11582 }
11583 }
11584
11585 /* Parse the statement. */
11586 cp_parser_statement (parser, in_statement_expr, true, NULL);
11587 }
11588 }
11589
11590 /* Return true if this is the C++20 version of range-based-for with
11591 init-statement. */
11592
11593 static bool
11594 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11595 {
11596 bool r = false;
11597
11598 /* Save tokens so that we can put them back. */
11599 cp_lexer_save_tokens (parser->lexer);
11600
11601 /* There has to be an unnested ; followed by an unnested :. */
11602 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11603 /*recovering=*/false,
11604 CPP_SEMICOLON,
11605 /*consume_paren=*/false) != -1)
11606 goto out;
11607
11608 /* We found the semicolon, eat it now. */
11609 cp_lexer_consume_token (parser->lexer);
11610
11611 /* Now look for ':' that is not nested in () or {}. */
11612 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11613 /*recovering=*/false,
11614 CPP_COLON,
11615 /*consume_paren=*/false) == -1);
11616
11617 out:
11618 /* Roll back the tokens we skipped. */
11619 cp_lexer_rollback_tokens (parser->lexer);
11620
11621 return r;
11622 }
11623
11624 /* Return true if we're looking at (init; cond), false otherwise. */
11625
11626 static bool
11627 cp_parser_init_statement_p (cp_parser *parser)
11628 {
11629 /* Save tokens so that we can put them back. */
11630 cp_lexer_save_tokens (parser->lexer);
11631
11632 /* Look for ';' that is not nested in () or {}. */
11633 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11634 /*recovering=*/false,
11635 CPP_SEMICOLON,
11636 /*consume_paren=*/false);
11637
11638 /* Roll back the tokens we skipped. */
11639 cp_lexer_rollback_tokens (parser->lexer);
11640
11641 return ret == -1;
11642 }
11643
11644 /* Parse a selection-statement.
11645
11646 selection-statement:
11647 if ( init-statement [opt] condition ) statement
11648 if ( init-statement [opt] condition ) statement else statement
11649 switch ( init-statement [opt] condition ) statement
11650
11651 Returns the new IF_STMT or SWITCH_STMT.
11652
11653 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11654 is a (possibly labeled) if statement which is not enclosed in
11655 braces and has an else clause. This is used to implement
11656 -Wparentheses.
11657
11658 CHAIN is a vector of if-else-if conditions. This is used to implement
11659 -Wduplicated-cond. */
11660
11661 static tree
11662 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11663 vec<tree> *chain)
11664 {
11665 cp_token *token;
11666 enum rid keyword;
11667 token_indent_info guard_tinfo;
11668
11669 if (if_p != NULL)
11670 *if_p = false;
11671
11672 /* Peek at the next token. */
11673 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11674 guard_tinfo = get_token_indent_info (token);
11675
11676 /* See what kind of keyword it is. */
11677 keyword = token->keyword;
11678 switch (keyword)
11679 {
11680 case RID_IF:
11681 case RID_SWITCH:
11682 {
11683 tree statement;
11684 tree condition;
11685
11686 bool cx = false;
11687 if (keyword == RID_IF
11688 && cp_lexer_next_token_is_keyword (parser->lexer,
11689 RID_CONSTEXPR))
11690 {
11691 cx = true;
11692 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11693 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11694 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11695 "with -std=c++17 or -std=gnu++17");
11696 }
11697
11698 /* Look for the `('. */
11699 matching_parens parens;
11700 if (!parens.require_open (parser))
11701 {
11702 cp_parser_skip_to_end_of_statement (parser);
11703 return error_mark_node;
11704 }
11705
11706 /* Begin the selection-statement. */
11707 if (keyword == RID_IF)
11708 {
11709 statement = begin_if_stmt ();
11710 IF_STMT_CONSTEXPR_P (statement) = cx;
11711 }
11712 else
11713 statement = begin_switch_stmt ();
11714
11715 /* Parse the optional init-statement. */
11716 if (cp_parser_init_statement_p (parser))
11717 {
11718 tree decl;
11719 if (cxx_dialect < cxx17)
11720 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11721 "init-statement in selection statements only available "
11722 "with -std=c++17 or -std=gnu++17");
11723 cp_parser_init_statement (parser, &decl);
11724 }
11725
11726 /* Parse the condition. */
11727 condition = cp_parser_condition (parser);
11728 /* Look for the `)'. */
11729 if (!parens.require_close (parser))
11730 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11731 /*consume_paren=*/true);
11732
11733 if (keyword == RID_IF)
11734 {
11735 bool nested_if;
11736 unsigned char in_statement;
11737
11738 /* Add the condition. */
11739 condition = finish_if_stmt_cond (condition, statement);
11740
11741 if (warn_duplicated_cond)
11742 warn_duplicated_cond_add_or_warn (token->location, condition,
11743 &chain);
11744
11745 /* Parse the then-clause. */
11746 in_statement = parser->in_statement;
11747 parser->in_statement |= IN_IF_STMT;
11748
11749 /* Outside a template, the non-selected branch of a constexpr
11750 if is a 'discarded statement', i.e. unevaluated. */
11751 bool was_discarded = in_discarded_stmt;
11752 bool discard_then = (cx && !processing_template_decl
11753 && integer_zerop (condition));
11754 if (discard_then)
11755 {
11756 in_discarded_stmt = true;
11757 ++c_inhibit_evaluation_warnings;
11758 }
11759
11760 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11761 guard_tinfo);
11762
11763 parser->in_statement = in_statement;
11764
11765 finish_then_clause (statement);
11766
11767 if (discard_then)
11768 {
11769 THEN_CLAUSE (statement) = NULL_TREE;
11770 in_discarded_stmt = was_discarded;
11771 --c_inhibit_evaluation_warnings;
11772 }
11773
11774 /* If the next token is `else', parse the else-clause. */
11775 if (cp_lexer_next_token_is_keyword (parser->lexer,
11776 RID_ELSE))
11777 {
11778 bool discard_else = (cx && !processing_template_decl
11779 && integer_nonzerop (condition));
11780 if (discard_else)
11781 {
11782 in_discarded_stmt = true;
11783 ++c_inhibit_evaluation_warnings;
11784 }
11785
11786 guard_tinfo
11787 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11788 /* Consume the `else' keyword. */
11789 cp_lexer_consume_token (parser->lexer);
11790 if (warn_duplicated_cond)
11791 {
11792 if (cp_lexer_next_token_is_keyword (parser->lexer,
11793 RID_IF)
11794 && chain == NULL)
11795 {
11796 /* We've got "if (COND) else if (COND2)". Start
11797 the condition chain and add COND as the first
11798 element. */
11799 chain = new vec<tree> ();
11800 if (!CONSTANT_CLASS_P (condition)
11801 && !TREE_SIDE_EFFECTS (condition))
11802 {
11803 /* Wrap it in a NOP_EXPR so that we can set the
11804 location of the condition. */
11805 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11806 condition);
11807 SET_EXPR_LOCATION (e, token->location);
11808 chain->safe_push (e);
11809 }
11810 }
11811 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11812 RID_IF))
11813 {
11814 /* This is if-else without subsequent if. Zap the
11815 condition chain; we would have already warned at
11816 this point. */
11817 delete chain;
11818 chain = NULL;
11819 }
11820 }
11821 begin_else_clause (statement);
11822 /* Parse the else-clause. */
11823 cp_parser_implicitly_scoped_statement (parser, NULL,
11824 guard_tinfo, chain);
11825
11826 finish_else_clause (statement);
11827
11828 /* If we are currently parsing a then-clause, then
11829 IF_P will not be NULL. We set it to true to
11830 indicate that this if statement has an else clause.
11831 This may trigger the Wparentheses warning below
11832 when we get back up to the parent if statement. */
11833 if (if_p != NULL)
11834 *if_p = true;
11835
11836 if (discard_else)
11837 {
11838 ELSE_CLAUSE (statement) = NULL_TREE;
11839 in_discarded_stmt = was_discarded;
11840 --c_inhibit_evaluation_warnings;
11841 }
11842 }
11843 else
11844 {
11845 /* This if statement does not have an else clause. If
11846 NESTED_IF is true, then the then-clause has an if
11847 statement which does have an else clause. We warn
11848 about the potential ambiguity. */
11849 if (nested_if)
11850 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11851 "suggest explicit braces to avoid ambiguous"
11852 " %<else%>");
11853 if (warn_duplicated_cond)
11854 {
11855 /* We don't need the condition chain anymore. */
11856 delete chain;
11857 chain = NULL;
11858 }
11859 }
11860
11861 /* Now we're all done with the if-statement. */
11862 finish_if_stmt (statement);
11863 }
11864 else
11865 {
11866 bool in_switch_statement_p;
11867 unsigned char in_statement;
11868
11869 /* Add the condition. */
11870 finish_switch_cond (condition, statement);
11871
11872 /* Parse the body of the switch-statement. */
11873 in_switch_statement_p = parser->in_switch_statement_p;
11874 in_statement = parser->in_statement;
11875 parser->in_switch_statement_p = true;
11876 parser->in_statement |= IN_SWITCH_STMT;
11877 cp_parser_implicitly_scoped_statement (parser, if_p,
11878 guard_tinfo);
11879 parser->in_switch_statement_p = in_switch_statement_p;
11880 parser->in_statement = in_statement;
11881
11882 /* Now we're all done with the switch-statement. */
11883 finish_switch_stmt (statement);
11884 }
11885
11886 return statement;
11887 }
11888 break;
11889
11890 default:
11891 cp_parser_error (parser, "expected selection-statement");
11892 return error_mark_node;
11893 }
11894 }
11895
11896 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11897 If we have seen at least one decl-specifier, and the next token
11898 is not a parenthesis, then we must be looking at a declaration.
11899 (After "int (" we might be looking at a functional cast.) */
11900
11901 static void
11902 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11903 bool any_specifiers_p)
11904 {
11905 if (any_specifiers_p
11906 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11907 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11908 && !cp_parser_error_occurred (parser))
11909 cp_parser_commit_to_tentative_parse (parser);
11910 }
11911
11912 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11913 The declarator shall not specify a function or an array. Returns
11914 TRUE if the declarator is valid, FALSE otherwise. */
11915
11916 static bool
11917 cp_parser_check_condition_declarator (cp_parser* parser,
11918 cp_declarator *declarator,
11919 location_t loc)
11920 {
11921 if (declarator == cp_error_declarator
11922 || function_declarator_p (declarator)
11923 || declarator->kind == cdk_array)
11924 {
11925 if (declarator == cp_error_declarator)
11926 /* Already complained. */;
11927 else if (declarator->kind == cdk_array)
11928 error_at (loc, "condition declares an array");
11929 else
11930 error_at (loc, "condition declares a function");
11931 if (parser->fully_implicit_function_template_p)
11932 abort_fully_implicit_template (parser);
11933 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11934 /*or_comma=*/false,
11935 /*consume_paren=*/false);
11936 return false;
11937 }
11938 else
11939 return true;
11940 }
11941
11942 /* Parse a condition.
11943
11944 condition:
11945 expression
11946 type-specifier-seq declarator = initializer-clause
11947 type-specifier-seq declarator braced-init-list
11948
11949 GNU Extension:
11950
11951 condition:
11952 type-specifier-seq declarator asm-specification [opt]
11953 attributes [opt] = assignment-expression
11954
11955 Returns the expression that should be tested. */
11956
11957 static tree
11958 cp_parser_condition (cp_parser* parser)
11959 {
11960 cp_decl_specifier_seq type_specifiers;
11961 const char *saved_message;
11962 int declares_class_or_enum;
11963
11964 /* Try the declaration first. */
11965 cp_parser_parse_tentatively (parser);
11966 /* New types are not allowed in the type-specifier-seq for a
11967 condition. */
11968 saved_message = parser->type_definition_forbidden_message;
11969 parser->type_definition_forbidden_message
11970 = G_("types may not be defined in conditions");
11971 /* Parse the type-specifier-seq. */
11972 cp_parser_decl_specifier_seq (parser,
11973 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11974 &type_specifiers,
11975 &declares_class_or_enum);
11976 /* Restore the saved message. */
11977 parser->type_definition_forbidden_message = saved_message;
11978
11979 cp_parser_maybe_commit_to_declaration (parser,
11980 type_specifiers.any_specifiers_p);
11981
11982 /* If all is well, we might be looking at a declaration. */
11983 if (!cp_parser_error_occurred (parser))
11984 {
11985 tree decl;
11986 tree asm_specification;
11987 tree attributes;
11988 cp_declarator *declarator;
11989 tree initializer = NULL_TREE;
11990 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11991
11992 /* Parse the declarator. */
11993 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11994 CP_PARSER_FLAGS_NONE,
11995 /*ctor_dtor_or_conv_p=*/NULL,
11996 /*parenthesized_p=*/NULL,
11997 /*member_p=*/false,
11998 /*friend_p=*/false,
11999 /*static_p=*/false);
12000 /* Parse the attributes. */
12001 attributes = cp_parser_attributes_opt (parser);
12002 /* Parse the asm-specification. */
12003 asm_specification = cp_parser_asm_specification_opt (parser);
12004 /* If the next token is not an `=' or '{', then we might still be
12005 looking at an expression. For example:
12006
12007 if (A(a).x)
12008
12009 looks like a decl-specifier-seq and a declarator -- but then
12010 there is no `=', so this is an expression. */
12011 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12012 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12013 cp_parser_simulate_error (parser);
12014
12015 /* If we did see an `=' or '{', then we are looking at a declaration
12016 for sure. */
12017 if (cp_parser_parse_definitely (parser))
12018 {
12019 tree pushed_scope;
12020 bool non_constant_p = false;
12021 int flags = LOOKUP_ONLYCONVERTING;
12022
12023 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
12024 return error_mark_node;
12025
12026 /* Create the declaration. */
12027 decl = start_decl (declarator, &type_specifiers,
12028 /*initialized_p=*/true,
12029 attributes, /*prefix_attributes=*/NULL_TREE,
12030 &pushed_scope);
12031
12032 /* Parse the initializer. */
12033 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12034 {
12035 initializer = cp_parser_braced_list (parser, &non_constant_p);
12036 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
12037 flags = 0;
12038 }
12039 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12040 {
12041 /* Consume the `='. */
12042 cp_lexer_consume_token (parser->lexer);
12043 initializer = cp_parser_initializer_clause (parser,
12044 &non_constant_p);
12045 }
12046 else
12047 {
12048 cp_parser_error (parser, "expected initializer");
12049 initializer = error_mark_node;
12050 }
12051 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
12052 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12053
12054 /* Process the initializer. */
12055 cp_finish_decl (decl,
12056 initializer, !non_constant_p,
12057 asm_specification,
12058 flags);
12059
12060 if (pushed_scope)
12061 pop_scope (pushed_scope);
12062
12063 return convert_from_reference (decl);
12064 }
12065 }
12066 /* If we didn't even get past the declarator successfully, we are
12067 definitely not looking at a declaration. */
12068 else
12069 cp_parser_abort_tentative_parse (parser);
12070
12071 /* Otherwise, we are looking at an expression. */
12072 return cp_parser_expression (parser);
12073 }
12074
12075 /* Parses a for-statement or range-for-statement until the closing ')',
12076 not included. */
12077
12078 static tree
12079 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12080 {
12081 tree init, scope, decl;
12082 bool is_range_for;
12083
12084 /* Begin the for-statement. */
12085 scope = begin_for_scope (&init);
12086
12087 /* Parse the initialization. */
12088 is_range_for = cp_parser_init_statement (parser, &decl);
12089
12090 if (is_range_for)
12091 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12092 false);
12093 else
12094 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12095 }
12096
12097 static tree
12098 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12099 unsigned short unroll)
12100 {
12101 /* Normal for loop */
12102 tree condition = NULL_TREE;
12103 tree expression = NULL_TREE;
12104 tree stmt;
12105
12106 stmt = begin_for_stmt (scope, init);
12107 /* The init-statement has already been parsed in
12108 cp_parser_init_statement, so no work is needed here. */
12109 finish_init_stmt (stmt);
12110
12111 /* If there's a condition, process it. */
12112 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12113 condition = cp_parser_condition (parser);
12114 else if (ivdep)
12115 {
12116 cp_parser_error (parser, "missing loop condition in loop with "
12117 "%<GCC ivdep%> pragma");
12118 condition = error_mark_node;
12119 }
12120 else if (unroll)
12121 {
12122 cp_parser_error (parser, "missing loop condition in loop with "
12123 "%<GCC unroll%> pragma");
12124 condition = error_mark_node;
12125 }
12126 finish_for_cond (condition, stmt, ivdep, unroll);
12127 /* Look for the `;'. */
12128 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12129
12130 /* If there's an expression, process it. */
12131 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12132 expression = cp_parser_expression (parser);
12133 finish_for_expr (expression, stmt);
12134
12135 return stmt;
12136 }
12137
12138 /* Tries to parse a range-based for-statement:
12139
12140 range-based-for:
12141 decl-specifier-seq declarator : expression
12142
12143 The decl-specifier-seq declarator and the `:' are already parsed by
12144 cp_parser_init_statement. If processing_template_decl it returns a
12145 newly created RANGE_FOR_STMT; if not, it is converted to a
12146 regular FOR_STMT. */
12147
12148 static tree
12149 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12150 bool ivdep, unsigned short unroll, bool is_omp)
12151 {
12152 tree stmt, range_expr;
12153 auto_vec <cxx_binding *, 16> bindings;
12154 auto_vec <tree, 16> names;
12155 tree decomp_first_name = NULL_TREE;
12156 unsigned int decomp_cnt = 0;
12157
12158 /* Get the range declaration momentarily out of the way so that
12159 the range expression doesn't clash with it. */
12160 if (range_decl != error_mark_node)
12161 {
12162 if (DECL_HAS_VALUE_EXPR_P (range_decl))
12163 {
12164 tree v = DECL_VALUE_EXPR (range_decl);
12165 /* For decomposition declaration get all of the corresponding
12166 declarations out of the way. */
12167 if (TREE_CODE (v) == ARRAY_REF
12168 && VAR_P (TREE_OPERAND (v, 0))
12169 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12170 {
12171 tree d = range_decl;
12172 range_decl = TREE_OPERAND (v, 0);
12173 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12174 decomp_first_name = d;
12175 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12176 {
12177 tree name = DECL_NAME (d);
12178 names.safe_push (name);
12179 bindings.safe_push (IDENTIFIER_BINDING (name));
12180 IDENTIFIER_BINDING (name)
12181 = IDENTIFIER_BINDING (name)->previous;
12182 }
12183 }
12184 }
12185 if (names.is_empty ())
12186 {
12187 tree name = DECL_NAME (range_decl);
12188 names.safe_push (name);
12189 bindings.safe_push (IDENTIFIER_BINDING (name));
12190 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12191 }
12192 }
12193
12194 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12195 {
12196 bool expr_non_constant_p;
12197 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12198 }
12199 else
12200 range_expr = cp_parser_expression (parser);
12201
12202 /* Put the range declaration(s) back into scope. */
12203 for (unsigned int i = 0; i < names.length (); i++)
12204 {
12205 cxx_binding *binding = bindings[i];
12206 binding->previous = IDENTIFIER_BINDING (names[i]);
12207 IDENTIFIER_BINDING (names[i]) = binding;
12208 }
12209
12210 /* finish_omp_for has its own code for the following, so just
12211 return the range_expr instead. */
12212 if (is_omp)
12213 return range_expr;
12214
12215 /* If in template, STMT is converted to a normal for-statement
12216 at instantiation. If not, it is done just ahead. */
12217 if (processing_template_decl)
12218 {
12219 if (check_for_bare_parameter_packs (range_expr))
12220 range_expr = error_mark_node;
12221 stmt = begin_range_for_stmt (scope, init);
12222 if (ivdep)
12223 RANGE_FOR_IVDEP (stmt) = 1;
12224 if (unroll)
12225 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12226 finish_range_for_decl (stmt, range_decl, range_expr);
12227 if (!type_dependent_expression_p (range_expr)
12228 /* do_auto_deduction doesn't mess with template init-lists. */
12229 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12230 do_range_for_auto_deduction (range_decl, range_expr);
12231 }
12232 else
12233 {
12234 stmt = begin_for_stmt (scope, init);
12235 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12236 decomp_first_name, decomp_cnt, ivdep,
12237 unroll);
12238 }
12239 return stmt;
12240 }
12241
12242 /* Subroutine of cp_convert_range_for: given the initializer expression,
12243 builds up the range temporary. */
12244
12245 static tree
12246 build_range_temp (tree range_expr)
12247 {
12248 tree range_type, range_temp;
12249
12250 /* Find out the type deduced by the declaration
12251 `auto &&__range = range_expr'. */
12252 range_type = cp_build_reference_type (make_auto (), true);
12253 range_type = do_auto_deduction (range_type, range_expr,
12254 type_uses_auto (range_type));
12255
12256 /* Create the __range variable. */
12257 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12258 range_type);
12259 TREE_USED (range_temp) = 1;
12260 DECL_ARTIFICIAL (range_temp) = 1;
12261
12262 return range_temp;
12263 }
12264
12265 /* Used by cp_parser_range_for in template context: we aren't going to
12266 do a full conversion yet, but we still need to resolve auto in the
12267 type of the for-range-declaration if present. This is basically
12268 a shortcut version of cp_convert_range_for. */
12269
12270 static void
12271 do_range_for_auto_deduction (tree decl, tree range_expr)
12272 {
12273 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12274 if (auto_node)
12275 {
12276 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12277 range_temp = convert_from_reference (build_range_temp (range_expr));
12278 iter_type = (cp_parser_perform_range_for_lookup
12279 (range_temp, &begin_dummy, &end_dummy));
12280 if (iter_type)
12281 {
12282 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12283 iter_type);
12284 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12285 RO_UNARY_STAR,
12286 tf_warning_or_error);
12287 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12288 iter_decl, auto_node);
12289 }
12290 }
12291 }
12292
12293 /* Converts a range-based for-statement into a normal
12294 for-statement, as per the definition.
12295
12296 for (RANGE_DECL : RANGE_EXPR)
12297 BLOCK
12298
12299 should be equivalent to:
12300
12301 {
12302 auto &&__range = RANGE_EXPR;
12303 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12304 __begin != __end;
12305 ++__begin)
12306 {
12307 RANGE_DECL = *__begin;
12308 BLOCK
12309 }
12310 }
12311
12312 If RANGE_EXPR is an array:
12313 BEGIN_EXPR = __range
12314 END_EXPR = __range + ARRAY_SIZE(__range)
12315 Else if RANGE_EXPR has a member 'begin' or 'end':
12316 BEGIN_EXPR = __range.begin()
12317 END_EXPR = __range.end()
12318 Else:
12319 BEGIN_EXPR = begin(__range)
12320 END_EXPR = end(__range);
12321
12322 If __range has a member 'begin' but not 'end', or vice versa, we must
12323 still use the second alternative (it will surely fail, however).
12324 When calling begin()/end() in the third alternative we must use
12325 argument dependent lookup, but always considering 'std' as an associated
12326 namespace. */
12327
12328 tree
12329 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12330 tree decomp_first_name, unsigned int decomp_cnt,
12331 bool ivdep, unsigned short unroll)
12332 {
12333 tree begin, end;
12334 tree iter_type, begin_expr, end_expr;
12335 tree condition, expression;
12336
12337 range_expr = mark_lvalue_use (range_expr);
12338
12339 if (range_decl == error_mark_node || range_expr == error_mark_node)
12340 /* If an error happened previously do nothing or else a lot of
12341 unhelpful errors would be issued. */
12342 begin_expr = end_expr = iter_type = error_mark_node;
12343 else
12344 {
12345 tree range_temp;
12346
12347 if (VAR_P (range_expr)
12348 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12349 /* Can't bind a reference to an array of runtime bound. */
12350 range_temp = range_expr;
12351 else
12352 {
12353 range_temp = build_range_temp (range_expr);
12354 pushdecl (range_temp);
12355 cp_finish_decl (range_temp, range_expr,
12356 /*is_constant_init*/false, NULL_TREE,
12357 LOOKUP_ONLYCONVERTING);
12358 range_temp = convert_from_reference (range_temp);
12359 }
12360 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12361 &begin_expr, &end_expr);
12362 }
12363
12364 /* The new for initialization statement. */
12365 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12366 iter_type);
12367 TREE_USED (begin) = 1;
12368 DECL_ARTIFICIAL (begin) = 1;
12369 pushdecl (begin);
12370 cp_finish_decl (begin, begin_expr,
12371 /*is_constant_init*/false, NULL_TREE,
12372 LOOKUP_ONLYCONVERTING);
12373
12374 if (cxx_dialect >= cxx17)
12375 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12376 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12377 TREE_USED (end) = 1;
12378 DECL_ARTIFICIAL (end) = 1;
12379 pushdecl (end);
12380 cp_finish_decl (end, end_expr,
12381 /*is_constant_init*/false, NULL_TREE,
12382 LOOKUP_ONLYCONVERTING);
12383
12384 finish_init_stmt (statement);
12385
12386 /* The new for condition. */
12387 condition = build_x_binary_op (input_location, NE_EXPR,
12388 begin, ERROR_MARK,
12389 end, ERROR_MARK,
12390 NULL, tf_warning_or_error);
12391 finish_for_cond (condition, statement, ivdep, unroll);
12392
12393 /* The new increment expression. */
12394 expression = finish_unary_op_expr (input_location,
12395 PREINCREMENT_EXPR, begin,
12396 tf_warning_or_error);
12397 finish_for_expr (expression, statement);
12398
12399 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12400 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12401
12402 /* The declaration is initialized with *__begin inside the loop body. */
12403 cp_finish_decl (range_decl,
12404 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12405 tf_warning_or_error),
12406 /*is_constant_init*/false, NULL_TREE,
12407 LOOKUP_ONLYCONVERTING);
12408 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12409 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12410
12411 return statement;
12412 }
12413
12414 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12415 We need to solve both at the same time because the method used
12416 depends on the existence of members begin or end.
12417 Returns the type deduced for the iterator expression. */
12418
12419 static tree
12420 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12421 {
12422 if (error_operand_p (range))
12423 {
12424 *begin = *end = error_mark_node;
12425 return error_mark_node;
12426 }
12427
12428 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12429 {
12430 error ("range-based %<for%> expression of type %qT "
12431 "has incomplete type", TREE_TYPE (range));
12432 *begin = *end = error_mark_node;
12433 return error_mark_node;
12434 }
12435 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12436 {
12437 /* If RANGE is an array, we will use pointer arithmetic. */
12438 *begin = decay_conversion (range, tf_warning_or_error);
12439 *end = build_binary_op (input_location, PLUS_EXPR,
12440 range,
12441 array_type_nelts_top (TREE_TYPE (range)),
12442 false);
12443 return TREE_TYPE (*begin);
12444 }
12445 else
12446 {
12447 /* If it is not an array, we must do a bit of magic. */
12448 tree id_begin, id_end;
12449 tree member_begin, member_end;
12450
12451 *begin = *end = error_mark_node;
12452
12453 id_begin = get_identifier ("begin");
12454 id_end = get_identifier ("end");
12455 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12456 /*protect=*/2, /*want_type=*/false,
12457 tf_warning_or_error);
12458 member_end = lookup_member (TREE_TYPE (range), id_end,
12459 /*protect=*/2, /*want_type=*/false,
12460 tf_warning_or_error);
12461
12462 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12463 {
12464 /* Use the member functions. */
12465 *begin = cp_parser_range_for_member_function (range, id_begin);
12466 *end = cp_parser_range_for_member_function (range, id_end);
12467 }
12468 else
12469 {
12470 /* Use global functions with ADL. */
12471 vec<tree, va_gc> *vec;
12472 vec = make_tree_vector ();
12473
12474 vec_safe_push (vec, range);
12475
12476 member_begin = perform_koenig_lookup (id_begin, vec,
12477 tf_warning_or_error);
12478 *begin = finish_call_expr (member_begin, &vec, false, true,
12479 tf_warning_or_error);
12480 member_end = perform_koenig_lookup (id_end, vec,
12481 tf_warning_or_error);
12482 *end = finish_call_expr (member_end, &vec, false, true,
12483 tf_warning_or_error);
12484
12485 release_tree_vector (vec);
12486 }
12487
12488 /* Last common checks. */
12489 if (*begin == error_mark_node || *end == error_mark_node)
12490 {
12491 /* If one of the expressions is an error do no more checks. */
12492 *begin = *end = error_mark_node;
12493 return error_mark_node;
12494 }
12495 else if (type_dependent_expression_p (*begin)
12496 || type_dependent_expression_p (*end))
12497 /* Can happen, when, eg, in a template context, Koenig lookup
12498 can't resolve begin/end (c++/58503). */
12499 return NULL_TREE;
12500 else
12501 {
12502 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12503 /* The unqualified type of the __begin and __end temporaries should
12504 be the same, as required by the multiple auto declaration. */
12505 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12506 {
12507 if (cxx_dialect >= cxx17
12508 && (build_x_binary_op (input_location, NE_EXPR,
12509 *begin, ERROR_MARK,
12510 *end, ERROR_MARK,
12511 NULL, tf_none)
12512 != error_mark_node))
12513 /* P0184R0 allows __begin and __end to have different types,
12514 but make sure they are comparable so we can give a better
12515 diagnostic. */;
12516 else
12517 error ("inconsistent begin/end types in range-based %<for%> "
12518 "statement: %qT and %qT",
12519 TREE_TYPE (*begin), TREE_TYPE (*end));
12520 }
12521 return iter_type;
12522 }
12523 }
12524 }
12525
12526 /* Helper function for cp_parser_perform_range_for_lookup.
12527 Builds a tree for RANGE.IDENTIFIER(). */
12528
12529 static tree
12530 cp_parser_range_for_member_function (tree range, tree identifier)
12531 {
12532 tree member, res;
12533 vec<tree, va_gc> *vec;
12534
12535 member = finish_class_member_access_expr (range, identifier,
12536 false, tf_warning_or_error);
12537 if (member == error_mark_node)
12538 return error_mark_node;
12539
12540 vec = make_tree_vector ();
12541 res = finish_call_expr (member, &vec,
12542 /*disallow_virtual=*/false,
12543 /*koenig_p=*/false,
12544 tf_warning_or_error);
12545 release_tree_vector (vec);
12546 return res;
12547 }
12548
12549 /* Parse an iteration-statement.
12550
12551 iteration-statement:
12552 while ( condition ) statement
12553 do statement while ( expression ) ;
12554 for ( init-statement condition [opt] ; expression [opt] )
12555 statement
12556
12557 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12558
12559 static tree
12560 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12561 unsigned short unroll)
12562 {
12563 cp_token *token;
12564 enum rid keyword;
12565 tree statement;
12566 unsigned char in_statement;
12567 token_indent_info guard_tinfo;
12568
12569 /* Peek at the next token. */
12570 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12571 if (!token)
12572 return error_mark_node;
12573
12574 guard_tinfo = get_token_indent_info (token);
12575
12576 /* Remember whether or not we are already within an iteration
12577 statement. */
12578 in_statement = parser->in_statement;
12579
12580 /* See what kind of keyword it is. */
12581 keyword = token->keyword;
12582 switch (keyword)
12583 {
12584 case RID_WHILE:
12585 {
12586 tree condition;
12587
12588 /* Begin the while-statement. */
12589 statement = begin_while_stmt ();
12590 /* Look for the `('. */
12591 matching_parens parens;
12592 parens.require_open (parser);
12593 /* Parse the condition. */
12594 condition = cp_parser_condition (parser);
12595 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12596 /* Look for the `)'. */
12597 parens.require_close (parser);
12598 /* Parse the dependent statement. */
12599 parser->in_statement = IN_ITERATION_STMT;
12600 bool prev = note_iteration_stmt_body_start ();
12601 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12602 note_iteration_stmt_body_end (prev);
12603 parser->in_statement = in_statement;
12604 /* We're done with the while-statement. */
12605 finish_while_stmt (statement);
12606 }
12607 break;
12608
12609 case RID_DO:
12610 {
12611 tree expression;
12612
12613 /* Begin the do-statement. */
12614 statement = begin_do_stmt ();
12615 /* Parse the body of the do-statement. */
12616 parser->in_statement = IN_ITERATION_STMT;
12617 bool prev = note_iteration_stmt_body_start ();
12618 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12619 note_iteration_stmt_body_end (prev);
12620 parser->in_statement = in_statement;
12621 finish_do_body (statement);
12622 /* Look for the `while' keyword. */
12623 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12624 /* Look for the `('. */
12625 matching_parens parens;
12626 parens.require_open (parser);
12627 /* Parse the expression. */
12628 expression = cp_parser_expression (parser);
12629 /* We're done with the do-statement. */
12630 finish_do_stmt (expression, statement, ivdep, unroll);
12631 /* Look for the `)'. */
12632 parens.require_close (parser);
12633 /* Look for the `;'. */
12634 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12635 }
12636 break;
12637
12638 case RID_FOR:
12639 {
12640 /* Look for the `('. */
12641 matching_parens parens;
12642 parens.require_open (parser);
12643
12644 statement = cp_parser_for (parser, ivdep, unroll);
12645
12646 /* Look for the `)'. */
12647 parens.require_close (parser);
12648
12649 /* Parse the body of the for-statement. */
12650 parser->in_statement = IN_ITERATION_STMT;
12651 bool prev = note_iteration_stmt_body_start ();
12652 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12653 note_iteration_stmt_body_end (prev);
12654 parser->in_statement = in_statement;
12655
12656 /* We're done with the for-statement. */
12657 finish_for_stmt (statement);
12658 }
12659 break;
12660
12661 default:
12662 cp_parser_error (parser, "expected iteration-statement");
12663 statement = error_mark_node;
12664 break;
12665 }
12666
12667 return statement;
12668 }
12669
12670 /* Parse a init-statement or the declarator of a range-based-for.
12671 Returns true if a range-based-for declaration is seen.
12672
12673 init-statement:
12674 expression-statement
12675 simple-declaration */
12676
12677 static bool
12678 cp_parser_init_statement (cp_parser *parser, tree *decl)
12679 {
12680 /* If the next token is a `;', then we have an empty
12681 expression-statement. Grammatically, this is also a
12682 simple-declaration, but an invalid one, because it does not
12683 declare anything. Therefore, if we did not handle this case
12684 specially, we would issue an error message about an invalid
12685 declaration. */
12686 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12687 {
12688 bool is_range_for = false;
12689 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12690
12691 /* Try to parse the init-statement. */
12692 if (cp_parser_range_based_for_with_init_p (parser))
12693 {
12694 tree dummy;
12695 cp_parser_parse_tentatively (parser);
12696 /* Parse the declaration. */
12697 cp_parser_simple_declaration (parser,
12698 /*function_definition_allowed_p=*/false,
12699 &dummy);
12700 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12701 if (!cp_parser_parse_definitely (parser))
12702 /* That didn't work, try to parse it as an expression-statement. */
12703 cp_parser_expression_statement (parser, NULL_TREE);
12704
12705 if (cxx_dialect < cxx2a)
12706 {
12707 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12708 "range-based %<for%> loops with initializer only "
12709 "available with -std=c++2a or -std=gnu++2a");
12710 *decl = error_mark_node;
12711 }
12712 }
12713
12714 /* A colon is used in range-based for. */
12715 parser->colon_corrects_to_scope_p = false;
12716
12717 /* We're going to speculatively look for a declaration, falling back
12718 to an expression, if necessary. */
12719 cp_parser_parse_tentatively (parser);
12720 /* Parse the declaration. */
12721 cp_parser_simple_declaration (parser,
12722 /*function_definition_allowed_p=*/false,
12723 decl);
12724 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12725 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12726 {
12727 /* It is a range-for, consume the ':'. */
12728 cp_lexer_consume_token (parser->lexer);
12729 is_range_for = true;
12730 if (cxx_dialect < cxx11)
12731 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12732 "range-based %<for%> loops only available with "
12733 "-std=c++11 or -std=gnu++11");
12734 }
12735 else
12736 /* The ';' is not consumed yet because we told
12737 cp_parser_simple_declaration not to. */
12738 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12739
12740 if (cp_parser_parse_definitely (parser))
12741 return is_range_for;
12742 /* If the tentative parse failed, then we shall need to look for an
12743 expression-statement. */
12744 }
12745 /* If we are here, it is an expression-statement. */
12746 cp_parser_expression_statement (parser, NULL_TREE);
12747 return false;
12748 }
12749
12750 /* Parse a jump-statement.
12751
12752 jump-statement:
12753 break ;
12754 continue ;
12755 return expression [opt] ;
12756 return braced-init-list ;
12757 goto identifier ;
12758
12759 GNU extension:
12760
12761 jump-statement:
12762 goto * expression ;
12763
12764 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12765
12766 static tree
12767 cp_parser_jump_statement (cp_parser* parser)
12768 {
12769 tree statement = error_mark_node;
12770 cp_token *token;
12771 enum rid keyword;
12772 unsigned char in_statement;
12773
12774 /* Peek at the next token. */
12775 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12776 if (!token)
12777 return error_mark_node;
12778
12779 /* See what kind of keyword it is. */
12780 keyword = token->keyword;
12781 switch (keyword)
12782 {
12783 case RID_BREAK:
12784 in_statement = parser->in_statement & ~IN_IF_STMT;
12785 switch (in_statement)
12786 {
12787 case 0:
12788 error_at (token->location, "break statement not within loop or switch");
12789 break;
12790 default:
12791 gcc_assert ((in_statement & IN_SWITCH_STMT)
12792 || in_statement == IN_ITERATION_STMT);
12793 statement = finish_break_stmt ();
12794 if (in_statement == IN_ITERATION_STMT)
12795 break_maybe_infinite_loop ();
12796 break;
12797 case IN_OMP_BLOCK:
12798 error_at (token->location, "invalid exit from OpenMP structured block");
12799 break;
12800 case IN_OMP_FOR:
12801 error_at (token->location, "break statement used with OpenMP for loop");
12802 break;
12803 }
12804 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12805 break;
12806
12807 case RID_CONTINUE:
12808 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12809 {
12810 case 0:
12811 error_at (token->location, "continue statement not within a loop");
12812 break;
12813 /* Fall through. */
12814 case IN_ITERATION_STMT:
12815 case IN_OMP_FOR:
12816 statement = finish_continue_stmt ();
12817 break;
12818 case IN_OMP_BLOCK:
12819 error_at (token->location, "invalid exit from OpenMP structured block");
12820 break;
12821 default:
12822 gcc_unreachable ();
12823 }
12824 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12825 break;
12826
12827 case RID_RETURN:
12828 {
12829 tree expr;
12830 bool expr_non_constant_p;
12831
12832 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12833 {
12834 cp_lexer_set_source_position (parser->lexer);
12835 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12836 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12837 }
12838 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12839 expr = cp_parser_expression (parser);
12840 else
12841 /* If the next token is a `;', then there is no
12842 expression. */
12843 expr = NULL_TREE;
12844 /* Build the return-statement. */
12845 if (current_function_auto_return_pattern && in_discarded_stmt)
12846 /* Don't deduce from a discarded return statement. */;
12847 else
12848 statement = finish_return_stmt (expr);
12849 /* Look for the final `;'. */
12850 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12851 }
12852 break;
12853
12854 case RID_GOTO:
12855 if (parser->in_function_body
12856 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12857 {
12858 error ("%<goto%> in %<constexpr%> function");
12859 cp_function_chain->invalid_constexpr = true;
12860 }
12861
12862 /* Create the goto-statement. */
12863 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12864 {
12865 /* Issue a warning about this use of a GNU extension. */
12866 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12867 /* Consume the '*' token. */
12868 cp_lexer_consume_token (parser->lexer);
12869 /* Parse the dependent expression. */
12870 finish_goto_stmt (cp_parser_expression (parser));
12871 }
12872 else
12873 finish_goto_stmt (cp_parser_identifier (parser));
12874 /* Look for the final `;'. */
12875 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12876 break;
12877
12878 default:
12879 cp_parser_error (parser, "expected jump-statement");
12880 break;
12881 }
12882
12883 return statement;
12884 }
12885
12886 /* Parse a declaration-statement.
12887
12888 declaration-statement:
12889 block-declaration */
12890
12891 static void
12892 cp_parser_declaration_statement (cp_parser* parser)
12893 {
12894 void *p;
12895
12896 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12897 p = obstack_alloc (&declarator_obstack, 0);
12898
12899 /* Parse the block-declaration. */
12900 cp_parser_block_declaration (parser, /*statement_p=*/true);
12901
12902 /* Free any declarators allocated. */
12903 obstack_free (&declarator_obstack, p);
12904 }
12905
12906 /* Some dependent statements (like `if (cond) statement'), are
12907 implicitly in their own scope. In other words, if the statement is
12908 a single statement (as opposed to a compound-statement), it is
12909 none-the-less treated as if it were enclosed in braces. Any
12910 declarations appearing in the dependent statement are out of scope
12911 after control passes that point. This function parses a statement,
12912 but ensures that is in its own scope, even if it is not a
12913 compound-statement.
12914
12915 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12916 is a (possibly labeled) if statement which is not enclosed in
12917 braces and has an else clause. This is used to implement
12918 -Wparentheses.
12919
12920 CHAIN is a vector of if-else-if conditions. This is used to implement
12921 -Wduplicated-cond.
12922
12923 Returns the new statement. */
12924
12925 static tree
12926 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12927 const token_indent_info &guard_tinfo,
12928 vec<tree> *chain)
12929 {
12930 tree statement;
12931 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12932 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12933 token_indent_info body_tinfo
12934 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12935
12936 if (if_p != NULL)
12937 *if_p = false;
12938
12939 /* Mark if () ; with a special NOP_EXPR. */
12940 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12941 {
12942 cp_lexer_consume_token (parser->lexer);
12943 statement = add_stmt (build_empty_stmt (body_loc));
12944
12945 if (guard_tinfo.keyword == RID_IF
12946 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12947 warning_at (body_loc, OPT_Wempty_body,
12948 "suggest braces around empty body in an %<if%> statement");
12949 else if (guard_tinfo.keyword == RID_ELSE)
12950 warning_at (body_loc, OPT_Wempty_body,
12951 "suggest braces around empty body in an %<else%> statement");
12952 }
12953 /* if a compound is opened, we simply parse the statement directly. */
12954 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12955 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12956 /* If the token is not a `{', then we must take special action. */
12957 else
12958 {
12959 /* Create a compound-statement. */
12960 statement = begin_compound_stmt (0);
12961 /* Parse the dependent-statement. */
12962 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12963 &body_loc_after_labels);
12964 /* Finish the dummy compound-statement. */
12965 finish_compound_stmt (statement);
12966 }
12967
12968 token_indent_info next_tinfo
12969 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12970 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12971
12972 if (body_loc_after_labels != UNKNOWN_LOCATION
12973 && next_tinfo.type != CPP_SEMICOLON)
12974 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12975 guard_tinfo.location, guard_tinfo.keyword);
12976
12977 /* Return the statement. */
12978 return statement;
12979 }
12980
12981 /* For some dependent statements (like `while (cond) statement'), we
12982 have already created a scope. Therefore, even if the dependent
12983 statement is a compound-statement, we do not want to create another
12984 scope. */
12985
12986 static void
12987 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12988 const token_indent_info &guard_tinfo)
12989 {
12990 /* If the token is a `{', then we must take special action. */
12991 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12992 {
12993 token_indent_info body_tinfo
12994 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12995 location_t loc_after_labels = UNKNOWN_LOCATION;
12996
12997 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12998 &loc_after_labels);
12999 token_indent_info next_tinfo
13000 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13001 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13002
13003 if (loc_after_labels != UNKNOWN_LOCATION
13004 && next_tinfo.type != CPP_SEMICOLON)
13005 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
13006 guard_tinfo.location,
13007 guard_tinfo.keyword);
13008 }
13009 else
13010 {
13011 /* Avoid calling cp_parser_compound_statement, so that we
13012 don't create a new scope. Do everything else by hand. */
13013 matching_braces braces;
13014 braces.require_open (parser);
13015 /* If the next keyword is `__label__' we have a label declaration. */
13016 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13017 cp_parser_label_declaration (parser);
13018 /* Parse an (optional) statement-seq. */
13019 cp_parser_statement_seq_opt (parser, NULL_TREE);
13020 braces.require_close (parser);
13021 }
13022 }
13023
13024 /* Declarations [gram.dcl.dcl] */
13025
13026 /* Parse an optional declaration-sequence.
13027
13028 declaration-seq:
13029 declaration
13030 declaration-seq declaration */
13031
13032 static void
13033 cp_parser_declaration_seq_opt (cp_parser* parser)
13034 {
13035 while (true)
13036 {
13037 cp_token *token = cp_lexer_peek_token (parser->lexer);
13038
13039 if (token->type == CPP_CLOSE_BRACE
13040 || token->type == CPP_EOF)
13041 break;
13042 else
13043 cp_parser_toplevel_declaration (parser);
13044 }
13045 }
13046
13047 /* Parse a declaration.
13048
13049 declaration:
13050 block-declaration
13051 function-definition
13052 template-declaration
13053 explicit-instantiation
13054 explicit-specialization
13055 linkage-specification
13056 namespace-definition
13057
13058 C++17:
13059 deduction-guide
13060
13061 GNU extension:
13062
13063 declaration:
13064 __extension__ declaration */
13065
13066 static void
13067 cp_parser_declaration (cp_parser* parser)
13068 {
13069 cp_token token1;
13070 cp_token token2;
13071 int saved_pedantic;
13072 void *p;
13073 tree attributes = NULL_TREE;
13074
13075 /* Check for the `__extension__' keyword. */
13076 if (cp_parser_extension_opt (parser, &saved_pedantic))
13077 {
13078 /* Parse the qualified declaration. */
13079 cp_parser_declaration (parser);
13080 /* Restore the PEDANTIC flag. */
13081 pedantic = saved_pedantic;
13082
13083 return;
13084 }
13085
13086 /* Try to figure out what kind of declaration is present. */
13087 token1 = *cp_lexer_peek_token (parser->lexer);
13088
13089 if (token1.type != CPP_EOF)
13090 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
13091 else
13092 {
13093 token2.type = CPP_EOF;
13094 token2.keyword = RID_MAX;
13095 }
13096
13097 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13098 p = obstack_alloc (&declarator_obstack, 0);
13099
13100 /* If the next token is `extern' and the following token is a string
13101 literal, then we have a linkage specification. */
13102 if (token1.keyword == RID_EXTERN
13103 && cp_parser_is_pure_string_literal (&token2))
13104 cp_parser_linkage_specification (parser);
13105 /* If the next token is `template', then we have either a template
13106 declaration, an explicit instantiation, or an explicit
13107 specialization. */
13108 else if (token1.keyword == RID_TEMPLATE)
13109 {
13110 /* `template <>' indicates a template specialization. */
13111 if (token2.type == CPP_LESS
13112 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13113 cp_parser_explicit_specialization (parser);
13114 /* `template <' indicates a template declaration. */
13115 else if (token2.type == CPP_LESS)
13116 cp_parser_template_declaration (parser, /*member_p=*/false);
13117 /* Anything else must be an explicit instantiation. */
13118 else
13119 cp_parser_explicit_instantiation (parser);
13120 }
13121 /* If the next token is `export', then we have a template
13122 declaration. */
13123 else if (token1.keyword == RID_EXPORT)
13124 cp_parser_template_declaration (parser, /*member_p=*/false);
13125 /* If the next token is `extern', 'static' or 'inline' and the one
13126 after that is `template', we have a GNU extended explicit
13127 instantiation directive. */
13128 else if (cp_parser_allow_gnu_extensions_p (parser)
13129 && (token1.keyword == RID_EXTERN
13130 || token1.keyword == RID_STATIC
13131 || token1.keyword == RID_INLINE)
13132 && token2.keyword == RID_TEMPLATE)
13133 cp_parser_explicit_instantiation (parser);
13134 /* If the next token is `namespace', check for a named or unnamed
13135 namespace definition. */
13136 else if (token1.keyword == RID_NAMESPACE
13137 && (/* A named namespace definition. */
13138 (token2.type == CPP_NAME
13139 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
13140 != CPP_EQ))
13141 || (token2.type == CPP_OPEN_SQUARE
13142 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
13143 == CPP_OPEN_SQUARE)
13144 /* An unnamed namespace definition. */
13145 || token2.type == CPP_OPEN_BRACE
13146 || token2.keyword == RID_ATTRIBUTE))
13147 cp_parser_namespace_definition (parser);
13148 /* An inline (associated) namespace definition. */
13149 else if (token1.keyword == RID_INLINE
13150 && token2.keyword == RID_NAMESPACE)
13151 cp_parser_namespace_definition (parser);
13152 /* Objective-C++ declaration/definition. */
13153 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
13154 cp_parser_objc_declaration (parser, NULL_TREE);
13155 else if (c_dialect_objc ()
13156 && token1.keyword == RID_ATTRIBUTE
13157 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
13158 cp_parser_objc_declaration (parser, attributes);
13159 /* At this point we may have a template declared by a concept
13160 introduction. */
13161 else if (flag_concepts
13162 && cp_parser_template_declaration_after_export (parser,
13163 /*member_p=*/false))
13164 /* We did. */;
13165 else
13166 /* Try to parse a block-declaration, or a function-definition. */
13167 cp_parser_block_declaration (parser, /*statement_p=*/false);
13168
13169 /* Free any declarators allocated. */
13170 obstack_free (&declarator_obstack, p);
13171 }
13172
13173 /* Parse a namespace-scope declaration. */
13174
13175 static void
13176 cp_parser_toplevel_declaration (cp_parser* parser)
13177 {
13178 cp_token *token = cp_lexer_peek_token (parser->lexer);
13179
13180 if (token->type == CPP_PRAGMA)
13181 /* A top-level declaration can consist solely of a #pragma. A
13182 nested declaration cannot, so this is done here and not in
13183 cp_parser_declaration. (A #pragma at block scope is
13184 handled in cp_parser_statement.) */
13185 cp_parser_pragma (parser, pragma_external, NULL);
13186 else if (token->type == CPP_SEMICOLON)
13187 {
13188 /* A declaration consisting of a single semicolon is
13189 invalid. Allow it unless we're being pedantic. */
13190 cp_lexer_consume_token (parser->lexer);
13191 if (!in_system_header_at (input_location))
13192 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
13193 }
13194 else
13195 /* Parse the declaration itself. */
13196 cp_parser_declaration (parser);
13197 }
13198
13199 /* Parse a block-declaration.
13200
13201 block-declaration:
13202 simple-declaration
13203 asm-definition
13204 namespace-alias-definition
13205 using-declaration
13206 using-directive
13207
13208 GNU Extension:
13209
13210 block-declaration:
13211 __extension__ block-declaration
13212
13213 C++0x Extension:
13214
13215 block-declaration:
13216 static_assert-declaration
13217
13218 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13219 part of a declaration-statement. */
13220
13221 static void
13222 cp_parser_block_declaration (cp_parser *parser,
13223 bool statement_p)
13224 {
13225 cp_token *token1;
13226 int saved_pedantic;
13227
13228 /* Check for the `__extension__' keyword. */
13229 if (cp_parser_extension_opt (parser, &saved_pedantic))
13230 {
13231 /* Parse the qualified declaration. */
13232 cp_parser_block_declaration (parser, statement_p);
13233 /* Restore the PEDANTIC flag. */
13234 pedantic = saved_pedantic;
13235
13236 return;
13237 }
13238
13239 /* Peek at the next token to figure out which kind of declaration is
13240 present. */
13241 token1 = cp_lexer_peek_token (parser->lexer);
13242
13243 /* If the next keyword is `asm', we have an asm-definition. */
13244 if (token1->keyword == RID_ASM)
13245 {
13246 if (statement_p)
13247 cp_parser_commit_to_tentative_parse (parser);
13248 cp_parser_asm_definition (parser);
13249 }
13250 /* If the next keyword is `namespace', we have a
13251 namespace-alias-definition. */
13252 else if (token1->keyword == RID_NAMESPACE)
13253 cp_parser_namespace_alias_definition (parser);
13254 /* If the next keyword is `using', we have a
13255 using-declaration, a using-directive, or an alias-declaration. */
13256 else if (token1->keyword == RID_USING)
13257 {
13258 cp_token *token2;
13259
13260 if (statement_p)
13261 cp_parser_commit_to_tentative_parse (parser);
13262 /* If the token after `using' is `namespace', then we have a
13263 using-directive. */
13264 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13265 if (token2->keyword == RID_NAMESPACE)
13266 cp_parser_using_directive (parser);
13267 /* If the second token after 'using' is '=', then we have an
13268 alias-declaration. */
13269 else if (cxx_dialect >= cxx11
13270 && token2->type == CPP_NAME
13271 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13272 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13273 cp_parser_alias_declaration (parser);
13274 /* Otherwise, it's a using-declaration. */
13275 else
13276 cp_parser_using_declaration (parser,
13277 /*access_declaration_p=*/false);
13278 }
13279 /* If the next keyword is `__label__' we have a misplaced label
13280 declaration. */
13281 else if (token1->keyword == RID_LABEL)
13282 {
13283 cp_lexer_consume_token (parser->lexer);
13284 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13285 cp_parser_skip_to_end_of_statement (parser);
13286 /* If the next token is now a `;', consume it. */
13287 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13288 cp_lexer_consume_token (parser->lexer);
13289 }
13290 /* If the next token is `static_assert' we have a static assertion. */
13291 else if (token1->keyword == RID_STATIC_ASSERT)
13292 cp_parser_static_assert (parser, /*member_p=*/false);
13293 /* Anything else must be a simple-declaration. */
13294 else
13295 cp_parser_simple_declaration (parser, !statement_p,
13296 /*maybe_range_for_decl*/NULL);
13297 }
13298
13299 /* Parse a simple-declaration.
13300
13301 simple-declaration:
13302 decl-specifier-seq [opt] init-declarator-list [opt] ;
13303 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13304 brace-or-equal-initializer ;
13305
13306 init-declarator-list:
13307 init-declarator
13308 init-declarator-list , init-declarator
13309
13310 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13311 function-definition as a simple-declaration.
13312
13313 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13314 parsed declaration if it is an uninitialized single declarator not followed
13315 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13316 if present, will not be consumed. */
13317
13318 static void
13319 cp_parser_simple_declaration (cp_parser* parser,
13320 bool function_definition_allowed_p,
13321 tree *maybe_range_for_decl)
13322 {
13323 cp_decl_specifier_seq decl_specifiers;
13324 int declares_class_or_enum;
13325 bool saw_declarator;
13326 location_t comma_loc = UNKNOWN_LOCATION;
13327 location_t init_loc = UNKNOWN_LOCATION;
13328
13329 if (maybe_range_for_decl)
13330 *maybe_range_for_decl = NULL_TREE;
13331
13332 /* Defer access checks until we know what is being declared; the
13333 checks for names appearing in the decl-specifier-seq should be
13334 done as if we were in the scope of the thing being declared. */
13335 push_deferring_access_checks (dk_deferred);
13336
13337 /* Parse the decl-specifier-seq. We have to keep track of whether
13338 or not the decl-specifier-seq declares a named class or
13339 enumeration type, since that is the only case in which the
13340 init-declarator-list is allowed to be empty.
13341
13342 [dcl.dcl]
13343
13344 In a simple-declaration, the optional init-declarator-list can be
13345 omitted only when declaring a class or enumeration, that is when
13346 the decl-specifier-seq contains either a class-specifier, an
13347 elaborated-type-specifier, or an enum-specifier. */
13348 cp_parser_decl_specifier_seq (parser,
13349 CP_PARSER_FLAGS_OPTIONAL,
13350 &decl_specifiers,
13351 &declares_class_or_enum);
13352 /* We no longer need to defer access checks. */
13353 stop_deferring_access_checks ();
13354
13355 /* In a block scope, a valid declaration must always have a
13356 decl-specifier-seq. By not trying to parse declarators, we can
13357 resolve the declaration/expression ambiguity more quickly. */
13358 if (!function_definition_allowed_p
13359 && !decl_specifiers.any_specifiers_p)
13360 {
13361 cp_parser_error (parser, "expected declaration");
13362 goto done;
13363 }
13364
13365 /* If the next two tokens are both identifiers, the code is
13366 erroneous. The usual cause of this situation is code like:
13367
13368 T t;
13369
13370 where "T" should name a type -- but does not. */
13371 if (!decl_specifiers.any_type_specifiers_p
13372 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13373 {
13374 /* If parsing tentatively, we should commit; we really are
13375 looking at a declaration. */
13376 cp_parser_commit_to_tentative_parse (parser);
13377 /* Give up. */
13378 goto done;
13379 }
13380
13381 cp_parser_maybe_commit_to_declaration (parser,
13382 decl_specifiers.any_specifiers_p);
13383
13384 /* Look for C++17 decomposition declaration. */
13385 for (size_t n = 1; ; n++)
13386 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13387 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13388 continue;
13389 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13390 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13391 && decl_specifiers.any_specifiers_p)
13392 {
13393 tree decl
13394 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13395 maybe_range_for_decl,
13396 &init_loc);
13397
13398 /* The next token should be either a `,' or a `;'. */
13399 cp_token *token = cp_lexer_peek_token (parser->lexer);
13400 /* If it's a `;', we are done. */
13401 if (token->type == CPP_SEMICOLON)
13402 goto finish;
13403 else if (maybe_range_for_decl)
13404 {
13405 if (*maybe_range_for_decl == NULL_TREE)
13406 *maybe_range_for_decl = error_mark_node;
13407 goto finish;
13408 }
13409 /* Anything else is an error. */
13410 else
13411 {
13412 /* If we have already issued an error message we don't need
13413 to issue another one. */
13414 if ((decl != error_mark_node
13415 && DECL_INITIAL (decl) != error_mark_node)
13416 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13417 cp_parser_error (parser, "expected %<,%> or %<;%>");
13418 /* Skip tokens until we reach the end of the statement. */
13419 cp_parser_skip_to_end_of_statement (parser);
13420 /* If the next token is now a `;', consume it. */
13421 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13422 cp_lexer_consume_token (parser->lexer);
13423 goto done;
13424 }
13425 }
13426 else
13427 break;
13428
13429 tree last_type;
13430 bool auto_specifier_p;
13431 /* NULL_TREE if both variable and function declaration are allowed,
13432 error_mark_node if function declaration are not allowed and
13433 a FUNCTION_DECL that should be diagnosed if it is followed by
13434 variable declarations. */
13435 tree auto_function_declaration;
13436
13437 last_type = NULL_TREE;
13438 auto_specifier_p
13439 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13440 auto_function_declaration = NULL_TREE;
13441
13442 /* Keep going until we hit the `;' at the end of the simple
13443 declaration. */
13444 saw_declarator = false;
13445 while (cp_lexer_next_token_is_not (parser->lexer,
13446 CPP_SEMICOLON))
13447 {
13448 cp_token *token;
13449 bool function_definition_p;
13450 tree decl;
13451 tree auto_result = NULL_TREE;
13452
13453 if (saw_declarator)
13454 {
13455 /* If we are processing next declarator, comma is expected */
13456 token = cp_lexer_peek_token (parser->lexer);
13457 gcc_assert (token->type == CPP_COMMA);
13458 cp_lexer_consume_token (parser->lexer);
13459 if (maybe_range_for_decl)
13460 {
13461 *maybe_range_for_decl = error_mark_node;
13462 if (comma_loc == UNKNOWN_LOCATION)
13463 comma_loc = token->location;
13464 }
13465 }
13466 else
13467 saw_declarator = true;
13468
13469 /* Parse the init-declarator. */
13470 decl = cp_parser_init_declarator (parser,
13471 CP_PARSER_FLAGS_NONE,
13472 &decl_specifiers,
13473 /*checks=*/NULL,
13474 function_definition_allowed_p,
13475 /*member_p=*/false,
13476 declares_class_or_enum,
13477 &function_definition_p,
13478 maybe_range_for_decl,
13479 &init_loc,
13480 &auto_result);
13481 /* If an error occurred while parsing tentatively, exit quickly.
13482 (That usually happens when in the body of a function; each
13483 statement is treated as a declaration-statement until proven
13484 otherwise.) */
13485 if (cp_parser_error_occurred (parser))
13486 goto done;
13487
13488 if (auto_specifier_p && cxx_dialect >= cxx14)
13489 {
13490 /* If the init-declarator-list contains more than one
13491 init-declarator, they shall all form declarations of
13492 variables. */
13493 if (auto_function_declaration == NULL_TREE)
13494 auto_function_declaration
13495 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13496 else if (TREE_CODE (decl) == FUNCTION_DECL
13497 || auto_function_declaration != error_mark_node)
13498 {
13499 error_at (decl_specifiers.locations[ds_type_spec],
13500 "non-variable %qD in declaration with more than one "
13501 "declarator with placeholder type",
13502 TREE_CODE (decl) == FUNCTION_DECL
13503 ? decl : auto_function_declaration);
13504 auto_function_declaration = error_mark_node;
13505 }
13506 }
13507
13508 if (auto_result
13509 && (!processing_template_decl || !type_uses_auto (auto_result)))
13510 {
13511 if (last_type
13512 && last_type != error_mark_node
13513 && !same_type_p (auto_result, last_type))
13514 {
13515 /* If the list of declarators contains more than one declarator,
13516 the type of each declared variable is determined as described
13517 above. If the type deduced for the template parameter U is not
13518 the same in each deduction, the program is ill-formed. */
13519 error_at (decl_specifiers.locations[ds_type_spec],
13520 "inconsistent deduction for %qT: %qT and then %qT",
13521 decl_specifiers.type, last_type, auto_result);
13522 last_type = error_mark_node;
13523 }
13524 else
13525 last_type = auto_result;
13526 }
13527
13528 /* Handle function definitions specially. */
13529 if (function_definition_p)
13530 {
13531 /* If the next token is a `,', then we are probably
13532 processing something like:
13533
13534 void f() {}, *p;
13535
13536 which is erroneous. */
13537 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13538 {
13539 cp_token *token = cp_lexer_peek_token (parser->lexer);
13540 error_at (token->location,
13541 "mixing"
13542 " declarations and function-definitions is forbidden");
13543 }
13544 /* Otherwise, we're done with the list of declarators. */
13545 else
13546 {
13547 pop_deferring_access_checks ();
13548 return;
13549 }
13550 }
13551 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13552 *maybe_range_for_decl = decl;
13553 /* The next token should be either a `,' or a `;'. */
13554 token = cp_lexer_peek_token (parser->lexer);
13555 /* If it's a `,', there are more declarators to come. */
13556 if (token->type == CPP_COMMA)
13557 /* will be consumed next time around */;
13558 /* If it's a `;', we are done. */
13559 else if (token->type == CPP_SEMICOLON)
13560 break;
13561 else if (maybe_range_for_decl)
13562 {
13563 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13564 permerror (decl_specifiers.locations[ds_type_spec],
13565 "types may not be defined in a for-range-declaration");
13566 break;
13567 }
13568 /* Anything else is an error. */
13569 else
13570 {
13571 /* If we have already issued an error message we don't need
13572 to issue another one. */
13573 if ((decl != error_mark_node
13574 && DECL_INITIAL (decl) != error_mark_node)
13575 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13576 cp_parser_error (parser, "expected %<,%> or %<;%>");
13577 /* Skip tokens until we reach the end of the statement. */
13578 cp_parser_skip_to_end_of_statement (parser);
13579 /* If the next token is now a `;', consume it. */
13580 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13581 cp_lexer_consume_token (parser->lexer);
13582 goto done;
13583 }
13584 /* After the first time around, a function-definition is not
13585 allowed -- even if it was OK at first. For example:
13586
13587 int i, f() {}
13588
13589 is not valid. */
13590 function_definition_allowed_p = false;
13591 }
13592
13593 /* Issue an error message if no declarators are present, and the
13594 decl-specifier-seq does not itself declare a class or
13595 enumeration: [dcl.dcl]/3. */
13596 if (!saw_declarator)
13597 {
13598 if (cp_parser_declares_only_class_p (parser))
13599 {
13600 if (!declares_class_or_enum
13601 && decl_specifiers.type
13602 && OVERLOAD_TYPE_P (decl_specifiers.type))
13603 /* Ensure an error is issued anyway when finish_decltype_type,
13604 called via cp_parser_decl_specifier_seq, returns a class or
13605 an enumeration (c++/51786). */
13606 decl_specifiers.type = NULL_TREE;
13607 shadow_tag (&decl_specifiers);
13608 }
13609 /* Perform any deferred access checks. */
13610 perform_deferred_access_checks (tf_warning_or_error);
13611 }
13612
13613 /* Consume the `;'. */
13614 finish:
13615 if (!maybe_range_for_decl)
13616 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13617 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13618 {
13619 if (init_loc != UNKNOWN_LOCATION)
13620 error_at (init_loc, "initializer in range-based %<for%> loop");
13621 if (comma_loc != UNKNOWN_LOCATION)
13622 error_at (comma_loc,
13623 "multiple declarations in range-based %<for%> loop");
13624 }
13625
13626 done:
13627 pop_deferring_access_checks ();
13628 }
13629
13630 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13631 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13632 initializer ; */
13633
13634 static tree
13635 cp_parser_decomposition_declaration (cp_parser *parser,
13636 cp_decl_specifier_seq *decl_specifiers,
13637 tree *maybe_range_for_decl,
13638 location_t *init_loc)
13639 {
13640 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13641 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13642 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13643
13644 /* Parse the identifier-list. */
13645 auto_vec<cp_expr, 10> v;
13646 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13647 while (true)
13648 {
13649 cp_expr e = cp_parser_identifier (parser);
13650 if (e.get_value () == error_mark_node)
13651 break;
13652 v.safe_push (e);
13653 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13654 break;
13655 cp_lexer_consume_token (parser->lexer);
13656 }
13657
13658 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13659 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13660 {
13661 end_loc = UNKNOWN_LOCATION;
13662 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13663 false);
13664 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13665 cp_lexer_consume_token (parser->lexer);
13666 else
13667 {
13668 cp_parser_skip_to_end_of_statement (parser);
13669 return error_mark_node;
13670 }
13671 }
13672
13673 if (cxx_dialect < cxx17)
13674 pedwarn (loc, 0, "structured bindings only available with "
13675 "-std=c++17 or -std=gnu++17");
13676
13677 tree pushed_scope;
13678 cp_declarator *declarator = make_declarator (cdk_decomp);
13679 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13680 declarator->id_loc = loc;
13681 if (ref_qual != REF_QUAL_NONE)
13682 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13683 ref_qual == REF_QUAL_RVALUE,
13684 NULL_TREE);
13685 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13686 NULL_TREE, decl_specifiers->attributes,
13687 &pushed_scope);
13688 tree orig_decl = decl;
13689
13690 unsigned int i;
13691 cp_expr e;
13692 cp_decl_specifier_seq decl_specs;
13693 clear_decl_specs (&decl_specs);
13694 decl_specs.type = make_auto ();
13695 tree prev = decl;
13696 FOR_EACH_VEC_ELT (v, i, e)
13697 {
13698 if (i == 0)
13699 declarator = make_id_declarator (NULL_TREE, e.get_value (),
13700 sfk_none, e.get_location ());
13701 else
13702 {
13703 declarator->u.id.unqualified_name = e.get_value ();
13704 declarator->id_loc = e.get_location ();
13705 }
13706 tree elt_pushed_scope;
13707 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13708 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13709 if (decl2 == error_mark_node)
13710 decl = error_mark_node;
13711 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13712 {
13713 /* Ensure we've diagnosed redeclaration if we aren't creating
13714 a new VAR_DECL. */
13715 gcc_assert (errorcount);
13716 decl = error_mark_node;
13717 }
13718 else
13719 prev = decl2;
13720 if (elt_pushed_scope)
13721 pop_scope (elt_pushed_scope);
13722 }
13723
13724 if (v.is_empty ())
13725 {
13726 error_at (loc, "empty structured binding declaration");
13727 decl = error_mark_node;
13728 }
13729
13730 if (maybe_range_for_decl == NULL
13731 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13732 {
13733 bool non_constant_p = false, is_direct_init = false;
13734 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13735 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13736 &non_constant_p);
13737 if (initializer == NULL_TREE
13738 || (TREE_CODE (initializer) == TREE_LIST
13739 && TREE_CHAIN (initializer))
13740 || (is_direct_init
13741 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13742 && CONSTRUCTOR_NELTS (initializer) != 1))
13743 {
13744 error_at (loc, "invalid initializer for structured binding "
13745 "declaration");
13746 initializer = error_mark_node;
13747 }
13748
13749 if (decl != error_mark_node)
13750 {
13751 cp_maybe_mangle_decomp (decl, prev, v.length ());
13752 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13753 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13754 cp_finish_decomp (decl, prev, v.length ());
13755 }
13756 }
13757 else if (decl != error_mark_node)
13758 {
13759 *maybe_range_for_decl = prev;
13760 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13761 the underlying DECL. */
13762 cp_finish_decomp (decl, prev, v.length ());
13763 }
13764
13765 if (pushed_scope)
13766 pop_scope (pushed_scope);
13767
13768 if (decl == error_mark_node && DECL_P (orig_decl))
13769 {
13770 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13771 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13772 }
13773
13774 return decl;
13775 }
13776
13777 /* Parse a decl-specifier-seq.
13778
13779 decl-specifier-seq:
13780 decl-specifier-seq [opt] decl-specifier
13781 decl-specifier attribute-specifier-seq [opt] (C++11)
13782
13783 decl-specifier:
13784 storage-class-specifier
13785 type-specifier
13786 function-specifier
13787 friend
13788 typedef
13789
13790 GNU Extension:
13791
13792 decl-specifier:
13793 attributes
13794
13795 Concepts Extension:
13796
13797 decl-specifier:
13798 concept
13799
13800 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13801
13802 The parser flags FLAGS is used to control type-specifier parsing.
13803
13804 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13805 flags:
13806
13807 1: one of the decl-specifiers is an elaborated-type-specifier
13808 (i.e., a type declaration)
13809 2: one of the decl-specifiers is an enum-specifier or a
13810 class-specifier (i.e., a type definition)
13811
13812 */
13813
13814 static void
13815 cp_parser_decl_specifier_seq (cp_parser* parser,
13816 cp_parser_flags flags,
13817 cp_decl_specifier_seq *decl_specs,
13818 int* declares_class_or_enum)
13819 {
13820 bool constructor_possible_p = !parser->in_declarator_p;
13821 bool found_decl_spec = false;
13822 cp_token *start_token = NULL;
13823 cp_decl_spec ds;
13824
13825 /* Clear DECL_SPECS. */
13826 clear_decl_specs (decl_specs);
13827
13828 /* Assume no class or enumeration type is declared. */
13829 *declares_class_or_enum = 0;
13830
13831 /* Keep reading specifiers until there are no more to read. */
13832 while (true)
13833 {
13834 bool constructor_p;
13835 cp_token *token;
13836 ds = ds_last;
13837
13838 /* Peek at the next token. */
13839 token = cp_lexer_peek_token (parser->lexer);
13840
13841 /* Save the first token of the decl spec list for error
13842 reporting. */
13843 if (!start_token)
13844 start_token = token;
13845 /* Handle attributes. */
13846 if (cp_next_tokens_can_be_attribute_p (parser))
13847 {
13848 /* Parse the attributes. */
13849 tree attrs = cp_parser_attributes_opt (parser);
13850
13851 /* In a sequence of declaration specifiers, c++11 attributes
13852 appertain to the type that precede them. In that case
13853 [dcl.spec]/1 says:
13854
13855 The attribute-specifier-seq affects the type only for
13856 the declaration it appears in, not other declarations
13857 involving the same type.
13858
13859 But for now let's force the user to position the
13860 attribute either at the beginning of the declaration or
13861 after the declarator-id, which would clearly mean that it
13862 applies to the declarator. */
13863 if (cxx11_attribute_p (attrs))
13864 {
13865 if (!found_decl_spec)
13866 /* The c++11 attribute is at the beginning of the
13867 declaration. It appertains to the entity being
13868 declared. */;
13869 else
13870 {
13871 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13872 {
13873 /* This is an attribute following a
13874 class-specifier. */
13875 if (decl_specs->type_definition_p)
13876 warn_misplaced_attr_for_class_type (token->location,
13877 decl_specs->type);
13878 attrs = NULL_TREE;
13879 }
13880 else
13881 {
13882 decl_specs->std_attributes
13883 = attr_chainon (decl_specs->std_attributes, attrs);
13884 if (decl_specs->locations[ds_std_attribute] == 0)
13885 decl_specs->locations[ds_std_attribute] = token->location;
13886 }
13887 continue;
13888 }
13889 }
13890
13891 decl_specs->attributes
13892 = attr_chainon (decl_specs->attributes, attrs);
13893 if (decl_specs->locations[ds_attribute] == 0)
13894 decl_specs->locations[ds_attribute] = token->location;
13895 continue;
13896 }
13897 /* Assume we will find a decl-specifier keyword. */
13898 found_decl_spec = true;
13899 /* If the next token is an appropriate keyword, we can simply
13900 add it to the list. */
13901 switch (token->keyword)
13902 {
13903 /* decl-specifier:
13904 friend
13905 constexpr */
13906 case RID_FRIEND:
13907 if (!at_class_scope_p ())
13908 {
13909 gcc_rich_location richloc (token->location);
13910 richloc.add_fixit_remove ();
13911 error_at (&richloc, "%<friend%> used outside of class");
13912 cp_lexer_purge_token (parser->lexer);
13913 }
13914 else
13915 {
13916 ds = ds_friend;
13917 /* Consume the token. */
13918 cp_lexer_consume_token (parser->lexer);
13919 }
13920 break;
13921
13922 case RID_CONSTEXPR:
13923 ds = ds_constexpr;
13924 cp_lexer_consume_token (parser->lexer);
13925 break;
13926
13927 case RID_CONCEPT:
13928 ds = ds_concept;
13929 cp_lexer_consume_token (parser->lexer);
13930 break;
13931
13932 /* function-specifier:
13933 inline
13934 virtual
13935 explicit */
13936 case RID_INLINE:
13937 case RID_VIRTUAL:
13938 case RID_EXPLICIT:
13939 cp_parser_function_specifier_opt (parser, decl_specs);
13940 break;
13941
13942 /* decl-specifier:
13943 typedef */
13944 case RID_TYPEDEF:
13945 ds = ds_typedef;
13946 /* Consume the token. */
13947 cp_lexer_consume_token (parser->lexer);
13948 /* A constructor declarator cannot appear in a typedef. */
13949 constructor_possible_p = false;
13950 /* The "typedef" keyword can only occur in a declaration; we
13951 may as well commit at this point. */
13952 cp_parser_commit_to_tentative_parse (parser);
13953
13954 if (decl_specs->storage_class != sc_none)
13955 decl_specs->conflicting_specifiers_p = true;
13956 break;
13957
13958 /* storage-class-specifier:
13959 auto
13960 register
13961 static
13962 extern
13963 mutable
13964
13965 GNU Extension:
13966 thread */
13967 case RID_AUTO:
13968 if (cxx_dialect == cxx98)
13969 {
13970 /* Consume the token. */
13971 cp_lexer_consume_token (parser->lexer);
13972
13973 /* Complain about `auto' as a storage specifier, if
13974 we're complaining about C++0x compatibility. */
13975 gcc_rich_location richloc (token->location);
13976 richloc.add_fixit_remove ();
13977 warning_at (&richloc, OPT_Wc__11_compat,
13978 "%<auto%> changes meaning in C++11; "
13979 "please remove it");
13980
13981 /* Set the storage class anyway. */
13982 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13983 token);
13984 }
13985 else
13986 /* C++0x auto type-specifier. */
13987 found_decl_spec = false;
13988 break;
13989
13990 case RID_REGISTER:
13991 case RID_STATIC:
13992 case RID_EXTERN:
13993 case RID_MUTABLE:
13994 /* Consume the token. */
13995 cp_lexer_consume_token (parser->lexer);
13996 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13997 token);
13998 break;
13999 case RID_THREAD:
14000 /* Consume the token. */
14001 ds = ds_thread;
14002 cp_lexer_consume_token (parser->lexer);
14003 break;
14004
14005 default:
14006 /* We did not yet find a decl-specifier yet. */
14007 found_decl_spec = false;
14008 break;
14009 }
14010
14011 if (found_decl_spec
14012 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
14013 && token->keyword != RID_CONSTEXPR)
14014 error ("decl-specifier invalid in condition");
14015
14016 if (found_decl_spec
14017 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14018 && token->keyword != RID_MUTABLE
14019 && token->keyword != RID_CONSTEXPR)
14020 error_at (token->location, "%qD invalid in lambda",
14021 ridpointers[token->keyword]);
14022
14023 if (ds != ds_last)
14024 set_and_check_decl_spec_loc (decl_specs, ds, token);
14025
14026 /* Constructors are a special case. The `S' in `S()' is not a
14027 decl-specifier; it is the beginning of the declarator. */
14028 constructor_p
14029 = (!found_decl_spec
14030 && constructor_possible_p
14031 && (cp_parser_constructor_declarator_p
14032 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
14033
14034 /* If we don't have a DECL_SPEC yet, then we must be looking at
14035 a type-specifier. */
14036 if (!found_decl_spec && !constructor_p)
14037 {
14038 int decl_spec_declares_class_or_enum;
14039 bool is_cv_qualifier;
14040 tree type_spec;
14041
14042 type_spec
14043 = cp_parser_type_specifier (parser, flags,
14044 decl_specs,
14045 /*is_declaration=*/true,
14046 &decl_spec_declares_class_or_enum,
14047 &is_cv_qualifier);
14048 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
14049
14050 /* If this type-specifier referenced a user-defined type
14051 (a typedef, class-name, etc.), then we can't allow any
14052 more such type-specifiers henceforth.
14053
14054 [dcl.spec]
14055
14056 The longest sequence of decl-specifiers that could
14057 possibly be a type name is taken as the
14058 decl-specifier-seq of a declaration. The sequence shall
14059 be self-consistent as described below.
14060
14061 [dcl.type]
14062
14063 As a general rule, at most one type-specifier is allowed
14064 in the complete decl-specifier-seq of a declaration. The
14065 only exceptions are the following:
14066
14067 -- const or volatile can be combined with any other
14068 type-specifier.
14069
14070 -- signed or unsigned can be combined with char, long,
14071 short, or int.
14072
14073 -- ..
14074
14075 Example:
14076
14077 typedef char* Pc;
14078 void g (const int Pc);
14079
14080 Here, Pc is *not* part of the decl-specifier seq; it's
14081 the declarator. Therefore, once we see a type-specifier
14082 (other than a cv-qualifier), we forbid any additional
14083 user-defined types. We *do* still allow things like `int
14084 int' to be considered a decl-specifier-seq, and issue the
14085 error message later. */
14086 if (type_spec && !is_cv_qualifier)
14087 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14088 /* A constructor declarator cannot follow a type-specifier. */
14089 if (type_spec)
14090 {
14091 constructor_possible_p = false;
14092 found_decl_spec = true;
14093 if (!is_cv_qualifier)
14094 decl_specs->any_type_specifiers_p = true;
14095
14096 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
14097 error_at (token->location, "type-specifier invalid in lambda");
14098 }
14099 }
14100
14101 /* If we still do not have a DECL_SPEC, then there are no more
14102 decl-specifiers. */
14103 if (!found_decl_spec)
14104 break;
14105
14106 decl_specs->any_specifiers_p = true;
14107 /* After we see one decl-specifier, further decl-specifiers are
14108 always optional. */
14109 flags |= CP_PARSER_FLAGS_OPTIONAL;
14110 }
14111
14112 /* Don't allow a friend specifier with a class definition. */
14113 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
14114 && (*declares_class_or_enum & 2))
14115 error_at (decl_specs->locations[ds_friend],
14116 "class definition may not be declared a friend");
14117 }
14118
14119 /* Parse an (optional) storage-class-specifier.
14120
14121 storage-class-specifier:
14122 auto
14123 register
14124 static
14125 extern
14126 mutable
14127
14128 GNU Extension:
14129
14130 storage-class-specifier:
14131 thread
14132
14133 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14134
14135 static tree
14136 cp_parser_storage_class_specifier_opt (cp_parser* parser)
14137 {
14138 switch (cp_lexer_peek_token (parser->lexer)->keyword)
14139 {
14140 case RID_AUTO:
14141 if (cxx_dialect != cxx98)
14142 return NULL_TREE;
14143 /* Fall through for C++98. */
14144 gcc_fallthrough ();
14145
14146 case RID_REGISTER:
14147 case RID_STATIC:
14148 case RID_EXTERN:
14149 case RID_MUTABLE:
14150 case RID_THREAD:
14151 /* Consume the token. */
14152 return cp_lexer_consume_token (parser->lexer)->u.value;
14153
14154 default:
14155 return NULL_TREE;
14156 }
14157 }
14158
14159 /* Parse an (optional) function-specifier.
14160
14161 function-specifier:
14162 inline
14163 virtual
14164 explicit
14165
14166 C++2A Extension:
14167 explicit(constant-expression)
14168
14169 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14170 Updates DECL_SPECS, if it is non-NULL. */
14171
14172 static tree
14173 cp_parser_function_specifier_opt (cp_parser* parser,
14174 cp_decl_specifier_seq *decl_specs)
14175 {
14176 cp_token *token = cp_lexer_peek_token (parser->lexer);
14177 switch (token->keyword)
14178 {
14179 case RID_INLINE:
14180 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
14181 break;
14182
14183 case RID_VIRTUAL:
14184 /* 14.5.2.3 [temp.mem]
14185
14186 A member function template shall not be virtual. */
14187 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14188 && current_class_type)
14189 error_at (token->location, "templates may not be %<virtual%>");
14190 else
14191 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
14192 break;
14193
14194 case RID_EXPLICIT:
14195 {
14196 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
14197 /* If we see '(', it's C++20 explicit(bool). */
14198 tree expr;
14199 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14200 {
14201 matching_parens parens;
14202 parens.consume_open (parser);
14203
14204 /* New types are not allowed in an explicit-specifier. */
14205 const char *saved_message
14206 = parser->type_definition_forbidden_message;
14207 parser->type_definition_forbidden_message
14208 = G_("types may not be defined in explicit-specifier");
14209
14210 if (cxx_dialect < cxx2a)
14211 pedwarn (token->location, 0,
14212 "%<explicit(bool)%> only available with -std=c++2a "
14213 "or -std=gnu++2a");
14214
14215 /* Parse the constant-expression. */
14216 expr = cp_parser_constant_expression (parser);
14217
14218 /* Restore the saved message. */
14219 parser->type_definition_forbidden_message = saved_message;
14220 parens.require_close (parser);
14221 }
14222 else
14223 /* The explicit-specifier explicit without a constant-expression is
14224 equivalent to the explicit-specifier explicit(true). */
14225 expr = boolean_true_node;
14226
14227 /* [dcl.fct.spec]
14228 "the constant-expression, if supplied, shall be a contextually
14229 converted constant expression of type bool." */
14230 expr = build_explicit_specifier (expr, tf_warning_or_error);
14231 /* We could evaluate it -- mark the decl as appropriate. */
14232 if (expr == boolean_true_node)
14233 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14234 else if (expr == boolean_false_node)
14235 /* Don't mark the decl as explicit. */;
14236 else if (decl_specs)
14237 /* The expression was value-dependent. Remember it so that we can
14238 substitute it later. */
14239 decl_specs->explicit_specifier = expr;
14240 return id;
14241 }
14242
14243 default:
14244 return NULL_TREE;
14245 }
14246
14247 /* Consume the token. */
14248 return cp_lexer_consume_token (parser->lexer)->u.value;
14249 }
14250
14251 /* Parse a linkage-specification.
14252
14253 linkage-specification:
14254 extern string-literal { declaration-seq [opt] }
14255 extern string-literal declaration */
14256
14257 static void
14258 cp_parser_linkage_specification (cp_parser* parser)
14259 {
14260 tree linkage;
14261
14262 /* Look for the `extern' keyword. */
14263 cp_token *extern_token
14264 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14265
14266 /* Look for the string-literal. */
14267 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14268 linkage = cp_parser_string_literal (parser, false, false);
14269
14270 /* Transform the literal into an identifier. If the literal is a
14271 wide-character string, or contains embedded NULs, then we can't
14272 handle it as the user wants. */
14273 if (strlen (TREE_STRING_POINTER (linkage))
14274 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14275 {
14276 cp_parser_error (parser, "invalid linkage-specification");
14277 /* Assume C++ linkage. */
14278 linkage = lang_name_cplusplus;
14279 }
14280 else
14281 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14282
14283 /* We're now using the new linkage. */
14284 push_lang_context (linkage);
14285
14286 /* Preserve the location of the the innermost linkage specification,
14287 tracking the locations of nested specifications via a local. */
14288 location_t saved_location
14289 = parser->innermost_linkage_specification_location;
14290 /* Construct a location ranging from the start of the "extern" to
14291 the end of the string-literal, with the caret at the start, e.g.:
14292 extern "C" {
14293 ^~~~~~~~~~
14294 */
14295 parser->innermost_linkage_specification_location
14296 = make_location (extern_token->location,
14297 extern_token->location,
14298 get_finish (string_token->location));
14299
14300 /* If the next token is a `{', then we're using the first
14301 production. */
14302 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14303 {
14304 cp_ensure_no_omp_declare_simd (parser);
14305 cp_ensure_no_oacc_routine (parser);
14306
14307 /* Consume the `{' token. */
14308 matching_braces braces;
14309 braces.consume_open (parser);
14310 /* Parse the declarations. */
14311 cp_parser_declaration_seq_opt (parser);
14312 /* Look for the closing `}'. */
14313 braces.require_close (parser);
14314 }
14315 /* Otherwise, there's just one declaration. */
14316 else
14317 {
14318 bool saved_in_unbraced_linkage_specification_p;
14319
14320 saved_in_unbraced_linkage_specification_p
14321 = parser->in_unbraced_linkage_specification_p;
14322 parser->in_unbraced_linkage_specification_p = true;
14323 cp_parser_declaration (parser);
14324 parser->in_unbraced_linkage_specification_p
14325 = saved_in_unbraced_linkage_specification_p;
14326 }
14327
14328 /* We're done with the linkage-specification. */
14329 pop_lang_context ();
14330
14331 /* Restore location of parent linkage specification, if any. */
14332 parser->innermost_linkage_specification_location = saved_location;
14333 }
14334
14335 /* Parse a static_assert-declaration.
14336
14337 static_assert-declaration:
14338 static_assert ( constant-expression , string-literal ) ;
14339 static_assert ( constant-expression ) ; (C++17)
14340
14341 If MEMBER_P, this static_assert is a class member. */
14342
14343 static void
14344 cp_parser_static_assert(cp_parser *parser, bool member_p)
14345 {
14346 cp_expr condition;
14347 location_t token_loc;
14348 tree message;
14349 bool dummy;
14350
14351 /* Peek at the `static_assert' token so we can keep track of exactly
14352 where the static assertion started. */
14353 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14354
14355 /* Look for the `static_assert' keyword. */
14356 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14357 RT_STATIC_ASSERT))
14358 return;
14359
14360 /* We know we are in a static assertion; commit to any tentative
14361 parse. */
14362 if (cp_parser_parsing_tentatively (parser))
14363 cp_parser_commit_to_tentative_parse (parser);
14364
14365 /* Parse the `(' starting the static assertion condition. */
14366 matching_parens parens;
14367 parens.require_open (parser);
14368
14369 /* Parse the constant-expression. Allow a non-constant expression
14370 here in order to give better diagnostics in finish_static_assert. */
14371 condition =
14372 cp_parser_constant_expression (parser,
14373 /*allow_non_constant_p=*/true,
14374 /*non_constant_p=*/&dummy);
14375
14376 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14377 {
14378 if (cxx_dialect < cxx17)
14379 pedwarn (input_location, OPT_Wpedantic,
14380 "static_assert without a message "
14381 "only available with -std=c++17 or -std=gnu++17");
14382 /* Eat the ')' */
14383 cp_lexer_consume_token (parser->lexer);
14384 message = build_string (1, "");
14385 TREE_TYPE (message) = char_array_type_node;
14386 fix_string_type (message);
14387 }
14388 else
14389 {
14390 /* Parse the separating `,'. */
14391 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14392
14393 /* Parse the string-literal message. */
14394 message = cp_parser_string_literal (parser,
14395 /*translate=*/false,
14396 /*wide_ok=*/true);
14397
14398 /* A `)' completes the static assertion. */
14399 if (!parens.require_close (parser))
14400 cp_parser_skip_to_closing_parenthesis (parser,
14401 /*recovering=*/true,
14402 /*or_comma=*/false,
14403 /*consume_paren=*/true);
14404 }
14405
14406 /* A semicolon terminates the declaration. */
14407 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14408
14409 /* Get the location for the static assertion. Use that of the
14410 condition if available, otherwise, use that of the "static_assert"
14411 token. */
14412 location_t assert_loc = condition.get_location ();
14413 if (assert_loc == UNKNOWN_LOCATION)
14414 assert_loc = token_loc;
14415
14416 /* Complete the static assertion, which may mean either processing
14417 the static assert now or saving it for template instantiation. */
14418 finish_static_assert (condition, message, assert_loc, member_p);
14419 }
14420
14421 /* Parse the expression in decltype ( expression ). */
14422
14423 static tree
14424 cp_parser_decltype_expr (cp_parser *parser,
14425 bool &id_expression_or_member_access_p)
14426 {
14427 cp_token *id_expr_start_token;
14428 tree expr;
14429
14430 /* Since we're going to preserve any side-effects from this parse, set up a
14431 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14432 in the expression. */
14433 tentative_firewall firewall (parser);
14434
14435 /* First, try parsing an id-expression. */
14436 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14437 cp_parser_parse_tentatively (parser);
14438 expr = cp_parser_id_expression (parser,
14439 /*template_keyword_p=*/false,
14440 /*check_dependency_p=*/true,
14441 /*template_p=*/NULL,
14442 /*declarator_p=*/false,
14443 /*optional_p=*/false);
14444
14445 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14446 {
14447 bool non_integral_constant_expression_p = false;
14448 tree id_expression = expr;
14449 cp_id_kind idk;
14450 const char *error_msg;
14451
14452 if (identifier_p (expr))
14453 /* Lookup the name we got back from the id-expression. */
14454 expr = cp_parser_lookup_name_simple (parser, expr,
14455 id_expr_start_token->location);
14456
14457 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14458 /* A template without args is not a complete id-expression. */
14459 expr = error_mark_node;
14460
14461 if (expr
14462 && expr != error_mark_node
14463 && TREE_CODE (expr) != TYPE_DECL
14464 && (TREE_CODE (expr) != BIT_NOT_EXPR
14465 || !TYPE_P (TREE_OPERAND (expr, 0)))
14466 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14467 {
14468 /* Complete lookup of the id-expression. */
14469 expr = (finish_id_expression
14470 (id_expression, expr, parser->scope, &idk,
14471 /*integral_constant_expression_p=*/false,
14472 /*allow_non_integral_constant_expression_p=*/true,
14473 &non_integral_constant_expression_p,
14474 /*template_p=*/false,
14475 /*done=*/true,
14476 /*address_p=*/false,
14477 /*template_arg_p=*/false,
14478 &error_msg,
14479 id_expr_start_token->location));
14480
14481 if (expr == error_mark_node)
14482 /* We found an id-expression, but it was something that we
14483 should not have found. This is an error, not something
14484 we can recover from, so note that we found an
14485 id-expression and we'll recover as gracefully as
14486 possible. */
14487 id_expression_or_member_access_p = true;
14488 }
14489
14490 if (expr
14491 && expr != error_mark_node
14492 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14493 /* We have an id-expression. */
14494 id_expression_or_member_access_p = true;
14495 }
14496
14497 if (!id_expression_or_member_access_p)
14498 {
14499 /* Abort the id-expression parse. */
14500 cp_parser_abort_tentative_parse (parser);
14501
14502 /* Parsing tentatively, again. */
14503 cp_parser_parse_tentatively (parser);
14504
14505 /* Parse a class member access. */
14506 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14507 /*cast_p=*/false, /*decltype*/true,
14508 /*member_access_only_p=*/true, NULL);
14509
14510 if (expr
14511 && expr != error_mark_node
14512 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14513 /* We have an id-expression. */
14514 id_expression_or_member_access_p = true;
14515 }
14516
14517 if (id_expression_or_member_access_p)
14518 /* We have parsed the complete id-expression or member access. */
14519 cp_parser_parse_definitely (parser);
14520 else
14521 {
14522 /* Abort our attempt to parse an id-expression or member access
14523 expression. */
14524 cp_parser_abort_tentative_parse (parser);
14525
14526 /* Commit to the tentative_firewall so we get syntax errors. */
14527 cp_parser_commit_to_tentative_parse (parser);
14528
14529 /* Parse a full expression. */
14530 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14531 /*decltype_p=*/true);
14532 }
14533
14534 return expr;
14535 }
14536
14537 /* Parse a `decltype' type. Returns the type.
14538
14539 simple-type-specifier:
14540 decltype ( expression )
14541 C++14 proposal:
14542 decltype ( auto ) */
14543
14544 static tree
14545 cp_parser_decltype (cp_parser *parser)
14546 {
14547 bool id_expression_or_member_access_p = false;
14548 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14549
14550 if (start_token->type == CPP_DECLTYPE)
14551 {
14552 /* Already parsed. */
14553 cp_lexer_consume_token (parser->lexer);
14554 return saved_checks_value (start_token->u.tree_check_value);
14555 }
14556
14557 /* Look for the `decltype' token. */
14558 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14559 return error_mark_node;
14560
14561 /* Parse the opening `('. */
14562 matching_parens parens;
14563 if (!parens.require_open (parser))
14564 return error_mark_node;
14565
14566 push_deferring_access_checks (dk_deferred);
14567
14568 tree expr = NULL_TREE;
14569
14570 if (cxx_dialect >= cxx14
14571 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14572 /* decltype (auto) */
14573 cp_lexer_consume_token (parser->lexer);
14574 else
14575 {
14576 /* decltype (expression) */
14577
14578 /* Types cannot be defined in a `decltype' expression. Save away the
14579 old message and set the new one. */
14580 const char *saved_message = parser->type_definition_forbidden_message;
14581 parser->type_definition_forbidden_message
14582 = G_("types may not be defined in %<decltype%> expressions");
14583
14584 /* The restrictions on constant-expressions do not apply inside
14585 decltype expressions. */
14586 bool saved_integral_constant_expression_p
14587 = parser->integral_constant_expression_p;
14588 bool saved_non_integral_constant_expression_p
14589 = parser->non_integral_constant_expression_p;
14590 parser->integral_constant_expression_p = false;
14591
14592 /* Within a parenthesized expression, a `>' token is always
14593 the greater-than operator. */
14594 bool saved_greater_than_is_operator_p
14595 = parser->greater_than_is_operator_p;
14596 parser->greater_than_is_operator_p = true;
14597
14598 /* Do not actually evaluate the expression. */
14599 ++cp_unevaluated_operand;
14600
14601 /* Do not warn about problems with the expression. */
14602 ++c_inhibit_evaluation_warnings;
14603
14604 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14605 STRIP_ANY_LOCATION_WRAPPER (expr);
14606
14607 /* Go back to evaluating expressions. */
14608 --cp_unevaluated_operand;
14609 --c_inhibit_evaluation_warnings;
14610
14611 /* The `>' token might be the end of a template-id or
14612 template-parameter-list now. */
14613 parser->greater_than_is_operator_p
14614 = saved_greater_than_is_operator_p;
14615
14616 /* Restore the old message and the integral constant expression
14617 flags. */
14618 parser->type_definition_forbidden_message = saved_message;
14619 parser->integral_constant_expression_p
14620 = saved_integral_constant_expression_p;
14621 parser->non_integral_constant_expression_p
14622 = saved_non_integral_constant_expression_p;
14623 }
14624
14625 /* Parse to the closing `)'. */
14626 if (!parens.require_close (parser))
14627 {
14628 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14629 /*consume_paren=*/true);
14630 pop_deferring_access_checks ();
14631 return error_mark_node;
14632 }
14633
14634 if (!expr)
14635 {
14636 /* Build auto. */
14637 expr = make_decltype_auto ();
14638 AUTO_IS_DECLTYPE (expr) = true;
14639 }
14640 else
14641 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14642 tf_warning_or_error);
14643
14644 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14645 it again. */
14646 start_token->type = CPP_DECLTYPE;
14647 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14648 start_token->u.tree_check_value->value = expr;
14649 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14650 start_token->keyword = RID_MAX;
14651 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14652
14653 pop_to_parent_deferring_access_checks ();
14654
14655 return expr;
14656 }
14657
14658 /* Special member functions [gram.special] */
14659
14660 /* Parse a conversion-function-id.
14661
14662 conversion-function-id:
14663 operator conversion-type-id
14664
14665 Returns an IDENTIFIER_NODE representing the operator. */
14666
14667 static tree
14668 cp_parser_conversion_function_id (cp_parser* parser)
14669 {
14670 tree type;
14671 tree saved_scope;
14672 tree saved_qualifying_scope;
14673 tree saved_object_scope;
14674 tree pushed_scope = NULL_TREE;
14675
14676 /* Look for the `operator' token. */
14677 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14678 return error_mark_node;
14679 /* When we parse the conversion-type-id, the current scope will be
14680 reset. However, we need that information in able to look up the
14681 conversion function later, so we save it here. */
14682 saved_scope = parser->scope;
14683 saved_qualifying_scope = parser->qualifying_scope;
14684 saved_object_scope = parser->object_scope;
14685 /* We must enter the scope of the class so that the names of
14686 entities declared within the class are available in the
14687 conversion-type-id. For example, consider:
14688
14689 struct S {
14690 typedef int I;
14691 operator I();
14692 };
14693
14694 S::operator I() { ... }
14695
14696 In order to see that `I' is a type-name in the definition, we
14697 must be in the scope of `S'. */
14698 if (saved_scope)
14699 pushed_scope = push_scope (saved_scope);
14700 /* Parse the conversion-type-id. */
14701 type = cp_parser_conversion_type_id (parser);
14702 /* Leave the scope of the class, if any. */
14703 if (pushed_scope)
14704 pop_scope (pushed_scope);
14705 /* Restore the saved scope. */
14706 parser->scope = saved_scope;
14707 parser->qualifying_scope = saved_qualifying_scope;
14708 parser->object_scope = saved_object_scope;
14709 /* If the TYPE is invalid, indicate failure. */
14710 if (type == error_mark_node)
14711 return error_mark_node;
14712 return make_conv_op_name (type);
14713 }
14714
14715 /* Parse a conversion-type-id:
14716
14717 conversion-type-id:
14718 type-specifier-seq conversion-declarator [opt]
14719
14720 Returns the TYPE specified. */
14721
14722 static tree
14723 cp_parser_conversion_type_id (cp_parser* parser)
14724 {
14725 tree attributes;
14726 cp_decl_specifier_seq type_specifiers;
14727 cp_declarator *declarator;
14728 tree type_specified;
14729 const char *saved_message;
14730
14731 /* Parse the attributes. */
14732 attributes = cp_parser_attributes_opt (parser);
14733
14734 saved_message = parser->type_definition_forbidden_message;
14735 parser->type_definition_forbidden_message
14736 = G_("types may not be defined in a conversion-type-id");
14737
14738 /* Parse the type-specifiers. */
14739 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
14740 /*is_declaration=*/false,
14741 /*is_trailing_return=*/false,
14742 &type_specifiers);
14743
14744 parser->type_definition_forbidden_message = saved_message;
14745
14746 /* If that didn't work, stop. */
14747 if (type_specifiers.type == error_mark_node)
14748 return error_mark_node;
14749 /* Parse the conversion-declarator. */
14750 declarator = cp_parser_conversion_declarator_opt (parser);
14751
14752 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14753 /*initialized=*/0, &attributes);
14754 if (attributes)
14755 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14756
14757 /* Don't give this error when parsing tentatively. This happens to
14758 work because we always parse this definitively once. */
14759 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14760 && type_uses_auto (type_specified))
14761 {
14762 if (cxx_dialect < cxx14)
14763 {
14764 error ("invalid use of %<auto%> in conversion operator");
14765 return error_mark_node;
14766 }
14767 else if (template_parm_scope_p ())
14768 warning (0, "use of %<auto%> in member template "
14769 "conversion operator can never be deduced");
14770 }
14771
14772 return type_specified;
14773 }
14774
14775 /* Parse an (optional) conversion-declarator.
14776
14777 conversion-declarator:
14778 ptr-operator conversion-declarator [opt]
14779
14780 */
14781
14782 static cp_declarator *
14783 cp_parser_conversion_declarator_opt (cp_parser* parser)
14784 {
14785 enum tree_code code;
14786 tree class_type, std_attributes = NULL_TREE;
14787 cp_cv_quals cv_quals;
14788
14789 /* We don't know if there's a ptr-operator next, or not. */
14790 cp_parser_parse_tentatively (parser);
14791 /* Try the ptr-operator. */
14792 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14793 &std_attributes);
14794 /* If it worked, look for more conversion-declarators. */
14795 if (cp_parser_parse_definitely (parser))
14796 {
14797 cp_declarator *declarator;
14798
14799 /* Parse another optional declarator. */
14800 declarator = cp_parser_conversion_declarator_opt (parser);
14801
14802 declarator = cp_parser_make_indirect_declarator
14803 (code, class_type, cv_quals, declarator, std_attributes);
14804
14805 return declarator;
14806 }
14807
14808 return NULL;
14809 }
14810
14811 /* Parse an (optional) ctor-initializer.
14812
14813 ctor-initializer:
14814 : mem-initializer-list */
14815
14816 static void
14817 cp_parser_ctor_initializer_opt (cp_parser* parser)
14818 {
14819 /* If the next token is not a `:', then there is no
14820 ctor-initializer. */
14821 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14822 {
14823 /* Do default initialization of any bases and members. */
14824 if (DECL_CONSTRUCTOR_P (current_function_decl))
14825 finish_mem_initializers (NULL_TREE);
14826 return;
14827 }
14828
14829 /* Consume the `:' token. */
14830 cp_lexer_consume_token (parser->lexer);
14831 /* And the mem-initializer-list. */
14832 cp_parser_mem_initializer_list (parser);
14833 }
14834
14835 /* Parse a mem-initializer-list.
14836
14837 mem-initializer-list:
14838 mem-initializer ... [opt]
14839 mem-initializer ... [opt] , mem-initializer-list */
14840
14841 static void
14842 cp_parser_mem_initializer_list (cp_parser* parser)
14843 {
14844 tree mem_initializer_list = NULL_TREE;
14845 tree target_ctor = error_mark_node;
14846 cp_token *token = cp_lexer_peek_token (parser->lexer);
14847
14848 /* Let the semantic analysis code know that we are starting the
14849 mem-initializer-list. */
14850 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14851 error_at (token->location,
14852 "only constructors take member initializers");
14853
14854 /* Loop through the list. */
14855 while (true)
14856 {
14857 tree mem_initializer;
14858
14859 token = cp_lexer_peek_token (parser->lexer);
14860 /* Parse the mem-initializer. */
14861 mem_initializer = cp_parser_mem_initializer (parser);
14862 /* If the next token is a `...', we're expanding member initializers. */
14863 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14864 if (ellipsis
14865 || (mem_initializer != error_mark_node
14866 && check_for_bare_parameter_packs (TREE_PURPOSE
14867 (mem_initializer))))
14868 {
14869 /* Consume the `...'. */
14870 if (ellipsis)
14871 cp_lexer_consume_token (parser->lexer);
14872
14873 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14874 can be expanded but members cannot. */
14875 if (mem_initializer != error_mark_node
14876 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14877 {
14878 error_at (token->location,
14879 "cannot expand initializer for member %qD",
14880 TREE_PURPOSE (mem_initializer));
14881 mem_initializer = error_mark_node;
14882 }
14883
14884 /* Construct the pack expansion type. */
14885 if (mem_initializer != error_mark_node)
14886 mem_initializer = make_pack_expansion (mem_initializer);
14887 }
14888 if (target_ctor != error_mark_node
14889 && mem_initializer != error_mark_node)
14890 {
14891 error ("mem-initializer for %qD follows constructor delegation",
14892 TREE_PURPOSE (mem_initializer));
14893 mem_initializer = error_mark_node;
14894 }
14895 /* Look for a target constructor. */
14896 if (mem_initializer != error_mark_node
14897 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14898 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14899 {
14900 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14901 if (mem_initializer_list)
14902 {
14903 error ("constructor delegation follows mem-initializer for %qD",
14904 TREE_PURPOSE (mem_initializer_list));
14905 mem_initializer = error_mark_node;
14906 }
14907 target_ctor = mem_initializer;
14908 }
14909 /* Add it to the list, unless it was erroneous. */
14910 if (mem_initializer != error_mark_node)
14911 {
14912 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14913 mem_initializer_list = mem_initializer;
14914 }
14915 /* If the next token is not a `,', we're done. */
14916 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14917 break;
14918 /* Consume the `,' token. */
14919 cp_lexer_consume_token (parser->lexer);
14920 }
14921
14922 /* Perform semantic analysis. */
14923 if (DECL_CONSTRUCTOR_P (current_function_decl))
14924 finish_mem_initializers (mem_initializer_list);
14925 }
14926
14927 /* Parse a mem-initializer.
14928
14929 mem-initializer:
14930 mem-initializer-id ( expression-list [opt] )
14931 mem-initializer-id braced-init-list
14932
14933 GNU extension:
14934
14935 mem-initializer:
14936 ( expression-list [opt] )
14937
14938 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14939 class) or FIELD_DECL (for a non-static data member) to initialize;
14940 the TREE_VALUE is the expression-list. An empty initialization
14941 list is represented by void_list_node. */
14942
14943 static tree
14944 cp_parser_mem_initializer (cp_parser* parser)
14945 {
14946 tree mem_initializer_id;
14947 tree expression_list;
14948 tree member;
14949 cp_token *token = cp_lexer_peek_token (parser->lexer);
14950
14951 /* Find out what is being initialized. */
14952 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14953 {
14954 permerror (token->location,
14955 "anachronistic old-style base class initializer");
14956 mem_initializer_id = NULL_TREE;
14957 }
14958 else
14959 {
14960 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14961 if (mem_initializer_id == error_mark_node)
14962 return mem_initializer_id;
14963 }
14964 member = expand_member_init (mem_initializer_id);
14965 if (member && !DECL_P (member))
14966 in_base_initializer = 1;
14967
14968 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14969 {
14970 bool expr_non_constant_p;
14971 cp_lexer_set_source_position (parser->lexer);
14972 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14973 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14974 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14975 expression_list = build_tree_list (NULL_TREE, expression_list);
14976 }
14977 else
14978 {
14979 vec<tree, va_gc> *vec;
14980 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14981 /*cast_p=*/false,
14982 /*allow_expansion_p=*/true,
14983 /*non_constant_p=*/NULL,
14984 /*close_paren_loc=*/NULL,
14985 /*wrap_locations_p=*/true);
14986 if (vec == NULL)
14987 return error_mark_node;
14988 expression_list = build_tree_list_vec (vec);
14989 release_tree_vector (vec);
14990 }
14991
14992 if (expression_list == error_mark_node)
14993 return error_mark_node;
14994 if (!expression_list)
14995 expression_list = void_type_node;
14996
14997 in_base_initializer = 0;
14998
14999 return member ? build_tree_list (member, expression_list) : error_mark_node;
15000 }
15001
15002 /* Parse a mem-initializer-id.
15003
15004 mem-initializer-id:
15005 :: [opt] nested-name-specifier [opt] class-name
15006 decltype-specifier (C++11)
15007 identifier
15008
15009 Returns a TYPE indicating the class to be initialized for the first
15010 production (and the second in C++11). Returns an IDENTIFIER_NODE
15011 indicating the data member to be initialized for the last production. */
15012
15013 static tree
15014 cp_parser_mem_initializer_id (cp_parser* parser)
15015 {
15016 bool global_scope_p;
15017 bool nested_name_specifier_p;
15018 bool template_p = false;
15019 tree id;
15020
15021 cp_token *token = cp_lexer_peek_token (parser->lexer);
15022
15023 /* `typename' is not allowed in this context ([temp.res]). */
15024 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15025 {
15026 error_at (token->location,
15027 "keyword %<typename%> not allowed in this context (a qualified "
15028 "member initializer is implicitly a type)");
15029 cp_lexer_consume_token (parser->lexer);
15030 }
15031 /* Look for the optional `::' operator. */
15032 global_scope_p
15033 = (cp_parser_global_scope_opt (parser,
15034 /*current_scope_valid_p=*/false)
15035 != NULL_TREE);
15036 /* Look for the optional nested-name-specifier. The simplest way to
15037 implement:
15038
15039 [temp.res]
15040
15041 The keyword `typename' is not permitted in a base-specifier or
15042 mem-initializer; in these contexts a qualified name that
15043 depends on a template-parameter is implicitly assumed to be a
15044 type name.
15045
15046 is to assume that we have seen the `typename' keyword at this
15047 point. */
15048 nested_name_specifier_p
15049 = (cp_parser_nested_name_specifier_opt (parser,
15050 /*typename_keyword_p=*/true,
15051 /*check_dependency_p=*/true,
15052 /*type_p=*/true,
15053 /*is_declaration=*/true)
15054 != NULL_TREE);
15055 if (nested_name_specifier_p)
15056 template_p = cp_parser_optional_template_keyword (parser);
15057 /* If there is a `::' operator or a nested-name-specifier, then we
15058 are definitely looking for a class-name. */
15059 if (global_scope_p || nested_name_specifier_p)
15060 return cp_parser_class_name (parser,
15061 /*typename_keyword_p=*/true,
15062 /*template_keyword_p=*/template_p,
15063 typename_type,
15064 /*check_dependency_p=*/true,
15065 /*class_head_p=*/false,
15066 /*is_declaration=*/true);
15067 /* Otherwise, we could also be looking for an ordinary identifier. */
15068 cp_parser_parse_tentatively (parser);
15069 if (cp_lexer_next_token_is_decltype (parser->lexer))
15070 /* Try a decltype-specifier. */
15071 id = cp_parser_decltype (parser);
15072 else
15073 /* Otherwise, try a class-name. */
15074 id = cp_parser_class_name (parser,
15075 /*typename_keyword_p=*/true,
15076 /*template_keyword_p=*/false,
15077 none_type,
15078 /*check_dependency_p=*/true,
15079 /*class_head_p=*/false,
15080 /*is_declaration=*/true);
15081 /* If we found one, we're done. */
15082 if (cp_parser_parse_definitely (parser))
15083 return id;
15084 /* Otherwise, look for an ordinary identifier. */
15085 return cp_parser_identifier (parser);
15086 }
15087
15088 /* Overloading [gram.over] */
15089
15090 /* Parse an operator-function-id.
15091
15092 operator-function-id:
15093 operator operator
15094
15095 Returns an IDENTIFIER_NODE for the operator which is a
15096 human-readable spelling of the identifier, e.g., `operator +'. */
15097
15098 static cp_expr
15099 cp_parser_operator_function_id (cp_parser* parser)
15100 {
15101 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
15102 /* Look for the `operator' keyword. */
15103 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15104 return error_mark_node;
15105 /* And then the name of the operator itself. */
15106 return cp_parser_operator (parser, start_loc);
15107 }
15108
15109 /* Return an identifier node for a user-defined literal operator.
15110 The suffix identifier is chained to the operator name identifier. */
15111
15112 tree
15113 cp_literal_operator_id (const char* name)
15114 {
15115 tree identifier;
15116 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
15117 + strlen (name) + 10);
15118 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
15119 identifier = get_identifier (buffer);
15120
15121 return identifier;
15122 }
15123
15124 /* Parse an operator.
15125
15126 operator:
15127 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15128 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15129 || ++ -- , ->* -> () []
15130
15131 GNU Extensions:
15132
15133 operator:
15134 <? >? <?= >?=
15135
15136 Returns an IDENTIFIER_NODE for the operator which is a
15137 human-readable spelling of the identifier, e.g., `operator +'. */
15138
15139 static cp_expr
15140 cp_parser_operator (cp_parser* parser, location_t start_loc)
15141 {
15142 tree id = NULL_TREE;
15143 cp_token *token;
15144 bool utf8 = false;
15145
15146 /* Peek at the next token. */
15147 token = cp_lexer_peek_token (parser->lexer);
15148
15149 location_t end_loc = token->location;
15150
15151 /* Figure out which operator we have. */
15152 enum tree_code op = ERROR_MARK;
15153 bool assop = false;
15154 bool consumed = false;
15155 switch (token->type)
15156 {
15157 case CPP_KEYWORD:
15158 {
15159 /* The keyword should be either `new' or `delete'. */
15160 if (token->keyword == RID_NEW)
15161 op = NEW_EXPR;
15162 else if (token->keyword == RID_DELETE)
15163 op = DELETE_EXPR;
15164 else
15165 break;
15166
15167 /* Consume the `new' or `delete' token. */
15168 end_loc = cp_lexer_consume_token (parser->lexer)->location;
15169
15170 /* Peek at the next token. */
15171 token = cp_lexer_peek_token (parser->lexer);
15172 /* If it's a `[' token then this is the array variant of the
15173 operator. */
15174 if (token->type == CPP_OPEN_SQUARE)
15175 {
15176 /* Consume the `[' token. */
15177 cp_lexer_consume_token (parser->lexer);
15178 /* Look for the `]' token. */
15179 if (cp_token *close_token
15180 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15181 end_loc = close_token->location;
15182 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
15183 }
15184 consumed = true;
15185 break;
15186 }
15187
15188 case CPP_PLUS:
15189 op = PLUS_EXPR;
15190 break;
15191
15192 case CPP_MINUS:
15193 op = MINUS_EXPR;
15194 break;
15195
15196 case CPP_MULT:
15197 op = MULT_EXPR;
15198 break;
15199
15200 case CPP_DIV:
15201 op = TRUNC_DIV_EXPR;
15202 break;
15203
15204 case CPP_MOD:
15205 op = TRUNC_MOD_EXPR;
15206 break;
15207
15208 case CPP_XOR:
15209 op = BIT_XOR_EXPR;
15210 break;
15211
15212 case CPP_AND:
15213 op = BIT_AND_EXPR;
15214 break;
15215
15216 case CPP_OR:
15217 op = BIT_IOR_EXPR;
15218 break;
15219
15220 case CPP_COMPL:
15221 op = BIT_NOT_EXPR;
15222 break;
15223
15224 case CPP_NOT:
15225 op = TRUTH_NOT_EXPR;
15226 break;
15227
15228 case CPP_EQ:
15229 assop = true;
15230 op = NOP_EXPR;
15231 break;
15232
15233 case CPP_LESS:
15234 op = LT_EXPR;
15235 break;
15236
15237 case CPP_GREATER:
15238 op = GT_EXPR;
15239 break;
15240
15241 case CPP_PLUS_EQ:
15242 assop = true;
15243 op = PLUS_EXPR;
15244 break;
15245
15246 case CPP_MINUS_EQ:
15247 assop = true;
15248 op = MINUS_EXPR;
15249 break;
15250
15251 case CPP_MULT_EQ:
15252 assop = true;
15253 op = MULT_EXPR;
15254 break;
15255
15256 case CPP_DIV_EQ:
15257 assop = true;
15258 op = TRUNC_DIV_EXPR;
15259 break;
15260
15261 case CPP_MOD_EQ:
15262 assop = true;
15263 op = TRUNC_MOD_EXPR;
15264 break;
15265
15266 case CPP_XOR_EQ:
15267 assop = true;
15268 op = BIT_XOR_EXPR;
15269 break;
15270
15271 case CPP_AND_EQ:
15272 assop = true;
15273 op = BIT_AND_EXPR;
15274 break;
15275
15276 case CPP_OR_EQ:
15277 assop = true;
15278 op = BIT_IOR_EXPR;
15279 break;
15280
15281 case CPP_LSHIFT:
15282 op = LSHIFT_EXPR;
15283 break;
15284
15285 case CPP_RSHIFT:
15286 op = RSHIFT_EXPR;
15287 break;
15288
15289 case CPP_LSHIFT_EQ:
15290 assop = true;
15291 op = LSHIFT_EXPR;
15292 break;
15293
15294 case CPP_RSHIFT_EQ:
15295 assop = true;
15296 op = RSHIFT_EXPR;
15297 break;
15298
15299 case CPP_EQ_EQ:
15300 op = EQ_EXPR;
15301 break;
15302
15303 case CPP_NOT_EQ:
15304 op = NE_EXPR;
15305 break;
15306
15307 case CPP_LESS_EQ:
15308 op = LE_EXPR;
15309 break;
15310
15311 case CPP_GREATER_EQ:
15312 op = GE_EXPR;
15313 break;
15314
15315 case CPP_AND_AND:
15316 op = TRUTH_ANDIF_EXPR;
15317 break;
15318
15319 case CPP_OR_OR:
15320 op = TRUTH_ORIF_EXPR;
15321 break;
15322
15323 case CPP_PLUS_PLUS:
15324 op = POSTINCREMENT_EXPR;
15325 break;
15326
15327 case CPP_MINUS_MINUS:
15328 op = PREDECREMENT_EXPR;
15329 break;
15330
15331 case CPP_COMMA:
15332 op = COMPOUND_EXPR;
15333 break;
15334
15335 case CPP_DEREF_STAR:
15336 op = MEMBER_REF;
15337 break;
15338
15339 case CPP_DEREF:
15340 op = COMPONENT_REF;
15341 break;
15342
15343 case CPP_OPEN_PAREN:
15344 {
15345 /* Consume the `('. */
15346 matching_parens parens;
15347 parens.consume_open (parser);
15348 /* Look for the matching `)'. */
15349 token = parens.require_close (parser);
15350 if (token)
15351 end_loc = token->location;
15352 op = CALL_EXPR;
15353 consumed = true;
15354 break;
15355 }
15356
15357 case CPP_OPEN_SQUARE:
15358 /* Consume the `['. */
15359 cp_lexer_consume_token (parser->lexer);
15360 /* Look for the matching `]'. */
15361 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15362 if (token)
15363 end_loc = token->location;
15364 op = ARRAY_REF;
15365 consumed = true;
15366 break;
15367
15368 case CPP_UTF8STRING:
15369 case CPP_UTF8STRING_USERDEF:
15370 utf8 = true;
15371 /* FALLTHRU */
15372 case CPP_STRING:
15373 case CPP_WSTRING:
15374 case CPP_STRING16:
15375 case CPP_STRING32:
15376 case CPP_STRING_USERDEF:
15377 case CPP_WSTRING_USERDEF:
15378 case CPP_STRING16_USERDEF:
15379 case CPP_STRING32_USERDEF:
15380 {
15381 cp_expr str;
15382 tree string_tree;
15383 int sz, len;
15384
15385 if (cxx_dialect == cxx98)
15386 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15387
15388 /* Consume the string. */
15389 str = cp_parser_string_literal (parser, /*translate=*/true,
15390 /*wide_ok=*/true, /*lookup_udlit=*/false);
15391 if (str == error_mark_node)
15392 return error_mark_node;
15393 else if (TREE_CODE (str) == USERDEF_LITERAL)
15394 {
15395 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
15396 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
15397 end_loc = str.get_location ();
15398 }
15399 else
15400 {
15401 string_tree = str;
15402 /* Look for the suffix identifier. */
15403 token = cp_lexer_peek_token (parser->lexer);
15404 if (token->type == CPP_NAME)
15405 {
15406 id = cp_parser_identifier (parser);
15407 end_loc = token->location;
15408 }
15409 else if (token->type == CPP_KEYWORD)
15410 {
15411 error ("unexpected keyword;"
15412 " remove space between quotes and suffix identifier");
15413 return error_mark_node;
15414 }
15415 else
15416 {
15417 error ("expected suffix identifier");
15418 return error_mark_node;
15419 }
15420 }
15421 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15422 (TREE_TYPE (TREE_TYPE (string_tree))));
15423 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15424 if (len != 0)
15425 {
15426 error ("expected empty string after %<operator%> keyword");
15427 return error_mark_node;
15428 }
15429 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15430 != char_type_node)
15431 {
15432 error ("invalid encoding prefix in literal operator");
15433 return error_mark_node;
15434 }
15435 if (id != error_mark_node)
15436 {
15437 const char *name = IDENTIFIER_POINTER (id);
15438 id = cp_literal_operator_id (name);
15439 }
15440 /* Generate a location of the form:
15441 "" _suffix_identifier
15442 ^~~~~~~~~~~~~~~~~~~~~
15443 with caret == start at the start token, finish at the end of the
15444 suffix identifier. */
15445 location_t finish_loc
15446 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15447 location_t combined_loc
15448 = make_location (start_loc, start_loc, finish_loc);
15449 return cp_expr (id, combined_loc);
15450 }
15451
15452 default:
15453 /* Anything else is an error. */
15454 break;
15455 }
15456
15457 /* If we have selected an identifier, we need to consume the
15458 operator token. */
15459 if (op != ERROR_MARK)
15460 {
15461 id = ovl_op_identifier (assop, op);
15462 if (!consumed)
15463 cp_lexer_consume_token (parser->lexer);
15464 }
15465 /* Otherwise, no valid operator name was present. */
15466 else
15467 {
15468 cp_parser_error (parser, "expected operator");
15469 id = error_mark_node;
15470 }
15471
15472 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15473 return cp_expr (id, start_loc);
15474 }
15475
15476 /* Parse a template-declaration.
15477
15478 template-declaration:
15479 export [opt] template < template-parameter-list > declaration
15480
15481 If MEMBER_P is TRUE, this template-declaration occurs within a
15482 class-specifier.
15483
15484 The grammar rule given by the standard isn't correct. What
15485 is really meant is:
15486
15487 template-declaration:
15488 export [opt] template-parameter-list-seq
15489 decl-specifier-seq [opt] init-declarator [opt] ;
15490 export [opt] template-parameter-list-seq
15491 function-definition
15492
15493 template-parameter-list-seq:
15494 template-parameter-list-seq [opt]
15495 template < template-parameter-list >
15496
15497 Concept Extensions:
15498
15499 template-parameter-list-seq:
15500 template < template-parameter-list > requires-clause [opt]
15501
15502 requires-clause:
15503 requires logical-or-expression */
15504
15505 static void
15506 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15507 {
15508 /* Check for `export'. */
15509 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15510 {
15511 /* Consume the `export' token. */
15512 cp_lexer_consume_token (parser->lexer);
15513 /* Warn that we do not support `export'. */
15514 warning (0, "keyword %<export%> not implemented, and will be ignored");
15515 }
15516
15517 cp_parser_template_declaration_after_export (parser, member_p);
15518 }
15519
15520 /* Parse a template-parameter-list.
15521
15522 template-parameter-list:
15523 template-parameter
15524 template-parameter-list , template-parameter
15525
15526 Returns a TREE_LIST. Each node represents a template parameter.
15527 The nodes are connected via their TREE_CHAINs. */
15528
15529 static tree
15530 cp_parser_template_parameter_list (cp_parser* parser)
15531 {
15532 tree parameter_list = NULL_TREE;
15533
15534 /* Don't create wrapper nodes within a template-parameter-list,
15535 since we don't want to have different types based on the
15536 spelling location of constants and decls within them. */
15537 auto_suppress_location_wrappers sentinel;
15538
15539 begin_template_parm_list ();
15540
15541 /* The loop below parses the template parms. We first need to know
15542 the total number of template parms to be able to compute proper
15543 canonical types of each dependent type. So after the loop, when
15544 we know the total number of template parms,
15545 end_template_parm_list computes the proper canonical types and
15546 fixes up the dependent types accordingly. */
15547 while (true)
15548 {
15549 tree parameter;
15550 bool is_non_type;
15551 bool is_parameter_pack;
15552 location_t parm_loc;
15553
15554 /* Parse the template-parameter. */
15555 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15556 parameter = cp_parser_template_parameter (parser,
15557 &is_non_type,
15558 &is_parameter_pack);
15559 /* Add it to the list. */
15560 if (parameter != error_mark_node)
15561 parameter_list = process_template_parm (parameter_list,
15562 parm_loc,
15563 parameter,
15564 is_non_type,
15565 is_parameter_pack);
15566 else
15567 {
15568 tree err_parm = build_tree_list (parameter, parameter);
15569 parameter_list = chainon (parameter_list, err_parm);
15570 }
15571
15572 /* If the next token is not a `,', we're done. */
15573 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15574 break;
15575 /* Otherwise, consume the `,' token. */
15576 cp_lexer_consume_token (parser->lexer);
15577 }
15578
15579 return end_template_parm_list (parameter_list);
15580 }
15581
15582 /* Parse a introduction-list.
15583
15584 introduction-list:
15585 introduced-parameter
15586 introduction-list , introduced-parameter
15587
15588 introduced-parameter:
15589 ...[opt] identifier
15590
15591 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15592 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15593 WILDCARD_DECL will also have DECL_NAME set and token location in
15594 DECL_SOURCE_LOCATION. */
15595
15596 static tree
15597 cp_parser_introduction_list (cp_parser *parser)
15598 {
15599 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15600
15601 while (true)
15602 {
15603 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15604 if (is_pack)
15605 cp_lexer_consume_token (parser->lexer);
15606
15607 tree identifier = cp_parser_identifier (parser);
15608 if (identifier == error_mark_node)
15609 break;
15610
15611 /* Build placeholder. */
15612 tree parm = build_nt (WILDCARD_DECL);
15613 DECL_SOURCE_LOCATION (parm)
15614 = cp_lexer_peek_token (parser->lexer)->location;
15615 DECL_NAME (parm) = identifier;
15616 WILDCARD_PACK_P (parm) = is_pack;
15617 vec_safe_push (introduction_vec, parm);
15618
15619 /* If the next token is not a `,', we're done. */
15620 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15621 break;
15622 /* Otherwise, consume the `,' token. */
15623 cp_lexer_consume_token (parser->lexer);
15624 }
15625
15626 /* Convert the vec into a TREE_VEC. */
15627 tree introduction_list = make_tree_vec (introduction_vec->length ());
15628 unsigned int n;
15629 tree parm;
15630 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15631 TREE_VEC_ELT (introduction_list, n) = parm;
15632
15633 release_tree_vector (introduction_vec);
15634 return introduction_list;
15635 }
15636
15637 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15638 is an abstract declarator. */
15639
15640 static inline cp_declarator*
15641 get_id_declarator (cp_declarator *declarator)
15642 {
15643 cp_declarator *d = declarator;
15644 while (d && d->kind != cdk_id)
15645 d = d->declarator;
15646 return d;
15647 }
15648
15649 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15650 is an abstract declarator. */
15651
15652 static inline tree
15653 get_unqualified_id (cp_declarator *declarator)
15654 {
15655 declarator = get_id_declarator (declarator);
15656 if (declarator)
15657 return declarator->u.id.unqualified_name;
15658 else
15659 return NULL_TREE;
15660 }
15661
15662 /* Returns true if DECL represents a constrained-parameter. */
15663
15664 static inline bool
15665 is_constrained_parameter (tree decl)
15666 {
15667 return (decl
15668 && TREE_CODE (decl) == TYPE_DECL
15669 && CONSTRAINED_PARM_CONCEPT (decl)
15670 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15671 }
15672
15673 /* Returns true if PARM declares a constrained-parameter. */
15674
15675 static inline bool
15676 is_constrained_parameter (cp_parameter_declarator *parm)
15677 {
15678 return is_constrained_parameter (parm->decl_specifiers.type);
15679 }
15680
15681 /* Check that the type parameter is only a declarator-id, and that its
15682 type is not cv-qualified. */
15683
15684 bool
15685 cp_parser_check_constrained_type_parm (cp_parser *parser,
15686 cp_parameter_declarator *parm)
15687 {
15688 if (!parm->declarator)
15689 return true;
15690
15691 if (parm->declarator->kind != cdk_id)
15692 {
15693 cp_parser_error (parser, "invalid constrained type parameter");
15694 return false;
15695 }
15696
15697 /* Don't allow cv-qualified type parameters. */
15698 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15699 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15700 {
15701 cp_parser_error (parser, "cv-qualified type parameter");
15702 return false;
15703 }
15704
15705 return true;
15706 }
15707
15708 /* Finish parsing/processing a template type parameter and checking
15709 various restrictions. */
15710
15711 static inline tree
15712 cp_parser_constrained_type_template_parm (cp_parser *parser,
15713 tree id,
15714 cp_parameter_declarator* parmdecl)
15715 {
15716 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15717 return finish_template_type_parm (class_type_node, id);
15718 else
15719 return error_mark_node;
15720 }
15721
15722 static tree
15723 finish_constrained_template_template_parm (tree proto, tree id)
15724 {
15725 /* FIXME: This should probably be copied, and we may need to adjust
15726 the template parameter depths. */
15727 tree saved_parms = current_template_parms;
15728 begin_template_parm_list ();
15729 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15730 end_template_parm_list ();
15731
15732 tree parm = finish_template_template_parm (class_type_node, id);
15733 current_template_parms = saved_parms;
15734
15735 return parm;
15736 }
15737
15738 /* Finish parsing/processing a template template parameter by borrowing
15739 the template parameter list from the prototype parameter. */
15740
15741 static tree
15742 cp_parser_constrained_template_template_parm (cp_parser *parser,
15743 tree proto,
15744 tree id,
15745 cp_parameter_declarator *parmdecl)
15746 {
15747 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15748 return error_mark_node;
15749 return finish_constrained_template_template_parm (proto, id);
15750 }
15751
15752 /* Create a new non-type template parameter from the given PARM
15753 declarator. */
15754
15755 static tree
15756 constrained_non_type_template_parm (bool *is_non_type,
15757 cp_parameter_declarator *parm)
15758 {
15759 *is_non_type = true;
15760 cp_declarator *decl = parm->declarator;
15761 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15762 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15763 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15764 }
15765
15766 /* Build a constrained template parameter based on the PARMDECL
15767 declarator. The type of PARMDECL is the constrained type, which
15768 refers to the prototype template parameter that ultimately
15769 specifies the type of the declared parameter. */
15770
15771 static tree
15772 finish_constrained_parameter (cp_parser *parser,
15773 cp_parameter_declarator *parmdecl,
15774 bool *is_non_type,
15775 bool *is_parameter_pack)
15776 {
15777 tree decl = parmdecl->decl_specifiers.type;
15778 tree id = get_unqualified_id (parmdecl->declarator);
15779 tree def = parmdecl->default_argument;
15780 tree proto = DECL_INITIAL (decl);
15781
15782 /* A template parameter constrained by a variadic concept shall also
15783 be declared as a template parameter pack. */
15784 bool is_variadic = template_parameter_pack_p (proto);
15785 if (is_variadic && !*is_parameter_pack)
15786 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15787
15788 /* Build the parameter. Return an error if the declarator was invalid. */
15789 tree parm;
15790 if (TREE_CODE (proto) == TYPE_DECL)
15791 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15792 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15793 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15794 parmdecl);
15795 else
15796 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15797 if (parm == error_mark_node)
15798 return error_mark_node;
15799
15800 /* Finish the parameter decl and create a node attaching the
15801 default argument and constraint. */
15802 parm = build_tree_list (def, parm);
15803 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15804
15805 return parm;
15806 }
15807
15808 /* Returns true if the parsed type actually represents the declaration
15809 of a type template-parameter. */
15810
15811 static inline bool
15812 declares_constrained_type_template_parameter (tree type)
15813 {
15814 return (is_constrained_parameter (type)
15815 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15816 }
15817
15818
15819 /* Returns true if the parsed type actually represents the declaration of
15820 a template template-parameter. */
15821
15822 static bool
15823 declares_constrained_template_template_parameter (tree type)
15824 {
15825 return (is_constrained_parameter (type)
15826 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15827 }
15828
15829 /* Parse a default argument for a type template-parameter.
15830 Note that diagnostics are handled in cp_parser_template_parameter. */
15831
15832 static tree
15833 cp_parser_default_type_template_argument (cp_parser *parser)
15834 {
15835 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15836
15837 /* Consume the `=' token. */
15838 cp_lexer_consume_token (parser->lexer);
15839
15840 cp_token *token = cp_lexer_peek_token (parser->lexer);
15841
15842 /* Parse the default-argument. */
15843 push_deferring_access_checks (dk_no_deferred);
15844 tree default_argument = cp_parser_type_id (parser,
15845 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15846 NULL);
15847 pop_deferring_access_checks ();
15848
15849 if (flag_concepts && type_uses_auto (default_argument))
15850 {
15851 error_at (token->location,
15852 "invalid use of %<auto%> in default template argument");
15853 return error_mark_node;
15854 }
15855
15856 return default_argument;
15857 }
15858
15859 /* Parse a default argument for a template template-parameter. */
15860
15861 static tree
15862 cp_parser_default_template_template_argument (cp_parser *parser)
15863 {
15864 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15865
15866 bool is_template;
15867
15868 /* Consume the `='. */
15869 cp_lexer_consume_token (parser->lexer);
15870 /* Parse the id-expression. */
15871 push_deferring_access_checks (dk_no_deferred);
15872 /* save token before parsing the id-expression, for error
15873 reporting */
15874 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15875 tree default_argument
15876 = cp_parser_id_expression (parser,
15877 /*template_keyword_p=*/false,
15878 /*check_dependency_p=*/true,
15879 /*template_p=*/&is_template,
15880 /*declarator_p=*/false,
15881 /*optional_p=*/false);
15882 if (TREE_CODE (default_argument) == TYPE_DECL)
15883 /* If the id-expression was a template-id that refers to
15884 a template-class, we already have the declaration here,
15885 so no further lookup is needed. */
15886 ;
15887 else
15888 /* Look up the name. */
15889 default_argument
15890 = cp_parser_lookup_name (parser, default_argument,
15891 none_type,
15892 /*is_template=*/is_template,
15893 /*is_namespace=*/false,
15894 /*check_dependency=*/true,
15895 /*ambiguous_decls=*/NULL,
15896 token->location);
15897 /* See if the default argument is valid. */
15898 default_argument = check_template_template_default_arg (default_argument);
15899 pop_deferring_access_checks ();
15900 return default_argument;
15901 }
15902
15903 /* Parse a template-parameter.
15904
15905 template-parameter:
15906 type-parameter
15907 parameter-declaration
15908
15909 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15910 the parameter. The TREE_PURPOSE is the default value, if any.
15911 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15912 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15913 set to true iff this parameter is a parameter pack. */
15914
15915 static tree
15916 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15917 bool *is_parameter_pack)
15918 {
15919 cp_token *token;
15920 cp_parameter_declarator *parameter_declarator;
15921 tree parm;
15922
15923 /* Assume it is a type parameter or a template parameter. */
15924 *is_non_type = false;
15925 /* Assume it not a parameter pack. */
15926 *is_parameter_pack = false;
15927 /* Peek at the next token. */
15928 token = cp_lexer_peek_token (parser->lexer);
15929 /* If it is `template', we have a type-parameter. */
15930 if (token->keyword == RID_TEMPLATE)
15931 return cp_parser_type_parameter (parser, is_parameter_pack);
15932 /* If it is `class' or `typename' we do not know yet whether it is a
15933 type parameter or a non-type parameter. Consider:
15934
15935 template <typename T, typename T::X X> ...
15936
15937 or:
15938
15939 template <class C, class D*> ...
15940
15941 Here, the first parameter is a type parameter, and the second is
15942 a non-type parameter. We can tell by looking at the token after
15943 the identifier -- if it is a `,', `=', or `>' then we have a type
15944 parameter. */
15945 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15946 {
15947 /* Peek at the token after `class' or `typename'. */
15948 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15949 /* If it's an ellipsis, we have a template type parameter
15950 pack. */
15951 if (token->type == CPP_ELLIPSIS)
15952 return cp_parser_type_parameter (parser, is_parameter_pack);
15953 /* If it's an identifier, skip it. */
15954 if (token->type == CPP_NAME)
15955 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15956 /* Now, see if the token looks like the end of a template
15957 parameter. */
15958 if (token->type == CPP_COMMA
15959 || token->type == CPP_EQ
15960 || token->type == CPP_GREATER)
15961 return cp_parser_type_parameter (parser, is_parameter_pack);
15962 }
15963
15964 /* Otherwise, it is a non-type parameter or a constrained parameter.
15965
15966 [temp.param]
15967
15968 When parsing a default template-argument for a non-type
15969 template-parameter, the first non-nested `>' is taken as the end
15970 of the template parameter-list rather than a greater-than
15971 operator. */
15972 parameter_declarator
15973 = cp_parser_parameter_declaration (parser,
15974 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15975 /*template_parm_p=*/true,
15976 /*parenthesized_p=*/NULL);
15977
15978 if (!parameter_declarator)
15979 return error_mark_node;
15980
15981 /* If the parameter declaration is marked as a parameter pack, set
15982 *IS_PARAMETER_PACK to notify the caller. */
15983 if (parameter_declarator->template_parameter_pack_p)
15984 *is_parameter_pack = true;
15985
15986 if (parameter_declarator->default_argument)
15987 {
15988 /* Can happen in some cases of erroneous input (c++/34892). */
15989 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15990 /* Consume the `...' for better error recovery. */
15991 cp_lexer_consume_token (parser->lexer);
15992 }
15993
15994 // The parameter may have been constrained.
15995 if (is_constrained_parameter (parameter_declarator))
15996 return finish_constrained_parameter (parser,
15997 parameter_declarator,
15998 is_non_type,
15999 is_parameter_pack);
16000
16001 // Now we're sure that the parameter is a non-type parameter.
16002 *is_non_type = true;
16003
16004 parm = grokdeclarator (parameter_declarator->declarator,
16005 &parameter_declarator->decl_specifiers,
16006 TPARM, /*initialized=*/0,
16007 /*attrlist=*/NULL);
16008 if (parm == error_mark_node)
16009 return error_mark_node;
16010
16011 return build_tree_list (parameter_declarator->default_argument, parm);
16012 }
16013
16014 /* Parse a type-parameter.
16015
16016 type-parameter:
16017 class identifier [opt]
16018 class identifier [opt] = type-id
16019 typename identifier [opt]
16020 typename identifier [opt] = type-id
16021 template < template-parameter-list > class identifier [opt]
16022 template < template-parameter-list > class identifier [opt]
16023 = id-expression
16024
16025 GNU Extension (variadic templates):
16026
16027 type-parameter:
16028 class ... identifier [opt]
16029 typename ... identifier [opt]
16030
16031 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16032 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16033 the declaration of the parameter.
16034
16035 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16036
16037 static tree
16038 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
16039 {
16040 cp_token *token;
16041 tree parameter;
16042
16043 /* Look for a keyword to tell us what kind of parameter this is. */
16044 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
16045 if (!token)
16046 return error_mark_node;
16047
16048 switch (token->keyword)
16049 {
16050 case RID_CLASS:
16051 case RID_TYPENAME:
16052 {
16053 tree identifier;
16054 tree default_argument;
16055
16056 /* If the next token is an ellipsis, we have a template
16057 argument pack. */
16058 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16059 {
16060 /* Consume the `...' token. */
16061 cp_lexer_consume_token (parser->lexer);
16062 maybe_warn_variadic_templates ();
16063
16064 *is_parameter_pack = true;
16065 }
16066
16067 /* If the next token is an identifier, then it names the
16068 parameter. */
16069 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16070 identifier = cp_parser_identifier (parser);
16071 else
16072 identifier = NULL_TREE;
16073
16074 /* Create the parameter. */
16075 parameter = finish_template_type_parm (class_type_node, identifier);
16076
16077 /* If the next token is an `=', we have a default argument. */
16078 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16079 {
16080 default_argument
16081 = cp_parser_default_type_template_argument (parser);
16082
16083 /* Template parameter packs cannot have default
16084 arguments. */
16085 if (*is_parameter_pack)
16086 {
16087 if (identifier)
16088 error_at (token->location,
16089 "template parameter pack %qD cannot have a "
16090 "default argument", identifier);
16091 else
16092 error_at (token->location,
16093 "template parameter packs cannot have "
16094 "default arguments");
16095 default_argument = NULL_TREE;
16096 }
16097 else if (check_for_bare_parameter_packs (default_argument))
16098 default_argument = error_mark_node;
16099 }
16100 else
16101 default_argument = NULL_TREE;
16102
16103 /* Create the combined representation of the parameter and the
16104 default argument. */
16105 parameter = build_tree_list (default_argument, parameter);
16106 }
16107 break;
16108
16109 case RID_TEMPLATE:
16110 {
16111 tree identifier;
16112 tree default_argument;
16113
16114 /* Look for the `<'. */
16115 cp_parser_require (parser, CPP_LESS, RT_LESS);
16116 /* Parse the template-parameter-list. */
16117 cp_parser_template_parameter_list (parser);
16118 /* Look for the `>'. */
16119 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16120
16121 // If template requirements are present, parse them.
16122 if (flag_concepts)
16123 {
16124 tree reqs = get_shorthand_constraints (current_template_parms);
16125 if (tree r = cp_parser_requires_clause_opt (parser))
16126 reqs = conjoin_constraints (reqs, normalize_expression (r));
16127 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
16128 }
16129
16130 /* Look for the `class' or 'typename' keywords. */
16131 cp_parser_type_parameter_key (parser);
16132 /* If the next token is an ellipsis, we have a template
16133 argument pack. */
16134 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16135 {
16136 /* Consume the `...' token. */
16137 cp_lexer_consume_token (parser->lexer);
16138 maybe_warn_variadic_templates ();
16139
16140 *is_parameter_pack = true;
16141 }
16142 /* If the next token is an `=', then there is a
16143 default-argument. If the next token is a `>', we are at
16144 the end of the parameter-list. If the next token is a `,',
16145 then we are at the end of this parameter. */
16146 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16147 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
16148 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16149 {
16150 identifier = cp_parser_identifier (parser);
16151 /* Treat invalid names as if the parameter were nameless. */
16152 if (identifier == error_mark_node)
16153 identifier = NULL_TREE;
16154 }
16155 else
16156 identifier = NULL_TREE;
16157
16158 /* Create the template parameter. */
16159 parameter = finish_template_template_parm (class_type_node,
16160 identifier);
16161
16162 /* If the next token is an `=', then there is a
16163 default-argument. */
16164 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16165 {
16166 default_argument
16167 = cp_parser_default_template_template_argument (parser);
16168
16169 /* Template parameter packs cannot have default
16170 arguments. */
16171 if (*is_parameter_pack)
16172 {
16173 if (identifier)
16174 error_at (token->location,
16175 "template parameter pack %qD cannot "
16176 "have a default argument",
16177 identifier);
16178 else
16179 error_at (token->location, "template parameter packs cannot "
16180 "have default arguments");
16181 default_argument = NULL_TREE;
16182 }
16183 }
16184 else
16185 default_argument = NULL_TREE;
16186
16187 /* Create the combined representation of the parameter and the
16188 default argument. */
16189 parameter = build_tree_list (default_argument, parameter);
16190 }
16191 break;
16192
16193 default:
16194 gcc_unreachable ();
16195 break;
16196 }
16197
16198 return parameter;
16199 }
16200
16201 /* Parse a template-id.
16202
16203 template-id:
16204 template-name < template-argument-list [opt] >
16205
16206 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16207 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16208 returned. Otherwise, if the template-name names a function, or set
16209 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16210 names a class, returns a TYPE_DECL for the specialization.
16211
16212 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16213 uninstantiated templates. */
16214
16215 static tree
16216 cp_parser_template_id (cp_parser *parser,
16217 bool template_keyword_p,
16218 bool check_dependency_p,
16219 enum tag_types tag_type,
16220 bool is_declaration)
16221 {
16222 tree templ;
16223 tree arguments;
16224 tree template_id;
16225 cp_token_position start_of_id = 0;
16226 cp_token *next_token = NULL, *next_token_2 = NULL;
16227 bool is_identifier;
16228
16229 /* If the next token corresponds to a template-id, there is no need
16230 to reparse it. */
16231 cp_token *token = cp_lexer_peek_token (parser->lexer);
16232 if (token->type == CPP_TEMPLATE_ID)
16233 {
16234 cp_lexer_consume_token (parser->lexer);
16235 return saved_checks_value (token->u.tree_check_value);
16236 }
16237
16238 /* Avoid performing name lookup if there is no possibility of
16239 finding a template-id. */
16240 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16241 || (token->type == CPP_NAME
16242 && !cp_parser_nth_token_starts_template_argument_list_p
16243 (parser, 2)))
16244 {
16245 cp_parser_error (parser, "expected template-id");
16246 return error_mark_node;
16247 }
16248
16249 /* Remember where the template-id starts. */
16250 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16251 start_of_id = cp_lexer_token_position (parser->lexer, false);
16252
16253 push_deferring_access_checks (dk_deferred);
16254
16255 /* Parse the template-name. */
16256 is_identifier = false;
16257 templ = cp_parser_template_name (parser, template_keyword_p,
16258 check_dependency_p,
16259 is_declaration,
16260 tag_type,
16261 &is_identifier);
16262
16263 /* Push any access checks inside the firewall we're about to create. */
16264 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
16265 pop_deferring_access_checks ();
16266 if (templ == error_mark_node || is_identifier)
16267 return templ;
16268
16269 /* Since we're going to preserve any side-effects from this parse, set up a
16270 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16271 in the template arguments. */
16272 tentative_firewall firewall (parser);
16273 reopen_deferring_access_checks (checks);
16274
16275 /* If we find the sequence `[:' after a template-name, it's probably
16276 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16277 parse correctly the argument list. */
16278 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16279 == CPP_OPEN_SQUARE)
16280 && next_token->flags & DIGRAPH
16281 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16282 == CPP_COLON)
16283 && !(next_token_2->flags & PREV_WHITE))
16284 {
16285 cp_parser_parse_tentatively (parser);
16286 /* Change `:' into `::'. */
16287 next_token_2->type = CPP_SCOPE;
16288 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16289 CPP_LESS. */
16290 cp_lexer_consume_token (parser->lexer);
16291
16292 /* Parse the arguments. */
16293 arguments = cp_parser_enclosed_template_argument_list (parser);
16294 if (!cp_parser_parse_definitely (parser))
16295 {
16296 /* If we couldn't parse an argument list, then we revert our changes
16297 and return simply an error. Maybe this is not a template-id
16298 after all. */
16299 next_token_2->type = CPP_COLON;
16300 cp_parser_error (parser, "expected %<<%>");
16301 pop_deferring_access_checks ();
16302 return error_mark_node;
16303 }
16304 /* Otherwise, emit an error about the invalid digraph, but continue
16305 parsing because we got our argument list. */
16306 if (permerror (next_token->location,
16307 "%<<::%> cannot begin a template-argument list"))
16308 {
16309 static bool hint = false;
16310 inform (next_token->location,
16311 "%<<:%> is an alternate spelling for %<[%>."
16312 " Insert whitespace between %<<%> and %<::%>");
16313 if (!hint && !flag_permissive)
16314 {
16315 inform (next_token->location, "(if you use %<-fpermissive%> "
16316 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16317 "accept your code)");
16318 hint = true;
16319 }
16320 }
16321 }
16322 else
16323 {
16324 /* Look for the `<' that starts the template-argument-list. */
16325 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16326 {
16327 pop_deferring_access_checks ();
16328 return error_mark_node;
16329 }
16330 /* Parse the arguments. */
16331 arguments = cp_parser_enclosed_template_argument_list (parser);
16332
16333 if ((cxx_dialect > cxx17)
16334 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16335 && !template_keyword_p
16336 && (cp_parser_error_occurred (parser)
16337 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16338 {
16339 /* This didn't go well. */
16340 if (TREE_CODE (templ) == FUNCTION_DECL)
16341 {
16342 /* C++2A says that "function-name < a;" is now ill-formed. */
16343 if (cp_parser_error_occurred (parser))
16344 {
16345 error_at (token->location, "invalid template-argument-list");
16346 inform (token->location, "function name as the left hand "
16347 "operand of %<<%> is ill-formed in C++2a; wrap the "
16348 "function name in %<()%>");
16349 }
16350 else
16351 /* We expect "f<targs>" to be followed by "(args)". */
16352 error_at (cp_lexer_peek_token (parser->lexer)->location,
16353 "expected %<(%> after template-argument-list");
16354 if (start_of_id)
16355 /* Purge all subsequent tokens. */
16356 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16357 }
16358 else
16359 cp_parser_simulate_error (parser);
16360 pop_deferring_access_checks ();
16361 return error_mark_node;
16362 }
16363 }
16364
16365 /* Set the location to be of the form:
16366 template-name < template-argument-list [opt] >
16367 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16368 with caret == start at the start of the template-name,
16369 ranging until the closing '>'. */
16370 location_t finish_loc
16371 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16372 location_t combined_loc
16373 = make_location (token->location, token->location, finish_loc);
16374
16375 /* Check for concepts autos where they don't belong. We could
16376 identify types in some cases of idnetifier TEMPL, looking ahead
16377 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16378 types. We reject them in functions, but if what we have is an
16379 identifier, even with none_type we can't conclude it's NOT a
16380 type, we have to wait for template substitution. */
16381 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16382 template_id = error_mark_node;
16383 /* Build a representation of the specialization. */
16384 else if (identifier_p (templ))
16385 template_id = build_min_nt_loc (combined_loc,
16386 TEMPLATE_ID_EXPR,
16387 templ, arguments);
16388 else if (DECL_TYPE_TEMPLATE_P (templ)
16389 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16390 {
16391 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16392 template (rather than some instantiation thereof) only if
16393 is not nested within some other construct. For example, in
16394 "template <typename T> void f(T) { A<T>::", A<T> is just an
16395 instantiation of A. */
16396 bool entering_scope
16397 = (template_parm_scope_p ()
16398 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16399 template_id
16400 = finish_template_type (templ, arguments, entering_scope);
16401 }
16402 /* A template-like identifier may be a partial concept id. */
16403 else if (flag_concepts
16404 && (template_id = (cp_parser_maybe_partial_concept_id
16405 (parser, templ, arguments))))
16406 return template_id;
16407 else if (variable_template_p (templ))
16408 {
16409 template_id = lookup_template_variable (templ, arguments);
16410 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16411 SET_EXPR_LOCATION (template_id, combined_loc);
16412 }
16413 else
16414 {
16415 /* If it's not a class-template or a template-template, it should be
16416 a function-template. */
16417 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16418 || TREE_CODE (templ) == OVERLOAD
16419 || TREE_CODE (templ) == FUNCTION_DECL
16420 || BASELINK_P (templ)));
16421
16422 template_id = lookup_template_function (templ, arguments);
16423 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16424 SET_EXPR_LOCATION (template_id, combined_loc);
16425 }
16426
16427 /* If parsing tentatively, replace the sequence of tokens that makes
16428 up the template-id with a CPP_TEMPLATE_ID token. That way,
16429 should we re-parse the token stream, we will not have to repeat
16430 the effort required to do the parse, nor will we issue duplicate
16431 error messages about problems during instantiation of the
16432 template. */
16433 if (start_of_id
16434 /* Don't do this if we had a parse error in a declarator; re-parsing
16435 might succeed if a name changes meaning (60361). */
16436 && !(cp_parser_error_occurred (parser)
16437 && cp_parser_parsing_tentatively (parser)
16438 && parser->in_declarator_p))
16439 {
16440 /* Reset the contents of the START_OF_ID token. */
16441 token->type = CPP_TEMPLATE_ID;
16442 token->location = combined_loc;
16443
16444 /* Retrieve any deferred checks. Do not pop this access checks yet
16445 so the memory will not be reclaimed during token replacing below. */
16446 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16447 token->u.tree_check_value->value = template_id;
16448 token->u.tree_check_value->checks = get_deferred_access_checks ();
16449 token->keyword = RID_MAX;
16450
16451 /* Purge all subsequent tokens. */
16452 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16453
16454 /* ??? Can we actually assume that, if template_id ==
16455 error_mark_node, we will have issued a diagnostic to the
16456 user, as opposed to simply marking the tentative parse as
16457 failed? */
16458 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16459 error_at (token->location, "parse error in template argument list");
16460 }
16461
16462 pop_to_parent_deferring_access_checks ();
16463 return template_id;
16464 }
16465
16466 /* Parse a template-name.
16467
16468 template-name:
16469 identifier
16470
16471 The standard should actually say:
16472
16473 template-name:
16474 identifier
16475 operator-function-id
16476
16477 A defect report has been filed about this issue.
16478
16479 A conversion-function-id cannot be a template name because they cannot
16480 be part of a template-id. In fact, looking at this code:
16481
16482 a.operator K<int>()
16483
16484 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16485 It is impossible to call a templated conversion-function-id with an
16486 explicit argument list, since the only allowed template parameter is
16487 the type to which it is converting.
16488
16489 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16490 `template' keyword, in a construction like:
16491
16492 T::template f<3>()
16493
16494 In that case `f' is taken to be a template-name, even though there
16495 is no way of knowing for sure.
16496
16497 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16498 name refers to a set of overloaded functions, at least one of which
16499 is a template, or an IDENTIFIER_NODE with the name of the template,
16500 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16501 names are looked up inside uninstantiated templates. */
16502
16503 static tree
16504 cp_parser_template_name (cp_parser* parser,
16505 bool template_keyword_p,
16506 bool check_dependency_p,
16507 bool is_declaration,
16508 enum tag_types tag_type,
16509 bool *is_identifier)
16510 {
16511 tree identifier;
16512 tree decl;
16513 cp_token *token = cp_lexer_peek_token (parser->lexer);
16514
16515 /* If the next token is `operator', then we have either an
16516 operator-function-id or a conversion-function-id. */
16517 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16518 {
16519 /* We don't know whether we're looking at an
16520 operator-function-id or a conversion-function-id. */
16521 cp_parser_parse_tentatively (parser);
16522 /* Try an operator-function-id. */
16523 identifier = cp_parser_operator_function_id (parser);
16524 /* If that didn't work, try a conversion-function-id. */
16525 if (!cp_parser_parse_definitely (parser))
16526 {
16527 cp_parser_error (parser, "expected template-name");
16528 return error_mark_node;
16529 }
16530 }
16531 /* Look for the identifier. */
16532 else
16533 identifier = cp_parser_identifier (parser);
16534
16535 /* If we didn't find an identifier, we don't have a template-id. */
16536 if (identifier == error_mark_node)
16537 return error_mark_node;
16538
16539 /* If the name immediately followed the `template' keyword, then it
16540 is a template-name. However, if the next token is not `<', then
16541 we do not treat it as a template-name, since it is not being used
16542 as part of a template-id. This enables us to handle constructs
16543 like:
16544
16545 template <typename T> struct S { S(); };
16546 template <typename T> S<T>::S();
16547
16548 correctly. We would treat `S' as a template -- if it were `S<T>'
16549 -- but we do not if there is no `<'. */
16550
16551 if (processing_template_decl
16552 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16553 {
16554 /* In a declaration, in a dependent context, we pretend that the
16555 "template" keyword was present in order to improve error
16556 recovery. For example, given:
16557
16558 template <typename T> void f(T::X<int>);
16559
16560 we want to treat "X<int>" as a template-id. */
16561 if (is_declaration
16562 && !template_keyword_p
16563 && parser->scope && TYPE_P (parser->scope)
16564 && check_dependency_p
16565 && dependent_scope_p (parser->scope)
16566 /* Do not do this for dtors (or ctors), since they never
16567 need the template keyword before their name. */
16568 && !constructor_name_p (identifier, parser->scope))
16569 {
16570 cp_token_position start = 0;
16571
16572 /* Explain what went wrong. */
16573 error_at (token->location, "non-template %qD used as template",
16574 identifier);
16575 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16576 parser->scope, identifier);
16577 /* If parsing tentatively, find the location of the "<" token. */
16578 if (cp_parser_simulate_error (parser))
16579 start = cp_lexer_token_position (parser->lexer, true);
16580 /* Parse the template arguments so that we can issue error
16581 messages about them. */
16582 cp_lexer_consume_token (parser->lexer);
16583 cp_parser_enclosed_template_argument_list (parser);
16584 /* Skip tokens until we find a good place from which to
16585 continue parsing. */
16586 cp_parser_skip_to_closing_parenthesis (parser,
16587 /*recovering=*/true,
16588 /*or_comma=*/true,
16589 /*consume_paren=*/false);
16590 /* If parsing tentatively, permanently remove the
16591 template argument list. That will prevent duplicate
16592 error messages from being issued about the missing
16593 "template" keyword. */
16594 if (start)
16595 cp_lexer_purge_tokens_after (parser->lexer, start);
16596 if (is_identifier)
16597 *is_identifier = true;
16598 parser->context->object_type = NULL_TREE;
16599 return identifier;
16600 }
16601
16602 /* If the "template" keyword is present, then there is generally
16603 no point in doing name-lookup, so we just return IDENTIFIER.
16604 But, if the qualifying scope is non-dependent then we can
16605 (and must) do name-lookup normally. */
16606 if (template_keyword_p)
16607 {
16608 tree scope = (parser->scope ? parser->scope
16609 : parser->context->object_type);
16610 if (scope && TYPE_P (scope)
16611 && (!CLASS_TYPE_P (scope)
16612 || (check_dependency_p && dependent_type_p (scope))))
16613 {
16614 /* We're optimizing away the call to cp_parser_lookup_name, but
16615 we still need to do this. */
16616 parser->context->object_type = NULL_TREE;
16617 return identifier;
16618 }
16619 }
16620 }
16621
16622 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16623 const bool scoped_p = ((parser->scope ? parser->scope
16624 : parser->context->object_type) != NULL_TREE);
16625
16626 /* Look up the name. */
16627 decl = cp_parser_lookup_name (parser, identifier,
16628 tag_type,
16629 /*is_template=*/true,
16630 /*is_namespace=*/false,
16631 check_dependency_p,
16632 /*ambiguous_decls=*/NULL,
16633 token->location);
16634
16635 decl = strip_using_decl (decl);
16636
16637 /* If DECL is a template, then the name was a template-name. */
16638 if (TREE_CODE (decl) == TEMPLATE_DECL)
16639 {
16640 if (TREE_DEPRECATED (decl)
16641 && deprecated_state != DEPRECATED_SUPPRESS)
16642 warn_deprecated_use (decl, NULL_TREE);
16643 }
16644 else
16645 {
16646 /* The standard does not explicitly indicate whether a name that
16647 names a set of overloaded declarations, some of which are
16648 templates, is a template-name. However, such a name should
16649 be a template-name; otherwise, there is no way to form a
16650 template-id for the overloaded templates. */
16651 bool found = false;
16652
16653 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16654 !found && iter; ++iter)
16655 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16656 found = true;
16657
16658 if (!found
16659 && (cxx_dialect > cxx17)
16660 && !scoped_p
16661 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
16662 && tag_type == none_type)
16663 {
16664 /* [temp.names] says "A name is also considered to refer to a template
16665 if it is an unqualified-id followed by a < and name lookup finds
16666 either one or more functions or finds nothing." */
16667
16668 /* The "more functions" case. Just use the OVERLOAD as normally.
16669 We don't use is_overloaded_fn here to avoid considering
16670 BASELINKs. */
16671 if (TREE_CODE (decl) == OVERLOAD
16672 /* Name lookup found one function. */
16673 || TREE_CODE (decl) == FUNCTION_DECL)
16674 found = true;
16675 /* Name lookup found nothing. */
16676 else if (decl == error_mark_node)
16677 return identifier;
16678 }
16679
16680 if (!found)
16681 {
16682 /* The name does not name a template. */
16683 cp_parser_error (parser, "expected template-name");
16684 return error_mark_node;
16685 }
16686 }
16687
16688 return decl;
16689 }
16690
16691 /* Parse a template-argument-list.
16692
16693 template-argument-list:
16694 template-argument ... [opt]
16695 template-argument-list , template-argument ... [opt]
16696
16697 Returns a TREE_VEC containing the arguments. */
16698
16699 static tree
16700 cp_parser_template_argument_list (cp_parser* parser)
16701 {
16702 tree fixed_args[10];
16703 unsigned n_args = 0;
16704 unsigned alloced = 10;
16705 tree *arg_ary = fixed_args;
16706 tree vec;
16707 bool saved_in_template_argument_list_p;
16708 bool saved_ice_p;
16709 bool saved_non_ice_p;
16710
16711 /* Don't create location wrapper nodes within a template-argument-list. */
16712 auto_suppress_location_wrappers sentinel;
16713
16714 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16715 parser->in_template_argument_list_p = true;
16716 /* Even if the template-id appears in an integral
16717 constant-expression, the contents of the argument list do
16718 not. */
16719 saved_ice_p = parser->integral_constant_expression_p;
16720 parser->integral_constant_expression_p = false;
16721 saved_non_ice_p = parser->non_integral_constant_expression_p;
16722 parser->non_integral_constant_expression_p = false;
16723
16724 /* Parse the arguments. */
16725 do
16726 {
16727 tree argument;
16728
16729 if (n_args)
16730 /* Consume the comma. */
16731 cp_lexer_consume_token (parser->lexer);
16732
16733 /* Parse the template-argument. */
16734 argument = cp_parser_template_argument (parser);
16735
16736 /* If the next token is an ellipsis, we're expanding a template
16737 argument pack. */
16738 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16739 {
16740 if (argument == error_mark_node)
16741 {
16742 cp_token *token = cp_lexer_peek_token (parser->lexer);
16743 error_at (token->location,
16744 "expected parameter pack before %<...%>");
16745 }
16746 /* Consume the `...' token. */
16747 cp_lexer_consume_token (parser->lexer);
16748
16749 /* Make the argument into a TYPE_PACK_EXPANSION or
16750 EXPR_PACK_EXPANSION. */
16751 argument = make_pack_expansion (argument);
16752 }
16753
16754 if (n_args == alloced)
16755 {
16756 alloced *= 2;
16757
16758 if (arg_ary == fixed_args)
16759 {
16760 arg_ary = XNEWVEC (tree, alloced);
16761 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16762 }
16763 else
16764 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16765 }
16766 arg_ary[n_args++] = argument;
16767 }
16768 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16769
16770 vec = make_tree_vec (n_args);
16771
16772 while (n_args--)
16773 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16774
16775 if (arg_ary != fixed_args)
16776 free (arg_ary);
16777 parser->non_integral_constant_expression_p = saved_non_ice_p;
16778 parser->integral_constant_expression_p = saved_ice_p;
16779 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16780 if (CHECKING_P)
16781 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16782 return vec;
16783 }
16784
16785 /* Parse a template-argument.
16786
16787 template-argument:
16788 assignment-expression
16789 type-id
16790 id-expression
16791
16792 The representation is that of an assignment-expression, type-id, or
16793 id-expression -- except that the qualified id-expression is
16794 evaluated, so that the value returned is either a DECL or an
16795 OVERLOAD.
16796
16797 Although the standard says "assignment-expression", it forbids
16798 throw-expressions or assignments in the template argument.
16799 Therefore, we use "conditional-expression" instead. */
16800
16801 static tree
16802 cp_parser_template_argument (cp_parser* parser)
16803 {
16804 tree argument;
16805 bool template_p;
16806 bool address_p;
16807 bool maybe_type_id = false;
16808 cp_token *token = NULL, *argument_start_token = NULL;
16809 location_t loc = 0;
16810 cp_id_kind idk;
16811
16812 /* There's really no way to know what we're looking at, so we just
16813 try each alternative in order.
16814
16815 [temp.arg]
16816
16817 In a template-argument, an ambiguity between a type-id and an
16818 expression is resolved to a type-id, regardless of the form of
16819 the corresponding template-parameter.
16820
16821 Therefore, we try a type-id first. */
16822 cp_parser_parse_tentatively (parser);
16823 argument = cp_parser_template_type_arg (parser);
16824 /* If there was no error parsing the type-id but the next token is a
16825 '>>', our behavior depends on which dialect of C++ we're
16826 parsing. In C++98, we probably found a typo for '> >'. But there
16827 are type-id which are also valid expressions. For instance:
16828
16829 struct X { int operator >> (int); };
16830 template <int V> struct Foo {};
16831 Foo<X () >> 5> r;
16832
16833 Here 'X()' is a valid type-id of a function type, but the user just
16834 wanted to write the expression "X() >> 5". Thus, we remember that we
16835 found a valid type-id, but we still try to parse the argument as an
16836 expression to see what happens.
16837
16838 In C++0x, the '>>' will be considered two separate '>'
16839 tokens. */
16840 if (!cp_parser_error_occurred (parser)
16841 && cxx_dialect == cxx98
16842 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16843 {
16844 maybe_type_id = true;
16845 cp_parser_abort_tentative_parse (parser);
16846 }
16847 else
16848 {
16849 /* If the next token isn't a `,' or a `>', then this argument wasn't
16850 really finished. This means that the argument is not a valid
16851 type-id. */
16852 if (!cp_parser_next_token_ends_template_argument_p (parser))
16853 cp_parser_error (parser, "expected template-argument");
16854 /* If that worked, we're done. */
16855 if (cp_parser_parse_definitely (parser))
16856 return argument;
16857 }
16858 /* We're still not sure what the argument will be. */
16859 cp_parser_parse_tentatively (parser);
16860 /* Try a template. */
16861 argument_start_token = cp_lexer_peek_token (parser->lexer);
16862 argument = cp_parser_id_expression (parser,
16863 /*template_keyword_p=*/false,
16864 /*check_dependency_p=*/true,
16865 &template_p,
16866 /*declarator_p=*/false,
16867 /*optional_p=*/false);
16868 /* If the next token isn't a `,' or a `>', then this argument wasn't
16869 really finished. */
16870 if (!cp_parser_next_token_ends_template_argument_p (parser))
16871 cp_parser_error (parser, "expected template-argument");
16872 if (!cp_parser_error_occurred (parser))
16873 {
16874 /* Figure out what is being referred to. If the id-expression
16875 was for a class template specialization, then we will have a
16876 TYPE_DECL at this point. There is no need to do name lookup
16877 at this point in that case. */
16878 if (TREE_CODE (argument) != TYPE_DECL)
16879 argument = cp_parser_lookup_name (parser, argument,
16880 none_type,
16881 /*is_template=*/template_p,
16882 /*is_namespace=*/false,
16883 /*check_dependency=*/true,
16884 /*ambiguous_decls=*/NULL,
16885 argument_start_token->location);
16886 /* Handle a constrained-type-specifier for a non-type template
16887 parameter. */
16888 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16889 argument = decl;
16890 else if (TREE_CODE (argument) != TEMPLATE_DECL
16891 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16892 cp_parser_error (parser, "expected template-name");
16893 }
16894 if (cp_parser_parse_definitely (parser))
16895 {
16896 if (TREE_DEPRECATED (argument))
16897 warn_deprecated_use (argument, NULL_TREE);
16898 return argument;
16899 }
16900 /* It must be a non-type argument. In C++17 any constant-expression is
16901 allowed. */
16902 if (cxx_dialect > cxx14)
16903 goto general_expr;
16904
16905 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16906
16907 -- an integral constant-expression of integral or enumeration
16908 type; or
16909
16910 -- the name of a non-type template-parameter; or
16911
16912 -- the name of an object or function with external linkage...
16913
16914 -- the address of an object or function with external linkage...
16915
16916 -- a pointer to member... */
16917 /* Look for a non-type template parameter. */
16918 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16919 {
16920 cp_parser_parse_tentatively (parser);
16921 argument = cp_parser_primary_expression (parser,
16922 /*address_p=*/false,
16923 /*cast_p=*/false,
16924 /*template_arg_p=*/true,
16925 &idk);
16926 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16927 || !cp_parser_next_token_ends_template_argument_p (parser))
16928 cp_parser_simulate_error (parser);
16929 if (cp_parser_parse_definitely (parser))
16930 return argument;
16931 }
16932
16933 /* If the next token is "&", the argument must be the address of an
16934 object or function with external linkage. */
16935 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16936 if (address_p)
16937 {
16938 loc = cp_lexer_peek_token (parser->lexer)->location;
16939 cp_lexer_consume_token (parser->lexer);
16940 }
16941 /* See if we might have an id-expression. */
16942 token = cp_lexer_peek_token (parser->lexer);
16943 if (token->type == CPP_NAME
16944 || token->keyword == RID_OPERATOR
16945 || token->type == CPP_SCOPE
16946 || token->type == CPP_TEMPLATE_ID
16947 || token->type == CPP_NESTED_NAME_SPECIFIER)
16948 {
16949 cp_parser_parse_tentatively (parser);
16950 argument = cp_parser_primary_expression (parser,
16951 address_p,
16952 /*cast_p=*/false,
16953 /*template_arg_p=*/true,
16954 &idk);
16955 if (cp_parser_error_occurred (parser)
16956 || !cp_parser_next_token_ends_template_argument_p (parser))
16957 cp_parser_abort_tentative_parse (parser);
16958 else
16959 {
16960 tree probe;
16961
16962 if (INDIRECT_REF_P (argument))
16963 {
16964 /* Strip the dereference temporarily. */
16965 gcc_assert (REFERENCE_REF_P (argument));
16966 argument = TREE_OPERAND (argument, 0);
16967 }
16968
16969 /* If we're in a template, we represent a qualified-id referring
16970 to a static data member as a SCOPE_REF even if the scope isn't
16971 dependent so that we can check access control later. */
16972 probe = argument;
16973 if (TREE_CODE (probe) == SCOPE_REF)
16974 probe = TREE_OPERAND (probe, 1);
16975 if (VAR_P (probe))
16976 {
16977 /* A variable without external linkage might still be a
16978 valid constant-expression, so no error is issued here
16979 if the external-linkage check fails. */
16980 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16981 cp_parser_simulate_error (parser);
16982 }
16983 else if (is_overloaded_fn (argument))
16984 /* All overloaded functions are allowed; if the external
16985 linkage test does not pass, an error will be issued
16986 later. */
16987 ;
16988 else if (address_p
16989 && (TREE_CODE (argument) == OFFSET_REF
16990 || TREE_CODE (argument) == SCOPE_REF))
16991 /* A pointer-to-member. */
16992 ;
16993 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16994 ;
16995 else
16996 cp_parser_simulate_error (parser);
16997
16998 if (cp_parser_parse_definitely (parser))
16999 {
17000 if (address_p)
17001 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
17002 tf_warning_or_error);
17003 else
17004 argument = convert_from_reference (argument);
17005 return argument;
17006 }
17007 }
17008 }
17009 /* If the argument started with "&", there are no other valid
17010 alternatives at this point. */
17011 if (address_p)
17012 {
17013 cp_parser_error (parser, "invalid non-type template argument");
17014 return error_mark_node;
17015 }
17016
17017 general_expr:
17018 /* If the argument wasn't successfully parsed as a type-id followed
17019 by '>>', the argument can only be a constant expression now.
17020 Otherwise, we try parsing the constant-expression tentatively,
17021 because the argument could really be a type-id. */
17022 if (maybe_type_id)
17023 cp_parser_parse_tentatively (parser);
17024
17025 if (cxx_dialect <= cxx14)
17026 argument = cp_parser_constant_expression (parser);
17027 else
17028 {
17029 /* In C++20, we can encounter a braced-init-list. */
17030 if (cxx_dialect >= cxx2a
17031 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17032 {
17033 bool expr_non_constant_p;
17034 return cp_parser_braced_list (parser, &expr_non_constant_p);
17035 }
17036
17037 /* With C++17 generalized non-type template arguments we need to handle
17038 lvalue constant expressions, too. */
17039 argument = cp_parser_assignment_expression (parser);
17040 require_potential_constant_expression (argument);
17041 }
17042
17043 if (!maybe_type_id)
17044 return argument;
17045 if (!cp_parser_next_token_ends_template_argument_p (parser))
17046 cp_parser_error (parser, "expected template-argument");
17047 if (cp_parser_parse_definitely (parser))
17048 return argument;
17049 /* We did our best to parse the argument as a non type-id, but that
17050 was the only alternative that matched (albeit with a '>' after
17051 it). We can assume it's just a typo from the user, and a
17052 diagnostic will then be issued. */
17053 return cp_parser_template_type_arg (parser);
17054 }
17055
17056 /* Parse an explicit-instantiation.
17057
17058 explicit-instantiation:
17059 template declaration
17060
17061 Although the standard says `declaration', what it really means is:
17062
17063 explicit-instantiation:
17064 template decl-specifier-seq [opt] declarator [opt] ;
17065
17066 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17067 supposed to be allowed. A defect report has been filed about this
17068 issue.
17069
17070 GNU Extension:
17071
17072 explicit-instantiation:
17073 storage-class-specifier template
17074 decl-specifier-seq [opt] declarator [opt] ;
17075 function-specifier template
17076 decl-specifier-seq [opt] declarator [opt] ; */
17077
17078 static void
17079 cp_parser_explicit_instantiation (cp_parser* parser)
17080 {
17081 int declares_class_or_enum;
17082 cp_decl_specifier_seq decl_specifiers;
17083 tree extension_specifier = NULL_TREE;
17084
17085 timevar_push (TV_TEMPLATE_INST);
17086
17087 /* Look for an (optional) storage-class-specifier or
17088 function-specifier. */
17089 if (cp_parser_allow_gnu_extensions_p (parser))
17090 {
17091 extension_specifier
17092 = cp_parser_storage_class_specifier_opt (parser);
17093 if (!extension_specifier)
17094 extension_specifier
17095 = cp_parser_function_specifier_opt (parser,
17096 /*decl_specs=*/NULL);
17097 }
17098
17099 /* Look for the `template' keyword. */
17100 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17101 /* Let the front end know that we are processing an explicit
17102 instantiation. */
17103 begin_explicit_instantiation ();
17104 /* [temp.explicit] says that we are supposed to ignore access
17105 control while processing explicit instantiation directives. */
17106 push_deferring_access_checks (dk_no_check);
17107 /* Parse a decl-specifier-seq. */
17108 cp_parser_decl_specifier_seq (parser,
17109 CP_PARSER_FLAGS_OPTIONAL,
17110 &decl_specifiers,
17111 &declares_class_or_enum);
17112 /* If there was exactly one decl-specifier, and it declared a class,
17113 and there's no declarator, then we have an explicit type
17114 instantiation. */
17115 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
17116 {
17117 tree type;
17118
17119 type = check_tag_decl (&decl_specifiers,
17120 /*explicit_type_instantiation_p=*/true);
17121 /* Turn access control back on for names used during
17122 template instantiation. */
17123 pop_deferring_access_checks ();
17124 if (type)
17125 do_type_instantiation (type, extension_specifier,
17126 /*complain=*/tf_error);
17127 }
17128 else
17129 {
17130 cp_declarator *declarator;
17131 tree decl;
17132
17133 /* Parse the declarator. */
17134 declarator
17135 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17136 CP_PARSER_FLAGS_NONE,
17137 /*ctor_dtor_or_conv_p=*/NULL,
17138 /*parenthesized_p=*/NULL,
17139 /*member_p=*/false,
17140 /*friend_p=*/false,
17141 /*static_p=*/false);
17142 if (declares_class_or_enum & 2)
17143 cp_parser_check_for_definition_in_return_type (declarator,
17144 decl_specifiers.type,
17145 decl_specifiers.locations[ds_type_spec]);
17146 if (declarator != cp_error_declarator)
17147 {
17148 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
17149 permerror (decl_specifiers.locations[ds_inline],
17150 "explicit instantiation shall not use"
17151 " %<inline%> specifier");
17152 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
17153 permerror (decl_specifiers.locations[ds_constexpr],
17154 "explicit instantiation shall not use"
17155 " %<constexpr%> specifier");
17156
17157 decl = grokdeclarator (declarator, &decl_specifiers,
17158 NORMAL, 0, &decl_specifiers.attributes);
17159 /* Turn access control back on for names used during
17160 template instantiation. */
17161 pop_deferring_access_checks ();
17162 /* Do the explicit instantiation. */
17163 do_decl_instantiation (decl, extension_specifier);
17164 }
17165 else
17166 {
17167 pop_deferring_access_checks ();
17168 /* Skip the body of the explicit instantiation. */
17169 cp_parser_skip_to_end_of_statement (parser);
17170 }
17171 }
17172 /* We're done with the instantiation. */
17173 end_explicit_instantiation ();
17174
17175 cp_parser_consume_semicolon_at_end_of_statement (parser);
17176
17177 timevar_pop (TV_TEMPLATE_INST);
17178 }
17179
17180 /* Parse an explicit-specialization.
17181
17182 explicit-specialization:
17183 template < > declaration
17184
17185 Although the standard says `declaration', what it really means is:
17186
17187 explicit-specialization:
17188 template <> decl-specifier [opt] init-declarator [opt] ;
17189 template <> function-definition
17190 template <> explicit-specialization
17191 template <> template-declaration */
17192
17193 static void
17194 cp_parser_explicit_specialization (cp_parser* parser)
17195 {
17196 bool need_lang_pop;
17197 cp_token *token = cp_lexer_peek_token (parser->lexer);
17198
17199 /* Look for the `template' keyword. */
17200 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17201 /* Look for the `<'. */
17202 cp_parser_require (parser, CPP_LESS, RT_LESS);
17203 /* Look for the `>'. */
17204 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17205 /* We have processed another parameter list. */
17206 ++parser->num_template_parameter_lists;
17207 /* [temp]
17208
17209 A template ... explicit specialization ... shall not have C
17210 linkage. */
17211 if (current_lang_name == lang_name_c)
17212 {
17213 error_at (token->location, "template specialization with C linkage");
17214 maybe_show_extern_c_location ();
17215 /* Give it C++ linkage to avoid confusing other parts of the
17216 front end. */
17217 push_lang_context (lang_name_cplusplus);
17218 need_lang_pop = true;
17219 }
17220 else
17221 need_lang_pop = false;
17222 /* Let the front end know that we are beginning a specialization. */
17223 if (!begin_specialization ())
17224 {
17225 end_specialization ();
17226 return;
17227 }
17228
17229 /* If the next keyword is `template', we need to figure out whether
17230 or not we're looking a template-declaration. */
17231 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17232 {
17233 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17234 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
17235 cp_parser_template_declaration_after_export (parser,
17236 /*member_p=*/false);
17237 else
17238 cp_parser_explicit_specialization (parser);
17239 }
17240 else
17241 /* Parse the dependent declaration. */
17242 cp_parser_single_declaration (parser,
17243 /*checks=*/NULL,
17244 /*member_p=*/false,
17245 /*explicit_specialization_p=*/true,
17246 /*friend_p=*/NULL);
17247 /* We're done with the specialization. */
17248 end_specialization ();
17249 /* For the erroneous case of a template with C linkage, we pushed an
17250 implicit C++ linkage scope; exit that scope now. */
17251 if (need_lang_pop)
17252 pop_lang_context ();
17253 /* We're done with this parameter list. */
17254 --parser->num_template_parameter_lists;
17255 }
17256
17257 /* Parse a type-specifier.
17258
17259 type-specifier:
17260 simple-type-specifier
17261 class-specifier
17262 enum-specifier
17263 elaborated-type-specifier
17264 cv-qualifier
17265
17266 GNU Extension:
17267
17268 type-specifier:
17269 __complex__
17270
17271 Returns a representation of the type-specifier. For a
17272 class-specifier, enum-specifier, or elaborated-type-specifier, a
17273 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17274
17275 The parser flags FLAGS is used to control type-specifier parsing.
17276
17277 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17278 in a decl-specifier-seq.
17279
17280 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17281 class-specifier, enum-specifier, or elaborated-type-specifier, then
17282 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17283 if a type is declared; 2 if it is defined. Otherwise, it is set to
17284 zero.
17285
17286 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17287 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17288 is set to FALSE. */
17289
17290 static tree
17291 cp_parser_type_specifier (cp_parser* parser,
17292 cp_parser_flags flags,
17293 cp_decl_specifier_seq *decl_specs,
17294 bool is_declaration,
17295 int* declares_class_or_enum,
17296 bool* is_cv_qualifier)
17297 {
17298 tree type_spec = NULL_TREE;
17299 cp_token *token;
17300 enum rid keyword;
17301 cp_decl_spec ds = ds_last;
17302
17303 /* Assume this type-specifier does not declare a new type. */
17304 if (declares_class_or_enum)
17305 *declares_class_or_enum = 0;
17306 /* And that it does not specify a cv-qualifier. */
17307 if (is_cv_qualifier)
17308 *is_cv_qualifier = false;
17309 /* Peek at the next token. */
17310 token = cp_lexer_peek_token (parser->lexer);
17311
17312 /* If we're looking at a keyword, we can use that to guide the
17313 production we choose. */
17314 keyword = token->keyword;
17315 switch (keyword)
17316 {
17317 case RID_ENUM:
17318 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17319 goto elaborated_type_specifier;
17320
17321 /* Look for the enum-specifier. */
17322 type_spec = cp_parser_enum_specifier (parser);
17323 /* If that worked, we're done. */
17324 if (type_spec)
17325 {
17326 if (declares_class_or_enum)
17327 *declares_class_or_enum = 2;
17328 if (decl_specs)
17329 cp_parser_set_decl_spec_type (decl_specs,
17330 type_spec,
17331 token,
17332 /*type_definition_p=*/true);
17333 return type_spec;
17334 }
17335 else
17336 goto elaborated_type_specifier;
17337
17338 /* Any of these indicate either a class-specifier, or an
17339 elaborated-type-specifier. */
17340 case RID_CLASS:
17341 case RID_STRUCT:
17342 case RID_UNION:
17343 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17344 goto elaborated_type_specifier;
17345
17346 /* Parse tentatively so that we can back up if we don't find a
17347 class-specifier. */
17348 cp_parser_parse_tentatively (parser);
17349 /* Look for the class-specifier. */
17350 type_spec = cp_parser_class_specifier (parser);
17351 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17352 /* If that worked, we're done. */
17353 if (cp_parser_parse_definitely (parser))
17354 {
17355 if (declares_class_or_enum)
17356 *declares_class_or_enum = 2;
17357 if (decl_specs)
17358 cp_parser_set_decl_spec_type (decl_specs,
17359 type_spec,
17360 token,
17361 /*type_definition_p=*/true);
17362 return type_spec;
17363 }
17364
17365 /* Fall through. */
17366 elaborated_type_specifier:
17367 /* We're declaring (not defining) a class or enum. */
17368 if (declares_class_or_enum)
17369 *declares_class_or_enum = 1;
17370
17371 /* Fall through. */
17372 case RID_TYPENAME:
17373 /* Look for an elaborated-type-specifier. */
17374 type_spec
17375 = (cp_parser_elaborated_type_specifier
17376 (parser,
17377 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17378 is_declaration));
17379 if (decl_specs)
17380 cp_parser_set_decl_spec_type (decl_specs,
17381 type_spec,
17382 token,
17383 /*type_definition_p=*/false);
17384 return type_spec;
17385
17386 case RID_CONST:
17387 ds = ds_const;
17388 if (is_cv_qualifier)
17389 *is_cv_qualifier = true;
17390 break;
17391
17392 case RID_VOLATILE:
17393 ds = ds_volatile;
17394 if (is_cv_qualifier)
17395 *is_cv_qualifier = true;
17396 break;
17397
17398 case RID_RESTRICT:
17399 ds = ds_restrict;
17400 if (is_cv_qualifier)
17401 *is_cv_qualifier = true;
17402 break;
17403
17404 case RID_COMPLEX:
17405 /* The `__complex__' keyword is a GNU extension. */
17406 ds = ds_complex;
17407 break;
17408
17409 default:
17410 break;
17411 }
17412
17413 /* Handle simple keywords. */
17414 if (ds != ds_last)
17415 {
17416 if (decl_specs)
17417 {
17418 set_and_check_decl_spec_loc (decl_specs, ds, token);
17419 decl_specs->any_specifiers_p = true;
17420 }
17421 return cp_lexer_consume_token (parser->lexer)->u.value;
17422 }
17423
17424 /* If we do not already have a type-specifier, assume we are looking
17425 at a simple-type-specifier. */
17426 type_spec = cp_parser_simple_type_specifier (parser,
17427 decl_specs,
17428 flags);
17429
17430 /* If we didn't find a type-specifier, and a type-specifier was not
17431 optional in this context, issue an error message. */
17432 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17433 {
17434 cp_parser_error (parser, "expected type specifier");
17435 return error_mark_node;
17436 }
17437
17438 return type_spec;
17439 }
17440
17441 /* Parse a simple-type-specifier.
17442
17443 simple-type-specifier:
17444 :: [opt] nested-name-specifier [opt] type-name
17445 :: [opt] nested-name-specifier template template-id
17446 char
17447 wchar_t
17448 bool
17449 short
17450 int
17451 long
17452 signed
17453 unsigned
17454 float
17455 double
17456 void
17457
17458 C++11 Extension:
17459
17460 simple-type-specifier:
17461 auto
17462 decltype ( expression )
17463 char16_t
17464 char32_t
17465 __underlying_type ( type-id )
17466
17467 C++17 extension:
17468
17469 nested-name-specifier(opt) template-name
17470
17471 GNU Extension:
17472
17473 simple-type-specifier:
17474 __int128
17475 __typeof__ unary-expression
17476 __typeof__ ( type-id )
17477 __typeof__ ( type-id ) { initializer-list , [opt] }
17478
17479 Concepts Extension:
17480
17481 simple-type-specifier:
17482 constrained-type-specifier
17483
17484 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17485 appropriately updated. */
17486
17487 static tree
17488 cp_parser_simple_type_specifier (cp_parser* parser,
17489 cp_decl_specifier_seq *decl_specs,
17490 cp_parser_flags flags)
17491 {
17492 tree type = NULL_TREE;
17493 cp_token *token;
17494 int idx;
17495
17496 /* Peek at the next token. */
17497 token = cp_lexer_peek_token (parser->lexer);
17498
17499 /* If we're looking at a keyword, things are easy. */
17500 switch (token->keyword)
17501 {
17502 case RID_CHAR:
17503 if (decl_specs)
17504 decl_specs->explicit_char_p = true;
17505 type = char_type_node;
17506 break;
17507 case RID_CHAR16:
17508 type = char16_type_node;
17509 break;
17510 case RID_CHAR32:
17511 type = char32_type_node;
17512 break;
17513 case RID_WCHAR:
17514 type = wchar_type_node;
17515 break;
17516 case RID_BOOL:
17517 type = boolean_type_node;
17518 break;
17519 case RID_SHORT:
17520 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17521 type = short_integer_type_node;
17522 break;
17523 case RID_INT:
17524 if (decl_specs)
17525 decl_specs->explicit_int_p = true;
17526 type = integer_type_node;
17527 break;
17528 case RID_INT_N_0:
17529 case RID_INT_N_1:
17530 case RID_INT_N_2:
17531 case RID_INT_N_3:
17532 idx = token->keyword - RID_INT_N_0;
17533 if (! int_n_enabled_p [idx])
17534 break;
17535 if (decl_specs)
17536 {
17537 decl_specs->explicit_intN_p = true;
17538 decl_specs->int_n_idx = idx;
17539 }
17540 type = int_n_trees [idx].signed_type;
17541 break;
17542 case RID_LONG:
17543 if (decl_specs)
17544 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17545 type = long_integer_type_node;
17546 break;
17547 case RID_SIGNED:
17548 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17549 type = integer_type_node;
17550 break;
17551 case RID_UNSIGNED:
17552 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17553 type = unsigned_type_node;
17554 break;
17555 case RID_FLOAT:
17556 type = float_type_node;
17557 break;
17558 case RID_DOUBLE:
17559 type = double_type_node;
17560 break;
17561 case RID_VOID:
17562 type = void_type_node;
17563 break;
17564
17565 case RID_AUTO:
17566 maybe_warn_cpp0x (CPP0X_AUTO);
17567 if (parser->auto_is_implicit_function_template_parm_p)
17568 {
17569 /* The 'auto' might be the placeholder return type for a function decl
17570 with trailing return type. */
17571 bool have_trailing_return_fn_decl = false;
17572
17573 cp_parser_parse_tentatively (parser);
17574 cp_lexer_consume_token (parser->lexer);
17575 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17576 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17577 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17578 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17579 {
17580 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17581 {
17582 cp_lexer_consume_token (parser->lexer);
17583 cp_parser_skip_to_closing_parenthesis (parser,
17584 /*recovering*/false,
17585 /*or_comma*/false,
17586 /*consume_paren*/true);
17587 continue;
17588 }
17589
17590 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17591 {
17592 have_trailing_return_fn_decl = true;
17593 break;
17594 }
17595
17596 cp_lexer_consume_token (parser->lexer);
17597 }
17598 cp_parser_abort_tentative_parse (parser);
17599
17600 if (have_trailing_return_fn_decl)
17601 {
17602 type = make_auto ();
17603 break;
17604 }
17605
17606 if (cxx_dialect >= cxx14)
17607 {
17608 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17609 type = TREE_TYPE (type);
17610 }
17611 else
17612 type = error_mark_node;
17613
17614 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17615 {
17616 if (cxx_dialect < cxx14)
17617 error_at (token->location,
17618 "use of %<auto%> in lambda parameter declaration "
17619 "only available with "
17620 "-std=c++14 or -std=gnu++14");
17621 }
17622 else if (cxx_dialect < cxx14)
17623 error_at (token->location,
17624 "use of %<auto%> in parameter declaration "
17625 "only available with "
17626 "-std=c++14 or -std=gnu++14");
17627 else if (!flag_concepts)
17628 pedwarn (token->location, 0,
17629 "use of %<auto%> in parameter declaration "
17630 "only available with -fconcepts");
17631 }
17632 else
17633 type = make_auto ();
17634 break;
17635
17636 case RID_DECLTYPE:
17637 /* Since DR 743, decltype can either be a simple-type-specifier by
17638 itself or begin a nested-name-specifier. Parsing it will replace
17639 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17640 handling below decide what to do. */
17641 cp_parser_decltype (parser);
17642 cp_lexer_set_token_position (parser->lexer, token);
17643 break;
17644
17645 case RID_TYPEOF:
17646 /* Consume the `typeof' token. */
17647 cp_lexer_consume_token (parser->lexer);
17648 /* Parse the operand to `typeof'. */
17649 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17650 /* If it is not already a TYPE, take its type. */
17651 if (!TYPE_P (type))
17652 type = finish_typeof (type);
17653
17654 if (decl_specs)
17655 cp_parser_set_decl_spec_type (decl_specs, type,
17656 token,
17657 /*type_definition_p=*/false);
17658
17659 return type;
17660
17661 case RID_UNDERLYING_TYPE:
17662 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17663 if (decl_specs)
17664 cp_parser_set_decl_spec_type (decl_specs, type,
17665 token,
17666 /*type_definition_p=*/false);
17667
17668 return type;
17669
17670 case RID_BASES:
17671 case RID_DIRECT_BASES:
17672 type = cp_parser_trait_expr (parser, token->keyword);
17673 if (decl_specs)
17674 cp_parser_set_decl_spec_type (decl_specs, type,
17675 token,
17676 /*type_definition_p=*/false);
17677 return type;
17678 default:
17679 break;
17680 }
17681
17682 /* If token is an already-parsed decltype not followed by ::,
17683 it's a simple-type-specifier. */
17684 if (token->type == CPP_DECLTYPE
17685 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17686 {
17687 type = saved_checks_value (token->u.tree_check_value);
17688 if (decl_specs)
17689 {
17690 cp_parser_set_decl_spec_type (decl_specs, type,
17691 token,
17692 /*type_definition_p=*/false);
17693 /* Remember that we are handling a decltype in order to
17694 implement the resolution of DR 1510 when the argument
17695 isn't instantiation dependent. */
17696 decl_specs->decltype_p = true;
17697 }
17698 cp_lexer_consume_token (parser->lexer);
17699 return type;
17700 }
17701
17702 /* If the type-specifier was for a built-in type, we're done. */
17703 if (type)
17704 {
17705 /* Record the type. */
17706 if (decl_specs
17707 && (token->keyword != RID_SIGNED
17708 && token->keyword != RID_UNSIGNED
17709 && token->keyword != RID_SHORT
17710 && token->keyword != RID_LONG))
17711 cp_parser_set_decl_spec_type (decl_specs,
17712 type,
17713 token,
17714 /*type_definition_p=*/false);
17715 if (decl_specs)
17716 decl_specs->any_specifiers_p = true;
17717
17718 /* Consume the token. */
17719 cp_lexer_consume_token (parser->lexer);
17720
17721 if (type == error_mark_node)
17722 return error_mark_node;
17723
17724 /* There is no valid C++ program where a non-template type is
17725 followed by a "<". That usually indicates that the user thought
17726 that the type was a template. */
17727 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17728 token->location);
17729
17730 return TYPE_NAME (type);
17731 }
17732
17733 /* The type-specifier must be a user-defined type. */
17734 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17735 {
17736 bool qualified_p;
17737 bool global_p;
17738 const bool typename_p = (cxx_dialect >= cxx2a
17739 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
17740
17741 /* Don't gobble tokens or issue error messages if this is an
17742 optional type-specifier. */
17743 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17744 cp_parser_parse_tentatively (parser);
17745
17746 token = cp_lexer_peek_token (parser->lexer);
17747
17748 /* Look for the optional `::' operator. */
17749 global_p
17750 = (cp_parser_global_scope_opt (parser,
17751 /*current_scope_valid_p=*/false)
17752 != NULL_TREE);
17753 /* Look for the nested-name specifier. */
17754 qualified_p
17755 = (cp_parser_nested_name_specifier_opt (parser,
17756 /*typename_keyword_p=*/false,
17757 /*check_dependency_p=*/true,
17758 /*type_p=*/false,
17759 /*is_declaration=*/false)
17760 != NULL_TREE);
17761 /* If we have seen a nested-name-specifier, and the next token
17762 is `template', then we are using the template-id production. */
17763 if (parser->scope
17764 && cp_parser_optional_template_keyword (parser))
17765 {
17766 /* Look for the template-id. */
17767 type = cp_parser_template_id (parser,
17768 /*template_keyword_p=*/true,
17769 /*check_dependency_p=*/true,
17770 none_type,
17771 /*is_declaration=*/false);
17772 /* If the template-id did not name a type, we are out of
17773 luck. */
17774 if (TREE_CODE (type) != TYPE_DECL)
17775 {
17776 /* ...unless we pretend we have seen 'typename'. */
17777 if (typename_p)
17778 type = cp_parser_make_typename_type (parser, type,
17779 token->location);
17780 else
17781 {
17782 cp_parser_error (parser, "expected template-id for type");
17783 type = NULL_TREE;
17784 }
17785 }
17786 }
17787 /* Otherwise, look for a type-name. */
17788 else
17789 type = cp_parser_type_name (parser, (qualified_p && typename_p));
17790
17791 /* Keep track of all name-lookups performed in class scopes. */
17792 if (type
17793 && !global_p
17794 && !qualified_p
17795 && TREE_CODE (type) == TYPE_DECL
17796 && identifier_p (DECL_NAME (type)))
17797 maybe_note_name_used_in_class (DECL_NAME (type), type);
17798 /* If it didn't work out, we don't have a TYPE. */
17799 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17800 && !cp_parser_parse_definitely (parser))
17801 type = NULL_TREE;
17802 if (!type && cxx_dialect >= cxx17)
17803 {
17804 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17805 cp_parser_parse_tentatively (parser);
17806
17807 cp_parser_global_scope_opt (parser,
17808 /*current_scope_valid_p=*/false);
17809 cp_parser_nested_name_specifier_opt (parser,
17810 /*typename_keyword_p=*/false,
17811 /*check_dependency_p=*/true,
17812 /*type_p=*/false,
17813 /*is_declaration=*/false);
17814 tree name = cp_parser_identifier (parser);
17815 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17816 && parser->scope != error_mark_node)
17817 {
17818 tree tmpl = cp_parser_lookup_name (parser, name,
17819 none_type,
17820 /*is_template=*/false,
17821 /*is_namespace=*/false,
17822 /*check_dependency=*/true,
17823 /*ambiguous_decls=*/NULL,
17824 token->location);
17825 if (tmpl && tmpl != error_mark_node
17826 && (DECL_CLASS_TEMPLATE_P (tmpl)
17827 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17828 type = make_template_placeholder (tmpl);
17829 else
17830 {
17831 type = error_mark_node;
17832 if (!cp_parser_simulate_error (parser))
17833 cp_parser_name_lookup_error (parser, name, tmpl,
17834 NLE_TYPE, token->location);
17835 }
17836 }
17837 else
17838 type = error_mark_node;
17839
17840 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17841 && !cp_parser_parse_definitely (parser))
17842 type = NULL_TREE;
17843 }
17844 if (type && decl_specs)
17845 cp_parser_set_decl_spec_type (decl_specs, type,
17846 token,
17847 /*type_definition_p=*/false);
17848 }
17849
17850 /* If we didn't get a type-name, issue an error message. */
17851 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17852 {
17853 cp_parser_error (parser, "expected type-name");
17854 return error_mark_node;
17855 }
17856
17857 if (type && type != error_mark_node)
17858 {
17859 /* See if TYPE is an Objective-C type, and if so, parse and
17860 accept any protocol references following it. Do this before
17861 the cp_parser_check_for_invalid_template_id() call, because
17862 Objective-C types can be followed by '<...>' which would
17863 enclose protocol names rather than template arguments, and so
17864 everything is fine. */
17865 if (c_dialect_objc () && !parser->scope
17866 && (objc_is_id (type) || objc_is_class_name (type)))
17867 {
17868 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17869 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17870
17871 /* Clobber the "unqualified" type previously entered into
17872 DECL_SPECS with the new, improved protocol-qualified version. */
17873 if (decl_specs)
17874 decl_specs->type = qual_type;
17875
17876 return qual_type;
17877 }
17878
17879 /* There is no valid C++ program where a non-template type is
17880 followed by a "<". That usually indicates that the user
17881 thought that the type was a template. */
17882 cp_parser_check_for_invalid_template_id (parser, type,
17883 none_type,
17884 token->location);
17885 }
17886
17887 return type;
17888 }
17889
17890 /* Parse a type-name.
17891
17892 type-name:
17893 class-name
17894 enum-name
17895 typedef-name
17896 simple-template-id [in c++0x]
17897
17898 enum-name:
17899 identifier
17900
17901 typedef-name:
17902 identifier
17903
17904 Concepts:
17905
17906 type-name:
17907 concept-name
17908 partial-concept-id
17909
17910 concept-name:
17911 identifier
17912
17913 Returns a TYPE_DECL for the type. */
17914
17915 static tree
17916 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17917 {
17918 tree type_decl;
17919
17920 /* We can't know yet whether it is a class-name or not. */
17921 cp_parser_parse_tentatively (parser);
17922 /* Try a class-name. */
17923 type_decl = cp_parser_class_name (parser,
17924 typename_keyword_p,
17925 /*template_keyword_p=*/false,
17926 none_type,
17927 /*check_dependency_p=*/true,
17928 /*class_head_p=*/false,
17929 /*is_declaration=*/false);
17930 /* If it's not a class-name, keep looking. */
17931 if (!cp_parser_parse_definitely (parser))
17932 {
17933 if (cxx_dialect < cxx11)
17934 /* It must be a typedef-name or an enum-name. */
17935 return cp_parser_nonclass_name (parser);
17936
17937 cp_parser_parse_tentatively (parser);
17938 /* It is either a simple-template-id representing an
17939 instantiation of an alias template... */
17940 type_decl = cp_parser_template_id (parser,
17941 /*template_keyword_p=*/false,
17942 /*check_dependency_p=*/true,
17943 none_type,
17944 /*is_declaration=*/false);
17945 /* Note that this must be an instantiation of an alias template
17946 because [temp.names]/6 says:
17947
17948 A template-id that names an alias template specialization
17949 is a type-name.
17950
17951 Whereas [temp.names]/7 says:
17952
17953 A simple-template-id that names a class template
17954 specialization is a class-name.
17955
17956 With concepts, this could also be a partial-concept-id that
17957 declares a non-type template parameter. */
17958 if (type_decl != NULL_TREE
17959 && TREE_CODE (type_decl) == TYPE_DECL
17960 && TYPE_DECL_ALIAS_P (type_decl))
17961 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17962 else if (is_constrained_parameter (type_decl))
17963 /* Don't do anything. */ ;
17964 else
17965 cp_parser_simulate_error (parser);
17966
17967 if (!cp_parser_parse_definitely (parser))
17968 /* ... Or a typedef-name or an enum-name. */
17969 return cp_parser_nonclass_name (parser);
17970 }
17971
17972 return type_decl;
17973 }
17974
17975 /* Check if DECL and ARGS can form a constrained-type-specifier.
17976 If ARGS is non-null, we try to form a concept check of the
17977 form DECL<?, ARGS> where ? is a wildcard that matches any
17978 kind of template argument. If ARGS is NULL, then we try to
17979 form a concept check of the form DECL<?>. */
17980
17981 static tree
17982 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17983 tree decl, tree args)
17984 {
17985 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17986
17987 /* If we a constrained-type-specifier cannot be deduced. */
17988 if (parser->prevent_constrained_type_specifiers)
17989 return NULL_TREE;
17990
17991 /* A constrained type specifier can only be found in an
17992 overload set or as a reference to a template declaration.
17993
17994 FIXME: This might be masking a bug. It's possible that
17995 that the deduction below is causing template specializations
17996 to be formed with the wildcard as an argument. */
17997 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17998 return NULL_TREE;
17999
18000 /* Try to build a call expression that evaluates the
18001 concept. This can fail if the overload set refers
18002 only to non-templates. */
18003 tree placeholder = build_nt (WILDCARD_DECL);
18004 tree check = build_concept_check (decl, placeholder, args);
18005 if (check == error_mark_node)
18006 return NULL_TREE;
18007
18008 /* Deduce the checked constraint and the prototype parameter.
18009
18010 FIXME: In certain cases, failure to deduce should be a
18011 diagnosable error. */
18012 tree conc;
18013 tree proto;
18014 if (!deduce_constrained_parameter (check, conc, proto))
18015 return NULL_TREE;
18016
18017 /* In template parameter scope, this results in a constrained
18018 parameter. Return a descriptor of that parm. */
18019 if (processing_template_parmlist)
18020 return build_constrained_parameter (conc, proto, args);
18021
18022 /* In a parameter-declaration-clause, constrained-type
18023 specifiers result in invented template parameters. */
18024 if (parser->auto_is_implicit_function_template_parm_p)
18025 {
18026 tree x = build_constrained_parameter (conc, proto, args);
18027 return synthesize_implicit_template_parm (parser, x);
18028 }
18029 else
18030 {
18031 /* Otherwise, we're in a context where the constrained
18032 type name is deduced and the constraint applies
18033 after deduction. */
18034 return make_constrained_auto (conc, args);
18035 }
18036
18037 return NULL_TREE;
18038 }
18039
18040 /* If DECL refers to a concept, return a TYPE_DECL representing
18041 the result of using the constrained type specifier in the
18042 current context. DECL refers to a concept if
18043
18044 - it is an overload set containing a function concept taking a single
18045 type argument, or
18046
18047 - it is a variable concept taking a single type argument. */
18048
18049 static tree
18050 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
18051 {
18052 if (flag_concepts
18053 && (TREE_CODE (decl) == OVERLOAD
18054 || BASELINK_P (decl)
18055 || variable_concept_p (decl)))
18056 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
18057 else
18058 return NULL_TREE;
18059 }
18060
18061 /* Check if DECL and ARGS form a partial-concept-id. If so,
18062 assign ID to the resulting constrained placeholder.
18063
18064 Returns true if the partial-concept-id designates a placeholder
18065 and false otherwise. Note that *id is set to NULL_TREE in
18066 this case. */
18067
18068 static tree
18069 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
18070 {
18071 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
18072 }
18073
18074 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18075 or a concept-name.
18076
18077 enum-name:
18078 identifier
18079
18080 typedef-name:
18081 identifier
18082
18083 concept-name:
18084 identifier
18085
18086 Returns a TYPE_DECL for the type. */
18087
18088 static tree
18089 cp_parser_nonclass_name (cp_parser* parser)
18090 {
18091 tree type_decl;
18092 tree identifier;
18093
18094 cp_token *token = cp_lexer_peek_token (parser->lexer);
18095 identifier = cp_parser_identifier (parser);
18096 if (identifier == error_mark_node)
18097 return error_mark_node;
18098
18099 /* Look up the type-name. */
18100 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
18101
18102 type_decl = strip_using_decl (type_decl);
18103
18104 /* If we found an overload set, then it may refer to a concept-name. */
18105 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
18106 type_decl = decl;
18107
18108 if (TREE_CODE (type_decl) != TYPE_DECL
18109 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
18110 {
18111 /* See if this is an Objective-C type. */
18112 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18113 tree type = objc_get_protocol_qualified_type (identifier, protos);
18114 if (type)
18115 type_decl = TYPE_NAME (type);
18116 }
18117
18118 /* Issue an error if we did not find a type-name. */
18119 if (TREE_CODE (type_decl) != TYPE_DECL
18120 /* In Objective-C, we have the complication that class names are
18121 normally type names and start declarations (eg, the
18122 "NSObject" in "NSObject *object;"), but can be used in an
18123 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18124 is an expression. So, a classname followed by a dot is not a
18125 valid type-name. */
18126 || (objc_is_class_name (TREE_TYPE (type_decl))
18127 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
18128 {
18129 if (!cp_parser_simulate_error (parser))
18130 cp_parser_name_lookup_error (parser, identifier, type_decl,
18131 NLE_TYPE, token->location);
18132 return error_mark_node;
18133 }
18134 /* Remember that the name was used in the definition of the
18135 current class so that we can check later to see if the
18136 meaning would have been different after the class was
18137 entirely defined. */
18138 else if (type_decl != error_mark_node
18139 && !parser->scope)
18140 maybe_note_name_used_in_class (identifier, type_decl);
18141
18142 return type_decl;
18143 }
18144
18145 /* Parse an elaborated-type-specifier. Note that the grammar given
18146 here incorporates the resolution to DR68.
18147
18148 elaborated-type-specifier:
18149 class-key :: [opt] nested-name-specifier [opt] identifier
18150 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18151 enum-key :: [opt] nested-name-specifier [opt] identifier
18152 typename :: [opt] nested-name-specifier identifier
18153 typename :: [opt] nested-name-specifier template [opt]
18154 template-id
18155
18156 GNU extension:
18157
18158 elaborated-type-specifier:
18159 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18160 class-key attributes :: [opt] nested-name-specifier [opt]
18161 template [opt] template-id
18162 enum attributes :: [opt] nested-name-specifier [opt] identifier
18163
18164 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18165 declared `friend'. If IS_DECLARATION is TRUE, then this
18166 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18167 something is being declared.
18168
18169 Returns the TYPE specified. */
18170
18171 static tree
18172 cp_parser_elaborated_type_specifier (cp_parser* parser,
18173 bool is_friend,
18174 bool is_declaration)
18175 {
18176 enum tag_types tag_type;
18177 tree identifier;
18178 tree type = NULL_TREE;
18179 tree attributes = NULL_TREE;
18180 tree globalscope;
18181 cp_token *token = NULL;
18182
18183 /* See if we're looking at the `enum' keyword. */
18184 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
18185 {
18186 /* Consume the `enum' token. */
18187 cp_lexer_consume_token (parser->lexer);
18188 /* Remember that it's an enumeration type. */
18189 tag_type = enum_type;
18190 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18191 enums) is used here. */
18192 cp_token *token = cp_lexer_peek_token (parser->lexer);
18193 if (cp_parser_is_keyword (token, RID_CLASS)
18194 || cp_parser_is_keyword (token, RID_STRUCT))
18195 {
18196 gcc_rich_location richloc (token->location);
18197 richloc.add_range (input_location);
18198 richloc.add_fixit_remove ();
18199 pedwarn (&richloc, 0, "elaborated-type-specifier for "
18200 "a scoped enum must not use the %qD keyword",
18201 token->u.value);
18202 /* Consume the `struct' or `class' and parse it anyway. */
18203 cp_lexer_consume_token (parser->lexer);
18204 }
18205 /* Parse the attributes. */
18206 attributes = cp_parser_attributes_opt (parser);
18207 }
18208 /* Or, it might be `typename'. */
18209 else if (cp_lexer_next_token_is_keyword (parser->lexer,
18210 RID_TYPENAME))
18211 {
18212 /* Consume the `typename' token. */
18213 cp_lexer_consume_token (parser->lexer);
18214 /* Remember that it's a `typename' type. */
18215 tag_type = typename_type;
18216 }
18217 /* Otherwise it must be a class-key. */
18218 else
18219 {
18220 tag_type = cp_parser_class_key (parser);
18221 if (tag_type == none_type)
18222 return error_mark_node;
18223 /* Parse the attributes. */
18224 attributes = cp_parser_attributes_opt (parser);
18225 }
18226
18227 /* Look for the `::' operator. */
18228 globalscope = cp_parser_global_scope_opt (parser,
18229 /*current_scope_valid_p=*/false);
18230 /* Look for the nested-name-specifier. */
18231 tree nested_name_specifier;
18232 if (tag_type == typename_type && !globalscope)
18233 {
18234 nested_name_specifier
18235 = cp_parser_nested_name_specifier (parser,
18236 /*typename_keyword_p=*/true,
18237 /*check_dependency_p=*/true,
18238 /*type_p=*/true,
18239 is_declaration);
18240 if (!nested_name_specifier)
18241 return error_mark_node;
18242 }
18243 else
18244 /* Even though `typename' is not present, the proposed resolution
18245 to Core Issue 180 says that in `class A<T>::B', `B' should be
18246 considered a type-name, even if `A<T>' is dependent. */
18247 nested_name_specifier
18248 = cp_parser_nested_name_specifier_opt (parser,
18249 /*typename_keyword_p=*/true,
18250 /*check_dependency_p=*/true,
18251 /*type_p=*/true,
18252 is_declaration);
18253 /* For everything but enumeration types, consider a template-id.
18254 For an enumeration type, consider only a plain identifier. */
18255 if (tag_type != enum_type)
18256 {
18257 bool template_p = false;
18258 tree decl;
18259
18260 /* Allow the `template' keyword. */
18261 template_p = cp_parser_optional_template_keyword (parser);
18262 /* If we didn't see `template', we don't know if there's a
18263 template-id or not. */
18264 if (!template_p)
18265 cp_parser_parse_tentatively (parser);
18266 /* The `template' keyword must follow a nested-name-specifier. */
18267 else if (!nested_name_specifier)
18268 {
18269 cp_parser_error (parser, "%<template%> must follow a nested-"
18270 "name-specifier");
18271 return error_mark_node;
18272 }
18273
18274 /* Parse the template-id. */
18275 token = cp_lexer_peek_token (parser->lexer);
18276 decl = cp_parser_template_id (parser, template_p,
18277 /*check_dependency_p=*/true,
18278 tag_type,
18279 is_declaration);
18280 /* If we didn't find a template-id, look for an ordinary
18281 identifier. */
18282 if (!template_p && !cp_parser_parse_definitely (parser))
18283 ;
18284 /* We can get here when cp_parser_template_id, called by
18285 cp_parser_class_name with tag_type == none_type, succeeds
18286 and caches a BASELINK. Then, when called again here,
18287 instead of failing and returning an error_mark_node
18288 returns it (see template/typename17.C in C++11).
18289 ??? Could we diagnose this earlier? */
18290 else if (tag_type == typename_type && BASELINK_P (decl))
18291 {
18292 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18293 type = error_mark_node;
18294 }
18295 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18296 in effect, then we must assume that, upon instantiation, the
18297 template will correspond to a class. */
18298 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18299 && tag_type == typename_type)
18300 type = make_typename_type (parser->scope, decl,
18301 typename_type,
18302 /*complain=*/tf_error);
18303 /* If the `typename' keyword is in effect and DECL is not a type
18304 decl, then type is non existent. */
18305 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18306 ;
18307 else if (TREE_CODE (decl) == TYPE_DECL)
18308 {
18309 type = check_elaborated_type_specifier (tag_type, decl,
18310 /*allow_template_p=*/true);
18311
18312 /* If the next token is a semicolon, this must be a specialization,
18313 instantiation, or friend declaration. Check the scope while we
18314 still know whether or not we had a nested-name-specifier. */
18315 if (type != error_mark_node
18316 && !nested_name_specifier && !is_friend
18317 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18318 check_unqualified_spec_or_inst (type, token->location);
18319 }
18320 else if (decl == error_mark_node)
18321 type = error_mark_node;
18322 }
18323
18324 if (!type)
18325 {
18326 token = cp_lexer_peek_token (parser->lexer);
18327 identifier = cp_parser_identifier (parser);
18328
18329 if (identifier == error_mark_node)
18330 {
18331 parser->scope = NULL_TREE;
18332 return error_mark_node;
18333 }
18334
18335 /* For a `typename', we needn't call xref_tag. */
18336 if (tag_type == typename_type
18337 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18338 return cp_parser_make_typename_type (parser, identifier,
18339 token->location);
18340
18341 /* Template parameter lists apply only if we are not within a
18342 function parameter list. */
18343 bool template_parm_lists_apply
18344 = parser->num_template_parameter_lists;
18345 if (template_parm_lists_apply)
18346 for (cp_binding_level *s = current_binding_level;
18347 s && s->kind != sk_template_parms;
18348 s = s->level_chain)
18349 if (s->kind == sk_function_parms)
18350 template_parm_lists_apply = false;
18351
18352 /* Look up a qualified name in the usual way. */
18353 if (parser->scope)
18354 {
18355 tree decl;
18356 tree ambiguous_decls;
18357
18358 decl = cp_parser_lookup_name (parser, identifier,
18359 tag_type,
18360 /*is_template=*/false,
18361 /*is_namespace=*/false,
18362 /*check_dependency=*/true,
18363 &ambiguous_decls,
18364 token->location);
18365
18366 /* If the lookup was ambiguous, an error will already have been
18367 issued. */
18368 if (ambiguous_decls)
18369 return error_mark_node;
18370
18371 /* If we are parsing friend declaration, DECL may be a
18372 TEMPLATE_DECL tree node here. However, we need to check
18373 whether this TEMPLATE_DECL results in valid code. Consider
18374 the following example:
18375
18376 namespace N {
18377 template <class T> class C {};
18378 }
18379 class X {
18380 template <class T> friend class N::C; // #1, valid code
18381 };
18382 template <class T> class Y {
18383 friend class N::C; // #2, invalid code
18384 };
18385
18386 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18387 name lookup of `N::C'. We see that friend declaration must
18388 be template for the code to be valid. Note that
18389 processing_template_decl does not work here since it is
18390 always 1 for the above two cases. */
18391
18392 decl = (cp_parser_maybe_treat_template_as_class
18393 (decl, /*tag_name_p=*/is_friend
18394 && template_parm_lists_apply));
18395
18396 if (TREE_CODE (decl) != TYPE_DECL)
18397 {
18398 cp_parser_diagnose_invalid_type_name (parser,
18399 identifier,
18400 token->location);
18401 return error_mark_node;
18402 }
18403
18404 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18405 {
18406 bool allow_template = (template_parm_lists_apply
18407 || DECL_SELF_REFERENCE_P (decl));
18408 type = check_elaborated_type_specifier (tag_type, decl,
18409 allow_template);
18410
18411 if (type == error_mark_node)
18412 return error_mark_node;
18413 }
18414
18415 /* Forward declarations of nested types, such as
18416
18417 class C1::C2;
18418 class C1::C2::C3;
18419
18420 are invalid unless all components preceding the final '::'
18421 are complete. If all enclosing types are complete, these
18422 declarations become merely pointless.
18423
18424 Invalid forward declarations of nested types are errors
18425 caught elsewhere in parsing. Those that are pointless arrive
18426 here. */
18427
18428 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18429 && !is_friend && !processing_explicit_instantiation)
18430 warning (0, "declaration %qD does not declare anything", decl);
18431
18432 type = TREE_TYPE (decl);
18433 }
18434 else
18435 {
18436 /* An elaborated-type-specifier sometimes introduces a new type and
18437 sometimes names an existing type. Normally, the rule is that it
18438 introduces a new type only if there is not an existing type of
18439 the same name already in scope. For example, given:
18440
18441 struct S {};
18442 void f() { struct S s; }
18443
18444 the `struct S' in the body of `f' is the same `struct S' as in
18445 the global scope; the existing definition is used. However, if
18446 there were no global declaration, this would introduce a new
18447 local class named `S'.
18448
18449 An exception to this rule applies to the following code:
18450
18451 namespace N { struct S; }
18452
18453 Here, the elaborated-type-specifier names a new type
18454 unconditionally; even if there is already an `S' in the
18455 containing scope this declaration names a new type.
18456 This exception only applies if the elaborated-type-specifier
18457 forms the complete declaration:
18458
18459 [class.name]
18460
18461 A declaration consisting solely of `class-key identifier ;' is
18462 either a redeclaration of the name in the current scope or a
18463 forward declaration of the identifier as a class name. It
18464 introduces the name into the current scope.
18465
18466 We are in this situation precisely when the next token is a `;'.
18467
18468 An exception to the exception is that a `friend' declaration does
18469 *not* name a new type; i.e., given:
18470
18471 struct S { friend struct T; };
18472
18473 `T' is not a new type in the scope of `S'.
18474
18475 Also, `new struct S' or `sizeof (struct S)' never results in the
18476 definition of a new type; a new type can only be declared in a
18477 declaration context. */
18478
18479 tag_scope ts;
18480 bool template_p;
18481
18482 if (is_friend)
18483 /* Friends have special name lookup rules. */
18484 ts = ts_within_enclosing_non_class;
18485 else if (is_declaration
18486 && cp_lexer_next_token_is (parser->lexer,
18487 CPP_SEMICOLON))
18488 /* This is a `class-key identifier ;' */
18489 ts = ts_current;
18490 else
18491 ts = ts_global;
18492
18493 template_p =
18494 (template_parm_lists_apply
18495 && (cp_parser_next_token_starts_class_definition_p (parser)
18496 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18497 /* An unqualified name was used to reference this type, so
18498 there were no qualifying templates. */
18499 if (template_parm_lists_apply
18500 && !cp_parser_check_template_parameters (parser,
18501 /*num_templates=*/0,
18502 /*template_id*/false,
18503 token->location,
18504 /*declarator=*/NULL))
18505 return error_mark_node;
18506 type = xref_tag (tag_type, identifier, ts, template_p);
18507 }
18508 }
18509
18510 if (type == error_mark_node)
18511 return error_mark_node;
18512
18513 /* Allow attributes on forward declarations of classes. */
18514 if (attributes)
18515 {
18516 if (TREE_CODE (type) == TYPENAME_TYPE)
18517 warning (OPT_Wattributes,
18518 "attributes ignored on uninstantiated type");
18519 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18520 && ! processing_explicit_instantiation)
18521 warning (OPT_Wattributes,
18522 "attributes ignored on template instantiation");
18523 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18524 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18525 else
18526 warning (OPT_Wattributes,
18527 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18528 }
18529
18530 if (tag_type != enum_type)
18531 {
18532 /* Indicate whether this class was declared as a `class' or as a
18533 `struct'. */
18534 if (CLASS_TYPE_P (type))
18535 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18536 cp_parser_check_class_key (tag_type, type);
18537 }
18538
18539 /* A "<" cannot follow an elaborated type specifier. If that
18540 happens, the user was probably trying to form a template-id. */
18541 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18542 token->location);
18543
18544 return type;
18545 }
18546
18547 /* Parse an enum-specifier.
18548
18549 enum-specifier:
18550 enum-head { enumerator-list [opt] }
18551 enum-head { enumerator-list , } [C++0x]
18552
18553 enum-head:
18554 enum-key identifier [opt] enum-base [opt]
18555 enum-key nested-name-specifier identifier enum-base [opt]
18556
18557 enum-key:
18558 enum
18559 enum class [C++0x]
18560 enum struct [C++0x]
18561
18562 enum-base: [C++0x]
18563 : type-specifier-seq
18564
18565 opaque-enum-specifier:
18566 enum-key identifier enum-base [opt] ;
18567
18568 GNU Extensions:
18569 enum-key attributes[opt] identifier [opt] enum-base [opt]
18570 { enumerator-list [opt] }attributes[opt]
18571 enum-key attributes[opt] identifier [opt] enum-base [opt]
18572 { enumerator-list, }attributes[opt] [C++0x]
18573
18574 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18575 if the token stream isn't an enum-specifier after all. */
18576
18577 static tree
18578 cp_parser_enum_specifier (cp_parser* parser)
18579 {
18580 tree identifier;
18581 tree type = NULL_TREE;
18582 tree prev_scope;
18583 tree nested_name_specifier = NULL_TREE;
18584 tree attributes;
18585 bool scoped_enum_p = false;
18586 bool has_underlying_type = false;
18587 bool nested_being_defined = false;
18588 bool new_value_list = false;
18589 bool is_new_type = false;
18590 bool is_unnamed = false;
18591 tree underlying_type = NULL_TREE;
18592 cp_token *type_start_token = NULL;
18593 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18594
18595 parser->colon_corrects_to_scope_p = false;
18596
18597 /* Parse tentatively so that we can back up if we don't find a
18598 enum-specifier. */
18599 cp_parser_parse_tentatively (parser);
18600
18601 /* Caller guarantees that the current token is 'enum', an identifier
18602 possibly follows, and the token after that is an opening brace.
18603 If we don't have an identifier, fabricate an anonymous name for
18604 the enumeration being defined. */
18605 cp_lexer_consume_token (parser->lexer);
18606
18607 /* Parse the "class" or "struct", which indicates a scoped
18608 enumeration type in C++0x. */
18609 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18610 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18611 {
18612 if (cxx_dialect < cxx11)
18613 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18614
18615 /* Consume the `struct' or `class' token. */
18616 cp_lexer_consume_token (parser->lexer);
18617
18618 scoped_enum_p = true;
18619 }
18620
18621 attributes = cp_parser_attributes_opt (parser);
18622
18623 /* Clear the qualification. */
18624 parser->scope = NULL_TREE;
18625 parser->qualifying_scope = NULL_TREE;
18626 parser->object_scope = NULL_TREE;
18627
18628 /* Figure out in what scope the declaration is being placed. */
18629 prev_scope = current_scope ();
18630
18631 type_start_token = cp_lexer_peek_token (parser->lexer);
18632
18633 push_deferring_access_checks (dk_no_check);
18634 nested_name_specifier
18635 = cp_parser_nested_name_specifier_opt (parser,
18636 /*typename_keyword_p=*/true,
18637 /*check_dependency_p=*/false,
18638 /*type_p=*/false,
18639 /*is_declaration=*/false);
18640
18641 if (nested_name_specifier)
18642 {
18643 tree name;
18644
18645 identifier = cp_parser_identifier (parser);
18646 name = cp_parser_lookup_name (parser, identifier,
18647 enum_type,
18648 /*is_template=*/false,
18649 /*is_namespace=*/false,
18650 /*check_dependency=*/true,
18651 /*ambiguous_decls=*/NULL,
18652 input_location);
18653 if (name && name != error_mark_node)
18654 {
18655 type = TREE_TYPE (name);
18656 if (TREE_CODE (type) == TYPENAME_TYPE)
18657 {
18658 /* Are template enums allowed in ISO? */
18659 if (template_parm_scope_p ())
18660 pedwarn (type_start_token->location, OPT_Wpedantic,
18661 "%qD is an enumeration template", name);
18662 /* ignore a typename reference, for it will be solved by name
18663 in start_enum. */
18664 type = NULL_TREE;
18665 }
18666 }
18667 else if (nested_name_specifier == error_mark_node)
18668 /* We already issued an error. */;
18669 else
18670 {
18671 error_at (type_start_token->location,
18672 "%qD does not name an enumeration in %qT",
18673 identifier, nested_name_specifier);
18674 nested_name_specifier = error_mark_node;
18675 }
18676 }
18677 else
18678 {
18679 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18680 identifier = cp_parser_identifier (parser);
18681 else
18682 {
18683 identifier = make_anon_name ();
18684 is_unnamed = true;
18685 if (scoped_enum_p)
18686 error_at (type_start_token->location,
18687 "unnamed scoped enum is not allowed");
18688 }
18689 }
18690 pop_deferring_access_checks ();
18691
18692 /* Check for the `:' that denotes a specified underlying type in C++0x.
18693 Note that a ':' could also indicate a bitfield width, however. */
18694 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18695 {
18696 cp_decl_specifier_seq type_specifiers;
18697
18698 /* Consume the `:'. */
18699 cp_lexer_consume_token (parser->lexer);
18700
18701 /* Parse the type-specifier-seq. */
18702 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
18703 /*is_declaration=*/false,
18704 /*is_trailing_return=*/false,
18705 &type_specifiers);
18706
18707 /* At this point this is surely not elaborated type specifier. */
18708 if (!cp_parser_parse_definitely (parser))
18709 return NULL_TREE;
18710
18711 if (cxx_dialect < cxx11)
18712 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18713
18714 has_underlying_type = true;
18715
18716 /* If that didn't work, stop. */
18717 if (type_specifiers.type != error_mark_node)
18718 {
18719 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18720 /*initialized=*/0, NULL);
18721 if (underlying_type == error_mark_node
18722 || check_for_bare_parameter_packs (underlying_type))
18723 underlying_type = NULL_TREE;
18724 }
18725 }
18726
18727 /* Look for the `{' but don't consume it yet. */
18728 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18729 {
18730 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18731 {
18732 cp_parser_error (parser, "expected %<{%>");
18733 if (has_underlying_type)
18734 {
18735 type = NULL_TREE;
18736 goto out;
18737 }
18738 }
18739 /* An opaque-enum-specifier must have a ';' here. */
18740 if ((scoped_enum_p || underlying_type)
18741 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18742 {
18743 cp_parser_error (parser, "expected %<;%> or %<{%>");
18744 if (has_underlying_type)
18745 {
18746 type = NULL_TREE;
18747 goto out;
18748 }
18749 }
18750 }
18751
18752 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18753 return NULL_TREE;
18754
18755 if (nested_name_specifier)
18756 {
18757 if (CLASS_TYPE_P (nested_name_specifier))
18758 {
18759 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18760 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18761 push_scope (nested_name_specifier);
18762 }
18763 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18764 {
18765 push_nested_namespace (nested_name_specifier);
18766 }
18767 }
18768
18769 /* Issue an error message if type-definitions are forbidden here. */
18770 if (!cp_parser_check_type_definition (parser))
18771 type = error_mark_node;
18772 else
18773 /* Create the new type. We do this before consuming the opening
18774 brace so the enum will be recorded as being on the line of its
18775 tag (or the 'enum' keyword, if there is no tag). */
18776 type = start_enum (identifier, type, underlying_type,
18777 attributes, scoped_enum_p, &is_new_type);
18778
18779 /* If the next token is not '{' it is an opaque-enum-specifier or an
18780 elaborated-type-specifier. */
18781 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18782 {
18783 timevar_push (TV_PARSE_ENUM);
18784 if (nested_name_specifier
18785 && nested_name_specifier != error_mark_node)
18786 {
18787 /* The following catches invalid code such as:
18788 enum class S<int>::E { A, B, C }; */
18789 if (!processing_specialization
18790 && CLASS_TYPE_P (nested_name_specifier)
18791 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18792 error_at (type_start_token->location, "cannot add an enumerator "
18793 "list to a template instantiation");
18794
18795 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18796 {
18797 error_at (type_start_token->location,
18798 "%<%T::%E%> has not been declared",
18799 TYPE_CONTEXT (nested_name_specifier),
18800 nested_name_specifier);
18801 type = error_mark_node;
18802 }
18803 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18804 && !CLASS_TYPE_P (nested_name_specifier))
18805 {
18806 error_at (type_start_token->location, "nested name specifier "
18807 "%qT for enum declaration does not name a class "
18808 "or namespace", nested_name_specifier);
18809 type = error_mark_node;
18810 }
18811 /* If that scope does not contain the scope in which the
18812 class was originally declared, the program is invalid. */
18813 else if (prev_scope && !is_ancestor (prev_scope,
18814 nested_name_specifier))
18815 {
18816 if (at_namespace_scope_p ())
18817 error_at (type_start_token->location,
18818 "declaration of %qD in namespace %qD which does not "
18819 "enclose %qD",
18820 type, prev_scope, nested_name_specifier);
18821 else
18822 error_at (type_start_token->location,
18823 "declaration of %qD in %qD which does not "
18824 "enclose %qD",
18825 type, prev_scope, nested_name_specifier);
18826 type = error_mark_node;
18827 }
18828 /* If that scope is the scope where the declaration is being placed
18829 the program is invalid. */
18830 else if (CLASS_TYPE_P (nested_name_specifier)
18831 && CLASS_TYPE_P (prev_scope)
18832 && same_type_p (nested_name_specifier, prev_scope))
18833 {
18834 permerror (type_start_token->location,
18835 "extra qualification not allowed");
18836 nested_name_specifier = NULL_TREE;
18837 }
18838 }
18839
18840 if (scoped_enum_p)
18841 begin_scope (sk_scoped_enum, type);
18842
18843 /* Consume the opening brace. */
18844 matching_braces braces;
18845 braces.consume_open (parser);
18846
18847 if (type == error_mark_node)
18848 ; /* Nothing to add */
18849 else if (OPAQUE_ENUM_P (type)
18850 || (cxx_dialect > cxx98 && processing_specialization))
18851 {
18852 new_value_list = true;
18853 SET_OPAQUE_ENUM_P (type, false);
18854 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18855 }
18856 else
18857 {
18858 error_at (type_start_token->location,
18859 "multiple definition of %q#T", type);
18860 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18861 "previous definition here");
18862 type = error_mark_node;
18863 }
18864
18865 if (type == error_mark_node)
18866 cp_parser_skip_to_end_of_block_or_statement (parser);
18867 /* If the next token is not '}', then there are some enumerators. */
18868 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18869 {
18870 if (is_unnamed && !scoped_enum_p)
18871 pedwarn (type_start_token->location, OPT_Wpedantic,
18872 "ISO C++ forbids empty unnamed enum");
18873 }
18874 else
18875 cp_parser_enumerator_list (parser, type);
18876
18877 /* Consume the final '}'. */
18878 braces.require_close (parser);
18879
18880 if (scoped_enum_p)
18881 finish_scope ();
18882 timevar_pop (TV_PARSE_ENUM);
18883 }
18884 else
18885 {
18886 /* If a ';' follows, then it is an opaque-enum-specifier
18887 and additional restrictions apply. */
18888 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18889 {
18890 if (is_unnamed)
18891 error_at (type_start_token->location,
18892 "opaque-enum-specifier without name");
18893 else if (nested_name_specifier)
18894 error_at (type_start_token->location,
18895 "opaque-enum-specifier must use a simple identifier");
18896 }
18897 }
18898
18899 /* Look for trailing attributes to apply to this enumeration, and
18900 apply them if appropriate. */
18901 if (cp_parser_allow_gnu_extensions_p (parser))
18902 {
18903 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18904 cplus_decl_attributes (&type,
18905 trailing_attr,
18906 (int) ATTR_FLAG_TYPE_IN_PLACE);
18907 }
18908
18909 /* Finish up the enumeration. */
18910 if (type != error_mark_node)
18911 {
18912 if (new_value_list)
18913 finish_enum_value_list (type);
18914 if (is_new_type)
18915 finish_enum (type);
18916 }
18917
18918 if (nested_name_specifier)
18919 {
18920 if (CLASS_TYPE_P (nested_name_specifier))
18921 {
18922 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18923 pop_scope (nested_name_specifier);
18924 }
18925 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18926 {
18927 pop_nested_namespace (nested_name_specifier);
18928 }
18929 }
18930 out:
18931 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18932 return type;
18933 }
18934
18935 /* Parse an enumerator-list. The enumerators all have the indicated
18936 TYPE.
18937
18938 enumerator-list:
18939 enumerator-definition
18940 enumerator-list , enumerator-definition */
18941
18942 static void
18943 cp_parser_enumerator_list (cp_parser* parser, tree type)
18944 {
18945 while (true)
18946 {
18947 /* Parse an enumerator-definition. */
18948 cp_parser_enumerator_definition (parser, type);
18949
18950 /* If the next token is not a ',', we've reached the end of
18951 the list. */
18952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18953 break;
18954 /* Otherwise, consume the `,' and keep going. */
18955 cp_lexer_consume_token (parser->lexer);
18956 /* If the next token is a `}', there is a trailing comma. */
18957 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18958 {
18959 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18960 pedwarn (input_location, OPT_Wpedantic,
18961 "comma at end of enumerator list");
18962 break;
18963 }
18964 }
18965 }
18966
18967 /* Parse an enumerator-definition. The enumerator has the indicated
18968 TYPE.
18969
18970 enumerator-definition:
18971 enumerator
18972 enumerator = constant-expression
18973
18974 enumerator:
18975 identifier
18976
18977 GNU Extensions:
18978
18979 enumerator-definition:
18980 enumerator attributes [opt]
18981 enumerator attributes [opt] = constant-expression */
18982
18983 static void
18984 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18985 {
18986 tree identifier;
18987 tree value;
18988 location_t loc;
18989
18990 /* Save the input location because we are interested in the location
18991 of the identifier and not the location of the explicit value. */
18992 loc = cp_lexer_peek_token (parser->lexer)->location;
18993
18994 /* Look for the identifier. */
18995 identifier = cp_parser_identifier (parser);
18996 if (identifier == error_mark_node)
18997 return;
18998
18999 /* Parse any specified attributes. */
19000 tree attrs = cp_parser_attributes_opt (parser);
19001
19002 /* If the next token is an '=', then there is an explicit value. */
19003 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19004 {
19005 /* Consume the `=' token. */
19006 cp_lexer_consume_token (parser->lexer);
19007 /* Parse the value. */
19008 value = cp_parser_constant_expression (parser);
19009 }
19010 else
19011 value = NULL_TREE;
19012
19013 /* If we are processing a template, make sure the initializer of the
19014 enumerator doesn't contain any bare template parameter pack. */
19015 if (check_for_bare_parameter_packs (value))
19016 value = error_mark_node;
19017
19018 /* Create the enumerator. */
19019 build_enumerator (identifier, value, type, attrs, loc);
19020 }
19021
19022 /* Parse a namespace-name.
19023
19024 namespace-name:
19025 original-namespace-name
19026 namespace-alias
19027
19028 Returns the NAMESPACE_DECL for the namespace. */
19029
19030 static tree
19031 cp_parser_namespace_name (cp_parser* parser)
19032 {
19033 tree identifier;
19034 tree namespace_decl;
19035
19036 cp_token *token = cp_lexer_peek_token (parser->lexer);
19037
19038 /* Get the name of the namespace. */
19039 identifier = cp_parser_identifier (parser);
19040 if (identifier == error_mark_node)
19041 return error_mark_node;
19042
19043 /* Look up the identifier in the currently active scope. Look only
19044 for namespaces, due to:
19045
19046 [basic.lookup.udir]
19047
19048 When looking up a namespace-name in a using-directive or alias
19049 definition, only namespace names are considered.
19050
19051 And:
19052
19053 [basic.lookup.qual]
19054
19055 During the lookup of a name preceding the :: scope resolution
19056 operator, object, function, and enumerator names are ignored.
19057
19058 (Note that cp_parser_qualifying_entity only calls this
19059 function if the token after the name is the scope resolution
19060 operator.) */
19061 namespace_decl = cp_parser_lookup_name (parser, identifier,
19062 none_type,
19063 /*is_template=*/false,
19064 /*is_namespace=*/true,
19065 /*check_dependency=*/true,
19066 /*ambiguous_decls=*/NULL,
19067 token->location);
19068 /* If it's not a namespace, issue an error. */
19069 if (namespace_decl == error_mark_node
19070 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
19071 {
19072 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19073 {
19074 auto_diagnostic_group d;
19075 name_hint hint;
19076 if (namespace_decl == error_mark_node
19077 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
19078 hint = suggest_alternative_in_explicit_scope (token->location,
19079 identifier,
19080 parser->scope);
19081 if (const char *suggestion = hint.suggestion ())
19082 {
19083 gcc_rich_location richloc (token->location);
19084 richloc.add_fixit_replace (suggestion);
19085 error_at (&richloc,
19086 "%qD is not a namespace-name; did you mean %qs?",
19087 identifier, suggestion);
19088 }
19089 else
19090 error_at (token->location, "%qD is not a namespace-name",
19091 identifier);
19092 }
19093 else
19094 cp_parser_error (parser, "expected namespace-name");
19095 namespace_decl = error_mark_node;
19096 }
19097
19098 return namespace_decl;
19099 }
19100
19101 /* Parse a namespace-definition.
19102
19103 namespace-definition:
19104 named-namespace-definition
19105 unnamed-namespace-definition
19106
19107 named-namespace-definition:
19108 original-namespace-definition
19109 extension-namespace-definition
19110
19111 original-namespace-definition:
19112 namespace identifier { namespace-body }
19113
19114 extension-namespace-definition:
19115 namespace original-namespace-name { namespace-body }
19116
19117 unnamed-namespace-definition:
19118 namespace { namespace-body } */
19119
19120 static void
19121 cp_parser_namespace_definition (cp_parser* parser)
19122 {
19123 tree identifier;
19124 int nested_definition_count = 0;
19125
19126 cp_ensure_no_omp_declare_simd (parser);
19127 cp_ensure_no_oacc_routine (parser);
19128
19129 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
19130 const bool topmost_inline_p = is_inline;
19131
19132 if (is_inline)
19133 {
19134 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
19135 cp_lexer_consume_token (parser->lexer);
19136 }
19137
19138 /* Look for the `namespace' keyword. */
19139 cp_token* token
19140 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19141
19142 /* Parse any specified attributes before the identifier. */
19143 tree attribs = cp_parser_attributes_opt (parser);
19144
19145 for (;;)
19146 {
19147 identifier = NULL_TREE;
19148
19149 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
19150 RID_INLINE);
19151 if (nested_inline_p && nested_definition_count != 0)
19152 {
19153 if (cxx_dialect < cxx2a)
19154 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
19155 OPT_Wpedantic, "nested inline namespace definitions only "
19156 "available with -std=c++2a or -std=gnu++2a");
19157 cp_lexer_consume_token (parser->lexer);
19158 }
19159
19160 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19161 {
19162 identifier = cp_parser_identifier (parser);
19163
19164 if (cp_next_tokens_can_be_std_attribute_p (parser))
19165 pedwarn (input_location, OPT_Wpedantic,
19166 "standard attributes on namespaces must precede "
19167 "the namespace name");
19168
19169 /* Parse any attributes specified after the identifier. */
19170 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
19171 }
19172
19173 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19174 {
19175 /* Don't forget that the innermost namespace might have been
19176 marked as inline. Use |= because we cannot overwrite
19177 IS_INLINE in case the outermost namespace is inline, but
19178 there are no nested inlines. */
19179 is_inline |= nested_inline_p;
19180 break;
19181 }
19182
19183 if (!nested_definition_count && cxx_dialect < cxx17)
19184 pedwarn (input_location, OPT_Wpedantic,
19185 "nested namespace definitions only available with "
19186 "-std=c++17 or -std=gnu++17");
19187
19188 /* Nested namespace names can create new namespaces (unlike
19189 other qualified-ids). */
19190 if (int count = (identifier
19191 ? push_namespace (identifier, nested_inline_p)
19192 : 0))
19193 nested_definition_count += count;
19194 else
19195 cp_parser_error (parser, "nested namespace name required");
19196 cp_lexer_consume_token (parser->lexer);
19197 }
19198
19199 if (nested_definition_count && !identifier)
19200 cp_parser_error (parser, "namespace name required");
19201
19202 if (nested_definition_count && attribs)
19203 error_at (token->location,
19204 "a nested namespace definition cannot have attributes");
19205 if (nested_definition_count && topmost_inline_p)
19206 error_at (token->location,
19207 "a nested namespace definition cannot be inline");
19208
19209 /* Start the namespace. */
19210 nested_definition_count += push_namespace (identifier, is_inline);
19211
19212 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
19213
19214 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
19215
19216 /* Look for the `{' to validate starting the namespace. */
19217 matching_braces braces;
19218 if (braces.require_open (parser))
19219 {
19220 /* Parse the body of the namespace. */
19221 cp_parser_namespace_body (parser);
19222
19223 /* Look for the final `}'. */
19224 braces.require_close (parser);
19225 }
19226
19227 if (has_visibility)
19228 pop_visibility (1);
19229
19230 /* Pop the nested namespace definitions. */
19231 while (nested_definition_count--)
19232 pop_namespace ();
19233 }
19234
19235 /* Parse a namespace-body.
19236
19237 namespace-body:
19238 declaration-seq [opt] */
19239
19240 static void
19241 cp_parser_namespace_body (cp_parser* parser)
19242 {
19243 cp_parser_declaration_seq_opt (parser);
19244 }
19245
19246 /* Parse a namespace-alias-definition.
19247
19248 namespace-alias-definition:
19249 namespace identifier = qualified-namespace-specifier ; */
19250
19251 static void
19252 cp_parser_namespace_alias_definition (cp_parser* parser)
19253 {
19254 tree identifier;
19255 tree namespace_specifier;
19256
19257 cp_token *token = cp_lexer_peek_token (parser->lexer);
19258
19259 /* Look for the `namespace' keyword. */
19260 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19261 /* Look for the identifier. */
19262 identifier = cp_parser_identifier (parser);
19263 if (identifier == error_mark_node)
19264 return;
19265 /* Look for the `=' token. */
19266 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
19267 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19268 {
19269 error_at (token->location, "%<namespace%> definition is not allowed here");
19270 /* Skip the definition. */
19271 cp_lexer_consume_token (parser->lexer);
19272 if (cp_parser_skip_to_closing_brace (parser))
19273 cp_lexer_consume_token (parser->lexer);
19274 return;
19275 }
19276 cp_parser_require (parser, CPP_EQ, RT_EQ);
19277 /* Look for the qualified-namespace-specifier. */
19278 namespace_specifier
19279 = cp_parser_qualified_namespace_specifier (parser);
19280 /* Look for the `;' token. */
19281 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19282
19283 /* Register the alias in the symbol table. */
19284 do_namespace_alias (identifier, namespace_specifier);
19285 }
19286
19287 /* Parse a qualified-namespace-specifier.
19288
19289 qualified-namespace-specifier:
19290 :: [opt] nested-name-specifier [opt] namespace-name
19291
19292 Returns a NAMESPACE_DECL corresponding to the specified
19293 namespace. */
19294
19295 static tree
19296 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19297 {
19298 /* Look for the optional `::'. */
19299 cp_parser_global_scope_opt (parser,
19300 /*current_scope_valid_p=*/false);
19301
19302 /* Look for the optional nested-name-specifier. */
19303 cp_parser_nested_name_specifier_opt (parser,
19304 /*typename_keyword_p=*/false,
19305 /*check_dependency_p=*/true,
19306 /*type_p=*/false,
19307 /*is_declaration=*/true);
19308
19309 return cp_parser_namespace_name (parser);
19310 }
19311
19312 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19313 access declaration.
19314
19315 using-declaration:
19316 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19317 using :: unqualified-id ;
19318
19319 access-declaration:
19320 qualified-id ;
19321
19322 */
19323
19324 static bool
19325 cp_parser_using_declaration (cp_parser* parser,
19326 bool access_declaration_p)
19327 {
19328 cp_token *token;
19329 bool typename_p = false;
19330 bool global_scope_p;
19331 tree decl;
19332 tree identifier;
19333 tree qscope;
19334 int oldcount = errorcount;
19335 cp_token *diag_token = NULL;
19336
19337 if (access_declaration_p)
19338 {
19339 diag_token = cp_lexer_peek_token (parser->lexer);
19340 cp_parser_parse_tentatively (parser);
19341 }
19342 else
19343 {
19344 /* Look for the `using' keyword. */
19345 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19346
19347 again:
19348 /* Peek at the next token. */
19349 token = cp_lexer_peek_token (parser->lexer);
19350 /* See if it's `typename'. */
19351 if (token->keyword == RID_TYPENAME)
19352 {
19353 /* Remember that we've seen it. */
19354 typename_p = true;
19355 /* Consume the `typename' token. */
19356 cp_lexer_consume_token (parser->lexer);
19357 }
19358 }
19359
19360 /* Look for the optional global scope qualification. */
19361 global_scope_p
19362 = (cp_parser_global_scope_opt (parser,
19363 /*current_scope_valid_p=*/false)
19364 != NULL_TREE);
19365
19366 /* If we saw `typename', or didn't see `::', then there must be a
19367 nested-name-specifier present. */
19368 if (typename_p || !global_scope_p)
19369 {
19370 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19371 /*check_dependency_p=*/true,
19372 /*type_p=*/false,
19373 /*is_declaration=*/true);
19374 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19375 {
19376 cp_parser_skip_to_end_of_block_or_statement (parser);
19377 return false;
19378 }
19379 }
19380 /* Otherwise, we could be in either of the two productions. In that
19381 case, treat the nested-name-specifier as optional. */
19382 else
19383 qscope = cp_parser_nested_name_specifier_opt (parser,
19384 /*typename_keyword_p=*/false,
19385 /*check_dependency_p=*/true,
19386 /*type_p=*/false,
19387 /*is_declaration=*/true);
19388 if (!qscope)
19389 qscope = global_namespace;
19390 else if (UNSCOPED_ENUM_P (qscope))
19391 qscope = CP_TYPE_CONTEXT (qscope);
19392
19393 if (access_declaration_p && cp_parser_error_occurred (parser))
19394 /* Something has already gone wrong; there's no need to parse
19395 further. Since an error has occurred, the return value of
19396 cp_parser_parse_definitely will be false, as required. */
19397 return cp_parser_parse_definitely (parser);
19398
19399 token = cp_lexer_peek_token (parser->lexer);
19400 /* Parse the unqualified-id. */
19401 identifier = cp_parser_unqualified_id (parser,
19402 /*template_keyword_p=*/false,
19403 /*check_dependency_p=*/true,
19404 /*declarator_p=*/true,
19405 /*optional_p=*/false);
19406
19407 if (access_declaration_p)
19408 {
19409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19410 cp_parser_simulate_error (parser);
19411 if (!cp_parser_parse_definitely (parser))
19412 return false;
19413 }
19414 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19415 {
19416 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19417 if (cxx_dialect < cxx17
19418 && !in_system_header_at (ell->location))
19419 pedwarn (ell->location, 0,
19420 "pack expansion in using-declaration only available "
19421 "with -std=c++17 or -std=gnu++17");
19422 qscope = make_pack_expansion (qscope);
19423 }
19424
19425 /* The function we call to handle a using-declaration is different
19426 depending on what scope we are in. */
19427 if (qscope == error_mark_node || identifier == error_mark_node)
19428 ;
19429 else if (!identifier_p (identifier)
19430 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19431 /* [namespace.udecl]
19432
19433 A using declaration shall not name a template-id. */
19434 error_at (token->location,
19435 "a template-id may not appear in a using-declaration");
19436 else
19437 {
19438 if (at_class_scope_p ())
19439 {
19440 /* Create the USING_DECL. */
19441 decl = do_class_using_decl (qscope, identifier);
19442
19443 if (decl && typename_p)
19444 USING_DECL_TYPENAME_P (decl) = 1;
19445
19446 if (check_for_bare_parameter_packs (decl))
19447 {
19448 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19449 return false;
19450 }
19451 else
19452 /* Add it to the list of members in this class. */
19453 finish_member_declaration (decl);
19454 }
19455 else
19456 {
19457 decl = cp_parser_lookup_name_simple (parser,
19458 identifier,
19459 token->location);
19460 if (decl == error_mark_node)
19461 cp_parser_name_lookup_error (parser, identifier,
19462 decl, NLE_NULL,
19463 token->location);
19464 else if (check_for_bare_parameter_packs (decl))
19465 {
19466 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19467 return false;
19468 }
19469 else if (!at_namespace_scope_p ())
19470 finish_local_using_decl (decl, qscope, identifier);
19471 else
19472 finish_namespace_using_decl (decl, qscope, identifier);
19473 }
19474 }
19475
19476 if (!access_declaration_p
19477 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19478 {
19479 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19480 if (cxx_dialect < cxx17)
19481 pedwarn (comma->location, 0,
19482 "comma-separated list in using-declaration only available "
19483 "with -std=c++17 or -std=gnu++17");
19484 goto again;
19485 }
19486
19487 /* Look for the final `;'. */
19488 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19489
19490 if (access_declaration_p && errorcount == oldcount)
19491 warning_at (diag_token->location, OPT_Wdeprecated,
19492 "access declarations are deprecated "
19493 "in favour of using-declarations; "
19494 "suggestion: add the %<using%> keyword");
19495
19496 return true;
19497 }
19498
19499 /* Parse an alias-declaration.
19500
19501 alias-declaration:
19502 using identifier attribute-specifier-seq [opt] = type-id */
19503
19504 static tree
19505 cp_parser_alias_declaration (cp_parser* parser)
19506 {
19507 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19508 location_t id_location, type_location;
19509 cp_declarator *declarator;
19510 cp_decl_specifier_seq decl_specs;
19511 bool member_p;
19512 const char *saved_message = NULL;
19513
19514 /* Look for the `using' keyword. */
19515 cp_token *using_token
19516 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19517 if (using_token == NULL)
19518 return error_mark_node;
19519
19520 id_location = cp_lexer_peek_token (parser->lexer)->location;
19521 id = cp_parser_identifier (parser);
19522 if (id == error_mark_node)
19523 return error_mark_node;
19524
19525 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19526 attributes = cp_parser_attributes_opt (parser);
19527 if (attributes == error_mark_node)
19528 return error_mark_node;
19529
19530 cp_parser_require (parser, CPP_EQ, RT_EQ);
19531
19532 if (cp_parser_error_occurred (parser))
19533 return error_mark_node;
19534
19535 cp_parser_commit_to_tentative_parse (parser);
19536
19537 /* Now we are going to parse the type-id of the declaration. */
19538
19539 /*
19540 [dcl.type]/3 says:
19541
19542 "A type-specifier-seq shall not define a class or enumeration
19543 unless it appears in the type-id of an alias-declaration (7.1.3) that
19544 is not the declaration of a template-declaration."
19545
19546 In other words, if we currently are in an alias template, the
19547 type-id should not define a type.
19548
19549 So let's set parser->type_definition_forbidden_message in that
19550 case; cp_parser_check_type_definition (called by
19551 cp_parser_class_specifier) will then emit an error if a type is
19552 defined in the type-id. */
19553 if (parser->num_template_parameter_lists)
19554 {
19555 saved_message = parser->type_definition_forbidden_message;
19556 parser->type_definition_forbidden_message =
19557 G_("types may not be defined in alias template declarations");
19558 }
19559
19560 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
19561 &type_location);
19562
19563 /* Restore the error message if need be. */
19564 if (parser->num_template_parameter_lists)
19565 parser->type_definition_forbidden_message = saved_message;
19566
19567 if (type == error_mark_node
19568 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19569 {
19570 cp_parser_skip_to_end_of_block_or_statement (parser);
19571 return error_mark_node;
19572 }
19573
19574 /* A typedef-name can also be introduced by an alias-declaration. The
19575 identifier following the using keyword becomes a typedef-name. It has
19576 the same semantics as if it were introduced by the typedef
19577 specifier. In particular, it does not define a new type and it shall
19578 not appear in the type-id. */
19579
19580 clear_decl_specs (&decl_specs);
19581 decl_specs.type = type;
19582 if (attributes != NULL_TREE)
19583 {
19584 decl_specs.attributes = attributes;
19585 set_and_check_decl_spec_loc (&decl_specs,
19586 ds_attribute,
19587 attrs_token);
19588 }
19589 set_and_check_decl_spec_loc (&decl_specs,
19590 ds_typedef,
19591 using_token);
19592 set_and_check_decl_spec_loc (&decl_specs,
19593 ds_alias,
19594 using_token);
19595 decl_specs.locations[ds_type_spec] = type_location;
19596
19597 if (parser->num_template_parameter_lists
19598 && !cp_parser_check_template_parameters (parser,
19599 /*num_templates=*/0,
19600 /*template_id*/false,
19601 id_location,
19602 /*declarator=*/NULL))
19603 return error_mark_node;
19604
19605 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
19606
19607 member_p = at_class_scope_p ();
19608 if (member_p)
19609 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19610 NULL_TREE, attributes);
19611 else
19612 decl = start_decl (declarator, &decl_specs, 0,
19613 attributes, NULL_TREE, &pushed_scope);
19614 if (decl == error_mark_node)
19615 return decl;
19616
19617 // Attach constraints to the alias declaration.
19618 if (flag_concepts && current_template_parms)
19619 {
19620 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19621 tree constr = build_constraints (reqs, NULL_TREE);
19622 set_constraints (decl, constr);
19623 }
19624
19625 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19626
19627 if (pushed_scope)
19628 pop_scope (pushed_scope);
19629
19630 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19631 added into the symbol table; otherwise, return the TYPE_DECL. */
19632 if (DECL_LANG_SPECIFIC (decl)
19633 && DECL_TEMPLATE_INFO (decl)
19634 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19635 {
19636 decl = DECL_TI_TEMPLATE (decl);
19637 if (member_p)
19638 check_member_template (decl);
19639 }
19640
19641 return decl;
19642 }
19643
19644 /* Parse a using-directive.
19645
19646 using-directive:
19647 using namespace :: [opt] nested-name-specifier [opt]
19648 namespace-name ; */
19649
19650 static void
19651 cp_parser_using_directive (cp_parser* parser)
19652 {
19653 tree namespace_decl;
19654 tree attribs;
19655
19656 /* Look for the `using' keyword. */
19657 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19658 /* And the `namespace' keyword. */
19659 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19660 /* Look for the optional `::' operator. */
19661 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19662 /* And the optional nested-name-specifier. */
19663 cp_parser_nested_name_specifier_opt (parser,
19664 /*typename_keyword_p=*/false,
19665 /*check_dependency_p=*/true,
19666 /*type_p=*/false,
19667 /*is_declaration=*/true);
19668 /* Get the namespace being used. */
19669 namespace_decl = cp_parser_namespace_name (parser);
19670 /* And any specified attributes. */
19671 attribs = cp_parser_attributes_opt (parser);
19672
19673 /* Update the symbol table. */
19674 if (namespace_bindings_p ())
19675 finish_namespace_using_directive (namespace_decl, attribs);
19676 else
19677 finish_local_using_directive (namespace_decl, attribs);
19678
19679 /* Look for the final `;'. */
19680 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19681 }
19682
19683 /* Parse an asm-definition.
19684
19685 asm-qualifier:
19686 volatile
19687 inline
19688 goto
19689
19690 asm-qualifier-list:
19691 asm-qualifier
19692 asm-qualifier-list asm-qualifier
19693
19694 asm-definition:
19695 asm ( string-literal ) ;
19696
19697 GNU Extension:
19698
19699 asm-definition:
19700 asm asm-qualifier-list [opt] ( string-literal ) ;
19701 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19702 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19703 : asm-operand-list [opt] ) ;
19704 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19705 : asm-operand-list [opt]
19706 : asm-clobber-list [opt] ) ;
19707 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19708 : asm-clobber-list [opt]
19709 : asm-goto-list ) ;
19710
19711 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19712 contains goto, and is the only allowed form in that case. No duplicates are
19713 allowed in an asm-qualifier-list. */
19714
19715 static void
19716 cp_parser_asm_definition (cp_parser* parser)
19717 {
19718 tree string;
19719 tree outputs = NULL_TREE;
19720 tree inputs = NULL_TREE;
19721 tree clobbers = NULL_TREE;
19722 tree labels = NULL_TREE;
19723 tree asm_stmt;
19724 bool extended_p = false;
19725 bool invalid_inputs_p = false;
19726 bool invalid_outputs_p = false;
19727 required_token missing = RT_NONE;
19728
19729 /* Look for the `asm' keyword. */
19730 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19731
19732 if (parser->in_function_body
19733 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19734 {
19735 error ("%<asm%> in %<constexpr%> function");
19736 cp_function_chain->invalid_constexpr = true;
19737 }
19738
19739 /* Handle the asm-qualifier-list. */
19740 location_t volatile_loc = UNKNOWN_LOCATION;
19741 location_t inline_loc = UNKNOWN_LOCATION;
19742 location_t goto_loc = UNKNOWN_LOCATION;
19743
19744 if (cp_parser_allow_gnu_extensions_p (parser) && parser->in_function_body)
19745 for (;;)
19746 {
19747 cp_token *token = cp_lexer_peek_token (parser->lexer);
19748 location_t loc = token->location;
19749 switch (cp_lexer_peek_token (parser->lexer)->keyword)
19750 {
19751 case RID_VOLATILE:
19752 if (volatile_loc)
19753 {
19754 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19755 inform (volatile_loc, "first seen here");
19756 }
19757 else
19758 volatile_loc = loc;
19759 cp_lexer_consume_token (parser->lexer);
19760 continue;
19761
19762 case RID_INLINE:
19763 if (inline_loc)
19764 {
19765 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19766 inform (inline_loc, "first seen here");
19767 }
19768 else
19769 inline_loc = loc;
19770 cp_lexer_consume_token (parser->lexer);
19771 continue;
19772
19773 case RID_GOTO:
19774 if (goto_loc)
19775 {
19776 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19777 inform (goto_loc, "first seen here");
19778 }
19779 else
19780 goto_loc = loc;
19781 cp_lexer_consume_token (parser->lexer);
19782 continue;
19783
19784 case RID_CONST:
19785 case RID_RESTRICT:
19786 error_at (loc, "%qT is not an asm qualifier", token->u.value);
19787 cp_lexer_consume_token (parser->lexer);
19788 continue;
19789
19790 default:
19791 break;
19792 }
19793 break;
19794 }
19795
19796 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
19797 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
19798 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
19799
19800 /* Look for the opening `('. */
19801 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19802 return;
19803 /* Look for the string. */
19804 string = cp_parser_string_literal (parser, false, false);
19805 if (string == error_mark_node)
19806 {
19807 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19808 /*consume_paren=*/true);
19809 return;
19810 }
19811
19812 /* If we're allowing GNU extensions, check for the extended assembly
19813 syntax. Unfortunately, the `:' tokens need not be separated by
19814 a space in C, and so, for compatibility, we tolerate that here
19815 too. Doing that means that we have to treat the `::' operator as
19816 two `:' tokens. */
19817 if (cp_parser_allow_gnu_extensions_p (parser)
19818 && parser->in_function_body
19819 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19820 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19821 {
19822 bool inputs_p = false;
19823 bool clobbers_p = false;
19824 bool labels_p = false;
19825
19826 /* The extended syntax was used. */
19827 extended_p = true;
19828
19829 /* Look for outputs. */
19830 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19831 {
19832 /* Consume the `:'. */
19833 cp_lexer_consume_token (parser->lexer);
19834 /* Parse the output-operands. */
19835 if (cp_lexer_next_token_is_not (parser->lexer,
19836 CPP_COLON)
19837 && cp_lexer_next_token_is_not (parser->lexer,
19838 CPP_SCOPE)
19839 && cp_lexer_next_token_is_not (parser->lexer,
19840 CPP_CLOSE_PAREN)
19841 && !goto_p)
19842 {
19843 outputs = cp_parser_asm_operand_list (parser);
19844 if (outputs == error_mark_node)
19845 invalid_outputs_p = true;
19846 }
19847 }
19848 /* If the next token is `::', there are no outputs, and the
19849 next token is the beginning of the inputs. */
19850 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19851 /* The inputs are coming next. */
19852 inputs_p = true;
19853
19854 /* Look for inputs. */
19855 if (inputs_p
19856 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19857 {
19858 /* Consume the `:' or `::'. */
19859 cp_lexer_consume_token (parser->lexer);
19860 /* Parse the output-operands. */
19861 if (cp_lexer_next_token_is_not (parser->lexer,
19862 CPP_COLON)
19863 && cp_lexer_next_token_is_not (parser->lexer,
19864 CPP_SCOPE)
19865 && cp_lexer_next_token_is_not (parser->lexer,
19866 CPP_CLOSE_PAREN))
19867 {
19868 inputs = cp_parser_asm_operand_list (parser);
19869 if (inputs == error_mark_node)
19870 invalid_inputs_p = true;
19871 }
19872 }
19873 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19874 /* The clobbers are coming next. */
19875 clobbers_p = true;
19876
19877 /* Look for clobbers. */
19878 if (clobbers_p
19879 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19880 {
19881 clobbers_p = true;
19882 /* Consume the `:' or `::'. */
19883 cp_lexer_consume_token (parser->lexer);
19884 /* Parse the clobbers. */
19885 if (cp_lexer_next_token_is_not (parser->lexer,
19886 CPP_COLON)
19887 && cp_lexer_next_token_is_not (parser->lexer,
19888 CPP_CLOSE_PAREN))
19889 clobbers = cp_parser_asm_clobber_list (parser);
19890 }
19891 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19892 /* The labels are coming next. */
19893 labels_p = true;
19894
19895 /* Look for labels. */
19896 if (labels_p
19897 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19898 {
19899 labels_p = true;
19900 /* Consume the `:' or `::'. */
19901 cp_lexer_consume_token (parser->lexer);
19902 /* Parse the labels. */
19903 labels = cp_parser_asm_label_list (parser);
19904 }
19905
19906 if (goto_p && !labels_p)
19907 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19908 }
19909 else if (goto_p)
19910 missing = RT_COLON_SCOPE;
19911
19912 /* Look for the closing `)'. */
19913 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19914 missing ? missing : RT_CLOSE_PAREN))
19915 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19916 /*consume_paren=*/true);
19917 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19918
19919 if (!invalid_inputs_p && !invalid_outputs_p)
19920 {
19921 /* Create the ASM_EXPR. */
19922 if (parser->in_function_body)
19923 {
19924 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19925 inputs, clobbers, labels, inline_p);
19926 /* If the extended syntax was not used, mark the ASM_EXPR. */
19927 if (!extended_p)
19928 {
19929 tree temp = asm_stmt;
19930 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19931 temp = TREE_OPERAND (temp, 0);
19932
19933 ASM_INPUT_P (temp) = 1;
19934 }
19935 }
19936 else
19937 symtab->finalize_toplevel_asm (string);
19938 }
19939 }
19940
19941 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19942 type that comes from the decl-specifier-seq. */
19943
19944 static tree
19945 strip_declarator_types (tree type, cp_declarator *declarator)
19946 {
19947 for (cp_declarator *d = declarator; d;)
19948 switch (d->kind)
19949 {
19950 case cdk_id:
19951 case cdk_decomp:
19952 case cdk_error:
19953 d = NULL;
19954 break;
19955
19956 default:
19957 if (TYPE_PTRMEMFUNC_P (type))
19958 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19959 type = TREE_TYPE (type);
19960 d = d->declarator;
19961 break;
19962 }
19963
19964 return type;
19965 }
19966
19967 /* Declarators [gram.dcl.decl] */
19968
19969 /* Parse an init-declarator.
19970
19971 init-declarator:
19972 declarator initializer [opt]
19973
19974 GNU Extension:
19975
19976 init-declarator:
19977 declarator asm-specification [opt] attributes [opt] initializer [opt]
19978
19979 function-definition:
19980 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19981 function-body
19982 decl-specifier-seq [opt] declarator function-try-block
19983
19984 GNU Extension:
19985
19986 function-definition:
19987 __extension__ function-definition
19988
19989 TM Extension:
19990
19991 function-definition:
19992 decl-specifier-seq [opt] declarator function-transaction-block
19993
19994 The parser flags FLAGS is used to control type-specifier parsing.
19995
19996 The DECL_SPECIFIERS apply to this declarator. Returns a
19997 representation of the entity declared. If MEMBER_P is TRUE, then
19998 this declarator appears in a class scope. The new DECL created by
19999 this declarator is returned.
20000
20001 The CHECKS are access checks that should be performed once we know
20002 what entity is being declared (and, therefore, what classes have
20003 befriended it).
20004
20005 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20006 for a function-definition here as well. If the declarator is a
20007 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20008 be TRUE upon return. By that point, the function-definition will
20009 have been completely parsed.
20010
20011 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20012 is FALSE.
20013
20014 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20015 parsed declaration if it is an uninitialized single declarator not followed
20016 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20017 if present, will not be consumed. If returned, this declarator will be
20018 created with SD_INITIALIZED but will not call cp_finish_decl.
20019
20020 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20021 and there is an initializer, the pointed location_t is set to the
20022 location of the '=' or `(', or '{' in C++11 token introducing the
20023 initializer. */
20024
20025 static tree
20026 cp_parser_init_declarator (cp_parser* parser,
20027 cp_parser_flags flags,
20028 cp_decl_specifier_seq *decl_specifiers,
20029 vec<deferred_access_check, va_gc> *checks,
20030 bool function_definition_allowed_p,
20031 bool member_p,
20032 int declares_class_or_enum,
20033 bool* function_definition_p,
20034 tree* maybe_range_for_decl,
20035 location_t* init_loc,
20036 tree* auto_result)
20037 {
20038 cp_token *token = NULL, *asm_spec_start_token = NULL,
20039 *attributes_start_token = NULL;
20040 cp_declarator *declarator;
20041 tree prefix_attributes;
20042 tree attributes = NULL;
20043 tree asm_specification;
20044 tree initializer;
20045 tree decl = NULL_TREE;
20046 tree scope;
20047 int is_initialized;
20048 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20049 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20050 "(...)". */
20051 enum cpp_ttype initialization_kind;
20052 bool is_direct_init = false;
20053 bool is_non_constant_init;
20054 int ctor_dtor_or_conv_p;
20055 bool friend_p = cp_parser_friend_p (decl_specifiers);
20056 tree pushed_scope = NULL_TREE;
20057 bool range_for_decl_p = false;
20058 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20059 location_t tmp_init_loc = UNKNOWN_LOCATION;
20060
20061 /* Gather the attributes that were provided with the
20062 decl-specifiers. */
20063 prefix_attributes = decl_specifiers->attributes;
20064
20065 /* Assume that this is not the declarator for a function
20066 definition. */
20067 if (function_definition_p)
20068 *function_definition_p = false;
20069
20070 /* Default arguments are only permitted for function parameters. */
20071 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
20072 parser->default_arg_ok_p = false;
20073
20074 /* Defer access checks while parsing the declarator; we cannot know
20075 what names are accessible until we know what is being
20076 declared. */
20077 resume_deferring_access_checks ();
20078
20079 token = cp_lexer_peek_token (parser->lexer);
20080
20081 /* Parse the declarator. */
20082 declarator
20083 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20084 flags, &ctor_dtor_or_conv_p,
20085 /*parenthesized_p=*/NULL,
20086 member_p, friend_p, /*static_p=*/false);
20087 /* Gather up the deferred checks. */
20088 stop_deferring_access_checks ();
20089
20090 parser->default_arg_ok_p = saved_default_arg_ok_p;
20091
20092 /* If the DECLARATOR was erroneous, there's no need to go
20093 further. */
20094 if (declarator == cp_error_declarator)
20095 return error_mark_node;
20096
20097 /* Check that the number of template-parameter-lists is OK. */
20098 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
20099 token->location))
20100 return error_mark_node;
20101
20102 if (declares_class_or_enum & 2)
20103 cp_parser_check_for_definition_in_return_type (declarator,
20104 decl_specifiers->type,
20105 decl_specifiers->locations[ds_type_spec]);
20106
20107 /* Figure out what scope the entity declared by the DECLARATOR is
20108 located in. `grokdeclarator' sometimes changes the scope, so
20109 we compute it now. */
20110 scope = get_scope_of_declarator (declarator);
20111
20112 /* Perform any lookups in the declared type which were thought to be
20113 dependent, but are not in the scope of the declarator. */
20114 decl_specifiers->type
20115 = maybe_update_decl_type (decl_specifiers->type, scope);
20116
20117 /* If we're allowing GNU extensions, look for an
20118 asm-specification. */
20119 if (cp_parser_allow_gnu_extensions_p (parser))
20120 {
20121 /* Look for an asm-specification. */
20122 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
20123 asm_specification = cp_parser_asm_specification_opt (parser);
20124 }
20125 else
20126 asm_specification = NULL_TREE;
20127
20128 /* Look for attributes. */
20129 attributes_start_token = cp_lexer_peek_token (parser->lexer);
20130 attributes = cp_parser_attributes_opt (parser);
20131
20132 /* Peek at the next token. */
20133 token = cp_lexer_peek_token (parser->lexer);
20134
20135 bool bogus_implicit_tmpl = false;
20136
20137 if (function_declarator_p (declarator))
20138 {
20139 /* Handle C++17 deduction guides. */
20140 if (!decl_specifiers->type
20141 && ctor_dtor_or_conv_p <= 0
20142 && cxx_dialect >= cxx17)
20143 {
20144 cp_declarator *id = get_id_declarator (declarator);
20145 tree name = id->u.id.unqualified_name;
20146 parser->scope = id->u.id.qualifying_scope;
20147 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
20148 if (tmpl
20149 && (DECL_CLASS_TEMPLATE_P (tmpl)
20150 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
20151 {
20152 id->u.id.unqualified_name = dguide_name (tmpl);
20153 id->u.id.sfk = sfk_deduction_guide;
20154 ctor_dtor_or_conv_p = 1;
20155 }
20156 }
20157
20158 /* Check to see if the token indicates the start of a
20159 function-definition. */
20160 if (cp_parser_token_starts_function_definition_p (token))
20161 {
20162 if (!function_definition_allowed_p)
20163 {
20164 /* If a function-definition should not appear here, issue an
20165 error message. */
20166 cp_parser_error (parser,
20167 "a function-definition is not allowed here");
20168 return error_mark_node;
20169 }
20170
20171 location_t func_brace_location
20172 = cp_lexer_peek_token (parser->lexer)->location;
20173
20174 /* Neither attributes nor an asm-specification are allowed
20175 on a function-definition. */
20176 if (asm_specification)
20177 error_at (asm_spec_start_token->location,
20178 "an asm-specification is not allowed "
20179 "on a function-definition");
20180 if (attributes)
20181 error_at (attributes_start_token->location,
20182 "attributes are not allowed "
20183 "on a function-definition");
20184 /* This is a function-definition. */
20185 *function_definition_p = true;
20186
20187 /* Parse the function definition. */
20188 if (member_p)
20189 decl = cp_parser_save_member_function_body (parser,
20190 decl_specifiers,
20191 declarator,
20192 prefix_attributes);
20193 else
20194 decl =
20195 (cp_parser_function_definition_from_specifiers_and_declarator
20196 (parser, decl_specifiers, prefix_attributes, declarator));
20197
20198 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
20199 {
20200 /* This is where the prologue starts... */
20201 DECL_STRUCT_FUNCTION (decl)->function_start_locus
20202 = func_brace_location;
20203 }
20204
20205 return decl;
20206 }
20207 }
20208 else if (parser->fully_implicit_function_template_p)
20209 {
20210 /* A non-template declaration involving a function parameter list
20211 containing an implicit template parameter will be made into a
20212 template. If the resulting declaration is not going to be an
20213 actual function then finish the template scope here to prevent it.
20214 An error message will be issued once we have a decl to talk about.
20215
20216 FIXME probably we should do type deduction rather than create an
20217 implicit template, but the standard currently doesn't allow it. */
20218 bogus_implicit_tmpl = true;
20219 finish_fully_implicit_template (parser, NULL_TREE);
20220 }
20221
20222 /* [dcl.dcl]
20223
20224 Only in function declarations for constructors, destructors, type
20225 conversions, and deduction guides can the decl-specifier-seq be omitted.
20226
20227 We explicitly postpone this check past the point where we handle
20228 function-definitions because we tolerate function-definitions
20229 that are missing their return types in some modes. */
20230 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
20231 {
20232 cp_parser_error (parser,
20233 "expected constructor, destructor, or type conversion");
20234 return error_mark_node;
20235 }
20236
20237 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20238 if (token->type == CPP_EQ
20239 || token->type == CPP_OPEN_PAREN
20240 || token->type == CPP_OPEN_BRACE)
20241 {
20242 is_initialized = SD_INITIALIZED;
20243 initialization_kind = token->type;
20244 if (maybe_range_for_decl)
20245 *maybe_range_for_decl = error_mark_node;
20246 tmp_init_loc = token->location;
20247 if (init_loc && *init_loc == UNKNOWN_LOCATION)
20248 *init_loc = tmp_init_loc;
20249
20250 if (token->type == CPP_EQ
20251 && function_declarator_p (declarator))
20252 {
20253 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
20254 if (t2->keyword == RID_DEFAULT)
20255 is_initialized = SD_DEFAULTED;
20256 else if (t2->keyword == RID_DELETE)
20257 is_initialized = SD_DELETED;
20258 }
20259 }
20260 else
20261 {
20262 /* If the init-declarator isn't initialized and isn't followed by a
20263 `,' or `;', it's not a valid init-declarator. */
20264 if (token->type != CPP_COMMA
20265 && token->type != CPP_SEMICOLON)
20266 {
20267 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
20268 range_for_decl_p = true;
20269 else
20270 {
20271 if (!maybe_range_for_decl)
20272 cp_parser_error (parser, "expected initializer");
20273 return error_mark_node;
20274 }
20275 }
20276 is_initialized = SD_UNINITIALIZED;
20277 initialization_kind = CPP_EOF;
20278 }
20279
20280 /* Because start_decl has side-effects, we should only call it if we
20281 know we're going ahead. By this point, we know that we cannot
20282 possibly be looking at any other construct. */
20283 cp_parser_commit_to_tentative_parse (parser);
20284
20285 /* Enter the newly declared entry in the symbol table. If we're
20286 processing a declaration in a class-specifier, we wait until
20287 after processing the initializer. */
20288 if (!member_p)
20289 {
20290 if (parser->in_unbraced_linkage_specification_p)
20291 decl_specifiers->storage_class = sc_extern;
20292 decl = start_decl (declarator, decl_specifiers,
20293 range_for_decl_p? SD_INITIALIZED : is_initialized,
20294 attributes, prefix_attributes, &pushed_scope);
20295 cp_finalize_omp_declare_simd (parser, decl);
20296 cp_finalize_oacc_routine (parser, decl, false);
20297 /* Adjust location of decl if declarator->id_loc is more appropriate:
20298 set, and decl wasn't merged with another decl, in which case its
20299 location would be different from input_location, and more accurate. */
20300 if (DECL_P (decl)
20301 && declarator->id_loc != UNKNOWN_LOCATION
20302 && DECL_SOURCE_LOCATION (decl) == input_location)
20303 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
20304 }
20305 else if (scope)
20306 /* Enter the SCOPE. That way unqualified names appearing in the
20307 initializer will be looked up in SCOPE. */
20308 pushed_scope = push_scope (scope);
20309
20310 /* Perform deferred access control checks, now that we know in which
20311 SCOPE the declared entity resides. */
20312 if (!member_p && decl)
20313 {
20314 tree saved_current_function_decl = NULL_TREE;
20315
20316 /* If the entity being declared is a function, pretend that we
20317 are in its scope. If it is a `friend', it may have access to
20318 things that would not otherwise be accessible. */
20319 if (TREE_CODE (decl) == FUNCTION_DECL)
20320 {
20321 saved_current_function_decl = current_function_decl;
20322 current_function_decl = decl;
20323 }
20324
20325 /* Perform access checks for template parameters. */
20326 cp_parser_perform_template_parameter_access_checks (checks);
20327
20328 /* Perform the access control checks for the declarator and the
20329 decl-specifiers. */
20330 perform_deferred_access_checks (tf_warning_or_error);
20331
20332 /* Restore the saved value. */
20333 if (TREE_CODE (decl) == FUNCTION_DECL)
20334 current_function_decl = saved_current_function_decl;
20335 }
20336
20337 /* Parse the initializer. */
20338 initializer = NULL_TREE;
20339 is_direct_init = false;
20340 is_non_constant_init = true;
20341 if (is_initialized)
20342 {
20343 if (function_declarator_p (declarator))
20344 {
20345 if (initialization_kind == CPP_EQ)
20346 initializer = cp_parser_pure_specifier (parser);
20347 else
20348 {
20349 /* If the declaration was erroneous, we don't really
20350 know what the user intended, so just silently
20351 consume the initializer. */
20352 if (decl != error_mark_node)
20353 error_at (tmp_init_loc, "initializer provided for function");
20354 cp_parser_skip_to_closing_parenthesis (parser,
20355 /*recovering=*/true,
20356 /*or_comma=*/false,
20357 /*consume_paren=*/true);
20358 }
20359 }
20360 else
20361 {
20362 /* We want to record the extra mangling scope for in-class
20363 initializers of class members and initializers of static data
20364 member templates. The former involves deferring
20365 parsing of the initializer until end of class as with default
20366 arguments. So right here we only handle the latter. */
20367 if (!member_p && processing_template_decl && decl != error_mark_node)
20368 start_lambda_scope (decl);
20369 initializer = cp_parser_initializer (parser,
20370 &is_direct_init,
20371 &is_non_constant_init);
20372 if (!member_p && processing_template_decl && decl != error_mark_node)
20373 finish_lambda_scope ();
20374 if (initializer == error_mark_node)
20375 cp_parser_skip_to_end_of_statement (parser);
20376 }
20377 }
20378
20379 /* The old parser allows attributes to appear after a parenthesized
20380 initializer. Mark Mitchell proposed removing this functionality
20381 on the GCC mailing lists on 2002-08-13. This parser accepts the
20382 attributes -- but ignores them. Made a permerror in GCC 8. */
20383 if (cp_parser_allow_gnu_extensions_p (parser)
20384 && initialization_kind == CPP_OPEN_PAREN
20385 && cp_parser_attributes_opt (parser)
20386 && permerror (input_location,
20387 "attributes after parenthesized initializer ignored"))
20388 {
20389 static bool hint;
20390 if (flag_permissive && !hint)
20391 {
20392 hint = true;
20393 inform (input_location,
20394 "this flexibility is deprecated and will be removed");
20395 }
20396 }
20397
20398 /* And now complain about a non-function implicit template. */
20399 if (bogus_implicit_tmpl && decl != error_mark_node)
20400 error_at (DECL_SOURCE_LOCATION (decl),
20401 "non-function %qD declared as implicit template", decl);
20402
20403 /* For an in-class declaration, use `grokfield' to create the
20404 declaration. */
20405 if (member_p)
20406 {
20407 if (pushed_scope)
20408 {
20409 pop_scope (pushed_scope);
20410 pushed_scope = NULL_TREE;
20411 }
20412 decl = grokfield (declarator, decl_specifiers,
20413 initializer, !is_non_constant_init,
20414 /*asmspec=*/NULL_TREE,
20415 attr_chainon (attributes, prefix_attributes));
20416 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20417 cp_parser_save_default_args (parser, decl);
20418 cp_finalize_omp_declare_simd (parser, decl);
20419 cp_finalize_oacc_routine (parser, decl, false);
20420 }
20421
20422 /* Finish processing the declaration. But, skip member
20423 declarations. */
20424 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20425 {
20426 cp_finish_decl (decl,
20427 initializer, !is_non_constant_init,
20428 asm_specification,
20429 /* If the initializer is in parentheses, then this is
20430 a direct-initialization, which means that an
20431 `explicit' constructor is OK. Otherwise, an
20432 `explicit' constructor cannot be used. */
20433 ((is_direct_init || !is_initialized)
20434 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20435 }
20436 else if ((cxx_dialect != cxx98) && friend_p
20437 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20438 /* Core issue #226 (C++0x only): A default template-argument
20439 shall not be specified in a friend class template
20440 declaration. */
20441 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20442 /*is_partial=*/false, /*is_friend_decl=*/1);
20443
20444 if (!friend_p && pushed_scope)
20445 pop_scope (pushed_scope);
20446
20447 if (function_declarator_p (declarator)
20448 && parser->fully_implicit_function_template_p)
20449 {
20450 if (member_p)
20451 decl = finish_fully_implicit_template (parser, decl);
20452 else
20453 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20454 }
20455
20456 if (auto_result && is_initialized && decl_specifiers->type
20457 && type_uses_auto (decl_specifiers->type))
20458 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20459
20460 return decl;
20461 }
20462
20463 /* Parse a declarator.
20464
20465 declarator:
20466 direct-declarator
20467 ptr-operator declarator
20468
20469 abstract-declarator:
20470 ptr-operator abstract-declarator [opt]
20471 direct-abstract-declarator
20472
20473 GNU Extensions:
20474
20475 declarator:
20476 attributes [opt] direct-declarator
20477 attributes [opt] ptr-operator declarator
20478
20479 abstract-declarator:
20480 attributes [opt] ptr-operator abstract-declarator [opt]
20481 attributes [opt] direct-abstract-declarator
20482
20483 The parser flags FLAGS is used to control type-specifier parsing.
20484
20485 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20486 detect constructors, destructors, deduction guides, or conversion operators.
20487 It is set to -1 if the declarator is a name, and +1 if it is a
20488 function. Otherwise it is set to zero. Usually you just want to
20489 test for >0, but internally the negative value is used.
20490
20491 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20492 a decl-specifier-seq unless it declares a constructor, destructor,
20493 or conversion. It might seem that we could check this condition in
20494 semantic analysis, rather than parsing, but that makes it difficult
20495 to handle something like `f()'. We want to notice that there are
20496 no decl-specifiers, and therefore realize that this is an
20497 expression, not a declaration.)
20498
20499 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20500 the declarator is a direct-declarator of the form "(...)".
20501
20502 MEMBER_P is true iff this declarator is a member-declarator.
20503
20504 FRIEND_P is true iff this declarator is a friend.
20505
20506 STATIC_P is true iff the keyword static was seen. */
20507
20508 static cp_declarator *
20509 cp_parser_declarator (cp_parser* parser,
20510 cp_parser_declarator_kind dcl_kind,
20511 cp_parser_flags flags,
20512 int* ctor_dtor_or_conv_p,
20513 bool* parenthesized_p,
20514 bool member_p, bool friend_p, bool static_p)
20515 {
20516 cp_declarator *declarator;
20517 enum tree_code code;
20518 cp_cv_quals cv_quals;
20519 tree class_type;
20520 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20521
20522 /* Assume this is not a constructor, destructor, or type-conversion
20523 operator. */
20524 if (ctor_dtor_or_conv_p)
20525 *ctor_dtor_or_conv_p = 0;
20526
20527 if (cp_parser_allow_gnu_extensions_p (parser))
20528 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20529
20530 /* Check for the ptr-operator production. */
20531 cp_parser_parse_tentatively (parser);
20532 /* Parse the ptr-operator. */
20533 code = cp_parser_ptr_operator (parser,
20534 &class_type,
20535 &cv_quals,
20536 &std_attributes);
20537
20538 /* If that worked, then we have a ptr-operator. */
20539 if (cp_parser_parse_definitely (parser))
20540 {
20541 /* If a ptr-operator was found, then this declarator was not
20542 parenthesized. */
20543 if (parenthesized_p)
20544 *parenthesized_p = true;
20545 /* The dependent declarator is optional if we are parsing an
20546 abstract-declarator. */
20547 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20548 cp_parser_parse_tentatively (parser);
20549
20550 /* Parse the dependent declarator. */
20551 declarator = cp_parser_declarator (parser, dcl_kind,
20552 CP_PARSER_FLAGS_NONE,
20553 /*ctor_dtor_or_conv_p=*/NULL,
20554 /*parenthesized_p=*/NULL,
20555 /*member_p=*/false,
20556 friend_p, /*static_p=*/false);
20557
20558 /* If we are parsing an abstract-declarator, we must handle the
20559 case where the dependent declarator is absent. */
20560 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20561 && !cp_parser_parse_definitely (parser))
20562 declarator = NULL;
20563
20564 declarator = cp_parser_make_indirect_declarator
20565 (code, class_type, cv_quals, declarator, std_attributes);
20566 }
20567 /* Everything else is a direct-declarator. */
20568 else
20569 {
20570 if (parenthesized_p)
20571 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20572 CPP_OPEN_PAREN);
20573 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20574 flags, ctor_dtor_or_conv_p,
20575 member_p, friend_p, static_p);
20576 }
20577
20578 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20579 declarator->attributes = gnu_attributes;
20580 return declarator;
20581 }
20582
20583 /* Parse a direct-declarator or direct-abstract-declarator.
20584
20585 direct-declarator:
20586 declarator-id
20587 direct-declarator ( parameter-declaration-clause )
20588 cv-qualifier-seq [opt]
20589 ref-qualifier [opt]
20590 exception-specification [opt]
20591 direct-declarator [ constant-expression [opt] ]
20592 ( declarator )
20593
20594 direct-abstract-declarator:
20595 direct-abstract-declarator [opt]
20596 ( parameter-declaration-clause )
20597 cv-qualifier-seq [opt]
20598 ref-qualifier [opt]
20599 exception-specification [opt]
20600 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20601 ( abstract-declarator )
20602
20603 Returns a representation of the declarator. DCL_KIND is
20604 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20605 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20606 we are parsing a direct-declarator. It is
20607 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20608 of ambiguity we prefer an abstract declarator, as per
20609 [dcl.ambig.res].
20610 The parser flags FLAGS is used to control type-specifier parsing.
20611 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
20612 as for cp_parser_declarator. */
20613
20614 static cp_declarator *
20615 cp_parser_direct_declarator (cp_parser* parser,
20616 cp_parser_declarator_kind dcl_kind,
20617 cp_parser_flags flags,
20618 int* ctor_dtor_or_conv_p,
20619 bool member_p, bool friend_p, bool static_p)
20620 {
20621 cp_token *token;
20622 cp_declarator *declarator = NULL;
20623 tree scope = NULL_TREE;
20624 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20625 bool saved_in_declarator_p = parser->in_declarator_p;
20626 bool first = true;
20627 tree pushed_scope = NULL_TREE;
20628 cp_token *open_paren = NULL, *close_paren = NULL;
20629
20630 while (true)
20631 {
20632 /* Peek at the next token. */
20633 token = cp_lexer_peek_token (parser->lexer);
20634 if (token->type == CPP_OPEN_PAREN)
20635 {
20636 /* This is either a parameter-declaration-clause, or a
20637 parenthesized declarator. When we know we are parsing a
20638 named declarator, it must be a parenthesized declarator
20639 if FIRST is true. For instance, `(int)' is a
20640 parameter-declaration-clause, with an omitted
20641 direct-abstract-declarator. But `((*))', is a
20642 parenthesized abstract declarator. Finally, when T is a
20643 template parameter `(T)' is a
20644 parameter-declaration-clause, and not a parenthesized
20645 named declarator.
20646
20647 We first try and parse a parameter-declaration-clause,
20648 and then try a nested declarator (if FIRST is true).
20649
20650 It is not an error for it not to be a
20651 parameter-declaration-clause, even when FIRST is
20652 false. Consider,
20653
20654 int i (int);
20655 int i (3);
20656
20657 The first is the declaration of a function while the
20658 second is the definition of a variable, including its
20659 initializer.
20660
20661 Having seen only the parenthesis, we cannot know which of
20662 these two alternatives should be selected. Even more
20663 complex are examples like:
20664
20665 int i (int (a));
20666 int i (int (3));
20667
20668 The former is a function-declaration; the latter is a
20669 variable initialization.
20670
20671 Thus again, we try a parameter-declaration-clause, and if
20672 that fails, we back out and return. */
20673
20674 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20675 {
20676 tree params;
20677 bool is_declarator = false;
20678
20679 open_paren = NULL;
20680
20681 /* In a member-declarator, the only valid interpretation
20682 of a parenthesis is the start of a
20683 parameter-declaration-clause. (It is invalid to
20684 initialize a static data member with a parenthesized
20685 initializer; only the "=" form of initialization is
20686 permitted.) */
20687 if (!member_p)
20688 cp_parser_parse_tentatively (parser);
20689
20690 /* Consume the `('. */
20691 matching_parens parens;
20692 parens.consume_open (parser);
20693 if (first)
20694 {
20695 /* If this is going to be an abstract declarator, we're
20696 in a declarator and we can't have default args. */
20697 parser->default_arg_ok_p = false;
20698 parser->in_declarator_p = true;
20699 }
20700
20701 begin_scope (sk_function_parms, NULL_TREE);
20702
20703 /* Parse the parameter-declaration-clause. */
20704 params
20705 = cp_parser_parameter_declaration_clause (parser, flags);
20706
20707 /* Consume the `)'. */
20708 parens.require_close (parser);
20709
20710 /* If all went well, parse the cv-qualifier-seq,
20711 ref-qualifier and the exception-specification. */
20712 if (member_p || cp_parser_parse_definitely (parser))
20713 {
20714 cp_cv_quals cv_quals;
20715 cp_virt_specifiers virt_specifiers;
20716 cp_ref_qualifier ref_qual;
20717 tree exception_specification;
20718 tree late_return;
20719 tree attrs;
20720 bool memfn = (member_p || (pushed_scope
20721 && CLASS_TYPE_P (pushed_scope)));
20722 unsigned char local_variables_forbidden_p
20723 = parser->local_variables_forbidden_p;
20724 /* 'this' is not allowed in static member functions. */
20725 if (static_p || friend_p)
20726 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
20727
20728 is_declarator = true;
20729
20730 if (ctor_dtor_or_conv_p)
20731 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20732 first = false;
20733
20734 /* Parse the cv-qualifier-seq. */
20735 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20736 /* Parse the ref-qualifier. */
20737 ref_qual = cp_parser_ref_qualifier_opt (parser);
20738 /* Parse the tx-qualifier. */
20739 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20740 /* And the exception-specification. */
20741 exception_specification
20742 = cp_parser_exception_specification_opt (parser);
20743
20744 attrs = cp_parser_std_attribute_spec_seq (parser);
20745
20746 /* In here, we handle cases where attribute is used after
20747 the function declaration. For example:
20748 void func (int x) __attribute__((vector(..))); */
20749 tree gnu_attrs = NULL_TREE;
20750 tree requires_clause = NULL_TREE;
20751 late_return = (cp_parser_late_return_type_opt
20752 (parser, declarator, requires_clause,
20753 memfn ? cv_quals : -1));
20754
20755 /* Parse the virt-specifier-seq. */
20756 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20757
20758 /* Create the function-declarator. */
20759 declarator = make_call_declarator (declarator,
20760 params,
20761 cv_quals,
20762 virt_specifiers,
20763 ref_qual,
20764 tx_qual,
20765 exception_specification,
20766 late_return,
20767 requires_clause);
20768 declarator->std_attributes = attrs;
20769 declarator->attributes = gnu_attrs;
20770 /* Any subsequent parameter lists are to do with
20771 return type, so are not those of the declared
20772 function. */
20773 parser->default_arg_ok_p = false;
20774
20775 /* Restore the state of local_variables_forbidden_p. */
20776 parser->local_variables_forbidden_p
20777 = local_variables_forbidden_p;
20778 }
20779
20780 /* Remove the function parms from scope. */
20781 pop_bindings_and_leave_scope ();
20782
20783 if (is_declarator)
20784 /* Repeat the main loop. */
20785 continue;
20786 }
20787
20788 /* If this is the first, we can try a parenthesized
20789 declarator. */
20790 if (first)
20791 {
20792 bool saved_in_type_id_in_expr_p;
20793
20794 parser->default_arg_ok_p = saved_default_arg_ok_p;
20795 parser->in_declarator_p = saved_in_declarator_p;
20796
20797 open_paren = token;
20798 /* Consume the `('. */
20799 matching_parens parens;
20800 parens.consume_open (parser);
20801 /* Parse the nested declarator. */
20802 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20803 parser->in_type_id_in_expr_p = true;
20804 declarator
20805 = cp_parser_declarator (parser, dcl_kind, flags,
20806 ctor_dtor_or_conv_p,
20807 /*parenthesized_p=*/NULL,
20808 member_p, friend_p,
20809 /*static_p=*/false);
20810 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20811 first = false;
20812 /* Expect a `)'. */
20813 close_paren = cp_lexer_peek_token (parser->lexer);
20814 if (!parens.require_close (parser))
20815 declarator = cp_error_declarator;
20816 if (declarator == cp_error_declarator)
20817 break;
20818
20819 goto handle_declarator;
20820 }
20821 /* Otherwise, we must be done. */
20822 else
20823 break;
20824 }
20825 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20826 && token->type == CPP_OPEN_SQUARE
20827 && !cp_next_tokens_can_be_attribute_p (parser))
20828 {
20829 /* Parse an array-declarator. */
20830 tree bounds, attrs;
20831
20832 if (ctor_dtor_or_conv_p)
20833 *ctor_dtor_or_conv_p = 0;
20834
20835 open_paren = NULL;
20836 first = false;
20837 parser->default_arg_ok_p = false;
20838 parser->in_declarator_p = true;
20839 /* Consume the `['. */
20840 cp_lexer_consume_token (parser->lexer);
20841 /* Peek at the next token. */
20842 token = cp_lexer_peek_token (parser->lexer);
20843 /* If the next token is `]', then there is no
20844 constant-expression. */
20845 if (token->type != CPP_CLOSE_SQUARE)
20846 {
20847 bool non_constant_p;
20848 bounds
20849 = cp_parser_constant_expression (parser,
20850 /*allow_non_constant=*/true,
20851 &non_constant_p);
20852 if (!non_constant_p)
20853 /* OK */;
20854 else if (error_operand_p (bounds))
20855 /* Already gave an error. */;
20856 else if (!parser->in_function_body
20857 || current_binding_level->kind == sk_function_parms)
20858 {
20859 /* Normally, the array bound must be an integral constant
20860 expression. However, as an extension, we allow VLAs
20861 in function scopes as long as they aren't part of a
20862 parameter declaration. */
20863 cp_parser_error (parser,
20864 "array bound is not an integer constant");
20865 bounds = error_mark_node;
20866 }
20867 else if (processing_template_decl
20868 && !type_dependent_expression_p (bounds))
20869 {
20870 /* Remember this wasn't a constant-expression. */
20871 bounds = build_nop (TREE_TYPE (bounds), bounds);
20872 TREE_SIDE_EFFECTS (bounds) = 1;
20873 }
20874 }
20875 else
20876 bounds = NULL_TREE;
20877 /* Look for the closing `]'. */
20878 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20879 {
20880 declarator = cp_error_declarator;
20881 break;
20882 }
20883
20884 attrs = cp_parser_std_attribute_spec_seq (parser);
20885 declarator = make_array_declarator (declarator, bounds);
20886 declarator->std_attributes = attrs;
20887 }
20888 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20889 {
20890 {
20891 tree qualifying_scope;
20892 tree unqualified_name;
20893 tree attrs;
20894 special_function_kind sfk;
20895 bool abstract_ok;
20896 bool pack_expansion_p = false;
20897 cp_token *declarator_id_start_token;
20898
20899 /* Parse a declarator-id */
20900 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20901 if (abstract_ok)
20902 {
20903 cp_parser_parse_tentatively (parser);
20904
20905 /* If we see an ellipsis, we should be looking at a
20906 parameter pack. */
20907 if (token->type == CPP_ELLIPSIS)
20908 {
20909 /* Consume the `...' */
20910 cp_lexer_consume_token (parser->lexer);
20911
20912 pack_expansion_p = true;
20913 }
20914 }
20915
20916 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20917 unqualified_name
20918 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20919 qualifying_scope = parser->scope;
20920 if (abstract_ok)
20921 {
20922 bool okay = false;
20923
20924 if (!unqualified_name && pack_expansion_p)
20925 {
20926 /* Check whether an error occurred. */
20927 okay = !cp_parser_error_occurred (parser);
20928
20929 /* We already consumed the ellipsis to mark a
20930 parameter pack, but we have no way to report it,
20931 so abort the tentative parse. We will be exiting
20932 immediately anyway. */
20933 cp_parser_abort_tentative_parse (parser);
20934 }
20935 else
20936 okay = cp_parser_parse_definitely (parser);
20937
20938 if (!okay)
20939 unqualified_name = error_mark_node;
20940 else if (unqualified_name
20941 && (qualifying_scope
20942 || (!identifier_p (unqualified_name))))
20943 {
20944 cp_parser_error (parser, "expected unqualified-id");
20945 unqualified_name = error_mark_node;
20946 }
20947 }
20948
20949 if (!unqualified_name)
20950 return NULL;
20951 if (unqualified_name == error_mark_node)
20952 {
20953 declarator = cp_error_declarator;
20954 pack_expansion_p = false;
20955 declarator->parameter_pack_p = false;
20956 break;
20957 }
20958
20959 attrs = cp_parser_std_attribute_spec_seq (parser);
20960
20961 if (qualifying_scope && at_namespace_scope_p ()
20962 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20963 {
20964 /* In the declaration of a member of a template class
20965 outside of the class itself, the SCOPE will sometimes
20966 be a TYPENAME_TYPE. For example, given:
20967
20968 template <typename T>
20969 int S<T>::R::i = 3;
20970
20971 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20972 this context, we must resolve S<T>::R to an ordinary
20973 type, rather than a typename type.
20974
20975 The reason we normally avoid resolving TYPENAME_TYPEs
20976 is that a specialization of `S' might render
20977 `S<T>::R' not a type. However, if `S' is
20978 specialized, then this `i' will not be used, so there
20979 is no harm in resolving the types here. */
20980 tree type;
20981
20982 /* Resolve the TYPENAME_TYPE. */
20983 type = resolve_typename_type (qualifying_scope,
20984 /*only_current_p=*/false);
20985 /* If that failed, the declarator is invalid. */
20986 if (TREE_CODE (type) == TYPENAME_TYPE)
20987 {
20988 if (typedef_variant_p (type))
20989 error_at (declarator_id_start_token->location,
20990 "cannot define member of dependent typedef "
20991 "%qT", type);
20992 else
20993 error_at (declarator_id_start_token->location,
20994 "%<%T::%E%> is not a type",
20995 TYPE_CONTEXT (qualifying_scope),
20996 TYPE_IDENTIFIER (qualifying_scope));
20997 }
20998 qualifying_scope = type;
20999 }
21000
21001 sfk = sfk_none;
21002
21003 if (unqualified_name)
21004 {
21005 tree class_type;
21006
21007 if (qualifying_scope
21008 && CLASS_TYPE_P (qualifying_scope))
21009 class_type = qualifying_scope;
21010 else
21011 class_type = current_class_type;
21012
21013 if (TREE_CODE (unqualified_name) == TYPE_DECL)
21014 {
21015 tree name_type = TREE_TYPE (unqualified_name);
21016
21017 if (!class_type || !same_type_p (name_type, class_type))
21018 {
21019 /* We do not attempt to print the declarator
21020 here because we do not have enough
21021 information about its original syntactic
21022 form. */
21023 cp_parser_error (parser, "invalid declarator");
21024 declarator = cp_error_declarator;
21025 break;
21026 }
21027 else if (qualifying_scope
21028 && CLASSTYPE_USE_TEMPLATE (name_type))
21029 {
21030 error_at (declarator_id_start_token->location,
21031 "invalid use of constructor as a template");
21032 inform (declarator_id_start_token->location,
21033 "use %<%T::%D%> instead of %<%T::%D%> to "
21034 "name the constructor in a qualified name",
21035 class_type,
21036 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
21037 class_type, name_type);
21038 declarator = cp_error_declarator;
21039 break;
21040 }
21041 unqualified_name = constructor_name (class_type);
21042 }
21043
21044 if (class_type)
21045 {
21046 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
21047 sfk = sfk_destructor;
21048 else if (identifier_p (unqualified_name)
21049 && IDENTIFIER_CONV_OP_P (unqualified_name))
21050 sfk = sfk_conversion;
21051 else if (/* There's no way to declare a constructor
21052 for an unnamed type, even if the type
21053 got a name for linkage purposes. */
21054 !TYPE_WAS_UNNAMED (class_type)
21055 /* Handle correctly (c++/19200):
21056
21057 struct S {
21058 struct T{};
21059 friend void S(T);
21060 };
21061
21062 and also:
21063
21064 namespace N {
21065 void S();
21066 }
21067
21068 struct S {
21069 friend void N::S();
21070 }; */
21071 && (!friend_p || class_type == qualifying_scope)
21072 && constructor_name_p (unqualified_name,
21073 class_type))
21074 sfk = sfk_constructor;
21075 else if (is_overloaded_fn (unqualified_name)
21076 && DECL_CONSTRUCTOR_P (get_first_fn
21077 (unqualified_name)))
21078 sfk = sfk_constructor;
21079
21080 if (ctor_dtor_or_conv_p && sfk != sfk_none)
21081 *ctor_dtor_or_conv_p = -1;
21082 }
21083 }
21084 declarator = make_id_declarator (qualifying_scope,
21085 unqualified_name,
21086 sfk, token->location);
21087 declarator->std_attributes = attrs;
21088 declarator->parameter_pack_p = pack_expansion_p;
21089
21090 if (pack_expansion_p)
21091 maybe_warn_variadic_templates ();
21092 }
21093
21094 handle_declarator:;
21095 scope = get_scope_of_declarator (declarator);
21096 if (scope)
21097 {
21098 /* Any names that appear after the declarator-id for a
21099 member are looked up in the containing scope. */
21100 if (at_function_scope_p ())
21101 {
21102 /* But declarations with qualified-ids can't appear in a
21103 function. */
21104 cp_parser_error (parser, "qualified-id in declaration");
21105 declarator = cp_error_declarator;
21106 break;
21107 }
21108 pushed_scope = push_scope (scope);
21109 }
21110 parser->in_declarator_p = true;
21111 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
21112 || (declarator && declarator->kind == cdk_id))
21113 /* Default args are only allowed on function
21114 declarations. */
21115 parser->default_arg_ok_p = saved_default_arg_ok_p;
21116 else
21117 parser->default_arg_ok_p = false;
21118
21119 first = false;
21120 }
21121 /* We're done. */
21122 else
21123 break;
21124 }
21125
21126 /* For an abstract declarator, we might wind up with nothing at this
21127 point. That's an error; the declarator is not optional. */
21128 if (!declarator)
21129 cp_parser_error (parser, "expected declarator");
21130 else if (open_paren)
21131 {
21132 /* Record overly parenthesized declarator so we can give a
21133 diagnostic about confusing decl/expr disambiguation. */
21134 if (declarator->kind == cdk_array)
21135 {
21136 /* If the open and close parens are on different lines, this
21137 is probably a formatting thing, so ignore. */
21138 expanded_location open = expand_location (open_paren->location);
21139 expanded_location close = expand_location (close_paren->location);
21140 if (open.line != close.line || open.file != close.file)
21141 open_paren = NULL;
21142 }
21143 if (open_paren)
21144 declarator->parenthesized = open_paren->location;
21145 }
21146
21147 /* If we entered a scope, we must exit it now. */
21148 if (pushed_scope)
21149 pop_scope (pushed_scope);
21150
21151 parser->default_arg_ok_p = saved_default_arg_ok_p;
21152 parser->in_declarator_p = saved_in_declarator_p;
21153
21154 return declarator;
21155 }
21156
21157 /* Parse a ptr-operator.
21158
21159 ptr-operator:
21160 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21161 * cv-qualifier-seq [opt]
21162 &
21163 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21164 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21165
21166 GNU Extension:
21167
21168 ptr-operator:
21169 & cv-qualifier-seq [opt]
21170
21171 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21172 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21173 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21174 filled in with the TYPE containing the member. *CV_QUALS is
21175 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21176 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21177 Note that the tree codes returned by this function have nothing
21178 to do with the types of trees that will be eventually be created
21179 to represent the pointer or reference type being parsed. They are
21180 just constants with suggestive names. */
21181 static enum tree_code
21182 cp_parser_ptr_operator (cp_parser* parser,
21183 tree* type,
21184 cp_cv_quals *cv_quals,
21185 tree *attributes)
21186 {
21187 enum tree_code code = ERROR_MARK;
21188 cp_token *token;
21189 tree attrs = NULL_TREE;
21190
21191 /* Assume that it's not a pointer-to-member. */
21192 *type = NULL_TREE;
21193 /* And that there are no cv-qualifiers. */
21194 *cv_quals = TYPE_UNQUALIFIED;
21195
21196 /* Peek at the next token. */
21197 token = cp_lexer_peek_token (parser->lexer);
21198
21199 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21200 if (token->type == CPP_MULT)
21201 code = INDIRECT_REF;
21202 else if (token->type == CPP_AND)
21203 code = ADDR_EXPR;
21204 else if ((cxx_dialect != cxx98) &&
21205 token->type == CPP_AND_AND) /* C++0x only */
21206 code = NON_LVALUE_EXPR;
21207
21208 if (code != ERROR_MARK)
21209 {
21210 /* Consume the `*', `&' or `&&'. */
21211 cp_lexer_consume_token (parser->lexer);
21212
21213 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21214 `&', if we are allowing GNU extensions. (The only qualifier
21215 that can legally appear after `&' is `restrict', but that is
21216 enforced during semantic analysis. */
21217 if (code == INDIRECT_REF
21218 || cp_parser_allow_gnu_extensions_p (parser))
21219 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21220
21221 attrs = cp_parser_std_attribute_spec_seq (parser);
21222 if (attributes != NULL)
21223 *attributes = attrs;
21224 }
21225 else
21226 {
21227 /* Try the pointer-to-member case. */
21228 cp_parser_parse_tentatively (parser);
21229 /* Look for the optional `::' operator. */
21230 cp_parser_global_scope_opt (parser,
21231 /*current_scope_valid_p=*/false);
21232 /* Look for the nested-name specifier. */
21233 token = cp_lexer_peek_token (parser->lexer);
21234 cp_parser_nested_name_specifier (parser,
21235 /*typename_keyword_p=*/false,
21236 /*check_dependency_p=*/true,
21237 /*type_p=*/false,
21238 /*is_declaration=*/false);
21239 /* If we found it, and the next token is a `*', then we are
21240 indeed looking at a pointer-to-member operator. */
21241 if (!cp_parser_error_occurred (parser)
21242 && cp_parser_require (parser, CPP_MULT, RT_MULT))
21243 {
21244 /* Indicate that the `*' operator was used. */
21245 code = INDIRECT_REF;
21246
21247 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21248 error_at (token->location, "%qD is a namespace", parser->scope);
21249 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
21250 error_at (token->location, "cannot form pointer to member of "
21251 "non-class %q#T", parser->scope);
21252 else
21253 {
21254 /* The type of which the member is a member is given by the
21255 current SCOPE. */
21256 *type = parser->scope;
21257 /* The next name will not be qualified. */
21258 parser->scope = NULL_TREE;
21259 parser->qualifying_scope = NULL_TREE;
21260 parser->object_scope = NULL_TREE;
21261 /* Look for optional c++11 attributes. */
21262 attrs = cp_parser_std_attribute_spec_seq (parser);
21263 if (attributes != NULL)
21264 *attributes = attrs;
21265 /* Look for the optional cv-qualifier-seq. */
21266 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21267 }
21268 }
21269 /* If that didn't work we don't have a ptr-operator. */
21270 if (!cp_parser_parse_definitely (parser))
21271 cp_parser_error (parser, "expected ptr-operator");
21272 }
21273
21274 return code;
21275 }
21276
21277 /* Parse an (optional) cv-qualifier-seq.
21278
21279 cv-qualifier-seq:
21280 cv-qualifier cv-qualifier-seq [opt]
21281
21282 cv-qualifier:
21283 const
21284 volatile
21285
21286 GNU Extension:
21287
21288 cv-qualifier:
21289 __restrict__
21290
21291 Returns a bitmask representing the cv-qualifiers. */
21292
21293 static cp_cv_quals
21294 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
21295 {
21296 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
21297
21298 while (true)
21299 {
21300 cp_token *token;
21301 cp_cv_quals cv_qualifier;
21302
21303 /* Peek at the next token. */
21304 token = cp_lexer_peek_token (parser->lexer);
21305 /* See if it's a cv-qualifier. */
21306 switch (token->keyword)
21307 {
21308 case RID_CONST:
21309 cv_qualifier = TYPE_QUAL_CONST;
21310 break;
21311
21312 case RID_VOLATILE:
21313 cv_qualifier = TYPE_QUAL_VOLATILE;
21314 break;
21315
21316 case RID_RESTRICT:
21317 cv_qualifier = TYPE_QUAL_RESTRICT;
21318 break;
21319
21320 default:
21321 cv_qualifier = TYPE_UNQUALIFIED;
21322 break;
21323 }
21324
21325 if (!cv_qualifier)
21326 break;
21327
21328 if (cv_quals & cv_qualifier)
21329 {
21330 gcc_rich_location richloc (token->location);
21331 richloc.add_fixit_remove ();
21332 error_at (&richloc, "duplicate cv-qualifier");
21333 cp_lexer_purge_token (parser->lexer);
21334 }
21335 else
21336 {
21337 cp_lexer_consume_token (parser->lexer);
21338 cv_quals |= cv_qualifier;
21339 }
21340 }
21341
21342 return cv_quals;
21343 }
21344
21345 /* Parse an (optional) ref-qualifier
21346
21347 ref-qualifier:
21348 &
21349 &&
21350
21351 Returns cp_ref_qualifier representing ref-qualifier. */
21352
21353 static cp_ref_qualifier
21354 cp_parser_ref_qualifier_opt (cp_parser* parser)
21355 {
21356 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
21357
21358 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21359 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
21360 return ref_qual;
21361
21362 while (true)
21363 {
21364 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
21365 cp_token *token = cp_lexer_peek_token (parser->lexer);
21366
21367 switch (token->type)
21368 {
21369 case CPP_AND:
21370 curr_ref_qual = REF_QUAL_LVALUE;
21371 break;
21372
21373 case CPP_AND_AND:
21374 curr_ref_qual = REF_QUAL_RVALUE;
21375 break;
21376
21377 default:
21378 curr_ref_qual = REF_QUAL_NONE;
21379 break;
21380 }
21381
21382 if (!curr_ref_qual)
21383 break;
21384 else if (ref_qual)
21385 {
21386 error_at (token->location, "multiple ref-qualifiers");
21387 cp_lexer_purge_token (parser->lexer);
21388 }
21389 else
21390 {
21391 ref_qual = curr_ref_qual;
21392 cp_lexer_consume_token (parser->lexer);
21393 }
21394 }
21395
21396 return ref_qual;
21397 }
21398
21399 /* Parse an optional tx-qualifier.
21400
21401 tx-qualifier:
21402 transaction_safe
21403 transaction_safe_dynamic */
21404
21405 static tree
21406 cp_parser_tx_qualifier_opt (cp_parser *parser)
21407 {
21408 cp_token *token = cp_lexer_peek_token (parser->lexer);
21409 if (token->type == CPP_NAME)
21410 {
21411 tree name = token->u.value;
21412 const char *p = IDENTIFIER_POINTER (name);
21413 const int len = strlen ("transaction_safe");
21414 if (!strncmp (p, "transaction_safe", len))
21415 {
21416 p += len;
21417 if (*p == '\0'
21418 || !strcmp (p, "_dynamic"))
21419 {
21420 cp_lexer_consume_token (parser->lexer);
21421 if (!flag_tm)
21422 {
21423 error ("%qE requires %<-fgnu-tm%>", name);
21424 return NULL_TREE;
21425 }
21426 else
21427 return name;
21428 }
21429 }
21430 }
21431 return NULL_TREE;
21432 }
21433
21434 /* Parse an (optional) virt-specifier-seq.
21435
21436 virt-specifier-seq:
21437 virt-specifier virt-specifier-seq [opt]
21438
21439 virt-specifier:
21440 override
21441 final
21442
21443 Returns a bitmask representing the virt-specifiers. */
21444
21445 static cp_virt_specifiers
21446 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21447 {
21448 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21449
21450 while (true)
21451 {
21452 cp_token *token;
21453 cp_virt_specifiers virt_specifier;
21454
21455 /* Peek at the next token. */
21456 token = cp_lexer_peek_token (parser->lexer);
21457 /* See if it's a virt-specifier-qualifier. */
21458 if (token->type != CPP_NAME)
21459 break;
21460 if (id_equal (token->u.value, "override"))
21461 {
21462 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21463 virt_specifier = VIRT_SPEC_OVERRIDE;
21464 }
21465 else if (id_equal (token->u.value, "final"))
21466 {
21467 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21468 virt_specifier = VIRT_SPEC_FINAL;
21469 }
21470 else if (id_equal (token->u.value, "__final"))
21471 {
21472 virt_specifier = VIRT_SPEC_FINAL;
21473 }
21474 else
21475 break;
21476
21477 if (virt_specifiers & virt_specifier)
21478 {
21479 gcc_rich_location richloc (token->location);
21480 richloc.add_fixit_remove ();
21481 error_at (&richloc, "duplicate virt-specifier");
21482 cp_lexer_purge_token (parser->lexer);
21483 }
21484 else
21485 {
21486 cp_lexer_consume_token (parser->lexer);
21487 virt_specifiers |= virt_specifier;
21488 }
21489 }
21490 return virt_specifiers;
21491 }
21492
21493 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21494 is in scope even though it isn't real. */
21495
21496 void
21497 inject_this_parameter (tree ctype, cp_cv_quals quals)
21498 {
21499 tree this_parm;
21500
21501 if (current_class_ptr)
21502 {
21503 /* We don't clear this between NSDMIs. Is it already what we want? */
21504 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21505 if (DECL_P (current_class_ptr)
21506 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21507 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21508 && cp_type_quals (type) == quals)
21509 return;
21510 }
21511
21512 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21513 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21514 current_class_ptr = NULL_TREE;
21515 current_class_ref
21516 = cp_build_fold_indirect_ref (this_parm);
21517 current_class_ptr = this_parm;
21518 }
21519
21520 /* Return true iff our current scope is a non-static data member
21521 initializer. */
21522
21523 bool
21524 parsing_nsdmi (void)
21525 {
21526 /* We recognize NSDMI context by the context-less 'this' pointer set up
21527 by the function above. */
21528 if (current_class_ptr
21529 && TREE_CODE (current_class_ptr) == PARM_DECL
21530 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21531 return true;
21532 return false;
21533 }
21534
21535 /* Parse a late-specified return type, if any. This is not a separate
21536 non-terminal, but part of a function declarator, which looks like
21537
21538 -> trailing-type-specifier-seq abstract-declarator(opt)
21539
21540 Returns the type indicated by the type-id.
21541
21542 In addition to this, parse any queued up #pragma omp declare simd
21543 clauses, and #pragma acc routine clauses.
21544
21545 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21546 function. */
21547
21548 static tree
21549 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21550 tree& requires_clause, cp_cv_quals quals)
21551 {
21552 cp_token *token;
21553 tree type = NULL_TREE;
21554 bool declare_simd_p = (parser->omp_declare_simd
21555 && declarator
21556 && declarator->kind == cdk_id);
21557
21558 bool oacc_routine_p = (parser->oacc_routine
21559 && declarator
21560 && declarator->kind == cdk_id);
21561
21562 /* Peek at the next token. */
21563 token = cp_lexer_peek_token (parser->lexer);
21564 /* A late-specified return type is indicated by an initial '->'. */
21565 if (token->type != CPP_DEREF
21566 && token->keyword != RID_REQUIRES
21567 && !(token->type == CPP_NAME
21568 && token->u.value == ridpointers[RID_REQUIRES])
21569 && !(declare_simd_p || oacc_routine_p))
21570 return NULL_TREE;
21571
21572 tree save_ccp = current_class_ptr;
21573 tree save_ccr = current_class_ref;
21574 if (quals >= 0)
21575 {
21576 /* DR 1207: 'this' is in scope in the trailing return type. */
21577 inject_this_parameter (current_class_type, quals);
21578 }
21579
21580 if (token->type == CPP_DEREF)
21581 {
21582 /* Consume the ->. */
21583 cp_lexer_consume_token (parser->lexer);
21584
21585 type = cp_parser_trailing_type_id (parser);
21586 }
21587
21588 /* Function declarations may be followed by a trailing
21589 requires-clause. */
21590 requires_clause = cp_parser_requires_clause_opt (parser);
21591
21592 if (declare_simd_p)
21593 declarator->attributes
21594 = cp_parser_late_parsing_omp_declare_simd (parser,
21595 declarator->attributes);
21596 if (oacc_routine_p)
21597 declarator->attributes
21598 = cp_parser_late_parsing_oacc_routine (parser,
21599 declarator->attributes);
21600
21601 if (quals >= 0)
21602 {
21603 current_class_ptr = save_ccp;
21604 current_class_ref = save_ccr;
21605 }
21606
21607 return type;
21608 }
21609
21610 /* Parse a declarator-id.
21611
21612 declarator-id:
21613 id-expression
21614 :: [opt] nested-name-specifier [opt] type-name
21615
21616 In the `id-expression' case, the value returned is as for
21617 cp_parser_id_expression if the id-expression was an unqualified-id.
21618 If the id-expression was a qualified-id, then a SCOPE_REF is
21619 returned. The first operand is the scope (either a NAMESPACE_DECL
21620 or TREE_TYPE), but the second is still just a representation of an
21621 unqualified-id. */
21622
21623 static tree
21624 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21625 {
21626 tree id;
21627 /* The expression must be an id-expression. Assume that qualified
21628 names are the names of types so that:
21629
21630 template <class T>
21631 int S<T>::R::i = 3;
21632
21633 will work; we must treat `S<T>::R' as the name of a type.
21634 Similarly, assume that qualified names are templates, where
21635 required, so that:
21636
21637 template <class T>
21638 int S<T>::R<T>::i = 3;
21639
21640 will work, too. */
21641 id = cp_parser_id_expression (parser,
21642 /*template_keyword_p=*/false,
21643 /*check_dependency_p=*/false,
21644 /*template_p=*/NULL,
21645 /*declarator_p=*/true,
21646 optional_p);
21647 if (id && BASELINK_P (id))
21648 id = BASELINK_FUNCTIONS (id);
21649 return id;
21650 }
21651
21652 /* Parse a type-id.
21653
21654 type-id:
21655 type-specifier-seq abstract-declarator [opt]
21656
21657 The parser flags FLAGS is used to control type-specifier parsing.
21658
21659 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
21660
21661 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21662 i.e. we've just seen "->".
21663
21664 Returns the TYPE specified. */
21665
21666 static tree
21667 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
21668 bool is_template_arg, bool is_trailing_return,
21669 location_t *type_location)
21670 {
21671 cp_decl_specifier_seq type_specifier_seq;
21672 cp_declarator *abstract_declarator;
21673
21674 /* Parse the type-specifier-seq. */
21675 cp_parser_type_specifier_seq (parser, flags,
21676 /*is_declaration=*/false,
21677 is_trailing_return,
21678 &type_specifier_seq);
21679 if (type_location)
21680 *type_location = type_specifier_seq.locations[ds_type_spec];
21681
21682 if (is_template_arg && type_specifier_seq.type
21683 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21684 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21685 /* A bare template name as a template argument is a template template
21686 argument, not a placeholder, so fail parsing it as a type argument. */
21687 {
21688 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21689 cp_parser_simulate_error (parser);
21690 return error_mark_node;
21691 }
21692 if (type_specifier_seq.type == error_mark_node)
21693 return error_mark_node;
21694
21695 /* There might or might not be an abstract declarator. */
21696 cp_parser_parse_tentatively (parser);
21697 /* Look for the declarator. */
21698 abstract_declarator
21699 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
21700 CP_PARSER_FLAGS_NONE, NULL,
21701 /*parenthesized_p=*/NULL,
21702 /*member_p=*/false,
21703 /*friend_p=*/false,
21704 /*static_p=*/false);
21705 /* Check to see if there really was a declarator. */
21706 if (!cp_parser_parse_definitely (parser))
21707 abstract_declarator = NULL;
21708
21709 if (type_specifier_seq.type
21710 /* The concepts TS allows 'auto' as a type-id. */
21711 && (!flag_concepts || parser->in_type_id_in_expr_p)
21712 /* None of the valid uses of 'auto' in C++14 involve the type-id
21713 nonterminal, but it is valid in a trailing-return-type. */
21714 && !(cxx_dialect >= cxx14 && is_trailing_return))
21715 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21716 {
21717 /* A type-id with type 'auto' is only ok if the abstract declarator
21718 is a function declarator with a late-specified return type.
21719
21720 A type-id with 'auto' is also valid in a trailing-return-type
21721 in a compound-requirement. */
21722 if (abstract_declarator
21723 && abstract_declarator->kind == cdk_function
21724 && abstract_declarator->u.function.late_return_type)
21725 /* OK */;
21726 else if (parser->in_result_type_constraint_p)
21727 /* OK */;
21728 else
21729 {
21730 location_t loc = type_specifier_seq.locations[ds_type_spec];
21731 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21732 {
21733 error_at (loc, "missing template arguments after %qT",
21734 auto_node);
21735 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21736 tmpl);
21737 }
21738 else
21739 error_at (loc, "invalid use of %qT", auto_node);
21740 return error_mark_node;
21741 }
21742 }
21743
21744 return groktypename (&type_specifier_seq, abstract_declarator,
21745 is_template_arg);
21746 }
21747
21748 /* Wrapper for cp_parser_type_id_1. */
21749
21750 static tree
21751 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
21752 location_t *type_location)
21753 {
21754 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
21755 }
21756
21757 /* Wrapper for cp_parser_type_id_1. */
21758
21759 static tree
21760 cp_parser_template_type_arg (cp_parser *parser)
21761 {
21762 tree r;
21763 const char *saved_message = parser->type_definition_forbidden_message;
21764 parser->type_definition_forbidden_message
21765 = G_("types may not be defined in template arguments");
21766 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
21767 parser->type_definition_forbidden_message = saved_message;
21768 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21769 {
21770 error ("invalid use of %<auto%> in template argument");
21771 r = error_mark_node;
21772 }
21773 return r;
21774 }
21775
21776 /* Wrapper for cp_parser_type_id_1. */
21777
21778 static tree
21779 cp_parser_trailing_type_id (cp_parser *parser)
21780 {
21781 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
21782 false, true, NULL);
21783 }
21784
21785 /* Parse a type-specifier-seq.
21786
21787 type-specifier-seq:
21788 type-specifier type-specifier-seq [opt]
21789
21790 GNU extension:
21791
21792 type-specifier-seq:
21793 attributes type-specifier-seq [opt]
21794
21795 The parser flags FLAGS is used to control type-specifier parsing.
21796
21797 If IS_DECLARATION is true, we are at the start of a "condition" or
21798 exception-declaration, so we might be followed by a declarator-id.
21799
21800 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21801 i.e. we've just seen "->".
21802
21803 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21804
21805 static void
21806 cp_parser_type_specifier_seq (cp_parser* parser,
21807 cp_parser_flags flags,
21808 bool is_declaration,
21809 bool is_trailing_return,
21810 cp_decl_specifier_seq *type_specifier_seq)
21811 {
21812 bool seen_type_specifier = false;
21813 cp_token *start_token = NULL;
21814
21815 /* Clear the TYPE_SPECIFIER_SEQ. */
21816 clear_decl_specs (type_specifier_seq);
21817
21818 flags |= CP_PARSER_FLAGS_OPTIONAL;
21819 /* In the context of a trailing return type, enum E { } is an
21820 elaborated-type-specifier followed by a function-body, not an
21821 enum-specifier. */
21822 if (is_trailing_return)
21823 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21824
21825 /* Parse the type-specifiers and attributes. */
21826 while (true)
21827 {
21828 tree type_specifier;
21829 bool is_cv_qualifier;
21830
21831 /* Check for attributes first. */
21832 if (cp_next_tokens_can_be_attribute_p (parser))
21833 {
21834 type_specifier_seq->attributes
21835 = attr_chainon (type_specifier_seq->attributes,
21836 cp_parser_attributes_opt (parser));
21837 continue;
21838 }
21839
21840 /* record the token of the beginning of the type specifier seq,
21841 for error reporting purposes*/
21842 if (!start_token)
21843 start_token = cp_lexer_peek_token (parser->lexer);
21844
21845 /* Look for the type-specifier. */
21846 type_specifier = cp_parser_type_specifier (parser,
21847 flags,
21848 type_specifier_seq,
21849 /*is_declaration=*/false,
21850 NULL,
21851 &is_cv_qualifier);
21852 if (!type_specifier)
21853 {
21854 /* If the first type-specifier could not be found, this is not a
21855 type-specifier-seq at all. */
21856 if (!seen_type_specifier)
21857 {
21858 /* Set in_declarator_p to avoid skipping to the semicolon. */
21859 int in_decl = parser->in_declarator_p;
21860 parser->in_declarator_p = true;
21861
21862 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21863 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21864 cp_parser_error (parser, "expected type-specifier");
21865
21866 parser->in_declarator_p = in_decl;
21867
21868 type_specifier_seq->type = error_mark_node;
21869 return;
21870 }
21871 /* If subsequent type-specifiers could not be found, the
21872 type-specifier-seq is complete. */
21873 break;
21874 }
21875
21876 seen_type_specifier = true;
21877 /* The standard says that a condition can be:
21878
21879 type-specifier-seq declarator = assignment-expression
21880
21881 However, given:
21882
21883 struct S {};
21884 if (int S = ...)
21885
21886 we should treat the "S" as a declarator, not as a
21887 type-specifier. The standard doesn't say that explicitly for
21888 type-specifier-seq, but it does say that for
21889 decl-specifier-seq in an ordinary declaration. Perhaps it
21890 would be clearer just to allow a decl-specifier-seq here, and
21891 then add a semantic restriction that if any decl-specifiers
21892 that are not type-specifiers appear, the program is invalid. */
21893 if (is_declaration && !is_cv_qualifier)
21894 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21895 }
21896 }
21897
21898 /* Return whether the function currently being declared has an associated
21899 template parameter list. */
21900
21901 static bool
21902 function_being_declared_is_template_p (cp_parser* parser)
21903 {
21904 if (!current_template_parms || processing_template_parmlist)
21905 return false;
21906
21907 if (parser->implicit_template_scope)
21908 return true;
21909
21910 if (at_class_scope_p ()
21911 && TYPE_BEING_DEFINED (current_class_type))
21912 return parser->num_template_parameter_lists != 0;
21913
21914 return ((int) parser->num_template_parameter_lists > template_class_depth
21915 (current_class_type));
21916 }
21917
21918 /* Parse a parameter-declaration-clause.
21919
21920 parameter-declaration-clause:
21921 parameter-declaration-list [opt] ... [opt]
21922 parameter-declaration-list , ...
21923
21924 The parser flags FLAGS is used to control type-specifier parsing.
21925
21926 Returns a representation for the parameter declarations. A return
21927 value of NULL indicates a parameter-declaration-clause consisting
21928 only of an ellipsis. */
21929
21930 static tree
21931 cp_parser_parameter_declaration_clause (cp_parser* parser,
21932 cp_parser_flags flags)
21933 {
21934 tree parameters;
21935 cp_token *token;
21936 bool ellipsis_p;
21937
21938 temp_override<bool> cleanup
21939 (parser->auto_is_implicit_function_template_parm_p);
21940
21941 if (!processing_specialization
21942 && !processing_template_parmlist
21943 && !processing_explicit_instantiation
21944 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21945 actual function or a random abstract declarator. */
21946 && parser->default_arg_ok_p)
21947 if (!current_function_decl
21948 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21949 parser->auto_is_implicit_function_template_parm_p = true;
21950
21951 /* Peek at the next token. */
21952 token = cp_lexer_peek_token (parser->lexer);
21953 /* Check for trivial parameter-declaration-clauses. */
21954 if (token->type == CPP_ELLIPSIS)
21955 {
21956 /* Consume the `...' token. */
21957 cp_lexer_consume_token (parser->lexer);
21958 return NULL_TREE;
21959 }
21960 else if (token->type == CPP_CLOSE_PAREN)
21961 /* There are no parameters. */
21962 return void_list_node;
21963 /* Check for `(void)', too, which is a special case. */
21964 else if (token->keyword == RID_VOID
21965 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21966 == CPP_CLOSE_PAREN))
21967 {
21968 /* Consume the `void' token. */
21969 cp_lexer_consume_token (parser->lexer);
21970 /* There are no parameters. */
21971 return void_list_node;
21972 }
21973
21974 /* Parse the parameter-declaration-list. */
21975 parameters = cp_parser_parameter_declaration_list (parser, flags);
21976 /* If a parse error occurred while parsing the
21977 parameter-declaration-list, then the entire
21978 parameter-declaration-clause is erroneous. */
21979 if (parameters == error_mark_node)
21980 return NULL_TREE;
21981
21982 /* Peek at the next token. */
21983 token = cp_lexer_peek_token (parser->lexer);
21984 /* If it's a `,', the clause should terminate with an ellipsis. */
21985 if (token->type == CPP_COMMA)
21986 {
21987 /* Consume the `,'. */
21988 cp_lexer_consume_token (parser->lexer);
21989 /* Expect an ellipsis. */
21990 ellipsis_p
21991 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21992 }
21993 /* It might also be `...' if the optional trailing `,' was
21994 omitted. */
21995 else if (token->type == CPP_ELLIPSIS)
21996 {
21997 /* Consume the `...' token. */
21998 cp_lexer_consume_token (parser->lexer);
21999 /* And remember that we saw it. */
22000 ellipsis_p = true;
22001 }
22002 else
22003 ellipsis_p = false;
22004
22005 /* Finish the parameter list. */
22006 if (!ellipsis_p)
22007 parameters = chainon (parameters, void_list_node);
22008
22009 return parameters;
22010 }
22011
22012 /* Parse a parameter-declaration-list.
22013
22014 parameter-declaration-list:
22015 parameter-declaration
22016 parameter-declaration-list , parameter-declaration
22017
22018 The parser flags FLAGS is used to control type-specifier parsing.
22019
22020 Returns a representation of the parameter-declaration-list, as for
22021 cp_parser_parameter_declaration_clause. However, the
22022 `void_list_node' is never appended to the list. */
22023
22024 static tree
22025 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
22026 {
22027 tree parameters = NULL_TREE;
22028 tree *tail = &parameters;
22029 bool saved_in_unbraced_linkage_specification_p;
22030 int index = 0;
22031
22032 /* The special considerations that apply to a function within an
22033 unbraced linkage specifications do not apply to the parameters
22034 to the function. */
22035 saved_in_unbraced_linkage_specification_p
22036 = parser->in_unbraced_linkage_specification_p;
22037 parser->in_unbraced_linkage_specification_p = false;
22038
22039 /* Look for more parameters. */
22040 while (true)
22041 {
22042 cp_parameter_declarator *parameter;
22043 tree decl = error_mark_node;
22044 bool parenthesized_p = false;
22045
22046 /* Parse the parameter. */
22047 parameter
22048 = cp_parser_parameter_declaration (parser, flags,
22049 /*template_parm_p=*/false,
22050 &parenthesized_p);
22051
22052 /* We don't know yet if the enclosing context is deprecated, so wait
22053 and warn in grokparms if appropriate. */
22054 deprecated_state = DEPRECATED_SUPPRESS;
22055
22056 if (parameter)
22057 {
22058 decl = grokdeclarator (parameter->declarator,
22059 &parameter->decl_specifiers,
22060 PARM,
22061 parameter->default_argument != NULL_TREE,
22062 &parameter->decl_specifiers.attributes);
22063 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
22064 DECL_SOURCE_LOCATION (decl) = parameter->loc;
22065 }
22066
22067 deprecated_state = DEPRECATED_NORMAL;
22068
22069 /* If a parse error occurred parsing the parameter declaration,
22070 then the entire parameter-declaration-list is erroneous. */
22071 if (decl == error_mark_node)
22072 {
22073 parameters = error_mark_node;
22074 break;
22075 }
22076
22077 if (parameter->decl_specifiers.attributes)
22078 cplus_decl_attributes (&decl,
22079 parameter->decl_specifiers.attributes,
22080 0);
22081 if (DECL_NAME (decl))
22082 decl = pushdecl (decl);
22083
22084 if (decl != error_mark_node)
22085 {
22086 retrofit_lang_decl (decl);
22087 DECL_PARM_INDEX (decl) = ++index;
22088 DECL_PARM_LEVEL (decl) = function_parm_depth ();
22089 }
22090
22091 /* Add the new parameter to the list. */
22092 *tail = build_tree_list (parameter->default_argument, decl);
22093 tail = &TREE_CHAIN (*tail);
22094
22095 /* Peek at the next token. */
22096 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
22097 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
22098 /* These are for Objective-C++ */
22099 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22100 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22101 /* The parameter-declaration-list is complete. */
22102 break;
22103 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22104 {
22105 cp_token *token;
22106
22107 /* Peek at the next token. */
22108 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22109 /* If it's an ellipsis, then the list is complete. */
22110 if (token->type == CPP_ELLIPSIS)
22111 break;
22112 /* Otherwise, there must be more parameters. Consume the
22113 `,'. */
22114 cp_lexer_consume_token (parser->lexer);
22115 /* When parsing something like:
22116
22117 int i(float f, double d)
22118
22119 we can tell after seeing the declaration for "f" that we
22120 are not looking at an initialization of a variable "i",
22121 but rather at the declaration of a function "i".
22122
22123 Due to the fact that the parsing of template arguments
22124 (as specified to a template-id) requires backtracking we
22125 cannot use this technique when inside a template argument
22126 list. */
22127 if (!parser->in_template_argument_list_p
22128 && !parser->in_type_id_in_expr_p
22129 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22130 /* However, a parameter-declaration of the form
22131 "float(f)" (which is a valid declaration of a
22132 parameter "f") can also be interpreted as an
22133 expression (the conversion of "f" to "float"). */
22134 && !parenthesized_p)
22135 cp_parser_commit_to_tentative_parse (parser);
22136 }
22137 else
22138 {
22139 cp_parser_error (parser, "expected %<,%> or %<...%>");
22140 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
22141 cp_parser_skip_to_closing_parenthesis (parser,
22142 /*recovering=*/true,
22143 /*or_comma=*/false,
22144 /*consume_paren=*/false);
22145 break;
22146 }
22147 }
22148
22149 parser->in_unbraced_linkage_specification_p
22150 = saved_in_unbraced_linkage_specification_p;
22151
22152 /* Reset implicit_template_scope if we are about to leave the function
22153 parameter list that introduced it. Note that for out-of-line member
22154 definitions, there will be one or more class scopes before we get to
22155 the template parameter scope. */
22156
22157 if (cp_binding_level *its = parser->implicit_template_scope)
22158 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
22159 {
22160 while (maybe_its->kind == sk_class)
22161 maybe_its = maybe_its->level_chain;
22162 if (maybe_its == its)
22163 {
22164 parser->implicit_template_parms = 0;
22165 parser->implicit_template_scope = 0;
22166 }
22167 }
22168
22169 return parameters;
22170 }
22171
22172 /* Parse a parameter declaration.
22173
22174 parameter-declaration:
22175 decl-specifier-seq ... [opt] declarator
22176 decl-specifier-seq declarator = assignment-expression
22177 decl-specifier-seq ... [opt] abstract-declarator [opt]
22178 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22179
22180 The parser flags FLAGS is used to control type-specifier parsing.
22181
22182 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22183 declares a template parameter. (In that case, a non-nested `>'
22184 token encountered during the parsing of the assignment-expression
22185 is not interpreted as a greater-than operator.)
22186
22187 Returns a representation of the parameter, or NULL if an error
22188 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22189 true iff the declarator is of the form "(p)". */
22190
22191 static cp_parameter_declarator *
22192 cp_parser_parameter_declaration (cp_parser *parser,
22193 cp_parser_flags flags,
22194 bool template_parm_p,
22195 bool *parenthesized_p)
22196 {
22197 int declares_class_or_enum;
22198 cp_decl_specifier_seq decl_specifiers;
22199 cp_declarator *declarator;
22200 tree default_argument;
22201 cp_token *token = NULL, *declarator_token_start = NULL;
22202 const char *saved_message;
22203 bool template_parameter_pack_p = false;
22204
22205 /* In a template parameter, `>' is not an operator.
22206
22207 [temp.param]
22208
22209 When parsing a default template-argument for a non-type
22210 template-parameter, the first non-nested `>' is taken as the end
22211 of the template parameter-list rather than a greater-than
22212 operator. */
22213
22214 /* Type definitions may not appear in parameter types. */
22215 saved_message = parser->type_definition_forbidden_message;
22216 parser->type_definition_forbidden_message
22217 = G_("types may not be defined in parameter types");
22218
22219 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
22220 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22221 (current_template_parms)) : 0);
22222
22223 /* Parse the declaration-specifiers. */
22224 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22225 cp_parser_decl_specifier_seq (parser,
22226 flags,
22227 &decl_specifiers,
22228 &declares_class_or_enum);
22229
22230 /* Complain about missing 'typename' or other invalid type names. */
22231 if (!decl_specifiers.any_type_specifiers_p
22232 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22233 decl_specifiers.type = error_mark_node;
22234
22235 /* If an error occurred, there's no reason to attempt to parse the
22236 rest of the declaration. */
22237 if (cp_parser_error_occurred (parser))
22238 {
22239 parser->type_definition_forbidden_message = saved_message;
22240 return NULL;
22241 }
22242
22243 /* Peek at the next token. */
22244 token = cp_lexer_peek_token (parser->lexer);
22245
22246 /* If the next token is a `)', `,', `=', `>', or `...', then there
22247 is no declarator. However, when variadic templates are enabled,
22248 there may be a declarator following `...'. */
22249 if (token->type == CPP_CLOSE_PAREN
22250 || token->type == CPP_COMMA
22251 || token->type == CPP_EQ
22252 || token->type == CPP_GREATER)
22253 {
22254 declarator = NULL;
22255 if (parenthesized_p)
22256 *parenthesized_p = false;
22257 }
22258 /* Otherwise, there should be a declarator. */
22259 else
22260 {
22261 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22262 parser->default_arg_ok_p = false;
22263
22264 /* After seeing a decl-specifier-seq, if the next token is not a
22265 "(", there is no possibility that the code is a valid
22266 expression. Therefore, if parsing tentatively, we commit at
22267 this point. */
22268 if (!parser->in_template_argument_list_p
22269 /* In an expression context, having seen:
22270
22271 (int((char ...
22272
22273 we cannot be sure whether we are looking at a
22274 function-type (taking a "char" as a parameter) or a cast
22275 of some object of type "char" to "int". */
22276 && !parser->in_type_id_in_expr_p
22277 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22278 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22279 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
22280 cp_parser_commit_to_tentative_parse (parser);
22281 /* Parse the declarator. */
22282 declarator_token_start = token;
22283 declarator = cp_parser_declarator (parser,
22284 CP_PARSER_DECLARATOR_EITHER,
22285 CP_PARSER_FLAGS_NONE,
22286 /*ctor_dtor_or_conv_p=*/NULL,
22287 parenthesized_p,
22288 /*member_p=*/false,
22289 /*friend_p=*/false,
22290 /*static_p=*/false);
22291 parser->default_arg_ok_p = saved_default_arg_ok_p;
22292 /* After the declarator, allow more attributes. */
22293 decl_specifiers.attributes
22294 = attr_chainon (decl_specifiers.attributes,
22295 cp_parser_attributes_opt (parser));
22296
22297 /* If the declarator is a template parameter pack, remember that and
22298 clear the flag in the declarator itself so we don't get errors
22299 from grokdeclarator. */
22300 if (template_parm_p && declarator && declarator->parameter_pack_p)
22301 {
22302 declarator->parameter_pack_p = false;
22303 template_parameter_pack_p = true;
22304 }
22305 }
22306
22307 /* If the next token is an ellipsis, and we have not seen a declarator
22308 name, and if either the type of the declarator contains parameter
22309 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22310 for, eg, abbreviated integral type names), then we actually have a
22311 parameter pack expansion expression. Otherwise, leave the ellipsis
22312 for a C-style variadic function. */
22313 token = cp_lexer_peek_token (parser->lexer);
22314
22315 /* If a function parameter pack was specified and an implicit template
22316 parameter was introduced during cp_parser_parameter_declaration,
22317 change any implicit parameters introduced into packs. */
22318 if (parser->implicit_template_parms
22319 && ((token->type == CPP_ELLIPSIS
22320 && declarator_can_be_parameter_pack (declarator))
22321 || (declarator && declarator->parameter_pack_p)))
22322 {
22323 int latest_template_parm_idx = TREE_VEC_LENGTH
22324 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
22325
22326 if (latest_template_parm_idx != template_parm_idx)
22327 decl_specifiers.type = convert_generic_types_to_packs
22328 (decl_specifiers.type,
22329 template_parm_idx, latest_template_parm_idx);
22330 }
22331
22332 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22333 {
22334 tree type = decl_specifiers.type;
22335
22336 if (type && DECL_P (type))
22337 type = TREE_TYPE (type);
22338
22339 if (((type
22340 && TREE_CODE (type) != TYPE_PACK_EXPANSION
22341 && (template_parm_p || uses_parameter_packs (type)))
22342 || (!type && template_parm_p))
22343 && declarator_can_be_parameter_pack (declarator))
22344 {
22345 /* Consume the `...'. */
22346 cp_lexer_consume_token (parser->lexer);
22347 maybe_warn_variadic_templates ();
22348
22349 /* Build a pack expansion type */
22350 if (template_parm_p)
22351 template_parameter_pack_p = true;
22352 else if (declarator)
22353 declarator->parameter_pack_p = true;
22354 else
22355 decl_specifiers.type = make_pack_expansion (type);
22356 }
22357 }
22358
22359 /* The restriction on defining new types applies only to the type
22360 of the parameter, not to the default argument. */
22361 parser->type_definition_forbidden_message = saved_message;
22362
22363 /* If the next token is `=', then process a default argument. */
22364 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22365 {
22366 tree type = decl_specifiers.type;
22367 token = cp_lexer_peek_token (parser->lexer);
22368 /* If we are defining a class, then the tokens that make up the
22369 default argument must be saved and processed later. */
22370 if (!template_parm_p && at_class_scope_p ()
22371 && TYPE_BEING_DEFINED (current_class_type)
22372 && !LAMBDA_TYPE_P (current_class_type))
22373 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
22374
22375 // A constrained-type-specifier may declare a type template-parameter.
22376 else if (declares_constrained_type_template_parameter (type))
22377 default_argument
22378 = cp_parser_default_type_template_argument (parser);
22379
22380 // A constrained-type-specifier may declare a template-template-parameter.
22381 else if (declares_constrained_template_template_parameter (type))
22382 default_argument
22383 = cp_parser_default_template_template_argument (parser);
22384
22385 /* Outside of a class definition, we can just parse the
22386 assignment-expression. */
22387 else
22388 default_argument
22389 = cp_parser_default_argument (parser, template_parm_p);
22390
22391 if (!parser->default_arg_ok_p)
22392 {
22393 permerror (token->location,
22394 "default arguments are only "
22395 "permitted for function parameters");
22396 }
22397 else if ((declarator && declarator->parameter_pack_p)
22398 || template_parameter_pack_p
22399 || (decl_specifiers.type
22400 && PACK_EXPANSION_P (decl_specifiers.type)))
22401 {
22402 /* Find the name of the parameter pack. */
22403 cp_declarator *id_declarator = declarator;
22404 while (id_declarator && id_declarator->kind != cdk_id)
22405 id_declarator = id_declarator->declarator;
22406
22407 if (id_declarator && id_declarator->kind == cdk_id)
22408 error_at (declarator_token_start->location,
22409 template_parm_p
22410 ? G_("template parameter pack %qD "
22411 "cannot have a default argument")
22412 : G_("parameter pack %qD cannot have "
22413 "a default argument"),
22414 id_declarator->u.id.unqualified_name);
22415 else
22416 error_at (declarator_token_start->location,
22417 template_parm_p
22418 ? G_("template parameter pack cannot have "
22419 "a default argument")
22420 : G_("parameter pack cannot have a "
22421 "default argument"));
22422
22423 default_argument = NULL_TREE;
22424 }
22425 }
22426 else
22427 default_argument = NULL_TREE;
22428
22429 if (default_argument)
22430 STRIP_ANY_LOCATION_WRAPPER (default_argument);
22431
22432 /* Generate a location for the parameter, ranging from the start of the
22433 initial token to the end of the final token (using input_location for
22434 the latter, set up by cp_lexer_set_source_position_from_token when
22435 consuming tokens).
22436
22437 If we have a identifier, then use it for the caret location, e.g.
22438
22439 extern int callee (int one, int (*two)(int, int), float three);
22440 ~~~~~~^~~~~~~~~~~~~~
22441
22442 otherwise, reuse the start location for the caret location e.g.:
22443
22444 extern int callee (int one, int (*)(int, int), float three);
22445 ^~~~~~~~~~~~~~~~~
22446
22447 */
22448 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22449 ? declarator->id_loc
22450 : decl_spec_token_start->location);
22451 location_t param_loc = make_location (caret_loc,
22452 decl_spec_token_start->location,
22453 input_location);
22454
22455 return make_parameter_declarator (&decl_specifiers,
22456 declarator,
22457 default_argument,
22458 param_loc,
22459 template_parameter_pack_p);
22460 }
22461
22462 /* Parse a default argument and return it.
22463
22464 TEMPLATE_PARM_P is true if this is a default argument for a
22465 non-type template parameter. */
22466 static tree
22467 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22468 {
22469 tree default_argument = NULL_TREE;
22470 bool saved_greater_than_is_operator_p;
22471 unsigned char saved_local_variables_forbidden_p;
22472 bool non_constant_p, is_direct_init;
22473
22474 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22475 set correctly. */
22476 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22477 parser->greater_than_is_operator_p = !template_parm_p;
22478 /* Local variable names (and the `this' keyword) may not
22479 appear in a default argument. */
22480 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22481 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
22482 /* Parse the assignment-expression. */
22483 if (template_parm_p)
22484 push_deferring_access_checks (dk_no_deferred);
22485 tree saved_class_ptr = NULL_TREE;
22486 tree saved_class_ref = NULL_TREE;
22487 /* The "this" pointer is not valid in a default argument. */
22488 if (cfun)
22489 {
22490 saved_class_ptr = current_class_ptr;
22491 cp_function_chain->x_current_class_ptr = NULL_TREE;
22492 saved_class_ref = current_class_ref;
22493 cp_function_chain->x_current_class_ref = NULL_TREE;
22494 }
22495 default_argument
22496 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22497 /* Restore the "this" pointer. */
22498 if (cfun)
22499 {
22500 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22501 cp_function_chain->x_current_class_ref = saved_class_ref;
22502 }
22503 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22504 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22505 if (template_parm_p)
22506 pop_deferring_access_checks ();
22507 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22508 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22509
22510 return default_argument;
22511 }
22512
22513 /* Parse a function-body.
22514
22515 function-body:
22516 compound_statement */
22517
22518 static void
22519 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22520 {
22521 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22522 ? BCS_TRY_BLOCK : BCS_NORMAL),
22523 true);
22524 }
22525
22526 /* Parse a ctor-initializer-opt followed by a function-body. Return
22527 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22528 is true we are parsing a function-try-block. */
22529
22530 static void
22531 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22532 bool in_function_try_block)
22533 {
22534 tree body, list;
22535 const bool check_body_p =
22536 DECL_CONSTRUCTOR_P (current_function_decl)
22537 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
22538 tree last = NULL;
22539
22540 /* Begin the function body. */
22541 body = begin_function_body ();
22542 /* Parse the optional ctor-initializer. */
22543 cp_parser_ctor_initializer_opt (parser);
22544
22545 /* If we're parsing a constexpr constructor definition, we need
22546 to check that the constructor body is indeed empty. However,
22547 before we get to cp_parser_function_body lot of junk has been
22548 generated, so we can't just check that we have an empty block.
22549 Rather we take a snapshot of the outermost block, and check whether
22550 cp_parser_function_body changed its state. */
22551 if (check_body_p)
22552 {
22553 list = cur_stmt_list;
22554 if (STATEMENT_LIST_TAIL (list))
22555 last = STATEMENT_LIST_TAIL (list)->stmt;
22556 }
22557 /* Parse the function-body. */
22558 cp_parser_function_body (parser, in_function_try_block);
22559 if (check_body_p)
22560 check_constexpr_ctor_body (last, list, /*complain=*/true);
22561 /* Finish the function body. */
22562 finish_function_body (body);
22563 }
22564
22565 /* Parse an initializer.
22566
22567 initializer:
22568 = initializer-clause
22569 ( expression-list )
22570
22571 Returns an expression representing the initializer. If no
22572 initializer is present, NULL_TREE is returned.
22573
22574 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22575 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22576 set to TRUE if there is no initializer present. If there is an
22577 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22578 is set to true; otherwise it is set to false. */
22579
22580 static tree
22581 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22582 bool* non_constant_p, bool subexpression_p)
22583 {
22584 cp_token *token;
22585 tree init;
22586
22587 /* Peek at the next token. */
22588 token = cp_lexer_peek_token (parser->lexer);
22589
22590 /* Let our caller know whether or not this initializer was
22591 parenthesized. */
22592 *is_direct_init = (token->type != CPP_EQ);
22593 /* Assume that the initializer is constant. */
22594 *non_constant_p = false;
22595
22596 if (token->type == CPP_EQ)
22597 {
22598 /* Consume the `='. */
22599 cp_lexer_consume_token (parser->lexer);
22600 /* Parse the initializer-clause. */
22601 init = cp_parser_initializer_clause (parser, non_constant_p);
22602 }
22603 else if (token->type == CPP_OPEN_PAREN)
22604 {
22605 vec<tree, va_gc> *vec;
22606 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22607 /*cast_p=*/false,
22608 /*allow_expansion_p=*/true,
22609 non_constant_p);
22610 if (vec == NULL)
22611 return error_mark_node;
22612 init = build_tree_list_vec (vec);
22613 release_tree_vector (vec);
22614 }
22615 else if (token->type == CPP_OPEN_BRACE)
22616 {
22617 cp_lexer_set_source_position (parser->lexer);
22618 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22619 init = cp_parser_braced_list (parser, non_constant_p);
22620 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22621 }
22622 else
22623 {
22624 /* Anything else is an error. */
22625 cp_parser_error (parser, "expected initializer");
22626 init = error_mark_node;
22627 }
22628
22629 if (!subexpression_p && check_for_bare_parameter_packs (init))
22630 init = error_mark_node;
22631
22632 return init;
22633 }
22634
22635 /* Parse an initializer-clause.
22636
22637 initializer-clause:
22638 assignment-expression
22639 braced-init-list
22640
22641 Returns an expression representing the initializer.
22642
22643 If the `assignment-expression' production is used the value
22644 returned is simply a representation for the expression.
22645
22646 Otherwise, calls cp_parser_braced_list. */
22647
22648 static cp_expr
22649 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22650 {
22651 cp_expr initializer;
22652
22653 /* Assume the expression is constant. */
22654 *non_constant_p = false;
22655
22656 /* If it is not a `{', then we are looking at an
22657 assignment-expression. */
22658 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22659 {
22660 initializer
22661 = cp_parser_constant_expression (parser,
22662 /*allow_non_constant_p=*/true,
22663 non_constant_p);
22664 }
22665 else
22666 initializer = cp_parser_braced_list (parser, non_constant_p);
22667
22668 return initializer;
22669 }
22670
22671 /* Parse a brace-enclosed initializer list.
22672
22673 braced-init-list:
22674 { initializer-list , [opt] }
22675 { designated-initializer-list , [opt] }
22676 { }
22677
22678 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22679 the elements of the initializer-list (or NULL, if the last
22680 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22681 NULL_TREE. There is no way to detect whether or not the optional
22682 trailing `,' was provided. NON_CONSTANT_P is as for
22683 cp_parser_initializer. */
22684
22685 static cp_expr
22686 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22687 {
22688 tree initializer;
22689 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22690
22691 /* Consume the `{' token. */
22692 matching_braces braces;
22693 braces.require_open (parser);
22694 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22695 initializer = make_node (CONSTRUCTOR);
22696 /* If it's not a `}', then there is a non-trivial initializer. */
22697 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22698 {
22699 /* Parse the initializer list. */
22700 CONSTRUCTOR_ELTS (initializer)
22701 = cp_parser_initializer_list (parser, non_constant_p);
22702 /* A trailing `,' token is allowed. */
22703 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22704 cp_lexer_consume_token (parser->lexer);
22705 }
22706 else
22707 *non_constant_p = false;
22708 /* Now, there should be a trailing `}'. */
22709 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22710 braces.require_close (parser);
22711 TREE_TYPE (initializer) = init_list_type_node;
22712
22713 cp_expr result (initializer);
22714 /* Build a location of the form:
22715 { ... }
22716 ^~~~~~~
22717 with caret==start at the open brace, finish at the close brace. */
22718 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22719 result.set_location (combined_loc);
22720 return result;
22721 }
22722
22723 /* Consume tokens up to, and including, the next non-nested closing `]'.
22724 Returns true iff we found a closing `]'. */
22725
22726 static bool
22727 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22728 {
22729 unsigned square_depth = 0;
22730
22731 while (true)
22732 {
22733 cp_token * token = cp_lexer_peek_token (parser->lexer);
22734
22735 switch (token->type)
22736 {
22737 case CPP_PRAGMA_EOL:
22738 if (!parser->lexer->in_pragma)
22739 break;
22740 /* FALLTHRU */
22741 case CPP_EOF:
22742 /* If we've run out of tokens, then there is no closing `]'. */
22743 return false;
22744
22745 case CPP_OPEN_SQUARE:
22746 ++square_depth;
22747 break;
22748
22749 case CPP_CLOSE_SQUARE:
22750 if (!square_depth--)
22751 {
22752 cp_lexer_consume_token (parser->lexer);
22753 return true;
22754 }
22755 break;
22756
22757 default:
22758 break;
22759 }
22760
22761 /* Consume the token. */
22762 cp_lexer_consume_token (parser->lexer);
22763 }
22764 }
22765
22766 /* Return true if we are looking at an array-designator, false otherwise. */
22767
22768 static bool
22769 cp_parser_array_designator_p (cp_parser *parser)
22770 {
22771 /* Consume the `['. */
22772 cp_lexer_consume_token (parser->lexer);
22773
22774 cp_lexer_save_tokens (parser->lexer);
22775
22776 /* Skip tokens until the next token is a closing square bracket.
22777 If we find the closing `]', and the next token is a `=', then
22778 we are looking at an array designator. */
22779 bool array_designator_p
22780 = (cp_parser_skip_to_closing_square_bracket (parser)
22781 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22782
22783 /* Roll back the tokens we skipped. */
22784 cp_lexer_rollback_tokens (parser->lexer);
22785
22786 return array_designator_p;
22787 }
22788
22789 /* Parse an initializer-list.
22790
22791 initializer-list:
22792 initializer-clause ... [opt]
22793 initializer-list , initializer-clause ... [opt]
22794
22795 C++2A Extension:
22796
22797 designated-initializer-list:
22798 designated-initializer-clause
22799 designated-initializer-list , designated-initializer-clause
22800
22801 designated-initializer-clause:
22802 designator brace-or-equal-initializer
22803
22804 designator:
22805 . identifier
22806
22807 GNU Extension:
22808
22809 initializer-list:
22810 designation initializer-clause ...[opt]
22811 initializer-list , designation initializer-clause ...[opt]
22812
22813 designation:
22814 . identifier =
22815 identifier :
22816 [ constant-expression ] =
22817
22818 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22819 for the initializer. If the INDEX of the elt is non-NULL, it is the
22820 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22821 as for cp_parser_initializer. */
22822
22823 static vec<constructor_elt, va_gc> *
22824 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22825 {
22826 vec<constructor_elt, va_gc> *v = NULL;
22827 bool first_p = true;
22828 tree first_designator = NULL_TREE;
22829
22830 /* Assume all of the expressions are constant. */
22831 *non_constant_p = false;
22832
22833 /* Parse the rest of the list. */
22834 while (true)
22835 {
22836 cp_token *token;
22837 tree designator;
22838 tree initializer;
22839 bool clause_non_constant_p;
22840 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22841
22842 /* Handle the C++2A syntax, '. id ='. */
22843 if ((cxx_dialect >= cxx2a
22844 || cp_parser_allow_gnu_extensions_p (parser))
22845 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22846 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22847 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22848 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22849 == CPP_OPEN_BRACE)))
22850 {
22851 if (cxx_dialect < cxx2a)
22852 pedwarn (loc, OPT_Wpedantic,
22853 "C++ designated initializers only available with "
22854 "-std=c++2a or -std=gnu++2a");
22855 /* Consume the `.'. */
22856 cp_lexer_consume_token (parser->lexer);
22857 /* Consume the identifier. */
22858 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22859 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22860 /* Consume the `='. */
22861 cp_lexer_consume_token (parser->lexer);
22862 }
22863 /* Also, if the next token is an identifier and the following one is a
22864 colon, we are looking at the GNU designated-initializer
22865 syntax. */
22866 else if (cp_parser_allow_gnu_extensions_p (parser)
22867 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22868 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22869 == CPP_COLON))
22870 {
22871 /* Warn the user that they are using an extension. */
22872 pedwarn (loc, OPT_Wpedantic,
22873 "ISO C++ does not allow GNU designated initializers");
22874 /* Consume the identifier. */
22875 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22876 /* Consume the `:'. */
22877 cp_lexer_consume_token (parser->lexer);
22878 }
22879 /* Also handle C99 array designators, '[ const ] ='. */
22880 else if (cp_parser_allow_gnu_extensions_p (parser)
22881 && !c_dialect_objc ()
22882 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22883 {
22884 /* In C++11, [ could start a lambda-introducer. */
22885 bool non_const = false;
22886
22887 cp_parser_parse_tentatively (parser);
22888
22889 if (!cp_parser_array_designator_p (parser))
22890 {
22891 cp_parser_simulate_error (parser);
22892 designator = NULL_TREE;
22893 }
22894 else
22895 {
22896 designator = cp_parser_constant_expression (parser, true,
22897 &non_const);
22898 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22899 cp_parser_require (parser, CPP_EQ, RT_EQ);
22900 }
22901
22902 if (!cp_parser_parse_definitely (parser))
22903 designator = NULL_TREE;
22904 else if (non_const
22905 && (!require_potential_rvalue_constant_expression
22906 (designator)))
22907 designator = NULL_TREE;
22908 if (designator)
22909 /* Warn the user that they are using an extension. */
22910 pedwarn (loc, OPT_Wpedantic,
22911 "ISO C++ does not allow C99 designated initializers");
22912 }
22913 else
22914 designator = NULL_TREE;
22915
22916 if (first_p)
22917 {
22918 first_designator = designator;
22919 first_p = false;
22920 }
22921 else if (cxx_dialect >= cxx2a
22922 && first_designator != error_mark_node
22923 && (!first_designator != !designator))
22924 {
22925 error_at (loc, "either all initializer clauses should be designated "
22926 "or none of them should be");
22927 first_designator = error_mark_node;
22928 }
22929 else if (cxx_dialect < cxx2a && !first_designator)
22930 first_designator = designator;
22931
22932 /* Parse the initializer. */
22933 initializer = cp_parser_initializer_clause (parser,
22934 &clause_non_constant_p);
22935 /* If any clause is non-constant, so is the entire initializer. */
22936 if (clause_non_constant_p)
22937 *non_constant_p = true;
22938
22939 /* If we have an ellipsis, this is an initializer pack
22940 expansion. */
22941 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22942 {
22943 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22944
22945 /* Consume the `...'. */
22946 cp_lexer_consume_token (parser->lexer);
22947
22948 if (designator && cxx_dialect >= cxx2a)
22949 error_at (loc,
22950 "%<...%> not allowed in designated initializer list");
22951
22952 /* Turn the initializer into an initializer expansion. */
22953 initializer = make_pack_expansion (initializer);
22954 }
22955
22956 /* Add it to the vector. */
22957 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22958
22959 /* If the next token is not a comma, we have reached the end of
22960 the list. */
22961 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22962 break;
22963
22964 /* Peek at the next token. */
22965 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22966 /* If the next token is a `}', then we're still done. An
22967 initializer-clause can have a trailing `,' after the
22968 initializer-list and before the closing `}'. */
22969 if (token->type == CPP_CLOSE_BRACE)
22970 break;
22971
22972 /* Consume the `,' token. */
22973 cp_lexer_consume_token (parser->lexer);
22974 }
22975
22976 /* The same identifier shall not appear in multiple designators
22977 of a designated-initializer-list. */
22978 if (first_designator)
22979 {
22980 unsigned int i;
22981 tree designator, val;
22982 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22983 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22984 {
22985 if (IDENTIFIER_MARKED (designator))
22986 {
22987 error_at (cp_expr_loc_or_loc (val, input_location),
22988 "%<.%s%> designator used multiple times in "
22989 "the same initializer list",
22990 IDENTIFIER_POINTER (designator));
22991 (*v)[i].index = error_mark_node;
22992 }
22993 else
22994 IDENTIFIER_MARKED (designator) = 1;
22995 }
22996 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22997 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22998 IDENTIFIER_MARKED (designator) = 0;
22999 }
23000
23001 return v;
23002 }
23003
23004 /* Classes [gram.class] */
23005
23006 /* Parse a class-name.
23007
23008 class-name:
23009 identifier
23010 template-id
23011
23012 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23013 to indicate that names looked up in dependent types should be
23014 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23015 keyword has been used to indicate that the name that appears next
23016 is a template. TAG_TYPE indicates the explicit tag given before
23017 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23018 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23019 is the class being defined in a class-head. If ENUM_OK is TRUE,
23020 enum-names are also accepted.
23021
23022 Returns the TYPE_DECL representing the class. */
23023
23024 static tree
23025 cp_parser_class_name (cp_parser *parser,
23026 bool typename_keyword_p,
23027 bool template_keyword_p,
23028 enum tag_types tag_type,
23029 bool check_dependency_p,
23030 bool class_head_p,
23031 bool is_declaration,
23032 bool enum_ok)
23033 {
23034 tree decl;
23035 tree scope;
23036 bool typename_p;
23037 cp_token *token;
23038 tree identifier = NULL_TREE;
23039
23040 /* All class-names start with an identifier. */
23041 token = cp_lexer_peek_token (parser->lexer);
23042 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
23043 {
23044 cp_parser_error (parser, "expected class-name");
23045 return error_mark_node;
23046 }
23047
23048 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23049 to a template-id, so we save it here. */
23050 scope = parser->scope;
23051 if (scope == error_mark_node)
23052 return error_mark_node;
23053
23054 /* Any name names a type if we're following the `typename' keyword
23055 in a qualified name where the enclosing scope is type-dependent. */
23056 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
23057 && dependent_type_p (scope));
23058 /* Handle the common case (an identifier, but not a template-id)
23059 efficiently. */
23060 if (token->type == CPP_NAME
23061 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
23062 {
23063 cp_token *identifier_token;
23064 bool ambiguous_p;
23065
23066 /* Look for the identifier. */
23067 identifier_token = cp_lexer_peek_token (parser->lexer);
23068 ambiguous_p = identifier_token->error_reported;
23069 identifier = cp_parser_identifier (parser);
23070 /* If the next token isn't an identifier, we are certainly not
23071 looking at a class-name. */
23072 if (identifier == error_mark_node)
23073 decl = error_mark_node;
23074 /* If we know this is a type-name, there's no need to look it
23075 up. */
23076 else if (typename_p)
23077 decl = identifier;
23078 else
23079 {
23080 tree ambiguous_decls;
23081 /* If we already know that this lookup is ambiguous, then
23082 we've already issued an error message; there's no reason
23083 to check again. */
23084 if (ambiguous_p)
23085 {
23086 cp_parser_simulate_error (parser);
23087 return error_mark_node;
23088 }
23089 /* If the next token is a `::', then the name must be a type
23090 name.
23091
23092 [basic.lookup.qual]
23093
23094 During the lookup for a name preceding the :: scope
23095 resolution operator, object, function, and enumerator
23096 names are ignored. */
23097 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23098 tag_type = scope_type;
23099 /* Look up the name. */
23100 decl = cp_parser_lookup_name (parser, identifier,
23101 tag_type,
23102 /*is_template=*/false,
23103 /*is_namespace=*/false,
23104 check_dependency_p,
23105 &ambiguous_decls,
23106 identifier_token->location);
23107 if (ambiguous_decls)
23108 {
23109 if (cp_parser_parsing_tentatively (parser))
23110 cp_parser_simulate_error (parser);
23111 return error_mark_node;
23112 }
23113 }
23114 }
23115 else
23116 {
23117 /* Try a template-id. */
23118 decl = cp_parser_template_id (parser, template_keyword_p,
23119 check_dependency_p,
23120 tag_type,
23121 is_declaration);
23122 if (decl == error_mark_node)
23123 return error_mark_node;
23124 }
23125
23126 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
23127
23128 /* If this is a typename, create a TYPENAME_TYPE. */
23129 if (typename_p && decl != error_mark_node)
23130 {
23131 decl = make_typename_type (scope, decl, typename_type,
23132 /*complain=*/tf_error);
23133 if (decl != error_mark_node)
23134 decl = TYPE_NAME (decl);
23135 }
23136
23137 decl = strip_using_decl (decl);
23138
23139 /* Check to see that it is really the name of a class. */
23140 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
23141 && identifier_p (TREE_OPERAND (decl, 0))
23142 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23143 /* Situations like this:
23144
23145 template <typename T> struct A {
23146 typename T::template X<int>::I i;
23147 };
23148
23149 are problematic. Is `T::template X<int>' a class-name? The
23150 standard does not seem to be definitive, but there is no other
23151 valid interpretation of the following `::'. Therefore, those
23152 names are considered class-names. */
23153 {
23154 decl = make_typename_type (scope, decl, tag_type, tf_error);
23155 if (decl != error_mark_node)
23156 decl = TYPE_NAME (decl);
23157 }
23158 else if (TREE_CODE (decl) != TYPE_DECL
23159 || TREE_TYPE (decl) == error_mark_node
23160 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
23161 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
23162 /* In Objective-C 2.0, a classname followed by '.' starts a
23163 dot-syntax expression, and it's not a type-name. */
23164 || (c_dialect_objc ()
23165 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
23166 && objc_is_class_name (decl)))
23167 decl = error_mark_node;
23168
23169 if (decl == error_mark_node)
23170 cp_parser_error (parser, "expected class-name");
23171 else if (identifier && !parser->scope)
23172 maybe_note_name_used_in_class (identifier, decl);
23173
23174 return decl;
23175 }
23176
23177 /* Parse a class-specifier.
23178
23179 class-specifier:
23180 class-head { member-specification [opt] }
23181
23182 Returns the TREE_TYPE representing the class. */
23183
23184 static tree
23185 cp_parser_class_specifier_1 (cp_parser* parser)
23186 {
23187 tree type;
23188 tree attributes = NULL_TREE;
23189 bool nested_name_specifier_p;
23190 unsigned saved_num_template_parameter_lists;
23191 bool saved_in_function_body;
23192 unsigned char in_statement;
23193 bool in_switch_statement_p;
23194 bool saved_in_unbraced_linkage_specification_p;
23195 tree old_scope = NULL_TREE;
23196 tree scope = NULL_TREE;
23197 cp_token *closing_brace;
23198
23199 push_deferring_access_checks (dk_no_deferred);
23200
23201 /* Parse the class-head. */
23202 type = cp_parser_class_head (parser,
23203 &nested_name_specifier_p);
23204 /* If the class-head was a semantic disaster, skip the entire body
23205 of the class. */
23206 if (!type)
23207 {
23208 cp_parser_skip_to_end_of_block_or_statement (parser);
23209 pop_deferring_access_checks ();
23210 return error_mark_node;
23211 }
23212
23213 /* Look for the `{'. */
23214 matching_braces braces;
23215 if (!braces.require_open (parser))
23216 {
23217 pop_deferring_access_checks ();
23218 return error_mark_node;
23219 }
23220
23221 cp_ensure_no_omp_declare_simd (parser);
23222 cp_ensure_no_oacc_routine (parser);
23223
23224 /* Issue an error message if type-definitions are forbidden here. */
23225 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
23226 /* Remember that we are defining one more class. */
23227 ++parser->num_classes_being_defined;
23228 /* Inside the class, surrounding template-parameter-lists do not
23229 apply. */
23230 saved_num_template_parameter_lists
23231 = parser->num_template_parameter_lists;
23232 parser->num_template_parameter_lists = 0;
23233 /* We are not in a function body. */
23234 saved_in_function_body = parser->in_function_body;
23235 parser->in_function_body = false;
23236 /* Or in a loop. */
23237 in_statement = parser->in_statement;
23238 parser->in_statement = 0;
23239 /* Or in a switch. */
23240 in_switch_statement_p = parser->in_switch_statement_p;
23241 parser->in_switch_statement_p = false;
23242 /* We are not immediately inside an extern "lang" block. */
23243 saved_in_unbraced_linkage_specification_p
23244 = parser->in_unbraced_linkage_specification_p;
23245 parser->in_unbraced_linkage_specification_p = false;
23246
23247 // Associate constraints with the type.
23248 if (flag_concepts)
23249 type = associate_classtype_constraints (type);
23250
23251 /* Start the class. */
23252 if (nested_name_specifier_p)
23253 {
23254 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
23255 old_scope = push_inner_scope (scope);
23256 }
23257 type = begin_class_definition (type);
23258
23259 if (type == error_mark_node)
23260 /* If the type is erroneous, skip the entire body of the class. */
23261 cp_parser_skip_to_closing_brace (parser);
23262 else
23263 /* Parse the member-specification. */
23264 cp_parser_member_specification_opt (parser);
23265
23266 /* Look for the trailing `}'. */
23267 closing_brace = braces.require_close (parser);
23268 /* Look for trailing attributes to apply to this class. */
23269 if (cp_parser_allow_gnu_extensions_p (parser))
23270 attributes = cp_parser_gnu_attributes_opt (parser);
23271 if (type != error_mark_node)
23272 type = finish_struct (type, attributes);
23273 if (nested_name_specifier_p)
23274 pop_inner_scope (old_scope, scope);
23275
23276 /* We've finished a type definition. Check for the common syntax
23277 error of forgetting a semicolon after the definition. We need to
23278 be careful, as we can't just check for not-a-semicolon and be done
23279 with it; the user might have typed:
23280
23281 class X { } c = ...;
23282 class X { } *p = ...;
23283
23284 and so forth. Instead, enumerate all the possible tokens that
23285 might follow this production; if we don't see one of them, then
23286 complain and silently insert the semicolon. */
23287 {
23288 cp_token *token = cp_lexer_peek_token (parser->lexer);
23289 bool want_semicolon = true;
23290
23291 if (cp_next_tokens_can_be_std_attribute_p (parser))
23292 /* Don't try to parse c++11 attributes here. As per the
23293 grammar, that should be a task for
23294 cp_parser_decl_specifier_seq. */
23295 want_semicolon = false;
23296
23297 switch (token->type)
23298 {
23299 case CPP_NAME:
23300 case CPP_SEMICOLON:
23301 case CPP_MULT:
23302 case CPP_AND:
23303 case CPP_OPEN_PAREN:
23304 case CPP_CLOSE_PAREN:
23305 case CPP_COMMA:
23306 want_semicolon = false;
23307 break;
23308
23309 /* While it's legal for type qualifiers and storage class
23310 specifiers to follow type definitions in the grammar, only
23311 compiler testsuites contain code like that. Assume that if
23312 we see such code, then what we're really seeing is a case
23313 like:
23314
23315 class X { }
23316 const <type> var = ...;
23317
23318 or
23319
23320 class Y { }
23321 static <type> func (...) ...
23322
23323 i.e. the qualifier or specifier applies to the next
23324 declaration. To do so, however, we need to look ahead one
23325 more token to see if *that* token is a type specifier.
23326
23327 This code could be improved to handle:
23328
23329 class Z { }
23330 static const <type> var = ...; */
23331 case CPP_KEYWORD:
23332 if (keyword_is_decl_specifier (token->keyword))
23333 {
23334 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
23335
23336 /* Handling user-defined types here would be nice, but very
23337 tricky. */
23338 want_semicolon
23339 = (lookahead->type == CPP_KEYWORD
23340 && keyword_begins_type_specifier (lookahead->keyword));
23341 }
23342 break;
23343 default:
23344 break;
23345 }
23346
23347 /* If we don't have a type, then something is very wrong and we
23348 shouldn't try to do anything clever. Likewise for not seeing the
23349 closing brace. */
23350 if (closing_brace && TYPE_P (type) && want_semicolon)
23351 {
23352 /* Locate the closing brace. */
23353 cp_token_position prev
23354 = cp_lexer_previous_token_position (parser->lexer);
23355 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
23356 location_t loc = prev_token->location;
23357
23358 /* We want to suggest insertion of a ';' immediately *after* the
23359 closing brace, so, if we can, offset the location by 1 column. */
23360 location_t next_loc = loc;
23361 if (!linemap_location_from_macro_expansion_p (line_table, loc))
23362 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
23363
23364 rich_location richloc (line_table, next_loc);
23365
23366 /* If we successfully offset the location, suggest the fix-it. */
23367 if (next_loc != loc)
23368 richloc.add_fixit_insert_before (next_loc, ";");
23369
23370 if (CLASSTYPE_DECLARED_CLASS (type))
23371 error_at (&richloc,
23372 "expected %<;%> after class definition");
23373 else if (TREE_CODE (type) == RECORD_TYPE)
23374 error_at (&richloc,
23375 "expected %<;%> after struct definition");
23376 else if (TREE_CODE (type) == UNION_TYPE)
23377 error_at (&richloc,
23378 "expected %<;%> after union definition");
23379 else
23380 gcc_unreachable ();
23381
23382 /* Unget one token and smash it to look as though we encountered
23383 a semicolon in the input stream. */
23384 cp_lexer_set_token_position (parser->lexer, prev);
23385 token = cp_lexer_peek_token (parser->lexer);
23386 token->type = CPP_SEMICOLON;
23387 token->keyword = RID_MAX;
23388 }
23389 }
23390
23391 /* If this class is not itself within the scope of another class,
23392 then we need to parse the bodies of all of the queued function
23393 definitions. Note that the queued functions defined in a class
23394 are not always processed immediately following the
23395 class-specifier for that class. Consider:
23396
23397 struct A {
23398 struct B { void f() { sizeof (A); } };
23399 };
23400
23401 If `f' were processed before the processing of `A' were
23402 completed, there would be no way to compute the size of `A'.
23403 Note that the nesting we are interested in here is lexical --
23404 not the semantic nesting given by TYPE_CONTEXT. In particular,
23405 for:
23406
23407 struct A { struct B; };
23408 struct A::B { void f() { } };
23409
23410 there is no need to delay the parsing of `A::B::f'. */
23411 if (--parser->num_classes_being_defined == 0)
23412 {
23413 tree decl;
23414 tree class_type = NULL_TREE;
23415 tree pushed_scope = NULL_TREE;
23416 unsigned ix;
23417 cp_default_arg_entry *e;
23418 tree save_ccp, save_ccr;
23419
23420 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
23421 {
23422 /* Skip default arguments, NSDMIs, etc, in order to improve
23423 error recovery (c++/71169, c++/71832). */
23424 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23425 vec_safe_truncate (unparsed_nsdmis, 0);
23426 vec_safe_truncate (unparsed_classes, 0);
23427 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23428 }
23429
23430 /* In a first pass, parse default arguments to the functions.
23431 Then, in a second pass, parse the bodies of the functions.
23432 This two-phased approach handles cases like:
23433
23434 struct S {
23435 void f() { g(); }
23436 void g(int i = 3);
23437 };
23438
23439 */
23440 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
23441 {
23442 decl = e->decl;
23443 /* If there are default arguments that have not yet been processed,
23444 take care of them now. */
23445 if (class_type != e->class_type)
23446 {
23447 if (pushed_scope)
23448 pop_scope (pushed_scope);
23449 class_type = e->class_type;
23450 pushed_scope = push_scope (class_type);
23451 }
23452 /* Make sure that any template parameters are in scope. */
23453 maybe_begin_member_template_processing (decl);
23454 /* Parse the default argument expressions. */
23455 cp_parser_late_parsing_default_args (parser, decl);
23456 /* Remove any template parameters from the symbol table. */
23457 maybe_end_member_template_processing ();
23458 }
23459 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23460 /* Now parse any NSDMIs. */
23461 save_ccp = current_class_ptr;
23462 save_ccr = current_class_ref;
23463 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23464 {
23465 if (class_type != DECL_CONTEXT (decl))
23466 {
23467 if (pushed_scope)
23468 pop_scope (pushed_scope);
23469 class_type = DECL_CONTEXT (decl);
23470 pushed_scope = push_scope (class_type);
23471 }
23472 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23473 cp_parser_late_parsing_nsdmi (parser, decl);
23474 }
23475 vec_safe_truncate (unparsed_nsdmis, 0);
23476 current_class_ptr = save_ccp;
23477 current_class_ref = save_ccr;
23478 if (pushed_scope)
23479 pop_scope (pushed_scope);
23480
23481 /* Now do some post-NSDMI bookkeeping. */
23482 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23483 after_nsdmi_defaulted_late_checks (class_type);
23484 vec_safe_truncate (unparsed_classes, 0);
23485 after_nsdmi_defaulted_late_checks (type);
23486
23487 /* Now parse the body of the functions. */
23488 if (flag_openmp)
23489 {
23490 /* OpenMP UDRs need to be parsed before all other functions. */
23491 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23492 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23493 cp_parser_late_parsing_for_member (parser, decl);
23494 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23495 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23496 cp_parser_late_parsing_for_member (parser, decl);
23497 }
23498 else
23499 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23500 cp_parser_late_parsing_for_member (parser, decl);
23501 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23502 }
23503 else
23504 vec_safe_push (unparsed_classes, type);
23505
23506 /* Put back any saved access checks. */
23507 pop_deferring_access_checks ();
23508
23509 /* Restore saved state. */
23510 parser->in_switch_statement_p = in_switch_statement_p;
23511 parser->in_statement = in_statement;
23512 parser->in_function_body = saved_in_function_body;
23513 parser->num_template_parameter_lists
23514 = saved_num_template_parameter_lists;
23515 parser->in_unbraced_linkage_specification_p
23516 = saved_in_unbraced_linkage_specification_p;
23517
23518 return type;
23519 }
23520
23521 static tree
23522 cp_parser_class_specifier (cp_parser* parser)
23523 {
23524 tree ret;
23525 timevar_push (TV_PARSE_STRUCT);
23526 ret = cp_parser_class_specifier_1 (parser);
23527 timevar_pop (TV_PARSE_STRUCT);
23528 return ret;
23529 }
23530
23531 /* Parse a class-head.
23532
23533 class-head:
23534 class-key identifier [opt] base-clause [opt]
23535 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23536 class-key nested-name-specifier [opt] template-id
23537 base-clause [opt]
23538
23539 class-virt-specifier:
23540 final
23541
23542 GNU Extensions:
23543 class-key attributes identifier [opt] base-clause [opt]
23544 class-key attributes nested-name-specifier identifier base-clause [opt]
23545 class-key attributes nested-name-specifier [opt] template-id
23546 base-clause [opt]
23547
23548 Upon return BASES is initialized to the list of base classes (or
23549 NULL, if there are none) in the same form returned by
23550 cp_parser_base_clause.
23551
23552 Returns the TYPE of the indicated class. Sets
23553 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23554 involving a nested-name-specifier was used, and FALSE otherwise.
23555
23556 Returns error_mark_node if this is not a class-head.
23557
23558 Returns NULL_TREE if the class-head is syntactically valid, but
23559 semantically invalid in a way that means we should skip the entire
23560 body of the class. */
23561
23562 static tree
23563 cp_parser_class_head (cp_parser* parser,
23564 bool* nested_name_specifier_p)
23565 {
23566 tree nested_name_specifier;
23567 enum tag_types class_key;
23568 tree id = NULL_TREE;
23569 tree type = NULL_TREE;
23570 tree attributes;
23571 tree bases;
23572 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23573 bool template_id_p = false;
23574 bool qualified_p = false;
23575 bool invalid_nested_name_p = false;
23576 bool invalid_explicit_specialization_p = false;
23577 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23578 tree pushed_scope = NULL_TREE;
23579 unsigned num_templates;
23580 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23581 /* Assume no nested-name-specifier will be present. */
23582 *nested_name_specifier_p = false;
23583 /* Assume no template parameter lists will be used in defining the
23584 type. */
23585 num_templates = 0;
23586 parser->colon_corrects_to_scope_p = false;
23587
23588 /* Look for the class-key. */
23589 class_key = cp_parser_class_key (parser);
23590 if (class_key == none_type)
23591 return error_mark_node;
23592
23593 location_t class_head_start_location = input_location;
23594
23595 /* Parse the attributes. */
23596 attributes = cp_parser_attributes_opt (parser);
23597
23598 /* If the next token is `::', that is invalid -- but sometimes
23599 people do try to write:
23600
23601 struct ::S {};
23602
23603 Handle this gracefully by accepting the extra qualifier, and then
23604 issuing an error about it later if this really is a
23605 class-head. If it turns out just to be an elaborated type
23606 specifier, remain silent. */
23607 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23608 qualified_p = true;
23609
23610 push_deferring_access_checks (dk_no_check);
23611
23612 /* Determine the name of the class. Begin by looking for an
23613 optional nested-name-specifier. */
23614 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23615 nested_name_specifier
23616 = cp_parser_nested_name_specifier_opt (parser,
23617 /*typename_keyword_p=*/false,
23618 /*check_dependency_p=*/false,
23619 /*type_p=*/true,
23620 /*is_declaration=*/false);
23621 /* If there was a nested-name-specifier, then there *must* be an
23622 identifier. */
23623
23624 cp_token *bad_template_keyword = NULL;
23625
23626 if (nested_name_specifier)
23627 {
23628 type_start_token = cp_lexer_peek_token (parser->lexer);
23629 /* Although the grammar says `identifier', it really means
23630 `class-name' or `template-name'. You are only allowed to
23631 define a class that has already been declared with this
23632 syntax.
23633
23634 The proposed resolution for Core Issue 180 says that wherever
23635 you see `class T::X' you should treat `X' as a type-name.
23636
23637 It is OK to define an inaccessible class; for example:
23638
23639 class A { class B; };
23640 class A::B {};
23641
23642 We do not know if we will see a class-name, or a
23643 template-name. We look for a class-name first, in case the
23644 class-name is a template-id; if we looked for the
23645 template-name first we would stop after the template-name. */
23646 cp_parser_parse_tentatively (parser);
23647 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23648 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23649 type = cp_parser_class_name (parser,
23650 /*typename_keyword_p=*/false,
23651 /*template_keyword_p=*/false,
23652 class_type,
23653 /*check_dependency_p=*/false,
23654 /*class_head_p=*/true,
23655 /*is_declaration=*/false);
23656 /* If that didn't work, ignore the nested-name-specifier. */
23657 if (!cp_parser_parse_definitely (parser))
23658 {
23659 invalid_nested_name_p = true;
23660 type_start_token = cp_lexer_peek_token (parser->lexer);
23661 id = cp_parser_identifier (parser);
23662 if (id == error_mark_node)
23663 id = NULL_TREE;
23664 }
23665 /* If we could not find a corresponding TYPE, treat this
23666 declaration like an unqualified declaration. */
23667 if (type == error_mark_node)
23668 nested_name_specifier = NULL_TREE;
23669 /* Otherwise, count the number of templates used in TYPE and its
23670 containing scopes. */
23671 else
23672 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23673 }
23674 /* Otherwise, the identifier is optional. */
23675 else
23676 {
23677 /* We don't know whether what comes next is a template-id,
23678 an identifier, or nothing at all. */
23679 cp_parser_parse_tentatively (parser);
23680 /* Check for a template-id. */
23681 type_start_token = cp_lexer_peek_token (parser->lexer);
23682 id = cp_parser_template_id (parser,
23683 /*template_keyword_p=*/false,
23684 /*check_dependency_p=*/true,
23685 class_key,
23686 /*is_declaration=*/true);
23687 /* If that didn't work, it could still be an identifier. */
23688 if (!cp_parser_parse_definitely (parser))
23689 {
23690 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23691 {
23692 type_start_token = cp_lexer_peek_token (parser->lexer);
23693 id = cp_parser_identifier (parser);
23694 }
23695 else
23696 id = NULL_TREE;
23697 }
23698 else
23699 {
23700 template_id_p = true;
23701 ++num_templates;
23702 }
23703 }
23704
23705 pop_deferring_access_checks ();
23706
23707 if (id)
23708 {
23709 cp_parser_check_for_invalid_template_id (parser, id,
23710 class_key,
23711 type_start_token->location);
23712 }
23713 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23714
23715 /* If it's not a `:' or a `{' then we can't really be looking at a
23716 class-head, since a class-head only appears as part of a
23717 class-specifier. We have to detect this situation before calling
23718 xref_tag, since that has irreversible side-effects. */
23719 if (!cp_parser_next_token_starts_class_definition_p (parser))
23720 {
23721 cp_parser_error (parser, "expected %<{%> or %<:%>");
23722 type = error_mark_node;
23723 goto out;
23724 }
23725
23726 /* At this point, we're going ahead with the class-specifier, even
23727 if some other problem occurs. */
23728 cp_parser_commit_to_tentative_parse (parser);
23729 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23730 {
23731 cp_parser_error (parser,
23732 "cannot specify %<override%> for a class");
23733 type = error_mark_node;
23734 goto out;
23735 }
23736 /* Issue the error about the overly-qualified name now. */
23737 if (qualified_p)
23738 {
23739 cp_parser_error (parser,
23740 "global qualification of class name is invalid");
23741 type = error_mark_node;
23742 goto out;
23743 }
23744 else if (invalid_nested_name_p)
23745 {
23746 cp_parser_error (parser,
23747 "qualified name does not name a class");
23748 type = error_mark_node;
23749 goto out;
23750 }
23751 else if (nested_name_specifier)
23752 {
23753 tree scope;
23754
23755 if (bad_template_keyword)
23756 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23757 keyword template shall not appear at the top level. */
23758 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23759 "keyword %<template%> not allowed in class-head-name");
23760
23761 /* Reject typedef-names in class heads. */
23762 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23763 {
23764 error_at (type_start_token->location,
23765 "invalid class name in declaration of %qD",
23766 type);
23767 type = NULL_TREE;
23768 goto done;
23769 }
23770
23771 /* Figure out in what scope the declaration is being placed. */
23772 scope = current_scope ();
23773 /* If that scope does not contain the scope in which the
23774 class was originally declared, the program is invalid. */
23775 if (scope && !is_ancestor (scope, nested_name_specifier))
23776 {
23777 if (at_namespace_scope_p ())
23778 error_at (type_start_token->location,
23779 "declaration of %qD in namespace %qD which does not "
23780 "enclose %qD",
23781 type, scope, nested_name_specifier);
23782 else
23783 error_at (type_start_token->location,
23784 "declaration of %qD in %qD which does not enclose %qD",
23785 type, scope, nested_name_specifier);
23786 type = NULL_TREE;
23787 goto done;
23788 }
23789 /* [dcl.meaning]
23790
23791 A declarator-id shall not be qualified except for the
23792 definition of a ... nested class outside of its class
23793 ... [or] the definition or explicit instantiation of a
23794 class member of a namespace outside of its namespace. */
23795 if (scope == nested_name_specifier)
23796 {
23797 permerror (nested_name_specifier_token_start->location,
23798 "extra qualification not allowed");
23799 nested_name_specifier = NULL_TREE;
23800 num_templates = 0;
23801 }
23802 }
23803 /* An explicit-specialization must be preceded by "template <>". If
23804 it is not, try to recover gracefully. */
23805 if (at_namespace_scope_p ()
23806 && parser->num_template_parameter_lists == 0
23807 && !processing_template_parmlist
23808 && template_id_p)
23809 {
23810 /* Build a location of this form:
23811 struct typename <ARGS>
23812 ^~~~~~~~~~~~~~~~~~~~~~
23813 with caret==start at the start token, and
23814 finishing at the end of the type. */
23815 location_t reported_loc
23816 = make_location (class_head_start_location,
23817 class_head_start_location,
23818 get_finish (type_start_token->location));
23819 rich_location richloc (line_table, reported_loc);
23820 richloc.add_fixit_insert_before (class_head_start_location,
23821 "template <> ");
23822 error_at (&richloc,
23823 "an explicit specialization must be preceded by"
23824 " %<template <>%>");
23825 invalid_explicit_specialization_p = true;
23826 /* Take the same action that would have been taken by
23827 cp_parser_explicit_specialization. */
23828 ++parser->num_template_parameter_lists;
23829 begin_specialization ();
23830 }
23831 /* There must be no "return" statements between this point and the
23832 end of this function; set "type "to the correct return value and
23833 use "goto done;" to return. */
23834 /* Make sure that the right number of template parameters were
23835 present. */
23836 if (!cp_parser_check_template_parameters (parser, num_templates,
23837 template_id_p,
23838 type_start_token->location,
23839 /*declarator=*/NULL))
23840 {
23841 /* If something went wrong, there is no point in even trying to
23842 process the class-definition. */
23843 type = NULL_TREE;
23844 goto done;
23845 }
23846
23847 /* Look up the type. */
23848 if (template_id_p)
23849 {
23850 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23851 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23852 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23853 {
23854 error_at (type_start_token->location,
23855 "function template %qD redeclared as a class template", id);
23856 type = error_mark_node;
23857 }
23858 else
23859 {
23860 type = TREE_TYPE (id);
23861 type = maybe_process_partial_specialization (type);
23862
23863 /* Check the scope while we still know whether or not we had a
23864 nested-name-specifier. */
23865 if (type != error_mark_node)
23866 check_unqualified_spec_or_inst (type, type_start_token->location);
23867 }
23868 if (nested_name_specifier)
23869 pushed_scope = push_scope (nested_name_specifier);
23870 }
23871 else if (nested_name_specifier)
23872 {
23873 tree class_type;
23874
23875 /* Given:
23876
23877 template <typename T> struct S { struct T };
23878 template <typename T> struct S<T>::T { };
23879
23880 we will get a TYPENAME_TYPE when processing the definition of
23881 `S::T'. We need to resolve it to the actual type before we
23882 try to define it. */
23883 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23884 {
23885 class_type = resolve_typename_type (TREE_TYPE (type),
23886 /*only_current_p=*/false);
23887 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23888 type = TYPE_NAME (class_type);
23889 else
23890 {
23891 cp_parser_error (parser, "could not resolve typename type");
23892 type = error_mark_node;
23893 }
23894 }
23895
23896 if (maybe_process_partial_specialization (TREE_TYPE (type))
23897 == error_mark_node)
23898 {
23899 type = NULL_TREE;
23900 goto done;
23901 }
23902
23903 class_type = current_class_type;
23904 /* Enter the scope indicated by the nested-name-specifier. */
23905 pushed_scope = push_scope (nested_name_specifier);
23906 /* Get the canonical version of this type. */
23907 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23908 /* Call push_template_decl if it seems like we should be defining a
23909 template either from the template headers or the type we're
23910 defining, so that we diagnose both extra and missing headers. */
23911 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23912 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23913 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23914 {
23915 type = push_template_decl (type);
23916 if (type == error_mark_node)
23917 {
23918 type = NULL_TREE;
23919 goto done;
23920 }
23921 }
23922
23923 type = TREE_TYPE (type);
23924 *nested_name_specifier_p = true;
23925 }
23926 else /* The name is not a nested name. */
23927 {
23928 /* If the class was unnamed, create a dummy name. */
23929 if (!id)
23930 id = make_anon_name ();
23931 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23932 ? ts_within_enclosing_non_class
23933 : ts_current);
23934 type = xref_tag (class_key, id, tag_scope,
23935 parser->num_template_parameter_lists);
23936 }
23937
23938 /* Indicate whether this class was declared as a `class' or as a
23939 `struct'. */
23940 if (TREE_CODE (type) == RECORD_TYPE)
23941 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23942 cp_parser_check_class_key (class_key, type);
23943
23944 /* If this type was already complete, and we see another definition,
23945 that's an error. */
23946 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23947 {
23948 error_at (type_start_token->location, "redefinition of %q#T",
23949 type);
23950 inform (location_of (type), "previous definition of %q#T",
23951 type);
23952 type = NULL_TREE;
23953 goto done;
23954 }
23955 else if (type == error_mark_node)
23956 type = NULL_TREE;
23957
23958 if (type)
23959 {
23960 /* Apply attributes now, before any use of the class as a template
23961 argument in its base list. */
23962 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23963 fixup_attribute_variants (type);
23964 }
23965
23966 /* We will have entered the scope containing the class; the names of
23967 base classes should be looked up in that context. For example:
23968
23969 struct A { struct B {}; struct C; };
23970 struct A::C : B {};
23971
23972 is valid. */
23973
23974 /* Get the list of base-classes, if there is one. */
23975 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23976 {
23977 /* PR59482: enter the class scope so that base-specifiers are looked
23978 up correctly. */
23979 if (type)
23980 pushclass (type);
23981 bases = cp_parser_base_clause (parser);
23982 /* PR59482: get out of the previously pushed class scope so that the
23983 subsequent pops pop the right thing. */
23984 if (type)
23985 popclass ();
23986 }
23987 else
23988 bases = NULL_TREE;
23989
23990 /* If we're really defining a class, process the base classes.
23991 If they're invalid, fail. */
23992 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23993 xref_basetypes (type, bases);
23994
23995 done:
23996 /* Leave the scope given by the nested-name-specifier. We will
23997 enter the class scope itself while processing the members. */
23998 if (pushed_scope)
23999 pop_scope (pushed_scope);
24000
24001 if (invalid_explicit_specialization_p)
24002 {
24003 end_specialization ();
24004 --parser->num_template_parameter_lists;
24005 }
24006
24007 if (type)
24008 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
24009 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
24010 CLASSTYPE_FINAL (type) = 1;
24011 out:
24012 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24013 return type;
24014 }
24015
24016 /* Parse a class-key.
24017
24018 class-key:
24019 class
24020 struct
24021 union
24022
24023 Returns the kind of class-key specified, or none_type to indicate
24024 error. */
24025
24026 static enum tag_types
24027 cp_parser_class_key (cp_parser* parser)
24028 {
24029 cp_token *token;
24030 enum tag_types tag_type;
24031
24032 /* Look for the class-key. */
24033 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
24034 if (!token)
24035 return none_type;
24036
24037 /* Check to see if the TOKEN is a class-key. */
24038 tag_type = cp_parser_token_is_class_key (token);
24039 if (!tag_type)
24040 cp_parser_error (parser, "expected class-key");
24041 return tag_type;
24042 }
24043
24044 /* Parse a type-parameter-key.
24045
24046 type-parameter-key:
24047 class
24048 typename
24049 */
24050
24051 static void
24052 cp_parser_type_parameter_key (cp_parser* parser)
24053 {
24054 /* Look for the type-parameter-key. */
24055 enum tag_types tag_type = none_type;
24056 cp_token *token = cp_lexer_peek_token (parser->lexer);
24057 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
24058 {
24059 cp_lexer_consume_token (parser->lexer);
24060 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
24061 /* typename is not allowed in a template template parameter
24062 by the standard until C++17. */
24063 pedwarn (token->location, OPT_Wpedantic,
24064 "ISO C++ forbids typename key in template template parameter;"
24065 " use -std=c++17 or -std=gnu++17");
24066 }
24067 else
24068 cp_parser_error (parser, "expected %<class%> or %<typename%>");
24069
24070 return;
24071 }
24072
24073 /* Parse an (optional) member-specification.
24074
24075 member-specification:
24076 member-declaration member-specification [opt]
24077 access-specifier : member-specification [opt] */
24078
24079 static void
24080 cp_parser_member_specification_opt (cp_parser* parser)
24081 {
24082 while (true)
24083 {
24084 cp_token *token;
24085 enum rid keyword;
24086
24087 /* Peek at the next token. */
24088 token = cp_lexer_peek_token (parser->lexer);
24089 /* If it's a `}', or EOF then we've seen all the members. */
24090 if (token->type == CPP_CLOSE_BRACE
24091 || token->type == CPP_EOF
24092 || token->type == CPP_PRAGMA_EOL)
24093 break;
24094
24095 /* See if this token is a keyword. */
24096 keyword = token->keyword;
24097 switch (keyword)
24098 {
24099 case RID_PUBLIC:
24100 case RID_PROTECTED:
24101 case RID_PRIVATE:
24102 /* Consume the access-specifier. */
24103 cp_lexer_consume_token (parser->lexer);
24104 /* Remember which access-specifier is active. */
24105 current_access_specifier = token->u.value;
24106 /* Look for the `:'. */
24107 cp_parser_require (parser, CPP_COLON, RT_COLON);
24108 break;
24109
24110 default:
24111 /* Accept #pragmas at class scope. */
24112 if (token->type == CPP_PRAGMA)
24113 {
24114 cp_parser_pragma (parser, pragma_member, NULL);
24115 break;
24116 }
24117
24118 /* Otherwise, the next construction must be a
24119 member-declaration. */
24120 cp_parser_member_declaration (parser);
24121 }
24122 }
24123 }
24124
24125 /* Parse a member-declaration.
24126
24127 member-declaration:
24128 decl-specifier-seq [opt] member-declarator-list [opt] ;
24129 function-definition ; [opt]
24130 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24131 using-declaration
24132 template-declaration
24133 alias-declaration
24134
24135 member-declarator-list:
24136 member-declarator
24137 member-declarator-list , member-declarator
24138
24139 member-declarator:
24140 declarator pure-specifier [opt]
24141 declarator constant-initializer [opt]
24142 identifier [opt] : constant-expression
24143
24144 GNU Extensions:
24145
24146 member-declaration:
24147 __extension__ member-declaration
24148
24149 member-declarator:
24150 declarator attributes [opt] pure-specifier [opt]
24151 declarator attributes [opt] constant-initializer [opt]
24152 identifier [opt] attributes [opt] : constant-expression
24153
24154 C++0x Extensions:
24155
24156 member-declaration:
24157 static_assert-declaration */
24158
24159 static void
24160 cp_parser_member_declaration (cp_parser* parser)
24161 {
24162 cp_decl_specifier_seq decl_specifiers;
24163 tree prefix_attributes;
24164 tree decl;
24165 int declares_class_or_enum;
24166 bool friend_p;
24167 cp_token *token = NULL;
24168 cp_token *decl_spec_token_start = NULL;
24169 cp_token *initializer_token_start = NULL;
24170 int saved_pedantic;
24171 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24172
24173 /* Check for the `__extension__' keyword. */
24174 if (cp_parser_extension_opt (parser, &saved_pedantic))
24175 {
24176 /* Recurse. */
24177 cp_parser_member_declaration (parser);
24178 /* Restore the old value of the PEDANTIC flag. */
24179 pedantic = saved_pedantic;
24180
24181 return;
24182 }
24183
24184 /* Check for a template-declaration. */
24185 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24186 {
24187 /* An explicit specialization here is an error condition, and we
24188 expect the specialization handler to detect and report this. */
24189 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
24190 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
24191 cp_parser_explicit_specialization (parser);
24192 else
24193 cp_parser_template_declaration (parser, /*member_p=*/true);
24194
24195 return;
24196 }
24197 /* Check for a template introduction. */
24198 else if (cp_parser_template_declaration_after_export (parser, true))
24199 return;
24200
24201 /* Check for a using-declaration. */
24202 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24203 {
24204 if (cxx_dialect < cxx11)
24205 {
24206 /* Parse the using-declaration. */
24207 cp_parser_using_declaration (parser,
24208 /*access_declaration_p=*/false);
24209 return;
24210 }
24211 else
24212 {
24213 tree decl;
24214 bool alias_decl_expected;
24215 cp_parser_parse_tentatively (parser);
24216 decl = cp_parser_alias_declaration (parser);
24217 /* Note that if we actually see the '=' token after the
24218 identifier, cp_parser_alias_declaration commits the
24219 tentative parse. In that case, we really expect an
24220 alias-declaration. Otherwise, we expect a using
24221 declaration. */
24222 alias_decl_expected =
24223 !cp_parser_uncommitted_to_tentative_parse_p (parser);
24224 cp_parser_parse_definitely (parser);
24225
24226 if (alias_decl_expected)
24227 finish_member_declaration (decl);
24228 else
24229 cp_parser_using_declaration (parser,
24230 /*access_declaration_p=*/false);
24231 return;
24232 }
24233 }
24234
24235 /* Check for @defs. */
24236 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
24237 {
24238 tree ivar, member;
24239 tree ivar_chains = cp_parser_objc_defs_expression (parser);
24240 ivar = ivar_chains;
24241 while (ivar)
24242 {
24243 member = ivar;
24244 ivar = TREE_CHAIN (member);
24245 TREE_CHAIN (member) = NULL_TREE;
24246 finish_member_declaration (member);
24247 }
24248 return;
24249 }
24250
24251 /* If the next token is `static_assert' we have a static assertion. */
24252 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
24253 {
24254 cp_parser_static_assert (parser, /*member_p=*/true);
24255 return;
24256 }
24257
24258 parser->colon_corrects_to_scope_p = false;
24259
24260 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
24261 goto out;
24262
24263 /* Parse the decl-specifier-seq. */
24264 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24265 cp_parser_decl_specifier_seq (parser,
24266 (CP_PARSER_FLAGS_OPTIONAL
24267 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
24268 &decl_specifiers,
24269 &declares_class_or_enum);
24270 /* Check for an invalid type-name. */
24271 if (!decl_specifiers.any_type_specifiers_p
24272 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24273 goto out;
24274 /* If there is no declarator, then the decl-specifier-seq should
24275 specify a type. */
24276 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24277 {
24278 /* If there was no decl-specifier-seq, and the next token is a
24279 `;', then we have something like:
24280
24281 struct S { ; };
24282
24283 [class.mem]
24284
24285 Each member-declaration shall declare at least one member
24286 name of the class. */
24287 if (!decl_specifiers.any_specifiers_p)
24288 {
24289 cp_token *token = cp_lexer_peek_token (parser->lexer);
24290 if (!in_system_header_at (token->location))
24291 {
24292 gcc_rich_location richloc (token->location);
24293 richloc.add_fixit_remove ();
24294 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
24295 }
24296 }
24297 else
24298 {
24299 tree type;
24300
24301 /* See if this declaration is a friend. */
24302 friend_p = cp_parser_friend_p (&decl_specifiers);
24303 /* If there were decl-specifiers, check to see if there was
24304 a class-declaration. */
24305 type = check_tag_decl (&decl_specifiers,
24306 /*explicit_type_instantiation_p=*/false);
24307 /* Nested classes have already been added to the class, but
24308 a `friend' needs to be explicitly registered. */
24309 if (friend_p)
24310 {
24311 /* If the `friend' keyword was present, the friend must
24312 be introduced with a class-key. */
24313 if (!declares_class_or_enum && cxx_dialect < cxx11)
24314 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
24315 "in C++03 a class-key must be used "
24316 "when declaring a friend");
24317 /* In this case:
24318
24319 template <typename T> struct A {
24320 friend struct A<T>::B;
24321 };
24322
24323 A<T>::B will be represented by a TYPENAME_TYPE, and
24324 therefore not recognized by check_tag_decl. */
24325 if (!type)
24326 {
24327 type = decl_specifiers.type;
24328 if (type && TREE_CODE (type) == TYPE_DECL)
24329 type = TREE_TYPE (type);
24330 }
24331 if (!type || !TYPE_P (type))
24332 error_at (decl_spec_token_start->location,
24333 "friend declaration does not name a class or "
24334 "function");
24335 else
24336 make_friend_class (current_class_type, type,
24337 /*complain=*/true);
24338 }
24339 /* If there is no TYPE, an error message will already have
24340 been issued. */
24341 else if (!type || type == error_mark_node)
24342 ;
24343 /* An anonymous aggregate has to be handled specially; such
24344 a declaration really declares a data member (with a
24345 particular type), as opposed to a nested class. */
24346 else if (ANON_AGGR_TYPE_P (type))
24347 {
24348 /* C++11 9.5/6. */
24349 if (decl_specifiers.storage_class != sc_none)
24350 error_at (decl_spec_token_start->location,
24351 "a storage class on an anonymous aggregate "
24352 "in class scope is not allowed");
24353
24354 /* Remove constructors and such from TYPE, now that we
24355 know it is an anonymous aggregate. */
24356 fixup_anonymous_aggr (type);
24357 /* And make the corresponding data member. */
24358 decl = build_decl (decl_spec_token_start->location,
24359 FIELD_DECL, NULL_TREE, type);
24360 /* Add it to the class. */
24361 finish_member_declaration (decl);
24362 }
24363 else
24364 cp_parser_check_access_in_redeclaration
24365 (TYPE_NAME (type),
24366 decl_spec_token_start->location);
24367 }
24368 }
24369 else
24370 {
24371 bool assume_semicolon = false;
24372
24373 /* Clear attributes from the decl_specifiers but keep them
24374 around as prefix attributes that apply them to the entity
24375 being declared. */
24376 prefix_attributes = decl_specifiers.attributes;
24377 decl_specifiers.attributes = NULL_TREE;
24378
24379 /* See if these declarations will be friends. */
24380 friend_p = cp_parser_friend_p (&decl_specifiers);
24381
24382 /* Keep going until we hit the `;' at the end of the
24383 declaration. */
24384 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24385 {
24386 tree attributes = NULL_TREE;
24387 tree first_attribute;
24388 tree initializer;
24389 bool named_bitfld = false;
24390
24391 /* Peek at the next token. */
24392 token = cp_lexer_peek_token (parser->lexer);
24393
24394 /* The following code wants to know early if it is a bit-field
24395 or some other declaration. Attributes can appear before
24396 the `:' token. Skip over them without consuming any tokens
24397 to peek if they are followed by `:'. */
24398 if (cp_next_tokens_can_be_attribute_p (parser)
24399 || (token->type == CPP_NAME
24400 && cp_nth_tokens_can_be_attribute_p (parser, 2)
24401 && (named_bitfld = true)))
24402 {
24403 size_t n
24404 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
24405 token = cp_lexer_peek_nth_token (parser->lexer, n);
24406 }
24407
24408 /* Check for a bitfield declaration. */
24409 if (token->type == CPP_COLON
24410 || (token->type == CPP_NAME
24411 && token == cp_lexer_peek_token (parser->lexer)
24412 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
24413 && (named_bitfld = true)))
24414 {
24415 tree identifier;
24416 tree width;
24417 tree late_attributes = NULL_TREE;
24418 location_t id_location
24419 = cp_lexer_peek_token (parser->lexer)->location;
24420
24421 if (named_bitfld)
24422 identifier = cp_parser_identifier (parser);
24423 else
24424 identifier = NULL_TREE;
24425
24426 /* Look for attributes that apply to the bitfield. */
24427 attributes = cp_parser_attributes_opt (parser);
24428
24429 /* Consume the `:' token. */
24430 cp_lexer_consume_token (parser->lexer);
24431
24432 /* Get the width of the bitfield. */
24433 width = cp_parser_constant_expression (parser, false, NULL,
24434 cxx_dialect >= cxx11);
24435
24436 /* In C++2A and as extension for C++11 and above we allow
24437 default member initializers for bit-fields. */
24438 initializer = NULL_TREE;
24439 if (cxx_dialect >= cxx11
24440 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
24441 || cp_lexer_next_token_is (parser->lexer,
24442 CPP_OPEN_BRACE)))
24443 {
24444 location_t loc
24445 = cp_lexer_peek_token (parser->lexer)->location;
24446 if (cxx_dialect < cxx2a
24447 && !in_system_header_at (loc)
24448 && identifier != NULL_TREE)
24449 pedwarn (loc, 0,
24450 "default member initializers for bit-fields "
24451 "only available with -std=c++2a or "
24452 "-std=gnu++2a");
24453
24454 initializer = cp_parser_save_nsdmi (parser);
24455 if (identifier == NULL_TREE)
24456 {
24457 error_at (loc, "default member initializer for "
24458 "unnamed bit-field");
24459 initializer = NULL_TREE;
24460 }
24461 }
24462 else
24463 {
24464 /* Look for attributes that apply to the bitfield after
24465 the `:' token and width. This is where GCC used to
24466 parse attributes in the past, pedwarn if there is
24467 a std attribute. */
24468 if (cp_next_tokens_can_be_std_attribute_p (parser))
24469 pedwarn (input_location, OPT_Wpedantic,
24470 "ISO C++ allows bit-field attributes only "
24471 "before the %<:%> token");
24472
24473 late_attributes = cp_parser_attributes_opt (parser);
24474 }
24475
24476 attributes = attr_chainon (attributes, late_attributes);
24477
24478 /* Remember which attributes are prefix attributes and
24479 which are not. */
24480 first_attribute = attributes;
24481 /* Combine the attributes. */
24482 attributes = attr_chainon (prefix_attributes, attributes);
24483
24484 /* Create the bitfield declaration. */
24485 decl = grokbitfield (identifier
24486 ? make_id_declarator (NULL_TREE,
24487 identifier,
24488 sfk_none,
24489 id_location)
24490 : NULL,
24491 &decl_specifiers,
24492 width, initializer,
24493 attributes);
24494 }
24495 else
24496 {
24497 cp_declarator *declarator;
24498 tree asm_specification;
24499 int ctor_dtor_or_conv_p;
24500 bool static_p = (decl_specifiers.storage_class == sc_static);
24501
24502 /* Parse the declarator. */
24503 declarator
24504 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24505 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24506 &ctor_dtor_or_conv_p,
24507 /*parenthesized_p=*/NULL,
24508 /*member_p=*/true,
24509 friend_p, static_p);
24510
24511 /* If something went wrong parsing the declarator, make sure
24512 that we at least consume some tokens. */
24513 if (declarator == cp_error_declarator)
24514 {
24515 /* Skip to the end of the statement. */
24516 cp_parser_skip_to_end_of_statement (parser);
24517 /* If the next token is not a semicolon, that is
24518 probably because we just skipped over the body of
24519 a function. So, we consume a semicolon if
24520 present, but do not issue an error message if it
24521 is not present. */
24522 if (cp_lexer_next_token_is (parser->lexer,
24523 CPP_SEMICOLON))
24524 cp_lexer_consume_token (parser->lexer);
24525 goto out;
24526 }
24527
24528 if (declares_class_or_enum & 2)
24529 cp_parser_check_for_definition_in_return_type
24530 (declarator, decl_specifiers.type,
24531 decl_specifiers.locations[ds_type_spec]);
24532
24533 /* Look for an asm-specification. */
24534 asm_specification = cp_parser_asm_specification_opt (parser);
24535 /* Look for attributes that apply to the declaration. */
24536 attributes = cp_parser_attributes_opt (parser);
24537 /* Remember which attributes are prefix attributes and
24538 which are not. */
24539 first_attribute = attributes;
24540 /* Combine the attributes. */
24541 attributes = attr_chainon (prefix_attributes, attributes);
24542
24543 /* If it's an `=', then we have a constant-initializer or a
24544 pure-specifier. It is not correct to parse the
24545 initializer before registering the member declaration
24546 since the member declaration should be in scope while
24547 its initializer is processed. However, the rest of the
24548 front end does not yet provide an interface that allows
24549 us to handle this correctly. */
24550 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24551 {
24552 /* In [class.mem]:
24553
24554 A pure-specifier shall be used only in the declaration of
24555 a virtual function.
24556
24557 A member-declarator can contain a constant-initializer
24558 only if it declares a static member of integral or
24559 enumeration type.
24560
24561 Therefore, if the DECLARATOR is for a function, we look
24562 for a pure-specifier; otherwise, we look for a
24563 constant-initializer. When we call `grokfield', it will
24564 perform more stringent semantics checks. */
24565 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24566 if (function_declarator_p (declarator)
24567 || (decl_specifiers.type
24568 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24569 && declarator->kind == cdk_id
24570 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24571 == FUNCTION_TYPE)))
24572 initializer = cp_parser_pure_specifier (parser);
24573 else if (decl_specifiers.storage_class != sc_static)
24574 initializer = cp_parser_save_nsdmi (parser);
24575 else if (cxx_dialect >= cxx11)
24576 {
24577 bool nonconst;
24578 /* Don't require a constant rvalue in C++11, since we
24579 might want a reference constant. We'll enforce
24580 constancy later. */
24581 cp_lexer_consume_token (parser->lexer);
24582 /* Parse the initializer. */
24583 initializer = cp_parser_initializer_clause (parser,
24584 &nonconst);
24585 }
24586 else
24587 /* Parse the initializer. */
24588 initializer = cp_parser_constant_initializer (parser);
24589 }
24590 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24591 && !function_declarator_p (declarator))
24592 {
24593 bool x;
24594 if (decl_specifiers.storage_class != sc_static)
24595 initializer = cp_parser_save_nsdmi (parser);
24596 else
24597 initializer = cp_parser_initializer (parser, &x, &x);
24598 }
24599 /* Otherwise, there is no initializer. */
24600 else
24601 initializer = NULL_TREE;
24602
24603 /* See if we are probably looking at a function
24604 definition. We are certainly not looking at a
24605 member-declarator. Calling `grokfield' has
24606 side-effects, so we must not do it unless we are sure
24607 that we are looking at a member-declarator. */
24608 if (cp_parser_token_starts_function_definition_p
24609 (cp_lexer_peek_token (parser->lexer)))
24610 {
24611 /* The grammar does not allow a pure-specifier to be
24612 used when a member function is defined. (It is
24613 possible that this fact is an oversight in the
24614 standard, since a pure function may be defined
24615 outside of the class-specifier. */
24616 if (initializer && initializer_token_start)
24617 error_at (initializer_token_start->location,
24618 "pure-specifier on function-definition");
24619 decl = cp_parser_save_member_function_body (parser,
24620 &decl_specifiers,
24621 declarator,
24622 attributes);
24623 if (parser->fully_implicit_function_template_p)
24624 decl = finish_fully_implicit_template (parser, decl);
24625 /* If the member was not a friend, declare it here. */
24626 if (!friend_p)
24627 finish_member_declaration (decl);
24628 /* Peek at the next token. */
24629 token = cp_lexer_peek_token (parser->lexer);
24630 /* If the next token is a semicolon, consume it. */
24631 if (token->type == CPP_SEMICOLON)
24632 {
24633 location_t semicolon_loc
24634 = cp_lexer_consume_token (parser->lexer)->location;
24635 gcc_rich_location richloc (semicolon_loc);
24636 richloc.add_fixit_remove ();
24637 warning_at (&richloc, OPT_Wextra_semi,
24638 "extra %<;%> after in-class "
24639 "function definition");
24640 }
24641 goto out;
24642 }
24643 else
24644 if (declarator->kind == cdk_function)
24645 declarator->id_loc = token->location;
24646 /* Create the declaration. */
24647 decl = grokfield (declarator, &decl_specifiers,
24648 initializer, /*init_const_expr_p=*/true,
24649 asm_specification, attributes);
24650 if (parser->fully_implicit_function_template_p)
24651 {
24652 if (friend_p)
24653 finish_fully_implicit_template (parser, 0);
24654 else
24655 decl = finish_fully_implicit_template (parser, decl);
24656 }
24657 }
24658
24659 cp_finalize_omp_declare_simd (parser, decl);
24660 cp_finalize_oacc_routine (parser, decl, false);
24661
24662 /* Reset PREFIX_ATTRIBUTES. */
24663 if (attributes != error_mark_node)
24664 {
24665 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24666 attributes = TREE_CHAIN (attributes);
24667 if (attributes)
24668 TREE_CHAIN (attributes) = NULL_TREE;
24669 }
24670
24671 /* If there is any qualification still in effect, clear it
24672 now; we will be starting fresh with the next declarator. */
24673 parser->scope = NULL_TREE;
24674 parser->qualifying_scope = NULL_TREE;
24675 parser->object_scope = NULL_TREE;
24676 /* If it's a `,', then there are more declarators. */
24677 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24678 {
24679 cp_lexer_consume_token (parser->lexer);
24680 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24681 {
24682 cp_token *token = cp_lexer_previous_token (parser->lexer);
24683 gcc_rich_location richloc (token->location);
24684 richloc.add_fixit_remove ();
24685 error_at (&richloc, "stray %<,%> at end of "
24686 "member declaration");
24687 }
24688 }
24689 /* If the next token isn't a `;', then we have a parse error. */
24690 else if (cp_lexer_next_token_is_not (parser->lexer,
24691 CPP_SEMICOLON))
24692 {
24693 /* The next token might be a ways away from where the
24694 actual semicolon is missing. Find the previous token
24695 and use that for our error position. */
24696 cp_token *token = cp_lexer_previous_token (parser->lexer);
24697 gcc_rich_location richloc (token->location);
24698 richloc.add_fixit_insert_after (";");
24699 error_at (&richloc, "expected %<;%> at end of "
24700 "member declaration");
24701
24702 /* Assume that the user meant to provide a semicolon. If
24703 we were to cp_parser_skip_to_end_of_statement, we might
24704 skip to a semicolon inside a member function definition
24705 and issue nonsensical error messages. */
24706 assume_semicolon = true;
24707 }
24708
24709 if (decl)
24710 {
24711 /* Add DECL to the list of members. */
24712 if (!friend_p
24713 /* Explicitly include, eg, NSDMIs, for better error
24714 recovery (c++/58650). */
24715 || !DECL_DECLARES_FUNCTION_P (decl))
24716 finish_member_declaration (decl);
24717
24718 if (TREE_CODE (decl) == FUNCTION_DECL)
24719 cp_parser_save_default_args (parser, decl);
24720 else if (TREE_CODE (decl) == FIELD_DECL
24721 && DECL_INITIAL (decl))
24722 /* Add DECL to the queue of NSDMI to be parsed later. */
24723 vec_safe_push (unparsed_nsdmis, decl);
24724 }
24725
24726 if (assume_semicolon)
24727 goto out;
24728 }
24729 }
24730
24731 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24732 out:
24733 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24734 }
24735
24736 /* Parse a pure-specifier.
24737
24738 pure-specifier:
24739 = 0
24740
24741 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24742 Otherwise, ERROR_MARK_NODE is returned. */
24743
24744 static tree
24745 cp_parser_pure_specifier (cp_parser* parser)
24746 {
24747 cp_token *token;
24748
24749 /* Look for the `=' token. */
24750 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24751 return error_mark_node;
24752 /* Look for the `0' token. */
24753 token = cp_lexer_peek_token (parser->lexer);
24754
24755 if (token->type == CPP_EOF
24756 || token->type == CPP_PRAGMA_EOL)
24757 return error_mark_node;
24758
24759 cp_lexer_consume_token (parser->lexer);
24760
24761 /* Accept = default or = delete in c++0x mode. */
24762 if (token->keyword == RID_DEFAULT
24763 || token->keyword == RID_DELETE)
24764 {
24765 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24766 return token->u.value;
24767 }
24768
24769 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24770 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24771 {
24772 cp_parser_error (parser,
24773 "invalid pure specifier (only %<= 0%> is allowed)");
24774 cp_parser_skip_to_end_of_statement (parser);
24775 return error_mark_node;
24776 }
24777 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24778 {
24779 error_at (token->location, "templates may not be %<virtual%>");
24780 return error_mark_node;
24781 }
24782
24783 return integer_zero_node;
24784 }
24785
24786 /* Parse a constant-initializer.
24787
24788 constant-initializer:
24789 = constant-expression
24790
24791 Returns a representation of the constant-expression. */
24792
24793 static tree
24794 cp_parser_constant_initializer (cp_parser* parser)
24795 {
24796 /* Look for the `=' token. */
24797 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24798 return error_mark_node;
24799
24800 /* It is invalid to write:
24801
24802 struct S { static const int i = { 7 }; };
24803
24804 */
24805 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24806 {
24807 cp_parser_error (parser,
24808 "a brace-enclosed initializer is not allowed here");
24809 /* Consume the opening brace. */
24810 matching_braces braces;
24811 braces.consume_open (parser);
24812 /* Skip the initializer. */
24813 cp_parser_skip_to_closing_brace (parser);
24814 /* Look for the trailing `}'. */
24815 braces.require_close (parser);
24816
24817 return error_mark_node;
24818 }
24819
24820 return cp_parser_constant_expression (parser);
24821 }
24822
24823 /* Derived classes [gram.class.derived] */
24824
24825 /* Parse a base-clause.
24826
24827 base-clause:
24828 : base-specifier-list
24829
24830 base-specifier-list:
24831 base-specifier ... [opt]
24832 base-specifier-list , base-specifier ... [opt]
24833
24834 Returns a TREE_LIST representing the base-classes, in the order in
24835 which they were declared. The representation of each node is as
24836 described by cp_parser_base_specifier.
24837
24838 In the case that no bases are specified, this function will return
24839 NULL_TREE, not ERROR_MARK_NODE. */
24840
24841 static tree
24842 cp_parser_base_clause (cp_parser* parser)
24843 {
24844 tree bases = NULL_TREE;
24845
24846 /* Look for the `:' that begins the list. */
24847 cp_parser_require (parser, CPP_COLON, RT_COLON);
24848
24849 /* Scan the base-specifier-list. */
24850 while (true)
24851 {
24852 cp_token *token;
24853 tree base;
24854 bool pack_expansion_p = false;
24855
24856 /* Look for the base-specifier. */
24857 base = cp_parser_base_specifier (parser);
24858 /* Look for the (optional) ellipsis. */
24859 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24860 {
24861 /* Consume the `...'. */
24862 cp_lexer_consume_token (parser->lexer);
24863
24864 pack_expansion_p = true;
24865 }
24866
24867 /* Add BASE to the front of the list. */
24868 if (base && base != error_mark_node)
24869 {
24870 if (pack_expansion_p)
24871 /* Make this a pack expansion type. */
24872 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24873
24874 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24875 {
24876 TREE_CHAIN (base) = bases;
24877 bases = base;
24878 }
24879 }
24880 /* Peek at the next token. */
24881 token = cp_lexer_peek_token (parser->lexer);
24882 /* If it's not a comma, then the list is complete. */
24883 if (token->type != CPP_COMMA)
24884 break;
24885 /* Consume the `,'. */
24886 cp_lexer_consume_token (parser->lexer);
24887 }
24888
24889 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24890 base class had a qualified name. However, the next name that
24891 appears is certainly not qualified. */
24892 parser->scope = NULL_TREE;
24893 parser->qualifying_scope = NULL_TREE;
24894 parser->object_scope = NULL_TREE;
24895
24896 return nreverse (bases);
24897 }
24898
24899 /* Parse a base-specifier.
24900
24901 base-specifier:
24902 :: [opt] nested-name-specifier [opt] class-name
24903 virtual access-specifier [opt] :: [opt] nested-name-specifier
24904 [opt] class-name
24905 access-specifier virtual [opt] :: [opt] nested-name-specifier
24906 [opt] class-name
24907
24908 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24909 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24910 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24911 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24912
24913 static tree
24914 cp_parser_base_specifier (cp_parser* parser)
24915 {
24916 cp_token *token;
24917 bool done = false;
24918 bool virtual_p = false;
24919 bool duplicate_virtual_error_issued_p = false;
24920 bool duplicate_access_error_issued_p = false;
24921 bool class_scope_p, template_p;
24922 tree access = access_default_node;
24923 tree type;
24924
24925 /* Process the optional `virtual' and `access-specifier'. */
24926 while (!done)
24927 {
24928 /* Peek at the next token. */
24929 token = cp_lexer_peek_token (parser->lexer);
24930 /* Process `virtual'. */
24931 switch (token->keyword)
24932 {
24933 case RID_VIRTUAL:
24934 /* If `virtual' appears more than once, issue an error. */
24935 if (virtual_p && !duplicate_virtual_error_issued_p)
24936 {
24937 cp_parser_error (parser,
24938 "%<virtual%> specified more than once in base-specifier");
24939 duplicate_virtual_error_issued_p = true;
24940 }
24941
24942 virtual_p = true;
24943
24944 /* Consume the `virtual' token. */
24945 cp_lexer_consume_token (parser->lexer);
24946
24947 break;
24948
24949 case RID_PUBLIC:
24950 case RID_PROTECTED:
24951 case RID_PRIVATE:
24952 /* If more than one access specifier appears, issue an
24953 error. */
24954 if (access != access_default_node
24955 && !duplicate_access_error_issued_p)
24956 {
24957 cp_parser_error (parser,
24958 "more than one access specifier in base-specifier");
24959 duplicate_access_error_issued_p = true;
24960 }
24961
24962 access = ridpointers[(int) token->keyword];
24963
24964 /* Consume the access-specifier. */
24965 cp_lexer_consume_token (parser->lexer);
24966
24967 break;
24968
24969 default:
24970 done = true;
24971 break;
24972 }
24973 }
24974 /* It is not uncommon to see programs mechanically, erroneously, use
24975 the 'typename' keyword to denote (dependent) qualified types
24976 as base classes. */
24977 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24978 {
24979 token = cp_lexer_peek_token (parser->lexer);
24980 if (!processing_template_decl)
24981 error_at (token->location,
24982 "keyword %<typename%> not allowed outside of templates");
24983 else
24984 error_at (token->location,
24985 "keyword %<typename%> not allowed in this context "
24986 "(the base class is implicitly a type)");
24987 cp_lexer_consume_token (parser->lexer);
24988 }
24989
24990 /* Look for the optional `::' operator. */
24991 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24992 /* Look for the nested-name-specifier. The simplest way to
24993 implement:
24994
24995 [temp.res]
24996
24997 The keyword `typename' is not permitted in a base-specifier or
24998 mem-initializer; in these contexts a qualified name that
24999 depends on a template-parameter is implicitly assumed to be a
25000 type name.
25001
25002 is to pretend that we have seen the `typename' keyword at this
25003 point. */
25004 cp_parser_nested_name_specifier_opt (parser,
25005 /*typename_keyword_p=*/true,
25006 /*check_dependency_p=*/true,
25007 /*type_p=*/true,
25008 /*is_declaration=*/true);
25009 /* If the base class is given by a qualified name, assume that names
25010 we see are type names or templates, as appropriate. */
25011 class_scope_p = (parser->scope && TYPE_P (parser->scope));
25012 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
25013
25014 if (!parser->scope
25015 && cp_lexer_next_token_is_decltype (parser->lexer))
25016 /* DR 950 allows decltype as a base-specifier. */
25017 type = cp_parser_decltype (parser);
25018 else
25019 {
25020 /* Otherwise, look for the class-name. */
25021 type = cp_parser_class_name (parser,
25022 class_scope_p,
25023 template_p,
25024 typename_type,
25025 /*check_dependency_p=*/true,
25026 /*class_head_p=*/false,
25027 /*is_declaration=*/true);
25028 type = TREE_TYPE (type);
25029 }
25030
25031 if (type == error_mark_node)
25032 return error_mark_node;
25033
25034 return finish_base_specifier (type, access, virtual_p);
25035 }
25036
25037 /* Exception handling [gram.exception] */
25038
25039 /* Parse an (optional) noexcept-specification.
25040
25041 noexcept-specification:
25042 noexcept ( constant-expression ) [opt]
25043
25044 If no noexcept-specification is present, returns NULL_TREE.
25045 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25046 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25047 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25048 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25049 in which case a boolean condition is returned instead. */
25050
25051 static tree
25052 cp_parser_noexcept_specification_opt (cp_parser* parser,
25053 bool require_constexpr,
25054 bool* consumed_expr,
25055 bool return_cond)
25056 {
25057 cp_token *token;
25058 const char *saved_message;
25059
25060 /* Peek at the next token. */
25061 token = cp_lexer_peek_token (parser->lexer);
25062
25063 /* Is it a noexcept-specification? */
25064 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
25065 {
25066 tree expr;
25067 cp_lexer_consume_token (parser->lexer);
25068
25069 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
25070 {
25071 matching_parens parens;
25072 parens.consume_open (parser);
25073
25074 tree save_ccp = current_class_ptr;
25075 tree save_ccr = current_class_ref;
25076
25077 if (current_class_type)
25078 inject_this_parameter (current_class_type, TYPE_UNQUALIFIED);
25079
25080 if (require_constexpr)
25081 {
25082 /* Types may not be defined in an exception-specification. */
25083 saved_message = parser->type_definition_forbidden_message;
25084 parser->type_definition_forbidden_message
25085 = G_("types may not be defined in an exception-specification");
25086
25087 expr = cp_parser_constant_expression (parser);
25088
25089 /* Restore the saved message. */
25090 parser->type_definition_forbidden_message = saved_message;
25091 }
25092 else
25093 {
25094 expr = cp_parser_expression (parser);
25095 *consumed_expr = true;
25096 }
25097
25098 parens.require_close (parser);
25099
25100 current_class_ptr = save_ccp;
25101 current_class_ref = save_ccr;
25102 }
25103 else
25104 {
25105 expr = boolean_true_node;
25106 if (!require_constexpr)
25107 *consumed_expr = false;
25108 }
25109
25110 /* We cannot build a noexcept-spec right away because this will check
25111 that expr is a constexpr. */
25112 if (!return_cond)
25113 return build_noexcept_spec (expr, tf_warning_or_error);
25114 else
25115 return expr;
25116 }
25117 else
25118 return NULL_TREE;
25119 }
25120
25121 /* Parse an (optional) exception-specification.
25122
25123 exception-specification:
25124 throw ( type-id-list [opt] )
25125
25126 Returns a TREE_LIST representing the exception-specification. The
25127 TREE_VALUE of each node is a type. */
25128
25129 static tree
25130 cp_parser_exception_specification_opt (cp_parser* parser)
25131 {
25132 cp_token *token;
25133 tree type_id_list;
25134 const char *saved_message;
25135
25136 /* Peek at the next token. */
25137 token = cp_lexer_peek_token (parser->lexer);
25138
25139 /* Is it a noexcept-specification? */
25140 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
25141 false);
25142 if (type_id_list != NULL_TREE)
25143 return type_id_list;
25144
25145 /* If it's not `throw', then there's no exception-specification. */
25146 if (!cp_parser_is_keyword (token, RID_THROW))
25147 return NULL_TREE;
25148
25149 location_t loc = token->location;
25150
25151 /* Consume the `throw'. */
25152 cp_lexer_consume_token (parser->lexer);
25153
25154 /* Look for the `('. */
25155 matching_parens parens;
25156 parens.require_open (parser);
25157
25158 /* Peek at the next token. */
25159 token = cp_lexer_peek_token (parser->lexer);
25160 /* If it's not a `)', then there is a type-id-list. */
25161 if (token->type != CPP_CLOSE_PAREN)
25162 {
25163 /* Types may not be defined in an exception-specification. */
25164 saved_message = parser->type_definition_forbidden_message;
25165 parser->type_definition_forbidden_message
25166 = G_("types may not be defined in an exception-specification");
25167 /* Parse the type-id-list. */
25168 type_id_list = cp_parser_type_id_list (parser);
25169 /* Restore the saved message. */
25170 parser->type_definition_forbidden_message = saved_message;
25171
25172 if (cxx_dialect >= cxx17)
25173 {
25174 error_at (loc, "ISO C++17 does not allow dynamic exception "
25175 "specifications");
25176 type_id_list = NULL_TREE;
25177 }
25178 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
25179 warning_at (loc, OPT_Wdeprecated,
25180 "dynamic exception specifications are deprecated in "
25181 "C++11");
25182 }
25183 /* In C++17, throw() is equivalent to noexcept (true). throw()
25184 is deprecated in C++11 and above as well, but is still widely used,
25185 so don't warn about it yet. */
25186 else if (cxx_dialect >= cxx17)
25187 type_id_list = noexcept_true_spec;
25188 else
25189 type_id_list = empty_except_spec;
25190
25191 /* Look for the `)'. */
25192 parens.require_close (parser);
25193
25194 return type_id_list;
25195 }
25196
25197 /* Parse an (optional) type-id-list.
25198
25199 type-id-list:
25200 type-id ... [opt]
25201 type-id-list , type-id ... [opt]
25202
25203 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25204 in the order that the types were presented. */
25205
25206 static tree
25207 cp_parser_type_id_list (cp_parser* parser)
25208 {
25209 tree types = NULL_TREE;
25210
25211 while (true)
25212 {
25213 cp_token *token;
25214 tree type;
25215
25216 token = cp_lexer_peek_token (parser->lexer);
25217
25218 /* Get the next type-id. */
25219 type = cp_parser_type_id (parser);
25220 /* Check for invalid 'auto'. */
25221 if (flag_concepts && type_uses_auto (type))
25222 {
25223 error_at (token->location,
25224 "invalid use of %<auto%> in exception-specification");
25225 type = error_mark_node;
25226 }
25227 /* Parse the optional ellipsis. */
25228 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25229 {
25230 /* Consume the `...'. */
25231 cp_lexer_consume_token (parser->lexer);
25232
25233 /* Turn the type into a pack expansion expression. */
25234 type = make_pack_expansion (type);
25235 }
25236 /* Add it to the list. */
25237 types = add_exception_specifier (types, type, /*complain=*/1);
25238 /* Peek at the next token. */
25239 token = cp_lexer_peek_token (parser->lexer);
25240 /* If it is not a `,', we are done. */
25241 if (token->type != CPP_COMMA)
25242 break;
25243 /* Consume the `,'. */
25244 cp_lexer_consume_token (parser->lexer);
25245 }
25246
25247 return nreverse (types);
25248 }
25249
25250 /* Parse a try-block.
25251
25252 try-block:
25253 try compound-statement handler-seq */
25254
25255 static tree
25256 cp_parser_try_block (cp_parser* parser)
25257 {
25258 tree try_block;
25259
25260 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
25261 if (parser->in_function_body
25262 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
25263 error ("%<try%> in %<constexpr%> function");
25264
25265 try_block = begin_try_block ();
25266 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
25267 finish_try_block (try_block);
25268 cp_parser_handler_seq (parser);
25269 finish_handler_sequence (try_block);
25270
25271 return try_block;
25272 }
25273
25274 /* Parse a function-try-block.
25275
25276 function-try-block:
25277 try ctor-initializer [opt] function-body handler-seq */
25278
25279 static void
25280 cp_parser_function_try_block (cp_parser* parser)
25281 {
25282 tree compound_stmt;
25283 tree try_block;
25284
25285 /* Look for the `try' keyword. */
25286 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
25287 return;
25288 /* Let the rest of the front end know where we are. */
25289 try_block = begin_function_try_block (&compound_stmt);
25290 /* Parse the function-body. */
25291 cp_parser_ctor_initializer_opt_and_function_body
25292 (parser, /*in_function_try_block=*/true);
25293 /* We're done with the `try' part. */
25294 finish_function_try_block (try_block);
25295 /* Parse the handlers. */
25296 cp_parser_handler_seq (parser);
25297 /* We're done with the handlers. */
25298 finish_function_handler_sequence (try_block, compound_stmt);
25299 }
25300
25301 /* Parse a handler-seq.
25302
25303 handler-seq:
25304 handler handler-seq [opt] */
25305
25306 static void
25307 cp_parser_handler_seq (cp_parser* parser)
25308 {
25309 while (true)
25310 {
25311 cp_token *token;
25312
25313 /* Parse the handler. */
25314 cp_parser_handler (parser);
25315 /* Peek at the next token. */
25316 token = cp_lexer_peek_token (parser->lexer);
25317 /* If it's not `catch' then there are no more handlers. */
25318 if (!cp_parser_is_keyword (token, RID_CATCH))
25319 break;
25320 }
25321 }
25322
25323 /* Parse a handler.
25324
25325 handler:
25326 catch ( exception-declaration ) compound-statement */
25327
25328 static void
25329 cp_parser_handler (cp_parser* parser)
25330 {
25331 tree handler;
25332 tree declaration;
25333
25334 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
25335 handler = begin_handler ();
25336 matching_parens parens;
25337 parens.require_open (parser);
25338 declaration = cp_parser_exception_declaration (parser);
25339 finish_handler_parms (declaration, handler);
25340 parens.require_close (parser);
25341 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
25342 finish_handler (handler);
25343 }
25344
25345 /* Parse an exception-declaration.
25346
25347 exception-declaration:
25348 type-specifier-seq declarator
25349 type-specifier-seq abstract-declarator
25350 type-specifier-seq
25351 ...
25352
25353 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25354 ellipsis variant is used. */
25355
25356 static tree
25357 cp_parser_exception_declaration (cp_parser* parser)
25358 {
25359 cp_decl_specifier_seq type_specifiers;
25360 cp_declarator *declarator;
25361 const char *saved_message;
25362
25363 /* If it's an ellipsis, it's easy to handle. */
25364 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25365 {
25366 /* Consume the `...' token. */
25367 cp_lexer_consume_token (parser->lexer);
25368 return NULL_TREE;
25369 }
25370
25371 /* Types may not be defined in exception-declarations. */
25372 saved_message = parser->type_definition_forbidden_message;
25373 parser->type_definition_forbidden_message
25374 = G_("types may not be defined in exception-declarations");
25375
25376 /* Parse the type-specifier-seq. */
25377 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
25378 /*is_declaration=*/true,
25379 /*is_trailing_return=*/false,
25380 &type_specifiers);
25381 /* If it's a `)', then there is no declarator. */
25382 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25383 declarator = NULL;
25384 else
25385 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
25386 CP_PARSER_FLAGS_NONE,
25387 /*ctor_dtor_or_conv_p=*/NULL,
25388 /*parenthesized_p=*/NULL,
25389 /*member_p=*/false,
25390 /*friend_p=*/false,
25391 /*static_p=*/false);
25392
25393 /* Restore the saved message. */
25394 parser->type_definition_forbidden_message = saved_message;
25395
25396 if (!type_specifiers.any_specifiers_p)
25397 return error_mark_node;
25398
25399 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
25400 }
25401
25402 /* Parse a throw-expression.
25403
25404 throw-expression:
25405 throw assignment-expression [opt]
25406
25407 Returns a THROW_EXPR representing the throw-expression. */
25408
25409 static tree
25410 cp_parser_throw_expression (cp_parser* parser)
25411 {
25412 tree expression;
25413 cp_token* token;
25414
25415 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
25416 token = cp_lexer_peek_token (parser->lexer);
25417 /* Figure out whether or not there is an assignment-expression
25418 following the "throw" keyword. */
25419 if (token->type == CPP_COMMA
25420 || token->type == CPP_SEMICOLON
25421 || token->type == CPP_CLOSE_PAREN
25422 || token->type == CPP_CLOSE_SQUARE
25423 || token->type == CPP_CLOSE_BRACE
25424 || token->type == CPP_COLON)
25425 expression = NULL_TREE;
25426 else
25427 expression = cp_parser_assignment_expression (parser);
25428
25429 return build_throw (expression);
25430 }
25431
25432 /* GNU Extensions */
25433
25434 /* Parse an (optional) asm-specification.
25435
25436 asm-specification:
25437 asm ( string-literal )
25438
25439 If the asm-specification is present, returns a STRING_CST
25440 corresponding to the string-literal. Otherwise, returns
25441 NULL_TREE. */
25442
25443 static tree
25444 cp_parser_asm_specification_opt (cp_parser* parser)
25445 {
25446 cp_token *token;
25447 tree asm_specification;
25448
25449 /* Peek at the next token. */
25450 token = cp_lexer_peek_token (parser->lexer);
25451 /* If the next token isn't the `asm' keyword, then there's no
25452 asm-specification. */
25453 if (!cp_parser_is_keyword (token, RID_ASM))
25454 return NULL_TREE;
25455
25456 /* Consume the `asm' token. */
25457 cp_lexer_consume_token (parser->lexer);
25458 /* Look for the `('. */
25459 matching_parens parens;
25460 parens.require_open (parser);
25461
25462 /* Look for the string-literal. */
25463 asm_specification = cp_parser_string_literal (parser, false, false);
25464
25465 /* Look for the `)'. */
25466 parens.require_close (parser);
25467
25468 return asm_specification;
25469 }
25470
25471 /* Parse an asm-operand-list.
25472
25473 asm-operand-list:
25474 asm-operand
25475 asm-operand-list , asm-operand
25476
25477 asm-operand:
25478 string-literal ( expression )
25479 [ string-literal ] string-literal ( expression )
25480
25481 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25482 each node is the expression. The TREE_PURPOSE is itself a
25483 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25484 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25485 is a STRING_CST for the string literal before the parenthesis. Returns
25486 ERROR_MARK_NODE if any of the operands are invalid. */
25487
25488 static tree
25489 cp_parser_asm_operand_list (cp_parser* parser)
25490 {
25491 tree asm_operands = NULL_TREE;
25492 bool invalid_operands = false;
25493
25494 while (true)
25495 {
25496 tree string_literal;
25497 tree expression;
25498 tree name;
25499
25500 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25501 {
25502 /* Consume the `[' token. */
25503 cp_lexer_consume_token (parser->lexer);
25504 /* Read the operand name. */
25505 name = cp_parser_identifier (parser);
25506 if (name != error_mark_node)
25507 name = build_string (IDENTIFIER_LENGTH (name),
25508 IDENTIFIER_POINTER (name));
25509 /* Look for the closing `]'. */
25510 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25511 }
25512 else
25513 name = NULL_TREE;
25514 /* Look for the string-literal. */
25515 string_literal = cp_parser_string_literal (parser, false, false);
25516
25517 /* Look for the `('. */
25518 matching_parens parens;
25519 parens.require_open (parser);
25520 /* Parse the expression. */
25521 expression = cp_parser_expression (parser);
25522 /* Look for the `)'. */
25523 parens.require_close (parser);
25524
25525 if (name == error_mark_node
25526 || string_literal == error_mark_node
25527 || expression == error_mark_node)
25528 invalid_operands = true;
25529
25530 /* Add this operand to the list. */
25531 asm_operands = tree_cons (build_tree_list (name, string_literal),
25532 expression,
25533 asm_operands);
25534 /* If the next token is not a `,', there are no more
25535 operands. */
25536 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25537 break;
25538 /* Consume the `,'. */
25539 cp_lexer_consume_token (parser->lexer);
25540 }
25541
25542 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25543 }
25544
25545 /* Parse an asm-clobber-list.
25546
25547 asm-clobber-list:
25548 string-literal
25549 asm-clobber-list , string-literal
25550
25551 Returns a TREE_LIST, indicating the clobbers in the order that they
25552 appeared. The TREE_VALUE of each node is a STRING_CST. */
25553
25554 static tree
25555 cp_parser_asm_clobber_list (cp_parser* parser)
25556 {
25557 tree clobbers = NULL_TREE;
25558
25559 while (true)
25560 {
25561 tree string_literal;
25562
25563 /* Look for the string literal. */
25564 string_literal = cp_parser_string_literal (parser, false, false);
25565 /* Add it to the list. */
25566 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25567 /* If the next token is not a `,', then the list is
25568 complete. */
25569 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25570 break;
25571 /* Consume the `,' token. */
25572 cp_lexer_consume_token (parser->lexer);
25573 }
25574
25575 return clobbers;
25576 }
25577
25578 /* Parse an asm-label-list.
25579
25580 asm-label-list:
25581 identifier
25582 asm-label-list , identifier
25583
25584 Returns a TREE_LIST, indicating the labels in the order that they
25585 appeared. The TREE_VALUE of each node is a label. */
25586
25587 static tree
25588 cp_parser_asm_label_list (cp_parser* parser)
25589 {
25590 tree labels = NULL_TREE;
25591
25592 while (true)
25593 {
25594 tree identifier, label, name;
25595
25596 /* Look for the identifier. */
25597 identifier = cp_parser_identifier (parser);
25598 if (!error_operand_p (identifier))
25599 {
25600 label = lookup_label (identifier);
25601 if (TREE_CODE (label) == LABEL_DECL)
25602 {
25603 TREE_USED (label) = 1;
25604 check_goto (label);
25605 name = build_string (IDENTIFIER_LENGTH (identifier),
25606 IDENTIFIER_POINTER (identifier));
25607 labels = tree_cons (name, label, labels);
25608 }
25609 }
25610 /* If the next token is not a `,', then the list is
25611 complete. */
25612 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25613 break;
25614 /* Consume the `,' token. */
25615 cp_lexer_consume_token (parser->lexer);
25616 }
25617
25618 return nreverse (labels);
25619 }
25620
25621 /* Return TRUE iff the next tokens in the stream are possibly the
25622 beginning of a GNU extension attribute. */
25623
25624 static bool
25625 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25626 {
25627 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25628 }
25629
25630 /* Return TRUE iff the next tokens in the stream are possibly the
25631 beginning of a standard C++-11 attribute specifier. */
25632
25633 static bool
25634 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25635 {
25636 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25637 }
25638
25639 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25640 beginning of a standard C++-11 attribute specifier. */
25641
25642 static bool
25643 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25644 {
25645 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25646
25647 return (cxx_dialect >= cxx11
25648 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25649 || (token->type == CPP_OPEN_SQUARE
25650 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25651 && token->type == CPP_OPEN_SQUARE)));
25652 }
25653
25654 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25655 beginning of a GNU extension attribute. */
25656
25657 static bool
25658 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25659 {
25660 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25661
25662 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25663 }
25664
25665 /* Return true iff the next tokens can be the beginning of either a
25666 GNU attribute list, or a standard C++11 attribute sequence. */
25667
25668 static bool
25669 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25670 {
25671 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25672 || cp_next_tokens_can_be_std_attribute_p (parser));
25673 }
25674
25675 /* Return true iff the next Nth tokens can be the beginning of either
25676 a GNU attribute list, or a standard C++11 attribute sequence. */
25677
25678 static bool
25679 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25680 {
25681 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25682 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25683 }
25684
25685 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25686 of GNU attributes, or return NULL. */
25687
25688 static tree
25689 cp_parser_attributes_opt (cp_parser *parser)
25690 {
25691 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25692 return cp_parser_gnu_attributes_opt (parser);
25693 return cp_parser_std_attribute_spec_seq (parser);
25694 }
25695
25696 /* Parse an (optional) series of attributes.
25697
25698 attributes:
25699 attributes attribute
25700
25701 attribute:
25702 __attribute__ (( attribute-list [opt] ))
25703
25704 The return value is as for cp_parser_gnu_attribute_list. */
25705
25706 static tree
25707 cp_parser_gnu_attributes_opt (cp_parser* parser)
25708 {
25709 tree attributes = NULL_TREE;
25710
25711 temp_override<bool> cleanup
25712 (parser->auto_is_implicit_function_template_parm_p, false);
25713
25714 while (true)
25715 {
25716 cp_token *token;
25717 tree attribute_list;
25718 bool ok = true;
25719
25720 /* Peek at the next token. */
25721 token = cp_lexer_peek_token (parser->lexer);
25722 /* If it's not `__attribute__', then we're done. */
25723 if (token->keyword != RID_ATTRIBUTE)
25724 break;
25725
25726 /* Consume the `__attribute__' keyword. */
25727 cp_lexer_consume_token (parser->lexer);
25728 /* Look for the two `(' tokens. */
25729 matching_parens outer_parens;
25730 outer_parens.require_open (parser);
25731 matching_parens inner_parens;
25732 inner_parens.require_open (parser);
25733
25734 /* Peek at the next token. */
25735 token = cp_lexer_peek_token (parser->lexer);
25736 if (token->type != CPP_CLOSE_PAREN)
25737 /* Parse the attribute-list. */
25738 attribute_list = cp_parser_gnu_attribute_list (parser);
25739 else
25740 /* If the next token is a `)', then there is no attribute
25741 list. */
25742 attribute_list = NULL;
25743
25744 /* Look for the two `)' tokens. */
25745 if (!inner_parens.require_close (parser))
25746 ok = false;
25747 if (!outer_parens.require_close (parser))
25748 ok = false;
25749 if (!ok)
25750 cp_parser_skip_to_end_of_statement (parser);
25751
25752 /* Add these new attributes to the list. */
25753 attributes = attr_chainon (attributes, attribute_list);
25754 }
25755
25756 return attributes;
25757 }
25758
25759 /* Parse a GNU attribute-list.
25760
25761 attribute-list:
25762 attribute
25763 attribute-list , attribute
25764
25765 attribute:
25766 identifier
25767 identifier ( identifier )
25768 identifier ( identifier , expression-list )
25769 identifier ( expression-list )
25770
25771 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25772 to an attribute. The TREE_PURPOSE of each node is the identifier
25773 indicating which attribute is in use. The TREE_VALUE represents
25774 the arguments, if any. */
25775
25776 static tree
25777 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
25778 {
25779 tree attribute_list = NULL_TREE;
25780 bool save_translate_strings_p = parser->translate_strings_p;
25781
25782 /* Don't create wrapper nodes within attributes: the
25783 handlers don't know how to handle them. */
25784 auto_suppress_location_wrappers sentinel;
25785
25786 parser->translate_strings_p = false;
25787 while (true)
25788 {
25789 cp_token *token;
25790 tree identifier;
25791 tree attribute;
25792
25793 /* Look for the identifier. We also allow keywords here; for
25794 example `__attribute__ ((const))' is legal. */
25795 token = cp_lexer_peek_token (parser->lexer);
25796 if (token->type == CPP_NAME
25797 || token->type == CPP_KEYWORD)
25798 {
25799 tree arguments = NULL_TREE;
25800
25801 /* Consume the token, but save it since we need it for the
25802 SIMD enabled function parsing. */
25803 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25804
25805 /* Save away the identifier that indicates which attribute
25806 this is. */
25807 identifier = (token->type == CPP_KEYWORD)
25808 /* For keywords, use the canonical spelling, not the
25809 parsed identifier. */
25810 ? ridpointers[(int) token->keyword]
25811 : id_token->u.value;
25812
25813 identifier = canonicalize_attr_name (identifier);
25814 attribute = build_tree_list (identifier, NULL_TREE);
25815
25816 /* Peek at the next token. */
25817 token = cp_lexer_peek_token (parser->lexer);
25818 /* If it's an `(', then parse the attribute arguments. */
25819 if (token->type == CPP_OPEN_PAREN)
25820 {
25821 vec<tree, va_gc> *vec;
25822 int attr_flag = (attribute_takes_identifier_p (identifier)
25823 ? id_attr : normal_attr);
25824 vec = cp_parser_parenthesized_expression_list
25825 (parser, attr_flag, /*cast_p=*/false,
25826 /*allow_expansion_p=*/false,
25827 /*non_constant_p=*/NULL);
25828 if (vec == NULL)
25829 arguments = error_mark_node;
25830 else
25831 {
25832 arguments = build_tree_list_vec (vec);
25833 release_tree_vector (vec);
25834 }
25835 /* Save the arguments away. */
25836 TREE_VALUE (attribute) = arguments;
25837 }
25838
25839 if (arguments != error_mark_node)
25840 {
25841 /* Add this attribute to the list. */
25842 TREE_CHAIN (attribute) = attribute_list;
25843 attribute_list = attribute;
25844 }
25845
25846 token = cp_lexer_peek_token (parser->lexer);
25847 }
25848 /* Unless EXACTLY_ONE is set look for more attributes.
25849 If the next token isn't a `,', we're done. */
25850 if (exactly_one || token->type != CPP_COMMA)
25851 break;
25852
25853 /* Consume the comma and keep going. */
25854 cp_lexer_consume_token (parser->lexer);
25855 }
25856 parser->translate_strings_p = save_translate_strings_p;
25857
25858 /* We built up the list in reverse order. */
25859 return nreverse (attribute_list);
25860 }
25861
25862 /* Parse a standard C++11 attribute.
25863
25864 The returned representation is a TREE_LIST which TREE_PURPOSE is
25865 the scoped name of the attribute, and the TREE_VALUE is its
25866 arguments list.
25867
25868 Note that the scoped name of the attribute is itself a TREE_LIST
25869 which TREE_PURPOSE is the namespace of the attribute, and
25870 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25871 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25872 and which TREE_PURPOSE is directly the attribute name.
25873
25874 Clients of the attribute code should use get_attribute_namespace
25875 and get_attribute_name to get the actual namespace and name of
25876 attributes, regardless of their being GNU or C++11 attributes.
25877
25878 attribute:
25879 attribute-token attribute-argument-clause [opt]
25880
25881 attribute-token:
25882 identifier
25883 attribute-scoped-token
25884
25885 attribute-scoped-token:
25886 attribute-namespace :: identifier
25887
25888 attribute-namespace:
25889 identifier
25890
25891 attribute-argument-clause:
25892 ( balanced-token-seq )
25893
25894 balanced-token-seq:
25895 balanced-token [opt]
25896 balanced-token-seq balanced-token
25897
25898 balanced-token:
25899 ( balanced-token-seq )
25900 [ balanced-token-seq ]
25901 { balanced-token-seq }. */
25902
25903 static tree
25904 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25905 {
25906 tree attribute, attr_id = NULL_TREE, arguments;
25907 cp_token *token;
25908
25909 temp_override<bool> cleanup
25910 (parser->auto_is_implicit_function_template_parm_p, false);
25911
25912 /* First, parse name of the attribute, a.k.a attribute-token. */
25913
25914 token = cp_lexer_peek_token (parser->lexer);
25915 if (token->type == CPP_NAME)
25916 attr_id = token->u.value;
25917 else if (token->type == CPP_KEYWORD)
25918 attr_id = ridpointers[(int) token->keyword];
25919 else if (token->flags & NAMED_OP)
25920 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25921
25922 if (attr_id == NULL_TREE)
25923 return NULL_TREE;
25924
25925 cp_lexer_consume_token (parser->lexer);
25926
25927 token = cp_lexer_peek_token (parser->lexer);
25928 if (token->type == CPP_SCOPE)
25929 {
25930 /* We are seeing a scoped attribute token. */
25931
25932 cp_lexer_consume_token (parser->lexer);
25933 if (attr_ns)
25934 error_at (token->location, "attribute using prefix used together "
25935 "with scoped attribute token");
25936 attr_ns = attr_id;
25937
25938 token = cp_lexer_consume_token (parser->lexer);
25939 if (token->type == CPP_NAME)
25940 attr_id = token->u.value;
25941 else if (token->type == CPP_KEYWORD)
25942 attr_id = ridpointers[(int) token->keyword];
25943 else if (token->flags & NAMED_OP)
25944 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25945 else
25946 {
25947 error_at (token->location,
25948 "expected an identifier for the attribute name");
25949 return error_mark_node;
25950 }
25951
25952 attr_ns = canonicalize_attr_name (attr_ns);
25953 attr_id = canonicalize_attr_name (attr_id);
25954 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25955 NULL_TREE);
25956 token = cp_lexer_peek_token (parser->lexer);
25957 }
25958 else if (attr_ns)
25959 {
25960 attr_ns = canonicalize_attr_name (attr_ns);
25961 attr_id = canonicalize_attr_name (attr_id);
25962 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25963 NULL_TREE);
25964 }
25965 else
25966 {
25967 attr_id = canonicalize_attr_name (attr_id);
25968 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25969 NULL_TREE);
25970 /* C++11 noreturn attribute is equivalent to GNU's. */
25971 if (is_attribute_p ("noreturn", attr_id))
25972 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25973 /* C++14 deprecated attribute is equivalent to GNU's. */
25974 else if (is_attribute_p ("deprecated", attr_id))
25975 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25976 /* C++17 fallthrough attribute is equivalent to GNU's. */
25977 else if (is_attribute_p ("fallthrough", attr_id))
25978 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25979 /* Transactional Memory TS optimize_for_synchronized attribute is
25980 equivalent to GNU transaction_callable. */
25981 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25982 TREE_PURPOSE (attribute)
25983 = get_identifier ("transaction_callable");
25984 /* Transactional Memory attributes are GNU attributes. */
25985 else if (tm_attr_to_mask (attr_id))
25986 TREE_PURPOSE (attribute) = attr_id;
25987 }
25988
25989 /* Now parse the optional argument clause of the attribute. */
25990
25991 if (token->type != CPP_OPEN_PAREN)
25992 return attribute;
25993
25994 {
25995 vec<tree, va_gc> *vec;
25996 int attr_flag = normal_attr;
25997
25998 if (attr_ns == gnu_identifier
25999 && attribute_takes_identifier_p (attr_id))
26000 /* A GNU attribute that takes an identifier in parameter. */
26001 attr_flag = id_attr;
26002
26003 vec = cp_parser_parenthesized_expression_list
26004 (parser, attr_flag, /*cast_p=*/false,
26005 /*allow_expansion_p=*/true,
26006 /*non_constant_p=*/NULL);
26007 if (vec == NULL)
26008 arguments = error_mark_node;
26009 else
26010 {
26011 arguments = build_tree_list_vec (vec);
26012 release_tree_vector (vec);
26013 }
26014
26015 if (arguments == error_mark_node)
26016 attribute = error_mark_node;
26017 else
26018 TREE_VALUE (attribute) = arguments;
26019 }
26020
26021 return attribute;
26022 }
26023
26024 /* Check that the attribute ATTRIBUTE appears at most once in the
26025 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
26026 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
26027 isn't implemented yet in GCC. */
26028
26029 static void
26030 cp_parser_check_std_attribute (tree attributes, tree attribute)
26031 {
26032 if (attributes)
26033 {
26034 tree name = get_attribute_name (attribute);
26035 if (is_attribute_p ("noreturn", name)
26036 && lookup_attribute ("noreturn", attributes))
26037 error ("attribute %<noreturn%> can appear at most once "
26038 "in an attribute-list");
26039 else if (is_attribute_p ("deprecated", name)
26040 && lookup_attribute ("deprecated", attributes))
26041 error ("attribute %<deprecated%> can appear at most once "
26042 "in an attribute-list");
26043 }
26044 }
26045
26046 /* Parse a list of standard C++-11 attributes.
26047
26048 attribute-list:
26049 attribute [opt]
26050 attribute-list , attribute[opt]
26051 attribute ...
26052 attribute-list , attribute ...
26053 */
26054
26055 static tree
26056 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
26057 {
26058 tree attributes = NULL_TREE, attribute = NULL_TREE;
26059 cp_token *token = NULL;
26060
26061 while (true)
26062 {
26063 attribute = cp_parser_std_attribute (parser, attr_ns);
26064 if (attribute == error_mark_node)
26065 break;
26066 if (attribute != NULL_TREE)
26067 {
26068 cp_parser_check_std_attribute (attributes, attribute);
26069 TREE_CHAIN (attribute) = attributes;
26070 attributes = attribute;
26071 }
26072 token = cp_lexer_peek_token (parser->lexer);
26073 if (token->type == CPP_ELLIPSIS)
26074 {
26075 cp_lexer_consume_token (parser->lexer);
26076 if (attribute == NULL_TREE)
26077 error_at (token->location,
26078 "expected attribute before %<...%>");
26079 else
26080 {
26081 tree pack = make_pack_expansion (TREE_VALUE (attribute));
26082 if (pack == error_mark_node)
26083 return error_mark_node;
26084 TREE_VALUE (attribute) = pack;
26085 }
26086 token = cp_lexer_peek_token (parser->lexer);
26087 }
26088 if (token->type != CPP_COMMA)
26089 break;
26090 cp_lexer_consume_token (parser->lexer);
26091 }
26092 attributes = nreverse (attributes);
26093 return attributes;
26094 }
26095
26096 /* Parse a standard C++-11 attribute specifier.
26097
26098 attribute-specifier:
26099 [ [ attribute-using-prefix [opt] attribute-list ] ]
26100 alignment-specifier
26101
26102 attribute-using-prefix:
26103 using attribute-namespace :
26104
26105 alignment-specifier:
26106 alignas ( type-id ... [opt] )
26107 alignas ( alignment-expression ... [opt] ). */
26108
26109 static tree
26110 cp_parser_std_attribute_spec (cp_parser *parser)
26111 {
26112 tree attributes = NULL_TREE;
26113 cp_token *token = cp_lexer_peek_token (parser->lexer);
26114
26115 if (token->type == CPP_OPEN_SQUARE
26116 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
26117 {
26118 tree attr_ns = NULL_TREE;
26119
26120 cp_lexer_consume_token (parser->lexer);
26121 cp_lexer_consume_token (parser->lexer);
26122
26123 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26124 {
26125 token = cp_lexer_peek_nth_token (parser->lexer, 2);
26126 if (token->type == CPP_NAME)
26127 attr_ns = token->u.value;
26128 else if (token->type == CPP_KEYWORD)
26129 attr_ns = ridpointers[(int) token->keyword];
26130 else if (token->flags & NAMED_OP)
26131 attr_ns = get_identifier (cpp_type2name (token->type,
26132 token->flags));
26133 if (attr_ns
26134 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
26135 {
26136 if (cxx_dialect < cxx17
26137 && !in_system_header_at (input_location))
26138 pedwarn (input_location, 0,
26139 "attribute using prefix only available "
26140 "with -std=c++17 or -std=gnu++17");
26141
26142 cp_lexer_consume_token (parser->lexer);
26143 cp_lexer_consume_token (parser->lexer);
26144 cp_lexer_consume_token (parser->lexer);
26145 }
26146 else
26147 attr_ns = NULL_TREE;
26148 }
26149
26150 attributes = cp_parser_std_attribute_list (parser, attr_ns);
26151
26152 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
26153 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
26154 cp_parser_skip_to_end_of_statement (parser);
26155 else
26156 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26157 when we are sure that we have actually parsed them. */
26158 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26159 }
26160 else
26161 {
26162 tree alignas_expr;
26163
26164 /* Look for an alignment-specifier. */
26165
26166 token = cp_lexer_peek_token (parser->lexer);
26167
26168 if (token->type != CPP_KEYWORD
26169 || token->keyword != RID_ALIGNAS)
26170 return NULL_TREE;
26171
26172 cp_lexer_consume_token (parser->lexer);
26173 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26174
26175 matching_parens parens;
26176 if (!parens.require_open (parser))
26177 return error_mark_node;
26178
26179 cp_parser_parse_tentatively (parser);
26180 alignas_expr = cp_parser_type_id (parser);
26181
26182 if (!cp_parser_parse_definitely (parser))
26183 {
26184 alignas_expr = cp_parser_assignment_expression (parser);
26185 if (alignas_expr == error_mark_node)
26186 cp_parser_skip_to_end_of_statement (parser);
26187 if (alignas_expr == NULL_TREE
26188 || alignas_expr == error_mark_node)
26189 return alignas_expr;
26190 }
26191
26192 alignas_expr = cxx_alignas_expr (alignas_expr);
26193 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
26194
26195 /* Handle alignas (pack...). */
26196 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26197 {
26198 cp_lexer_consume_token (parser->lexer);
26199 alignas_expr = make_pack_expansion (alignas_expr);
26200 }
26201
26202 /* Something went wrong, so don't build the attribute. */
26203 if (alignas_expr == error_mark_node)
26204 return error_mark_node;
26205
26206 if (!parens.require_close (parser))
26207 return error_mark_node;
26208
26209 /* Build the C++-11 representation of an 'aligned'
26210 attribute. */
26211 attributes
26212 = build_tree_list (build_tree_list (gnu_identifier,
26213 aligned_identifier), alignas_expr);
26214 }
26215
26216 return attributes;
26217 }
26218
26219 /* Parse a standard C++-11 attribute-specifier-seq.
26220
26221 attribute-specifier-seq:
26222 attribute-specifier-seq [opt] attribute-specifier
26223 */
26224
26225 static tree
26226 cp_parser_std_attribute_spec_seq (cp_parser *parser)
26227 {
26228 tree attr_specs = NULL_TREE;
26229 tree attr_last = NULL_TREE;
26230
26231 /* Don't create wrapper nodes within attributes: the
26232 handlers don't know how to handle them. */
26233 auto_suppress_location_wrappers sentinel;
26234
26235 while (true)
26236 {
26237 tree attr_spec = cp_parser_std_attribute_spec (parser);
26238 if (attr_spec == NULL_TREE)
26239 break;
26240 if (attr_spec == error_mark_node)
26241 return error_mark_node;
26242
26243 if (attr_last)
26244 TREE_CHAIN (attr_last) = attr_spec;
26245 else
26246 attr_specs = attr_last = attr_spec;
26247 attr_last = tree_last (attr_last);
26248 }
26249
26250 return attr_specs;
26251 }
26252
26253 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26254 return index of the first token after balanced-token, or N on failure. */
26255
26256 static size_t
26257 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
26258 {
26259 size_t orig_n = n;
26260 int nparens = 0, nbraces = 0, nsquares = 0;
26261 do
26262 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
26263 {
26264 case CPP_PRAGMA_EOL:
26265 if (!parser->lexer->in_pragma)
26266 break;
26267 /* FALLTHRU */
26268 case CPP_EOF:
26269 /* Ran out of tokens. */
26270 return orig_n;
26271 case CPP_OPEN_PAREN:
26272 ++nparens;
26273 break;
26274 case CPP_OPEN_BRACE:
26275 ++nbraces;
26276 break;
26277 case CPP_OPEN_SQUARE:
26278 ++nsquares;
26279 break;
26280 case CPP_CLOSE_PAREN:
26281 --nparens;
26282 break;
26283 case CPP_CLOSE_BRACE:
26284 --nbraces;
26285 break;
26286 case CPP_CLOSE_SQUARE:
26287 --nsquares;
26288 break;
26289 default:
26290 break;
26291 }
26292 while (nparens || nbraces || nsquares);
26293 return n;
26294 }
26295
26296 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26297 return index of the first token after the GNU attribute tokens, or N on
26298 failure. */
26299
26300 static size_t
26301 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
26302 {
26303 while (true)
26304 {
26305 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
26306 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
26307 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
26308 break;
26309
26310 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
26311 if (n2 == n + 2)
26312 break;
26313 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
26314 break;
26315 n = n2 + 1;
26316 }
26317 return n;
26318 }
26319
26320 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26321 next token), return index of the first token after the standard C++11
26322 attribute tokens, or N on failure. */
26323
26324 static size_t
26325 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
26326 {
26327 while (true)
26328 {
26329 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
26330 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
26331 {
26332 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26333 if (n2 == n + 1)
26334 break;
26335 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
26336 break;
26337 n = n2 + 1;
26338 }
26339 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
26340 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
26341 {
26342 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26343 if (n2 == n + 1)
26344 break;
26345 n = n2;
26346 }
26347 else
26348 break;
26349 }
26350 return n;
26351 }
26352
26353 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26354 as the next token), return index of the first token after the attribute
26355 tokens, or N on failure. */
26356
26357 static size_t
26358 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
26359 {
26360 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
26361 return cp_parser_skip_gnu_attributes_opt (parser, n);
26362 return cp_parser_skip_std_attribute_spec_seq (parser, n);
26363 }
26364
26365 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26366 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26367 current value of the PEDANTIC flag, regardless of whether or not
26368 the `__extension__' keyword is present. The caller is responsible
26369 for restoring the value of the PEDANTIC flag. */
26370
26371 static bool
26372 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
26373 {
26374 /* Save the old value of the PEDANTIC flag. */
26375 *saved_pedantic = pedantic;
26376
26377 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
26378 {
26379 /* Consume the `__extension__' token. */
26380 cp_lexer_consume_token (parser->lexer);
26381 /* We're not being pedantic while the `__extension__' keyword is
26382 in effect. */
26383 pedantic = 0;
26384
26385 return true;
26386 }
26387
26388 return false;
26389 }
26390
26391 /* Parse a label declaration.
26392
26393 label-declaration:
26394 __label__ label-declarator-seq ;
26395
26396 label-declarator-seq:
26397 identifier , label-declarator-seq
26398 identifier */
26399
26400 static void
26401 cp_parser_label_declaration (cp_parser* parser)
26402 {
26403 /* Look for the `__label__' keyword. */
26404 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
26405
26406 while (true)
26407 {
26408 tree identifier;
26409
26410 /* Look for an identifier. */
26411 identifier = cp_parser_identifier (parser);
26412 /* If we failed, stop. */
26413 if (identifier == error_mark_node)
26414 break;
26415 /* Declare it as a label. */
26416 finish_label_decl (identifier);
26417 /* If the next token is a `;', stop. */
26418 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26419 break;
26420 /* Look for the `,' separating the label declarations. */
26421 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
26422 }
26423
26424 /* Look for the final `;'. */
26425 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26426 }
26427
26428 // -------------------------------------------------------------------------- //
26429 // Requires Clause
26430
26431 // Parse a requires clause.
26432 //
26433 // requires-clause:
26434 // 'requires' logical-or-expression
26435 //
26436 // The required logical-or-expression must be a constant expression. Note
26437 // that we don't check that the expression is constepxr here. We defer until
26438 // we analyze constraints and then, we only check atomic constraints.
26439 static tree
26440 cp_parser_requires_clause (cp_parser *parser)
26441 {
26442 // Parse the requires clause so that it is not automatically folded.
26443 ++processing_template_decl;
26444 tree expr = cp_parser_binary_expression (parser, false, false,
26445 PREC_NOT_OPERATOR, NULL);
26446 if (check_for_bare_parameter_packs (expr))
26447 expr = error_mark_node;
26448 --processing_template_decl;
26449 return expr;
26450 }
26451
26452 // Optionally parse a requires clause:
26453 static tree
26454 cp_parser_requires_clause_opt (cp_parser *parser)
26455 {
26456 cp_token *tok = cp_lexer_peek_token (parser->lexer);
26457 if (tok->keyword != RID_REQUIRES)
26458 {
26459 if (!flag_concepts && tok->type == CPP_NAME
26460 && tok->u.value == ridpointers[RID_REQUIRES])
26461 {
26462 error_at (cp_lexer_peek_token (parser->lexer)->location,
26463 "%<requires%> only available with -fconcepts");
26464 /* Parse and discard the requires-clause. */
26465 cp_lexer_consume_token (parser->lexer);
26466 cp_parser_requires_clause (parser);
26467 }
26468 return NULL_TREE;
26469 }
26470 cp_lexer_consume_token (parser->lexer);
26471 return cp_parser_requires_clause (parser);
26472 }
26473
26474
26475 /*---------------------------------------------------------------------------
26476 Requires expressions
26477 ---------------------------------------------------------------------------*/
26478
26479 /* Parse a requires expression
26480
26481 requirement-expression:
26482 'requires' requirement-parameter-list [opt] requirement-body */
26483 static tree
26484 cp_parser_requires_expression (cp_parser *parser)
26485 {
26486 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26487 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26488
26489 /* A requires-expression shall appear only within a concept
26490 definition or a requires-clause.
26491
26492 TODO: Implement this diagnostic correctly. */
26493 if (!processing_template_decl)
26494 {
26495 error_at (loc, "a requires expression cannot appear outside a template");
26496 cp_parser_skip_to_end_of_statement (parser);
26497 return error_mark_node;
26498 }
26499
26500 tree parms, reqs;
26501 {
26502 /* Local parameters are delared as variables within the scope
26503 of the expression. They are not visible past the end of
26504 the expression. Expressions within the requires-expression
26505 are unevaluated. */
26506 struct scope_sentinel
26507 {
26508 scope_sentinel ()
26509 {
26510 ++cp_unevaluated_operand;
26511 begin_scope (sk_block, NULL_TREE);
26512 }
26513
26514 ~scope_sentinel ()
26515 {
26516 pop_bindings_and_leave_scope ();
26517 --cp_unevaluated_operand;
26518 }
26519 } s;
26520
26521 /* Parse the optional parameter list. */
26522 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26523 {
26524 parms = cp_parser_requirement_parameter_list (parser);
26525 if (parms == error_mark_node)
26526 return error_mark_node;
26527 }
26528 else
26529 parms = NULL_TREE;
26530
26531 /* Parse the requirement body. */
26532 reqs = cp_parser_requirement_body (parser);
26533 if (reqs == error_mark_node)
26534 return error_mark_node;
26535 }
26536
26537 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26538 the parm chain. */
26539 grokparms (parms, &parms);
26540 return finish_requires_expr (parms, reqs);
26541 }
26542
26543 /* Parse a parameterized requirement.
26544
26545 requirement-parameter-list:
26546 '(' parameter-declaration-clause ')' */
26547 static tree
26548 cp_parser_requirement_parameter_list (cp_parser *parser)
26549 {
26550 matching_parens parens;
26551 if (!parens.require_open (parser))
26552 return error_mark_node;
26553
26554 tree parms
26555 = cp_parser_parameter_declaration_clause (parser, CP_PARSER_FLAGS_NONE);
26556
26557 if (!parens.require_close (parser))
26558 return error_mark_node;
26559
26560 return parms;
26561 }
26562
26563 /* Parse the body of a requirement.
26564
26565 requirement-body:
26566 '{' requirement-list '}' */
26567 static tree
26568 cp_parser_requirement_body (cp_parser *parser)
26569 {
26570 matching_braces braces;
26571 if (!braces.require_open (parser))
26572 return error_mark_node;
26573
26574 tree reqs = cp_parser_requirement_list (parser);
26575
26576 if (!braces.require_close (parser))
26577 return error_mark_node;
26578
26579 return reqs;
26580 }
26581
26582 /* Parse a list of requirements.
26583
26584 requirement-list:
26585 requirement
26586 requirement-list ';' requirement[opt] */
26587 static tree
26588 cp_parser_requirement_list (cp_parser *parser)
26589 {
26590 tree result = NULL_TREE;
26591 while (true)
26592 {
26593 tree req = cp_parser_requirement (parser);
26594 if (req == error_mark_node)
26595 return error_mark_node;
26596
26597 result = tree_cons (NULL_TREE, req, result);
26598
26599 /* If we see a semi-colon, consume it. */
26600 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26601 cp_lexer_consume_token (parser->lexer);
26602
26603 /* Stop processing at the end of the list. */
26604 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26605 break;
26606 }
26607
26608 /* Reverse the order of requirements so they are analyzed in
26609 declaration order. */
26610 return nreverse (result);
26611 }
26612
26613 /* Parse a syntactic requirement or type requirement.
26614
26615 requirement:
26616 simple-requirement
26617 compound-requirement
26618 type-requirement
26619 nested-requirement */
26620 static tree
26621 cp_parser_requirement (cp_parser *parser)
26622 {
26623 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26624 return cp_parser_compound_requirement (parser);
26625 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26626 return cp_parser_type_requirement (parser);
26627 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26628 return cp_parser_nested_requirement (parser);
26629 else
26630 return cp_parser_simple_requirement (parser);
26631 }
26632
26633 /* Parse a simple requirement.
26634
26635 simple-requirement:
26636 expression ';' */
26637 static tree
26638 cp_parser_simple_requirement (cp_parser *parser)
26639 {
26640 tree expr = cp_parser_expression (parser, NULL, false, false);
26641 if (!expr || expr == error_mark_node)
26642 return error_mark_node;
26643
26644 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26645 return error_mark_node;
26646
26647 return finish_simple_requirement (expr);
26648 }
26649
26650 /* Parse a type requirement
26651
26652 type-requirement
26653 nested-name-specifier [opt] required-type-name ';'
26654
26655 required-type-name:
26656 type-name
26657 'template' [opt] simple-template-id */
26658 static tree
26659 cp_parser_type_requirement (cp_parser *parser)
26660 {
26661 cp_lexer_consume_token (parser->lexer);
26662
26663 // Save the scope before parsing name specifiers.
26664 tree saved_scope = parser->scope;
26665 tree saved_object_scope = parser->object_scope;
26666 tree saved_qualifying_scope = parser->qualifying_scope;
26667 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26668 cp_parser_nested_name_specifier_opt (parser,
26669 /*typename_keyword_p=*/true,
26670 /*check_dependency_p=*/false,
26671 /*type_p=*/true,
26672 /*is_declaration=*/false);
26673
26674 tree type;
26675 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26676 {
26677 cp_lexer_consume_token (parser->lexer);
26678 type = cp_parser_template_id (parser,
26679 /*template_keyword_p=*/true,
26680 /*check_dependency=*/false,
26681 /*tag_type=*/none_type,
26682 /*is_declaration=*/false);
26683 type = make_typename_type (parser->scope, type, typename_type,
26684 /*complain=*/tf_error);
26685 }
26686 else
26687 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26688
26689 if (TREE_CODE (type) == TYPE_DECL)
26690 type = TREE_TYPE (type);
26691
26692 parser->scope = saved_scope;
26693 parser->object_scope = saved_object_scope;
26694 parser->qualifying_scope = saved_qualifying_scope;
26695
26696 if (type == error_mark_node)
26697 cp_parser_skip_to_end_of_statement (parser);
26698
26699 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26700 return error_mark_node;
26701 if (type == error_mark_node)
26702 return error_mark_node;
26703
26704 return finish_type_requirement (type);
26705 }
26706
26707 /* Parse a compound requirement
26708
26709 compound-requirement:
26710 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26711 static tree
26712 cp_parser_compound_requirement (cp_parser *parser)
26713 {
26714 /* Parse an expression enclosed in '{ }'s. */
26715 matching_braces braces;
26716 if (!braces.require_open (parser))
26717 return error_mark_node;
26718
26719 tree expr = cp_parser_expression (parser, NULL, false, false);
26720 if (!expr || expr == error_mark_node)
26721 return error_mark_node;
26722
26723 if (!braces.require_close (parser))
26724 return error_mark_node;
26725
26726 /* Parse the optional noexcept. */
26727 bool noexcept_p = false;
26728 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26729 {
26730 cp_lexer_consume_token (parser->lexer);
26731 noexcept_p = true;
26732 }
26733
26734 /* Parse the optional trailing return type. */
26735 tree type = NULL_TREE;
26736 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26737 {
26738 cp_lexer_consume_token (parser->lexer);
26739 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26740 parser->in_result_type_constraint_p = true;
26741 type = cp_parser_trailing_type_id (parser);
26742 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26743 if (type == error_mark_node)
26744 return error_mark_node;
26745 }
26746
26747 return finish_compound_requirement (expr, type, noexcept_p);
26748 }
26749
26750 /* Parse a nested requirement. This is the same as a requires clause.
26751
26752 nested-requirement:
26753 requires-clause */
26754 static tree
26755 cp_parser_nested_requirement (cp_parser *parser)
26756 {
26757 cp_lexer_consume_token (parser->lexer);
26758 tree req = cp_parser_requires_clause (parser);
26759 if (req == error_mark_node)
26760 return error_mark_node;
26761 return finish_nested_requirement (req);
26762 }
26763
26764 /* Support Functions */
26765
26766 /* Return the appropriate prefer_type argument for lookup_name_real based on
26767 tag_type and template_mem_access. */
26768
26769 static inline int
26770 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26771 {
26772 /* DR 141: When looking in the current enclosing context for a template-name
26773 after -> or ., only consider class templates. */
26774 if (template_mem_access)
26775 return 2;
26776 switch (tag_type)
26777 {
26778 case none_type: return 0; // No preference.
26779 case scope_type: return 1; // Type or namespace.
26780 default: return 2; // Type only.
26781 }
26782 }
26783
26784 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26785 NAME should have one of the representations used for an
26786 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26787 is returned. If PARSER->SCOPE is a dependent type, then a
26788 SCOPE_REF is returned.
26789
26790 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26791 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26792 was formed. Abstractly, such entities should not be passed to this
26793 function, because they do not need to be looked up, but it is
26794 simpler to check for this special case here, rather than at the
26795 call-sites.
26796
26797 In cases not explicitly covered above, this function returns a
26798 DECL, OVERLOAD, or baselink representing the result of the lookup.
26799 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26800 is returned.
26801
26802 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26803 (e.g., "struct") that was used. In that case bindings that do not
26804 refer to types are ignored.
26805
26806 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26807 ignored.
26808
26809 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26810 are ignored.
26811
26812 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26813 types.
26814
26815 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26816 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26817 NULL_TREE otherwise. */
26818
26819 static cp_expr
26820 cp_parser_lookup_name (cp_parser *parser, tree name,
26821 enum tag_types tag_type,
26822 bool is_template,
26823 bool is_namespace,
26824 bool check_dependency,
26825 tree *ambiguous_decls,
26826 location_t name_location)
26827 {
26828 tree decl;
26829 tree object_type = parser->context->object_type;
26830
26831 /* Assume that the lookup will be unambiguous. */
26832 if (ambiguous_decls)
26833 *ambiguous_decls = NULL_TREE;
26834
26835 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26836 no longer valid. Note that if we are parsing tentatively, and
26837 the parse fails, OBJECT_TYPE will be automatically restored. */
26838 parser->context->object_type = NULL_TREE;
26839
26840 if (name == error_mark_node)
26841 return error_mark_node;
26842
26843 /* A template-id has already been resolved; there is no lookup to
26844 do. */
26845 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26846 return name;
26847 if (BASELINK_P (name))
26848 {
26849 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26850 == TEMPLATE_ID_EXPR);
26851 return name;
26852 }
26853
26854 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26855 it should already have been checked to make sure that the name
26856 used matches the type being destroyed. */
26857 if (TREE_CODE (name) == BIT_NOT_EXPR)
26858 {
26859 tree type;
26860
26861 /* Figure out to which type this destructor applies. */
26862 if (parser->scope)
26863 type = parser->scope;
26864 else if (object_type)
26865 type = object_type;
26866 else
26867 type = current_class_type;
26868 /* If that's not a class type, there is no destructor. */
26869 if (!type || !CLASS_TYPE_P (type))
26870 return error_mark_node;
26871
26872 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26873 lazily_declare_fn (sfk_destructor, type);
26874
26875 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26876 return dtor;
26877
26878 return error_mark_node;
26879 }
26880
26881 /* By this point, the NAME should be an ordinary identifier. If
26882 the id-expression was a qualified name, the qualifying scope is
26883 stored in PARSER->SCOPE at this point. */
26884 gcc_assert (identifier_p (name));
26885
26886 /* Perform the lookup. */
26887 if (parser->scope)
26888 {
26889 bool dependent_p;
26890
26891 if (parser->scope == error_mark_node)
26892 return error_mark_node;
26893
26894 /* If the SCOPE is dependent, the lookup must be deferred until
26895 the template is instantiated -- unless we are explicitly
26896 looking up names in uninstantiated templates. Even then, we
26897 cannot look up the name if the scope is not a class type; it
26898 might, for example, be a template type parameter. */
26899 dependent_p = (TYPE_P (parser->scope)
26900 && dependent_scope_p (parser->scope));
26901 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26902 && dependent_p)
26903 /* Defer lookup. */
26904 decl = error_mark_node;
26905 else
26906 {
26907 tree pushed_scope = NULL_TREE;
26908
26909 /* If PARSER->SCOPE is a dependent type, then it must be a
26910 class type, and we must not be checking dependencies;
26911 otherwise, we would have processed this lookup above. So
26912 that PARSER->SCOPE is not considered a dependent base by
26913 lookup_member, we must enter the scope here. */
26914 if (dependent_p)
26915 pushed_scope = push_scope (parser->scope);
26916
26917 /* If the PARSER->SCOPE is a template specialization, it
26918 may be instantiated during name lookup. In that case,
26919 errors may be issued. Even if we rollback the current
26920 tentative parse, those errors are valid. */
26921 decl = lookup_qualified_name (parser->scope, name,
26922 prefer_type_arg (tag_type),
26923 /*complain=*/true);
26924
26925 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26926 lookup result and the nested-name-specifier nominates a class C:
26927 * if the name specified after the nested-name-specifier, when
26928 looked up in C, is the injected-class-name of C (Clause 9), or
26929 * if the name specified after the nested-name-specifier is the
26930 same as the identifier or the simple-template-id's template-
26931 name in the last component of the nested-name-specifier,
26932 the name is instead considered to name the constructor of
26933 class C. [ Note: for example, the constructor is not an
26934 acceptable lookup result in an elaborated-type-specifier so
26935 the constructor would not be used in place of the
26936 injected-class-name. --end note ] Such a constructor name
26937 shall be used only in the declarator-id of a declaration that
26938 names a constructor or in a using-declaration. */
26939 if (tag_type == none_type
26940 && DECL_SELF_REFERENCE_P (decl)
26941 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26942 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26943 prefer_type_arg (tag_type),
26944 /*complain=*/true);
26945
26946 /* If we have a single function from a using decl, pull it out. */
26947 if (TREE_CODE (decl) == OVERLOAD
26948 && !really_overloaded_fn (decl))
26949 decl = OVL_FUNCTION (decl);
26950
26951 if (pushed_scope)
26952 pop_scope (pushed_scope);
26953 }
26954
26955 /* If the scope is a dependent type and either we deferred lookup or
26956 we did lookup but didn't find the name, rememeber the name. */
26957 if (decl == error_mark_node && TYPE_P (parser->scope)
26958 && dependent_type_p (parser->scope))
26959 {
26960 if (tag_type)
26961 {
26962 tree type;
26963
26964 /* The resolution to Core Issue 180 says that `struct
26965 A::B' should be considered a type-name, even if `A'
26966 is dependent. */
26967 type = make_typename_type (parser->scope, name, tag_type,
26968 /*complain=*/tf_error);
26969 if (type != error_mark_node)
26970 decl = TYPE_NAME (type);
26971 }
26972 else if (is_template
26973 && (cp_parser_next_token_ends_template_argument_p (parser)
26974 || cp_lexer_next_token_is (parser->lexer,
26975 CPP_CLOSE_PAREN)))
26976 decl = make_unbound_class_template (parser->scope,
26977 name, NULL_TREE,
26978 /*complain=*/tf_error);
26979 else
26980 decl = build_qualified_name (/*type=*/NULL_TREE,
26981 parser->scope, name,
26982 is_template);
26983 }
26984 parser->qualifying_scope = parser->scope;
26985 parser->object_scope = NULL_TREE;
26986 }
26987 else if (object_type)
26988 {
26989 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26990 OBJECT_TYPE is not a class. */
26991 if (CLASS_TYPE_P (object_type))
26992 /* If the OBJECT_TYPE is a template specialization, it may
26993 be instantiated during name lookup. In that case, errors
26994 may be issued. Even if we rollback the current tentative
26995 parse, those errors are valid. */
26996 decl = lookup_member (object_type,
26997 name,
26998 /*protect=*/0,
26999 prefer_type_arg (tag_type),
27000 tf_warning_or_error);
27001 else
27002 decl = NULL_TREE;
27003
27004 if (!decl)
27005 /* Look it up in the enclosing context. DR 141: When looking for a
27006 template-name after -> or ., only consider class templates. */
27007 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
27008 /*nonclass=*/0,
27009 /*block_p=*/true, is_namespace, 0);
27010 if (object_type == unknown_type_node)
27011 /* The object is type-dependent, so we can't look anything up; we used
27012 this to get the DR 141 behavior. */
27013 object_type = NULL_TREE;
27014 parser->object_scope = object_type;
27015 parser->qualifying_scope = NULL_TREE;
27016 }
27017 else
27018 {
27019 decl = lookup_name_real (name, prefer_type_arg (tag_type),
27020 /*nonclass=*/0,
27021 /*block_p=*/true, is_namespace, 0);
27022 parser->qualifying_scope = NULL_TREE;
27023 parser->object_scope = NULL_TREE;
27024 }
27025
27026 /* If the lookup failed, let our caller know. */
27027 if (!decl || decl == error_mark_node)
27028 return error_mark_node;
27029
27030 /* Pull out the template from an injected-class-name (or multiple). */
27031 if (is_template)
27032 decl = maybe_get_template_decl_from_type_decl (decl);
27033
27034 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
27035 if (TREE_CODE (decl) == TREE_LIST)
27036 {
27037 if (ambiguous_decls)
27038 *ambiguous_decls = decl;
27039 /* The error message we have to print is too complicated for
27040 cp_parser_error, so we incorporate its actions directly. */
27041 if (!cp_parser_simulate_error (parser))
27042 {
27043 error_at (name_location, "reference to %qD is ambiguous",
27044 name);
27045 print_candidates (decl);
27046 }
27047 return error_mark_node;
27048 }
27049
27050 gcc_assert (DECL_P (decl)
27051 || TREE_CODE (decl) == OVERLOAD
27052 || TREE_CODE (decl) == SCOPE_REF
27053 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
27054 || BASELINK_P (decl));
27055
27056 /* If we have resolved the name of a member declaration, check to
27057 see if the declaration is accessible. When the name resolves to
27058 set of overloaded functions, accessibility is checked when
27059 overload resolution is done.
27060
27061 During an explicit instantiation, access is not checked at all,
27062 as per [temp.explicit]. */
27063 if (DECL_P (decl))
27064 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
27065
27066 maybe_record_typedef_use (decl);
27067
27068 return cp_expr (decl, name_location);
27069 }
27070
27071 /* Like cp_parser_lookup_name, but for use in the typical case where
27072 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
27073 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
27074
27075 static tree
27076 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
27077 {
27078 return cp_parser_lookup_name (parser, name,
27079 none_type,
27080 /*is_template=*/false,
27081 /*is_namespace=*/false,
27082 /*check_dependency=*/true,
27083 /*ambiguous_decls=*/NULL,
27084 location);
27085 }
27086
27087 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
27088 the current context, return the TYPE_DECL. If TAG_NAME_P is
27089 true, the DECL indicates the class being defined in a class-head,
27090 or declared in an elaborated-type-specifier.
27091
27092 Otherwise, return DECL. */
27093
27094 static tree
27095 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
27096 {
27097 /* If the TEMPLATE_DECL is being declared as part of a class-head,
27098 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
27099
27100 struct A {
27101 template <typename T> struct B;
27102 };
27103
27104 template <typename T> struct A::B {};
27105
27106 Similarly, in an elaborated-type-specifier:
27107
27108 namespace N { struct X{}; }
27109
27110 struct A {
27111 template <typename T> friend struct N::X;
27112 };
27113
27114 However, if the DECL refers to a class type, and we are in
27115 the scope of the class, then the name lookup automatically
27116 finds the TYPE_DECL created by build_self_reference rather
27117 than a TEMPLATE_DECL. For example, in:
27118
27119 template <class T> struct S {
27120 S s;
27121 };
27122
27123 there is no need to handle such case. */
27124
27125 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
27126 return DECL_TEMPLATE_RESULT (decl);
27127
27128 return decl;
27129 }
27130
27131 /* If too many, or too few, template-parameter lists apply to the
27132 declarator, issue an error message. Returns TRUE if all went well,
27133 and FALSE otherwise. */
27134
27135 static bool
27136 cp_parser_check_declarator_template_parameters (cp_parser* parser,
27137 cp_declarator *declarator,
27138 location_t declarator_location)
27139 {
27140 switch (declarator->kind)
27141 {
27142 case cdk_id:
27143 {
27144 unsigned num_templates = 0;
27145 tree scope = declarator->u.id.qualifying_scope;
27146 bool template_id_p = false;
27147
27148 if (scope)
27149 num_templates = num_template_headers_for_class (scope);
27150 else if (TREE_CODE (declarator->u.id.unqualified_name)
27151 == TEMPLATE_ID_EXPR)
27152 {
27153 /* If the DECLARATOR has the form `X<y>' then it uses one
27154 additional level of template parameters. */
27155 ++num_templates;
27156 template_id_p = true;
27157 }
27158
27159 return cp_parser_check_template_parameters
27160 (parser, num_templates, template_id_p, declarator_location,
27161 declarator);
27162 }
27163
27164 case cdk_function:
27165 case cdk_array:
27166 case cdk_pointer:
27167 case cdk_reference:
27168 case cdk_ptrmem:
27169 return (cp_parser_check_declarator_template_parameters
27170 (parser, declarator->declarator, declarator_location));
27171
27172 case cdk_decomp:
27173 case cdk_error:
27174 return true;
27175
27176 default:
27177 gcc_unreachable ();
27178 }
27179 return false;
27180 }
27181
27182 /* NUM_TEMPLATES were used in the current declaration. If that is
27183 invalid, return FALSE and issue an error messages. Otherwise,
27184 return TRUE. If DECLARATOR is non-NULL, then we are checking a
27185 declarator and we can print more accurate diagnostics. */
27186
27187 static bool
27188 cp_parser_check_template_parameters (cp_parser* parser,
27189 unsigned num_templates,
27190 bool template_id_p,
27191 location_t location,
27192 cp_declarator *declarator)
27193 {
27194 /* If there are the same number of template classes and parameter
27195 lists, that's OK. */
27196 if (parser->num_template_parameter_lists == num_templates)
27197 return true;
27198 /* If there are more, but only one more, and the name ends in an identifier,
27199 then we are declaring a primary template. That's OK too. */
27200 if (!template_id_p
27201 && parser->num_template_parameter_lists == num_templates + 1)
27202 return true;
27203 /* If there are more template classes than parameter lists, we have
27204 something like:
27205
27206 template <class T> void S<T>::R<T>::f (); */
27207 if (parser->num_template_parameter_lists < num_templates)
27208 {
27209 if (declarator && !current_function_decl)
27210 error_at (location, "specializing member %<%T::%E%> "
27211 "requires %<template<>%> syntax",
27212 declarator->u.id.qualifying_scope,
27213 declarator->u.id.unqualified_name);
27214 else if (declarator)
27215 error_at (location, "invalid declaration of %<%T::%E%>",
27216 declarator->u.id.qualifying_scope,
27217 declarator->u.id.unqualified_name);
27218 else
27219 error_at (location, "too few template-parameter-lists");
27220 return false;
27221 }
27222 /* Otherwise, there are too many template parameter lists. We have
27223 something like:
27224
27225 template <class T> template <class U> void S::f(); */
27226 error_at (location, "too many template-parameter-lists");
27227 return false;
27228 }
27229
27230 /* Parse an optional `::' token indicating that the following name is
27231 from the global namespace. If so, PARSER->SCOPE is set to the
27232 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
27233 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
27234 Returns the new value of PARSER->SCOPE, if the `::' token is
27235 present, and NULL_TREE otherwise. */
27236
27237 static tree
27238 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
27239 {
27240 cp_token *token;
27241
27242 /* Peek at the next token. */
27243 token = cp_lexer_peek_token (parser->lexer);
27244 /* If we're looking at a `::' token then we're starting from the
27245 global namespace, not our current location. */
27246 if (token->type == CPP_SCOPE)
27247 {
27248 /* Consume the `::' token. */
27249 cp_lexer_consume_token (parser->lexer);
27250 /* Set the SCOPE so that we know where to start the lookup. */
27251 parser->scope = global_namespace;
27252 parser->qualifying_scope = global_namespace;
27253 parser->object_scope = NULL_TREE;
27254
27255 return parser->scope;
27256 }
27257 else if (!current_scope_valid_p)
27258 {
27259 parser->scope = NULL_TREE;
27260 parser->qualifying_scope = NULL_TREE;
27261 parser->object_scope = NULL_TREE;
27262 }
27263
27264 return NULL_TREE;
27265 }
27266
27267 /* Returns TRUE if the upcoming token sequence is the start of a
27268 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27269 declarator is preceded by the `friend' specifier. */
27270
27271 static bool
27272 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
27273 {
27274 bool constructor_p;
27275 bool outside_class_specifier_p;
27276 tree nested_name_specifier;
27277 cp_token *next_token;
27278
27279 /* The common case is that this is not a constructor declarator, so
27280 try to avoid doing lots of work if at all possible. It's not
27281 valid declare a constructor at function scope. */
27282 if (parser->in_function_body)
27283 return false;
27284 /* And only certain tokens can begin a constructor declarator. */
27285 next_token = cp_lexer_peek_token (parser->lexer);
27286 if (next_token->type != CPP_NAME
27287 && next_token->type != CPP_SCOPE
27288 && next_token->type != CPP_NESTED_NAME_SPECIFIER
27289 && next_token->type != CPP_TEMPLATE_ID)
27290 return false;
27291
27292 /* Parse tentatively; we are going to roll back all of the tokens
27293 consumed here. */
27294 cp_parser_parse_tentatively (parser);
27295 /* Assume that we are looking at a constructor declarator. */
27296 constructor_p = true;
27297
27298 /* Look for the optional `::' operator. */
27299 cp_parser_global_scope_opt (parser,
27300 /*current_scope_valid_p=*/false);
27301 /* Look for the nested-name-specifier. */
27302 nested_name_specifier
27303 = (cp_parser_nested_name_specifier_opt (parser,
27304 /*typename_keyword_p=*/false,
27305 /*check_dependency_p=*/false,
27306 /*type_p=*/false,
27307 /*is_declaration=*/false));
27308
27309 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
27310 if (nested_name_specifier
27311 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
27312 {
27313 tree s = resolve_typename_type (nested_name_specifier,
27314 /*only_current_p=*/false);
27315 if (TREE_CODE (s) != TYPENAME_TYPE)
27316 nested_name_specifier = s;
27317 }
27318
27319 outside_class_specifier_p = (!at_class_scope_p ()
27320 || !TYPE_BEING_DEFINED (current_class_type)
27321 || friend_p);
27322
27323 /* Outside of a class-specifier, there must be a
27324 nested-name-specifier. Except in C++17 mode, where we
27325 might be declaring a guiding declaration. */
27326 if (!nested_name_specifier && outside_class_specifier_p
27327 && cxx_dialect < cxx17)
27328 constructor_p = false;
27329 else if (nested_name_specifier == error_mark_node)
27330 constructor_p = false;
27331
27332 /* If we have a class scope, this is easy; DR 147 says that S::S always
27333 names the constructor, and no other qualified name could. */
27334 if (constructor_p && nested_name_specifier
27335 && CLASS_TYPE_P (nested_name_specifier))
27336 {
27337 tree id = cp_parser_unqualified_id (parser,
27338 /*template_keyword_p=*/false,
27339 /*check_dependency_p=*/false,
27340 /*declarator_p=*/true,
27341 /*optional_p=*/false);
27342 if (is_overloaded_fn (id))
27343 id = DECL_NAME (get_first_fn (id));
27344 if (!constructor_name_p (id, nested_name_specifier))
27345 constructor_p = false;
27346 }
27347 /* If we still think that this might be a constructor-declarator,
27348 look for a class-name. */
27349 else if (constructor_p)
27350 {
27351 /* If we have:
27352
27353 template <typename T> struct S {
27354 S();
27355 };
27356
27357 we must recognize that the nested `S' names a class. */
27358 if (cxx_dialect >= cxx17)
27359 cp_parser_parse_tentatively (parser);
27360
27361 tree type_decl;
27362 type_decl = cp_parser_class_name (parser,
27363 /*typename_keyword_p=*/false,
27364 /*template_keyword_p=*/false,
27365 none_type,
27366 /*check_dependency_p=*/false,
27367 /*class_head_p=*/false,
27368 /*is_declaration=*/false);
27369
27370 if (cxx_dialect >= cxx17
27371 && !cp_parser_parse_definitely (parser))
27372 {
27373 type_decl = NULL_TREE;
27374 tree tmpl = cp_parser_template_name (parser,
27375 /*template_keyword*/false,
27376 /*check_dependency_p*/false,
27377 /*is_declaration*/false,
27378 none_type,
27379 /*is_identifier*/NULL);
27380 if (DECL_CLASS_TEMPLATE_P (tmpl)
27381 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27382 /* It's a deduction guide, return true. */;
27383 else
27384 cp_parser_simulate_error (parser);
27385 }
27386
27387 /* If there was no class-name, then this is not a constructor.
27388 Otherwise, if we are in a class-specifier and we aren't
27389 handling a friend declaration, check that its type matches
27390 current_class_type (c++/38313). Note: error_mark_node
27391 is left alone for error recovery purposes. */
27392 constructor_p = (!cp_parser_error_occurred (parser)
27393 && (outside_class_specifier_p
27394 || type_decl == NULL_TREE
27395 || type_decl == error_mark_node
27396 || same_type_p (current_class_type,
27397 TREE_TYPE (type_decl))));
27398
27399 /* If we're still considering a constructor, we have to see a `(',
27400 to begin the parameter-declaration-clause, followed by either a
27401 `)', an `...', or a decl-specifier. We need to check for a
27402 type-specifier to avoid being fooled into thinking that:
27403
27404 S (f) (int);
27405
27406 is a constructor. (It is actually a function named `f' that
27407 takes one parameter (of type `int') and returns a value of type
27408 `S'. */
27409 if (constructor_p
27410 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27411 constructor_p = false;
27412
27413 if (constructor_p
27414 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
27415 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
27416 /* A parameter declaration begins with a decl-specifier,
27417 which is either the "attribute" keyword, a storage class
27418 specifier, or (usually) a type-specifier. */
27419 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
27420 {
27421 tree type;
27422 tree pushed_scope = NULL_TREE;
27423 unsigned saved_num_template_parameter_lists;
27424
27425 /* Names appearing in the type-specifier should be looked up
27426 in the scope of the class. */
27427 if (current_class_type)
27428 type = NULL_TREE;
27429 else if (type_decl)
27430 {
27431 type = TREE_TYPE (type_decl);
27432 if (TREE_CODE (type) == TYPENAME_TYPE)
27433 {
27434 type = resolve_typename_type (type,
27435 /*only_current_p=*/false);
27436 if (TREE_CODE (type) == TYPENAME_TYPE)
27437 {
27438 cp_parser_abort_tentative_parse (parser);
27439 return false;
27440 }
27441 }
27442 pushed_scope = push_scope (type);
27443 }
27444
27445 /* Inside the constructor parameter list, surrounding
27446 template-parameter-lists do not apply. */
27447 saved_num_template_parameter_lists
27448 = parser->num_template_parameter_lists;
27449 parser->num_template_parameter_lists = 0;
27450
27451 /* Look for the type-specifier. */
27452 cp_parser_type_specifier (parser,
27453 CP_PARSER_FLAGS_NONE,
27454 /*decl_specs=*/NULL,
27455 /*is_declarator=*/true,
27456 /*declares_class_or_enum=*/NULL,
27457 /*is_cv_qualifier=*/NULL);
27458
27459 parser->num_template_parameter_lists
27460 = saved_num_template_parameter_lists;
27461
27462 /* Leave the scope of the class. */
27463 if (pushed_scope)
27464 pop_scope (pushed_scope);
27465
27466 constructor_p = !cp_parser_error_occurred (parser);
27467 }
27468 }
27469
27470 /* We did not really want to consume any tokens. */
27471 cp_parser_abort_tentative_parse (parser);
27472
27473 return constructor_p;
27474 }
27475
27476 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27477 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27478 they must be performed once we are in the scope of the function.
27479
27480 Returns the function defined. */
27481
27482 static tree
27483 cp_parser_function_definition_from_specifiers_and_declarator
27484 (cp_parser* parser,
27485 cp_decl_specifier_seq *decl_specifiers,
27486 tree attributes,
27487 const cp_declarator *declarator)
27488 {
27489 tree fn;
27490 bool success_p;
27491
27492 /* Begin the function-definition. */
27493 success_p = start_function (decl_specifiers, declarator, attributes);
27494
27495 /* The things we're about to see are not directly qualified by any
27496 template headers we've seen thus far. */
27497 reset_specialization ();
27498
27499 /* If there were names looked up in the decl-specifier-seq that we
27500 did not check, check them now. We must wait until we are in the
27501 scope of the function to perform the checks, since the function
27502 might be a friend. */
27503 perform_deferred_access_checks (tf_warning_or_error);
27504
27505 if (success_p)
27506 {
27507 cp_finalize_omp_declare_simd (parser, current_function_decl);
27508 parser->omp_declare_simd = NULL;
27509 cp_finalize_oacc_routine (parser, current_function_decl, true);
27510 parser->oacc_routine = NULL;
27511 }
27512
27513 if (!success_p)
27514 {
27515 /* Skip the entire function. */
27516 cp_parser_skip_to_end_of_block_or_statement (parser);
27517 fn = error_mark_node;
27518 }
27519 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27520 {
27521 /* Seen already, skip it. An error message has already been output. */
27522 cp_parser_skip_to_end_of_block_or_statement (parser);
27523 fn = current_function_decl;
27524 current_function_decl = NULL_TREE;
27525 /* If this is a function from a class, pop the nested class. */
27526 if (current_class_name)
27527 pop_nested_class ();
27528 }
27529 else
27530 {
27531 timevar_id_t tv;
27532 if (DECL_DECLARED_INLINE_P (current_function_decl))
27533 tv = TV_PARSE_INLINE;
27534 else
27535 tv = TV_PARSE_FUNC;
27536 timevar_push (tv);
27537 fn = cp_parser_function_definition_after_declarator (parser,
27538 /*inline_p=*/false);
27539 timevar_pop (tv);
27540 }
27541
27542 return fn;
27543 }
27544
27545 /* Parse the part of a function-definition that follows the
27546 declarator. INLINE_P is TRUE iff this function is an inline
27547 function defined within a class-specifier.
27548
27549 Returns the function defined. */
27550
27551 static tree
27552 cp_parser_function_definition_after_declarator (cp_parser* parser,
27553 bool inline_p)
27554 {
27555 tree fn;
27556 bool saved_in_unbraced_linkage_specification_p;
27557 bool saved_in_function_body;
27558 unsigned saved_num_template_parameter_lists;
27559 cp_token *token;
27560 bool fully_implicit_function_template_p
27561 = parser->fully_implicit_function_template_p;
27562 parser->fully_implicit_function_template_p = false;
27563 tree implicit_template_parms
27564 = parser->implicit_template_parms;
27565 parser->implicit_template_parms = 0;
27566 cp_binding_level* implicit_template_scope
27567 = parser->implicit_template_scope;
27568 parser->implicit_template_scope = 0;
27569
27570 saved_in_function_body = parser->in_function_body;
27571 parser->in_function_body = true;
27572 /* If the next token is `return', then the code may be trying to
27573 make use of the "named return value" extension that G++ used to
27574 support. */
27575 token = cp_lexer_peek_token (parser->lexer);
27576 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27577 {
27578 /* Consume the `return' keyword. */
27579 cp_lexer_consume_token (parser->lexer);
27580 /* Look for the identifier that indicates what value is to be
27581 returned. */
27582 cp_parser_identifier (parser);
27583 /* Issue an error message. */
27584 error_at (token->location,
27585 "named return values are no longer supported");
27586 /* Skip tokens until we reach the start of the function body. */
27587 while (true)
27588 {
27589 cp_token *token = cp_lexer_peek_token (parser->lexer);
27590 if (token->type == CPP_OPEN_BRACE
27591 || token->type == CPP_EOF
27592 || token->type == CPP_PRAGMA_EOL)
27593 break;
27594 cp_lexer_consume_token (parser->lexer);
27595 }
27596 }
27597 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27598 anything declared inside `f'. */
27599 saved_in_unbraced_linkage_specification_p
27600 = parser->in_unbraced_linkage_specification_p;
27601 parser->in_unbraced_linkage_specification_p = false;
27602 /* Inside the function, surrounding template-parameter-lists do not
27603 apply. */
27604 saved_num_template_parameter_lists
27605 = parser->num_template_parameter_lists;
27606 parser->num_template_parameter_lists = 0;
27607
27608 /* If the next token is `try', `__transaction_atomic', or
27609 `__transaction_relaxed`, then we are looking at either function-try-block
27610 or function-transaction-block. Note that all of these include the
27611 function-body. */
27612 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27613 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27614 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27615 RID_TRANSACTION_RELAXED))
27616 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27617 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27618 cp_parser_function_try_block (parser);
27619 else
27620 cp_parser_ctor_initializer_opt_and_function_body
27621 (parser, /*in_function_try_block=*/false);
27622
27623 /* Finish the function. */
27624 fn = finish_function (inline_p);
27625 /* Generate code for it, if necessary. */
27626 expand_or_defer_fn (fn);
27627 /* Restore the saved values. */
27628 parser->in_unbraced_linkage_specification_p
27629 = saved_in_unbraced_linkage_specification_p;
27630 parser->num_template_parameter_lists
27631 = saved_num_template_parameter_lists;
27632 parser->in_function_body = saved_in_function_body;
27633
27634 parser->fully_implicit_function_template_p
27635 = fully_implicit_function_template_p;
27636 parser->implicit_template_parms
27637 = implicit_template_parms;
27638 parser->implicit_template_scope
27639 = implicit_template_scope;
27640
27641 if (parser->fully_implicit_function_template_p)
27642 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27643
27644 return fn;
27645 }
27646
27647 /* Parse a template-declaration body (following argument list). */
27648
27649 static void
27650 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27651 tree parameter_list,
27652 bool member_p)
27653 {
27654 tree decl = NULL_TREE;
27655 bool friend_p = false;
27656
27657 /* We just processed one more parameter list. */
27658 ++parser->num_template_parameter_lists;
27659
27660 /* Get the deferred access checks from the parameter list. These
27661 will be checked once we know what is being declared, as for a
27662 member template the checks must be performed in the scope of the
27663 class containing the member. */
27664 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27665
27666 /* Tentatively parse for a new template parameter list, which can either be
27667 the template keyword or a template introduction. */
27668 if (cp_parser_template_declaration_after_export (parser, member_p))
27669 /* OK */;
27670 else if (cxx_dialect >= cxx11
27671 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27672 decl = cp_parser_alias_declaration (parser);
27673 else
27674 {
27675 /* There are no access checks when parsing a template, as we do not
27676 know if a specialization will be a friend. */
27677 push_deferring_access_checks (dk_no_check);
27678 cp_token *token = cp_lexer_peek_token (parser->lexer);
27679 decl = cp_parser_single_declaration (parser,
27680 checks,
27681 member_p,
27682 /*explicit_specialization_p=*/false,
27683 &friend_p);
27684 pop_deferring_access_checks ();
27685
27686 /* If this is a member template declaration, let the front
27687 end know. */
27688 if (member_p && !friend_p && decl)
27689 {
27690 if (TREE_CODE (decl) == TYPE_DECL)
27691 cp_parser_check_access_in_redeclaration (decl, token->location);
27692
27693 decl = finish_member_template_decl (decl);
27694 }
27695 else if (friend_p && decl
27696 && DECL_DECLARES_TYPE_P (decl))
27697 make_friend_class (current_class_type, TREE_TYPE (decl),
27698 /*complain=*/true);
27699 }
27700 /* We are done with the current parameter list. */
27701 --parser->num_template_parameter_lists;
27702
27703 pop_deferring_access_checks ();
27704
27705 /* Finish up. */
27706 finish_template_decl (parameter_list);
27707
27708 /* Check the template arguments for a literal operator template. */
27709 if (decl
27710 && DECL_DECLARES_FUNCTION_P (decl)
27711 && UDLIT_OPER_P (DECL_NAME (decl)))
27712 {
27713 bool ok = true;
27714 if (parameter_list == NULL_TREE)
27715 ok = false;
27716 else
27717 {
27718 int num_parms = TREE_VEC_LENGTH (parameter_list);
27719 if (num_parms == 1)
27720 {
27721 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27722 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27723 if (CLASS_TYPE_P (TREE_TYPE (parm)))
27724 /* OK, C++20 string literal operator template. We don't need
27725 to warn in lower dialects here because we will have already
27726 warned about the template parameter. */;
27727 else if (TREE_TYPE (parm) != char_type_node
27728 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27729 ok = false;
27730 }
27731 else if (num_parms == 2 && cxx_dialect >= cxx14)
27732 {
27733 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27734 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27735 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27736 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27737 if (parm == error_mark_node
27738 || TREE_TYPE (parm) != TREE_TYPE (type)
27739 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27740 ok = false;
27741 else
27742 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27743 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
27744 "ISO C++ did not adopt string literal operator templa"
27745 "tes taking an argument pack of characters");
27746 }
27747 else
27748 ok = false;
27749 }
27750 if (!ok)
27751 {
27752 if (cxx_dialect > cxx17)
27753 error ("literal operator template %qD has invalid parameter list;"
27754 " Expected non-type template parameter pack <char...> "
27755 " or single non-type parameter of class type",
27756 decl);
27757 else
27758 error ("literal operator template %qD has invalid parameter list."
27759 " Expected non-type template parameter pack <char...>",
27760 decl);
27761 }
27762 }
27763
27764 /* Register member declarations. */
27765 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27766 finish_member_declaration (decl);
27767 /* If DECL is a function template, we must return to parse it later.
27768 (Even though there is no definition, there might be default
27769 arguments that need handling.) */
27770 if (member_p && decl
27771 && DECL_DECLARES_FUNCTION_P (decl))
27772 vec_safe_push (unparsed_funs_with_definitions, decl);
27773 }
27774
27775 /* Parse a template introduction header for a template-declaration. Returns
27776 false if tentative parse fails. */
27777
27778 static bool
27779 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27780 {
27781 cp_parser_parse_tentatively (parser);
27782
27783 tree saved_scope = parser->scope;
27784 tree saved_object_scope = parser->object_scope;
27785 tree saved_qualifying_scope = parser->qualifying_scope;
27786
27787 /* Look for the optional `::' operator. */
27788 cp_parser_global_scope_opt (parser,
27789 /*current_scope_valid_p=*/false);
27790 /* Look for the nested-name-specifier. */
27791 cp_parser_nested_name_specifier_opt (parser,
27792 /*typename_keyword_p=*/false,
27793 /*check_dependency_p=*/true,
27794 /*type_p=*/false,
27795 /*is_declaration=*/false);
27796
27797 cp_token *token = cp_lexer_peek_token (parser->lexer);
27798 tree concept_name = cp_parser_identifier (parser);
27799
27800 /* Look up the concept for which we will be matching
27801 template parameters. */
27802 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27803 token->location);
27804 parser->scope = saved_scope;
27805 parser->object_scope = saved_object_scope;
27806 parser->qualifying_scope = saved_qualifying_scope;
27807
27808 if (concept_name == error_mark_node)
27809 cp_parser_simulate_error (parser);
27810
27811 /* Look for opening brace for introduction. */
27812 matching_braces braces;
27813 braces.require_open (parser);
27814
27815 if (!cp_parser_parse_definitely (parser))
27816 return false;
27817
27818 push_deferring_access_checks (dk_deferred);
27819
27820 /* Build vector of placeholder parameters and grab
27821 matching identifiers. */
27822 tree introduction_list = cp_parser_introduction_list (parser);
27823
27824 /* Look for closing brace for introduction. */
27825 if (!braces.require_close (parser))
27826 return true;
27827
27828 /* The introduction-list shall not be empty. */
27829 int nargs = TREE_VEC_LENGTH (introduction_list);
27830 if (nargs == 0)
27831 {
27832 /* In cp_parser_introduction_list we have already issued an error. */
27833 return true;
27834 }
27835
27836 if (tmpl_decl == error_mark_node)
27837 {
27838 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27839 token->location);
27840 return true;
27841 }
27842
27843 /* Build and associate the constraint. */
27844 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27845 if (parms && parms != error_mark_node)
27846 {
27847 cp_parser_template_declaration_after_parameters (parser, parms,
27848 member_p);
27849 return true;
27850 }
27851
27852 error_at (token->location, "no matching concept for template-introduction");
27853 return true;
27854 }
27855
27856 /* Parse a normal template-declaration following the template keyword. */
27857
27858 static void
27859 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27860 {
27861 tree parameter_list;
27862 bool need_lang_pop;
27863 location_t location = input_location;
27864
27865 /* Look for the `<' token. */
27866 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27867 return;
27868 if (at_class_scope_p () && current_function_decl)
27869 {
27870 /* 14.5.2.2 [temp.mem]
27871
27872 A local class shall not have member templates. */
27873 error_at (location,
27874 "invalid declaration of member template in local class");
27875 cp_parser_skip_to_end_of_block_or_statement (parser);
27876 return;
27877 }
27878 /* [temp]
27879
27880 A template ... shall not have C linkage. */
27881 if (current_lang_name == lang_name_c)
27882 {
27883 error_at (location, "template with C linkage");
27884 maybe_show_extern_c_location ();
27885 /* Give it C++ linkage to avoid confusing other parts of the
27886 front end. */
27887 push_lang_context (lang_name_cplusplus);
27888 need_lang_pop = true;
27889 }
27890 else
27891 need_lang_pop = false;
27892
27893 /* We cannot perform access checks on the template parameter
27894 declarations until we know what is being declared, just as we
27895 cannot check the decl-specifier list. */
27896 push_deferring_access_checks (dk_deferred);
27897
27898 /* If the next token is `>', then we have an invalid
27899 specialization. Rather than complain about an invalid template
27900 parameter, issue an error message here. */
27901 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27902 {
27903 cp_parser_error (parser, "invalid explicit specialization");
27904 begin_specialization ();
27905 parameter_list = NULL_TREE;
27906 }
27907 else
27908 {
27909 /* Parse the template parameters. */
27910 parameter_list = cp_parser_template_parameter_list (parser);
27911 }
27912
27913 /* Look for the `>'. */
27914 cp_parser_skip_to_end_of_template_parameter_list (parser);
27915
27916 /* Manage template requirements */
27917 if (flag_concepts)
27918 {
27919 tree reqs = get_shorthand_constraints (current_template_parms);
27920 if (tree r = cp_parser_requires_clause_opt (parser))
27921 reqs = conjoin_constraints (reqs, normalize_expression (r));
27922 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27923 }
27924
27925 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27926 member_p);
27927
27928 /* For the erroneous case of a template with C linkage, we pushed an
27929 implicit C++ linkage scope; exit that scope now. */
27930 if (need_lang_pop)
27931 pop_lang_context ();
27932 }
27933
27934 /* Parse a template-declaration, assuming that the `export' (and
27935 `extern') keywords, if present, has already been scanned. MEMBER_P
27936 is as for cp_parser_template_declaration. */
27937
27938 static bool
27939 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27940 {
27941 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27942 {
27943 cp_lexer_consume_token (parser->lexer);
27944 cp_parser_explicit_template_declaration (parser, member_p);
27945 return true;
27946 }
27947 else if (flag_concepts)
27948 return cp_parser_template_introduction (parser, member_p);
27949
27950 return false;
27951 }
27952
27953 /* Perform the deferred access checks from a template-parameter-list.
27954 CHECKS is a TREE_LIST of access checks, as returned by
27955 get_deferred_access_checks. */
27956
27957 static void
27958 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27959 {
27960 ++processing_template_parmlist;
27961 perform_access_checks (checks, tf_warning_or_error);
27962 --processing_template_parmlist;
27963 }
27964
27965 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27966 `function-definition' sequence that follows a template header.
27967 If MEMBER_P is true, this declaration appears in a class scope.
27968
27969 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27970 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27971
27972 static tree
27973 cp_parser_single_declaration (cp_parser* parser,
27974 vec<deferred_access_check, va_gc> *checks,
27975 bool member_p,
27976 bool explicit_specialization_p,
27977 bool* friend_p)
27978 {
27979 int declares_class_or_enum;
27980 tree decl = NULL_TREE;
27981 cp_decl_specifier_seq decl_specifiers;
27982 bool function_definition_p = false;
27983 cp_token *decl_spec_token_start;
27984
27985 /* This function is only used when processing a template
27986 declaration. */
27987 gcc_assert (innermost_scope_kind () == sk_template_parms
27988 || innermost_scope_kind () == sk_template_spec);
27989
27990 /* Defer access checks until we know what is being declared. */
27991 push_deferring_access_checks (dk_deferred);
27992
27993 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27994 alternative. */
27995 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27996 cp_parser_decl_specifier_seq (parser,
27997 (CP_PARSER_FLAGS_OPTIONAL
27998 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
27999 &decl_specifiers,
28000 &declares_class_or_enum);
28001 if (friend_p)
28002 *friend_p = cp_parser_friend_p (&decl_specifiers);
28003
28004 /* There are no template typedefs. */
28005 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
28006 {
28007 error_at (decl_spec_token_start->location,
28008 "template declaration of %<typedef%>");
28009 decl = error_mark_node;
28010 }
28011
28012 /* Gather up the access checks that occurred the
28013 decl-specifier-seq. */
28014 stop_deferring_access_checks ();
28015
28016 /* Check for the declaration of a template class. */
28017 if (declares_class_or_enum)
28018 {
28019 if (cp_parser_declares_only_class_p (parser)
28020 || (declares_class_or_enum & 2))
28021 {
28022 // If this is a declaration, but not a definition, associate
28023 // any constraints with the type declaration. Constraints
28024 // are associated with definitions in cp_parser_class_specifier.
28025 if (declares_class_or_enum == 1)
28026 associate_classtype_constraints (decl_specifiers.type);
28027
28028 decl = shadow_tag (&decl_specifiers);
28029
28030 /* In this case:
28031
28032 struct C {
28033 friend template <typename T> struct A<T>::B;
28034 };
28035
28036 A<T>::B will be represented by a TYPENAME_TYPE, and
28037 therefore not recognized by shadow_tag. */
28038 if (friend_p && *friend_p
28039 && !decl
28040 && decl_specifiers.type
28041 && TYPE_P (decl_specifiers.type))
28042 decl = decl_specifiers.type;
28043
28044 if (decl && decl != error_mark_node)
28045 decl = TYPE_NAME (decl);
28046 else
28047 decl = error_mark_node;
28048
28049 /* Perform access checks for template parameters. */
28050 cp_parser_perform_template_parameter_access_checks (checks);
28051
28052 /* Give a helpful diagnostic for
28053 template <class T> struct A { } a;
28054 if we aren't already recovering from an error. */
28055 if (!cp_parser_declares_only_class_p (parser)
28056 && !seen_error ())
28057 {
28058 error_at (cp_lexer_peek_token (parser->lexer)->location,
28059 "a class template declaration must not declare "
28060 "anything else");
28061 cp_parser_skip_to_end_of_block_or_statement (parser);
28062 goto out;
28063 }
28064 }
28065 }
28066
28067 /* Complain about missing 'typename' or other invalid type names. */
28068 if (!decl_specifiers.any_type_specifiers_p
28069 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
28070 {
28071 /* cp_parser_parse_and_diagnose_invalid_type_name calls
28072 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
28073 the rest of this declaration. */
28074 decl = error_mark_node;
28075 goto out;
28076 }
28077
28078 /* If it's not a template class, try for a template function. If
28079 the next token is a `;', then this declaration does not declare
28080 anything. But, if there were errors in the decl-specifiers, then
28081 the error might well have come from an attempted class-specifier.
28082 In that case, there's no need to warn about a missing declarator. */
28083 if (!decl
28084 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
28085 || decl_specifiers.type != error_mark_node))
28086 {
28087 decl = cp_parser_init_declarator (parser,
28088 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
28089 &decl_specifiers,
28090 checks,
28091 /*function_definition_allowed_p=*/true,
28092 member_p,
28093 declares_class_or_enum,
28094 &function_definition_p,
28095 NULL, NULL, NULL);
28096
28097 /* 7.1.1-1 [dcl.stc]
28098
28099 A storage-class-specifier shall not be specified in an explicit
28100 specialization... */
28101 if (decl
28102 && explicit_specialization_p
28103 && decl_specifiers.storage_class != sc_none)
28104 {
28105 error_at (decl_spec_token_start->location,
28106 "explicit template specialization cannot have a storage class");
28107 decl = error_mark_node;
28108 }
28109
28110 if (decl && VAR_P (decl))
28111 check_template_variable (decl);
28112 }
28113
28114 /* Look for a trailing `;' after the declaration. */
28115 if (!function_definition_p
28116 && (decl == error_mark_node
28117 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
28118 cp_parser_skip_to_end_of_block_or_statement (parser);
28119
28120 out:
28121 pop_deferring_access_checks ();
28122
28123 /* Clear any current qualification; whatever comes next is the start
28124 of something new. */
28125 parser->scope = NULL_TREE;
28126 parser->qualifying_scope = NULL_TREE;
28127 parser->object_scope = NULL_TREE;
28128
28129 return decl;
28130 }
28131
28132 /* Parse a cast-expression that is not the operand of a unary "&". */
28133
28134 static cp_expr
28135 cp_parser_simple_cast_expression (cp_parser *parser)
28136 {
28137 return cp_parser_cast_expression (parser, /*address_p=*/false,
28138 /*cast_p=*/false, /*decltype*/false, NULL);
28139 }
28140
28141 /* Parse a functional cast to TYPE. Returns an expression
28142 representing the cast. */
28143
28144 static cp_expr
28145 cp_parser_functional_cast (cp_parser* parser, tree type)
28146 {
28147 vec<tree, va_gc> *vec;
28148 tree expression_list;
28149 cp_expr cast;
28150 bool nonconst_p;
28151
28152 location_t start_loc = input_location;
28153
28154 if (!type)
28155 type = error_mark_node;
28156
28157 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28158 {
28159 cp_lexer_set_source_position (parser->lexer);
28160 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28161 expression_list = cp_parser_braced_list (parser, &nonconst_p);
28162 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
28163 if (TREE_CODE (type) == TYPE_DECL)
28164 type = TREE_TYPE (type);
28165
28166 cast = finish_compound_literal (type, expression_list,
28167 tf_warning_or_error, fcl_functional);
28168 /* Create a location of the form:
28169 type_name{i, f}
28170 ^~~~~~~~~~~~~~~
28171 with caret == start at the start of the type name,
28172 finishing at the closing brace. */
28173 location_t finish_loc
28174 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28175 location_t combined_loc = make_location (start_loc, start_loc,
28176 finish_loc);
28177 cast.set_location (combined_loc);
28178 return cast;
28179 }
28180
28181
28182 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
28183 /*cast_p=*/true,
28184 /*allow_expansion_p=*/true,
28185 /*non_constant_p=*/NULL);
28186 if (vec == NULL)
28187 expression_list = error_mark_node;
28188 else
28189 {
28190 expression_list = build_tree_list_vec (vec);
28191 release_tree_vector (vec);
28192 }
28193
28194 cast = build_functional_cast (type, expression_list,
28195 tf_warning_or_error);
28196 /* [expr.const]/1: In an integral constant expression "only type
28197 conversions to integral or enumeration type can be used". */
28198 if (TREE_CODE (type) == TYPE_DECL)
28199 type = TREE_TYPE (type);
28200 if (cast != error_mark_node
28201 && !cast_valid_in_integral_constant_expression_p (type)
28202 && cp_parser_non_integral_constant_expression (parser,
28203 NIC_CONSTRUCTOR))
28204 return error_mark_node;
28205
28206 /* Create a location of the form:
28207 float(i)
28208 ^~~~~~~~
28209 with caret == start at the start of the type name,
28210 finishing at the closing paren. */
28211 location_t finish_loc
28212 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28213 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
28214 cast.set_location (combined_loc);
28215 return cast;
28216 }
28217
28218 /* Save the tokens that make up the body of a member function defined
28219 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
28220 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
28221 specifiers applied to the declaration. Returns the FUNCTION_DECL
28222 for the member function. */
28223
28224 static tree
28225 cp_parser_save_member_function_body (cp_parser* parser,
28226 cp_decl_specifier_seq *decl_specifiers,
28227 cp_declarator *declarator,
28228 tree attributes)
28229 {
28230 cp_token *first;
28231 cp_token *last;
28232 tree fn;
28233 bool function_try_block = false;
28234
28235 /* Create the FUNCTION_DECL. */
28236 fn = grokmethod (decl_specifiers, declarator, attributes);
28237 cp_finalize_omp_declare_simd (parser, fn);
28238 cp_finalize_oacc_routine (parser, fn, true);
28239 /* If something went badly wrong, bail out now. */
28240 if (fn == error_mark_node)
28241 {
28242 /* If there's a function-body, skip it. */
28243 if (cp_parser_token_starts_function_definition_p
28244 (cp_lexer_peek_token (parser->lexer)))
28245 cp_parser_skip_to_end_of_block_or_statement (parser);
28246 return error_mark_node;
28247 }
28248
28249 /* Remember it, if there default args to post process. */
28250 cp_parser_save_default_args (parser, fn);
28251
28252 /* Save away the tokens that make up the body of the
28253 function. */
28254 first = parser->lexer->next_token;
28255
28256 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
28257 cp_lexer_consume_token (parser->lexer);
28258 else if (cp_lexer_next_token_is_keyword (parser->lexer,
28259 RID_TRANSACTION_ATOMIC))
28260 {
28261 cp_lexer_consume_token (parser->lexer);
28262 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
28263 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
28264 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
28265 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
28266 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28267 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
28268 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
28269 {
28270 cp_lexer_consume_token (parser->lexer);
28271 cp_lexer_consume_token (parser->lexer);
28272 cp_lexer_consume_token (parser->lexer);
28273 cp_lexer_consume_token (parser->lexer);
28274 cp_lexer_consume_token (parser->lexer);
28275 }
28276 else
28277 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
28278 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
28279 {
28280 cp_lexer_consume_token (parser->lexer);
28281 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28282 break;
28283 }
28284 }
28285
28286 /* Handle function try blocks. */
28287 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28288 {
28289 cp_lexer_consume_token (parser->lexer);
28290 function_try_block = true;
28291 }
28292 /* We can have braced-init-list mem-initializers before the fn body. */
28293 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28294 {
28295 cp_lexer_consume_token (parser->lexer);
28296 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
28297 {
28298 /* cache_group will stop after an un-nested { } pair, too. */
28299 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28300 break;
28301
28302 /* variadic mem-inits have ... after the ')'. */
28303 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28304 cp_lexer_consume_token (parser->lexer);
28305 }
28306 }
28307 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28308 /* Handle function try blocks. */
28309 if (function_try_block)
28310 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
28311 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28312 last = parser->lexer->next_token;
28313
28314 /* Save away the inline definition; we will process it when the
28315 class is complete. */
28316 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
28317 DECL_PENDING_INLINE_P (fn) = 1;
28318
28319 /* We need to know that this was defined in the class, so that
28320 friend templates are handled correctly. */
28321 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
28322
28323 /* Add FN to the queue of functions to be parsed later. */
28324 vec_safe_push (unparsed_funs_with_definitions, fn);
28325
28326 return fn;
28327 }
28328
28329 /* Save the tokens that make up the in-class initializer for a non-static
28330 data member. Returns a DEFAULT_ARG. */
28331
28332 static tree
28333 cp_parser_save_nsdmi (cp_parser* parser)
28334 {
28335 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
28336 }
28337
28338 /* Parse a template-argument-list, as well as the trailing ">" (but
28339 not the opening "<"). See cp_parser_template_argument_list for the
28340 return value. */
28341
28342 static tree
28343 cp_parser_enclosed_template_argument_list (cp_parser* parser)
28344 {
28345 tree arguments;
28346 tree saved_scope;
28347 tree saved_qualifying_scope;
28348 tree saved_object_scope;
28349 bool saved_greater_than_is_operator_p;
28350
28351 /* [temp.names]
28352
28353 When parsing a template-id, the first non-nested `>' is taken as
28354 the end of the template-argument-list rather than a greater-than
28355 operator. */
28356 saved_greater_than_is_operator_p
28357 = parser->greater_than_is_operator_p;
28358 parser->greater_than_is_operator_p = false;
28359 /* Parsing the argument list may modify SCOPE, so we save it
28360 here. */
28361 saved_scope = parser->scope;
28362 saved_qualifying_scope = parser->qualifying_scope;
28363 saved_object_scope = parser->object_scope;
28364 /* We need to evaluate the template arguments, even though this
28365 template-id may be nested within a "sizeof". */
28366 cp_evaluated ev;
28367 /* Parse the template-argument-list itself. */
28368 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
28369 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28370 arguments = NULL_TREE;
28371 else
28372 arguments = cp_parser_template_argument_list (parser);
28373 /* Look for the `>' that ends the template-argument-list. If we find
28374 a '>>' instead, it's probably just a typo. */
28375 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28376 {
28377 if (cxx_dialect != cxx98)
28378 {
28379 /* In C++0x, a `>>' in a template argument list or cast
28380 expression is considered to be two separate `>'
28381 tokens. So, change the current token to a `>', but don't
28382 consume it: it will be consumed later when the outer
28383 template argument list (or cast expression) is parsed.
28384 Note that this replacement of `>' for `>>' is necessary
28385 even if we are parsing tentatively: in the tentative
28386 case, after calling
28387 cp_parser_enclosed_template_argument_list we will always
28388 throw away all of the template arguments and the first
28389 closing `>', either because the template argument list
28390 was erroneous or because we are replacing those tokens
28391 with a CPP_TEMPLATE_ID token. The second `>' (which will
28392 not have been thrown away) is needed either to close an
28393 outer template argument list or to complete a new-style
28394 cast. */
28395 cp_token *token = cp_lexer_peek_token (parser->lexer);
28396 token->type = CPP_GREATER;
28397 }
28398 else if (!saved_greater_than_is_operator_p)
28399 {
28400 /* If we're in a nested template argument list, the '>>' has
28401 to be a typo for '> >'. We emit the error message, but we
28402 continue parsing and we push a '>' as next token, so that
28403 the argument list will be parsed correctly. Note that the
28404 global source location is still on the token before the
28405 '>>', so we need to say explicitly where we want it. */
28406 cp_token *token = cp_lexer_peek_token (parser->lexer);
28407 gcc_rich_location richloc (token->location);
28408 richloc.add_fixit_replace ("> >");
28409 error_at (&richloc, "%<>>%> should be %<> >%> "
28410 "within a nested template argument list");
28411
28412 token->type = CPP_GREATER;
28413 }
28414 else
28415 {
28416 /* If this is not a nested template argument list, the '>>'
28417 is a typo for '>'. Emit an error message and continue.
28418 Same deal about the token location, but here we can get it
28419 right by consuming the '>>' before issuing the diagnostic. */
28420 cp_token *token = cp_lexer_consume_token (parser->lexer);
28421 error_at (token->location,
28422 "spurious %<>>%>, use %<>%> to terminate "
28423 "a template argument list");
28424 }
28425 }
28426 else
28427 cp_parser_skip_to_end_of_template_parameter_list (parser);
28428 /* The `>' token might be a greater-than operator again now. */
28429 parser->greater_than_is_operator_p
28430 = saved_greater_than_is_operator_p;
28431 /* Restore the SAVED_SCOPE. */
28432 parser->scope = saved_scope;
28433 parser->qualifying_scope = saved_qualifying_scope;
28434 parser->object_scope = saved_object_scope;
28435
28436 return arguments;
28437 }
28438
28439 /* MEMBER_FUNCTION is a member function, or a friend. If default
28440 arguments, or the body of the function have not yet been parsed,
28441 parse them now. */
28442
28443 static void
28444 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
28445 {
28446 timevar_push (TV_PARSE_INMETH);
28447 /* If this member is a template, get the underlying
28448 FUNCTION_DECL. */
28449 if (DECL_FUNCTION_TEMPLATE_P (member_function))
28450 member_function = DECL_TEMPLATE_RESULT (member_function);
28451
28452 /* There should not be any class definitions in progress at this
28453 point; the bodies of members are only parsed outside of all class
28454 definitions. */
28455 gcc_assert (parser->num_classes_being_defined == 0);
28456 /* While we're parsing the member functions we might encounter more
28457 classes. We want to handle them right away, but we don't want
28458 them getting mixed up with functions that are currently in the
28459 queue. */
28460 push_unparsed_function_queues (parser);
28461
28462 /* Make sure that any template parameters are in scope. */
28463 maybe_begin_member_template_processing (member_function);
28464
28465 /* If the body of the function has not yet been parsed, parse it
28466 now. */
28467 if (DECL_PENDING_INLINE_P (member_function))
28468 {
28469 tree function_scope;
28470 cp_token_cache *tokens;
28471
28472 /* The function is no longer pending; we are processing it. */
28473 tokens = DECL_PENDING_INLINE_INFO (member_function);
28474 DECL_PENDING_INLINE_INFO (member_function) = NULL;
28475 DECL_PENDING_INLINE_P (member_function) = 0;
28476
28477 /* If this is a local class, enter the scope of the containing
28478 function. */
28479 function_scope = current_function_decl;
28480 if (function_scope)
28481 push_function_context ();
28482
28483 /* Push the body of the function onto the lexer stack. */
28484 cp_parser_push_lexer_for_tokens (parser, tokens);
28485
28486 /* Let the front end know that we going to be defining this
28487 function. */
28488 start_preparsed_function (member_function, NULL_TREE,
28489 SF_PRE_PARSED | SF_INCLASS_INLINE);
28490
28491 /* Don't do access checking if it is a templated function. */
28492 if (processing_template_decl)
28493 push_deferring_access_checks (dk_no_check);
28494
28495 /* #pragma omp declare reduction needs special parsing. */
28496 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28497 {
28498 parser->lexer->in_pragma = true;
28499 cp_parser_omp_declare_reduction_exprs (member_function, parser);
28500 finish_function (/*inline_p=*/true);
28501 cp_check_omp_declare_reduction (member_function);
28502 }
28503 else
28504 /* Now, parse the body of the function. */
28505 cp_parser_function_definition_after_declarator (parser,
28506 /*inline_p=*/true);
28507
28508 if (processing_template_decl)
28509 pop_deferring_access_checks ();
28510
28511 /* Leave the scope of the containing function. */
28512 if (function_scope)
28513 pop_function_context ();
28514 cp_parser_pop_lexer (parser);
28515 }
28516
28517 /* Remove any template parameters from the symbol table. */
28518 maybe_end_member_template_processing ();
28519
28520 /* Restore the queue. */
28521 pop_unparsed_function_queues (parser);
28522 timevar_pop (TV_PARSE_INMETH);
28523 }
28524
28525 /* If DECL contains any default args, remember it on the unparsed
28526 functions queue. */
28527
28528 static void
28529 cp_parser_save_default_args (cp_parser* parser, tree decl)
28530 {
28531 tree probe;
28532
28533 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28534 probe;
28535 probe = TREE_CHAIN (probe))
28536 if (TREE_PURPOSE (probe))
28537 {
28538 cp_default_arg_entry entry = {current_class_type, decl};
28539 vec_safe_push (unparsed_funs_with_default_args, entry);
28540 break;
28541 }
28542 }
28543
28544 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28545 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28546 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28547 from the parameter-type-list. */
28548
28549 static tree
28550 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28551 tree default_arg, tree parmtype)
28552 {
28553 cp_token_cache *tokens;
28554 tree parsed_arg;
28555 bool dummy;
28556
28557 if (default_arg == error_mark_node)
28558 return error_mark_node;
28559
28560 /* Push the saved tokens for the default argument onto the parser's
28561 lexer stack. */
28562 tokens = DEFARG_TOKENS (default_arg);
28563 cp_parser_push_lexer_for_tokens (parser, tokens);
28564
28565 start_lambda_scope (decl);
28566
28567 /* Parse the default argument. */
28568 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28569 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28570 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28571
28572 finish_lambda_scope ();
28573
28574 if (parsed_arg == error_mark_node)
28575 cp_parser_skip_to_end_of_statement (parser);
28576
28577 if (!processing_template_decl)
28578 {
28579 /* In a non-template class, check conversions now. In a template,
28580 we'll wait and instantiate these as needed. */
28581 if (TREE_CODE (decl) == PARM_DECL)
28582 parsed_arg = check_default_argument (parmtype, parsed_arg,
28583 tf_warning_or_error);
28584 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28585 parsed_arg = error_mark_node;
28586 else
28587 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28588 }
28589
28590 /* If the token stream has not been completely used up, then
28591 there was extra junk after the end of the default
28592 argument. */
28593 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28594 {
28595 if (TREE_CODE (decl) == PARM_DECL)
28596 cp_parser_error (parser, "expected %<,%>");
28597 else
28598 cp_parser_error (parser, "expected %<;%>");
28599 }
28600
28601 /* Revert to the main lexer. */
28602 cp_parser_pop_lexer (parser);
28603
28604 return parsed_arg;
28605 }
28606
28607 /* FIELD is a non-static data member with an initializer which we saved for
28608 later; parse it now. */
28609
28610 static void
28611 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28612 {
28613 tree def;
28614
28615 maybe_begin_member_template_processing (field);
28616
28617 push_unparsed_function_queues (parser);
28618 def = cp_parser_late_parse_one_default_arg (parser, field,
28619 DECL_INITIAL (field),
28620 NULL_TREE);
28621 pop_unparsed_function_queues (parser);
28622
28623 maybe_end_member_template_processing ();
28624
28625 DECL_INITIAL (field) = def;
28626 }
28627
28628 /* FN is a FUNCTION_DECL which may contains a parameter with an
28629 unparsed DEFAULT_ARG. Parse the default args now. This function
28630 assumes that the current scope is the scope in which the default
28631 argument should be processed. */
28632
28633 static void
28634 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28635 {
28636 unsigned char saved_local_variables_forbidden_p;
28637 tree parm, parmdecl;
28638
28639 /* While we're parsing the default args, we might (due to the
28640 statement expression extension) encounter more classes. We want
28641 to handle them right away, but we don't want them getting mixed
28642 up with default args that are currently in the queue. */
28643 push_unparsed_function_queues (parser);
28644
28645 /* Local variable names (and the `this' keyword) may not appear
28646 in a default argument. */
28647 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28648 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
28649
28650 push_defarg_context (fn);
28651
28652 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28653 parmdecl = DECL_ARGUMENTS (fn);
28654 parm && parm != void_list_node;
28655 parm = TREE_CHAIN (parm),
28656 parmdecl = DECL_CHAIN (parmdecl))
28657 {
28658 tree default_arg = TREE_PURPOSE (parm);
28659 tree parsed_arg;
28660 vec<tree, va_gc> *insts;
28661 tree copy;
28662 unsigned ix;
28663
28664 if (!default_arg)
28665 continue;
28666
28667 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28668 /* This can happen for a friend declaration for a function
28669 already declared with default arguments. */
28670 continue;
28671
28672 parsed_arg
28673 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28674 default_arg,
28675 TREE_VALUE (parm));
28676 TREE_PURPOSE (parm) = parsed_arg;
28677
28678 /* Update any instantiations we've already created. */
28679 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28680 vec_safe_iterate (insts, ix, &copy); ix++)
28681 TREE_PURPOSE (copy) = parsed_arg;
28682 }
28683
28684 pop_defarg_context ();
28685
28686 /* Make sure no default arg is missing. */
28687 check_default_args (fn);
28688
28689 /* Restore the state of local_variables_forbidden_p. */
28690 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28691
28692 /* Restore the queue. */
28693 pop_unparsed_function_queues (parser);
28694 }
28695
28696 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28697
28698 sizeof ... ( identifier )
28699
28700 where the 'sizeof' token has already been consumed. */
28701
28702 static tree
28703 cp_parser_sizeof_pack (cp_parser *parser)
28704 {
28705 /* Consume the `...'. */
28706 cp_lexer_consume_token (parser->lexer);
28707 maybe_warn_variadic_templates ();
28708
28709 matching_parens parens;
28710 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28711 if (paren)
28712 parens.consume_open (parser);
28713 else
28714 permerror (cp_lexer_peek_token (parser->lexer)->location,
28715 "%<sizeof...%> argument must be surrounded by parentheses");
28716
28717 cp_token *token = cp_lexer_peek_token (parser->lexer);
28718 tree name = cp_parser_identifier (parser);
28719 if (name == error_mark_node)
28720 return error_mark_node;
28721 /* The name is not qualified. */
28722 parser->scope = NULL_TREE;
28723 parser->qualifying_scope = NULL_TREE;
28724 parser->object_scope = NULL_TREE;
28725 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28726 if (expr == error_mark_node)
28727 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28728 token->location);
28729 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28730 expr = TREE_TYPE (expr);
28731 else if (TREE_CODE (expr) == CONST_DECL)
28732 expr = DECL_INITIAL (expr);
28733 expr = make_pack_expansion (expr);
28734 PACK_EXPANSION_SIZEOF_P (expr) = true;
28735
28736 if (paren)
28737 parens.require_close (parser);
28738
28739 return expr;
28740 }
28741
28742 /* Parse the operand of `sizeof' (or a similar operator). Returns
28743 either a TYPE or an expression, depending on the form of the
28744 input. The KEYWORD indicates which kind of expression we have
28745 encountered. */
28746
28747 static tree
28748 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28749 {
28750 tree expr = NULL_TREE;
28751 const char *saved_message;
28752 char *tmp;
28753 bool saved_integral_constant_expression_p;
28754 bool saved_non_integral_constant_expression_p;
28755
28756 /* If it's a `...', then we are computing the length of a parameter
28757 pack. */
28758 if (keyword == RID_SIZEOF
28759 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28760 return cp_parser_sizeof_pack (parser);
28761
28762 /* Types cannot be defined in a `sizeof' expression. Save away the
28763 old message. */
28764 saved_message = parser->type_definition_forbidden_message;
28765 /* And create the new one. */
28766 tmp = concat ("types may not be defined in %<",
28767 IDENTIFIER_POINTER (ridpointers[keyword]),
28768 "%> expressions", NULL);
28769 parser->type_definition_forbidden_message = tmp;
28770
28771 /* The restrictions on constant-expressions do not apply inside
28772 sizeof expressions. */
28773 saved_integral_constant_expression_p
28774 = parser->integral_constant_expression_p;
28775 saved_non_integral_constant_expression_p
28776 = parser->non_integral_constant_expression_p;
28777 parser->integral_constant_expression_p = false;
28778
28779 /* Do not actually evaluate the expression. */
28780 ++cp_unevaluated_operand;
28781 ++c_inhibit_evaluation_warnings;
28782 /* If it's a `(', then we might be looking at the type-id
28783 construction. */
28784 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28785 {
28786 tree type = NULL_TREE;
28787
28788 /* We can't be sure yet whether we're looking at a type-id or an
28789 expression. */
28790 cp_parser_parse_tentatively (parser);
28791
28792 matching_parens parens;
28793 parens.consume_open (parser);
28794
28795 /* Note: as a GNU Extension, compound literals are considered
28796 postfix-expressions as they are in C99, so they are valid
28797 arguments to sizeof. See comment in cp_parser_cast_expression
28798 for details. */
28799 if (cp_parser_compound_literal_p (parser))
28800 cp_parser_simulate_error (parser);
28801 else
28802 {
28803 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28804 parser->in_type_id_in_expr_p = true;
28805 /* Look for the type-id. */
28806 type = cp_parser_type_id (parser);
28807 /* Look for the closing `)'. */
28808 parens.require_close (parser);
28809 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28810 }
28811
28812 /* If all went well, then we're done. */
28813 if (cp_parser_parse_definitely (parser))
28814 expr = type;
28815 }
28816
28817 /* If the type-id production did not work out, then we must be
28818 looking at the unary-expression production. */
28819 if (!expr)
28820 expr = cp_parser_unary_expression (parser);
28821
28822 /* Go back to evaluating expressions. */
28823 --cp_unevaluated_operand;
28824 --c_inhibit_evaluation_warnings;
28825
28826 /* Free the message we created. */
28827 free (tmp);
28828 /* And restore the old one. */
28829 parser->type_definition_forbidden_message = saved_message;
28830 parser->integral_constant_expression_p
28831 = saved_integral_constant_expression_p;
28832 parser->non_integral_constant_expression_p
28833 = saved_non_integral_constant_expression_p;
28834
28835 return expr;
28836 }
28837
28838 /* If the current declaration has no declarator, return true. */
28839
28840 static bool
28841 cp_parser_declares_only_class_p (cp_parser *parser)
28842 {
28843 /* If the next token is a `;' or a `,' then there is no
28844 declarator. */
28845 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28846 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28847 }
28848
28849 /* Update the DECL_SPECS to reflect the storage class indicated by
28850 KEYWORD. */
28851
28852 static void
28853 cp_parser_set_storage_class (cp_parser *parser,
28854 cp_decl_specifier_seq *decl_specs,
28855 enum rid keyword,
28856 cp_token *token)
28857 {
28858 cp_storage_class storage_class;
28859
28860 if (parser->in_unbraced_linkage_specification_p)
28861 {
28862 error_at (token->location, "invalid use of %qD in linkage specification",
28863 ridpointers[keyword]);
28864 return;
28865 }
28866 else if (decl_specs->storage_class != sc_none)
28867 {
28868 decl_specs->conflicting_specifiers_p = true;
28869 return;
28870 }
28871
28872 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28873 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28874 && decl_specs->gnu_thread_keyword_p)
28875 {
28876 pedwarn (decl_specs->locations[ds_thread], 0,
28877 "%<__thread%> before %qD", ridpointers[keyword]);
28878 }
28879
28880 switch (keyword)
28881 {
28882 case RID_AUTO:
28883 storage_class = sc_auto;
28884 break;
28885 case RID_REGISTER:
28886 storage_class = sc_register;
28887 break;
28888 case RID_STATIC:
28889 storage_class = sc_static;
28890 break;
28891 case RID_EXTERN:
28892 storage_class = sc_extern;
28893 break;
28894 case RID_MUTABLE:
28895 storage_class = sc_mutable;
28896 break;
28897 default:
28898 gcc_unreachable ();
28899 }
28900 decl_specs->storage_class = storage_class;
28901 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28902
28903 /* A storage class specifier cannot be applied alongside a typedef
28904 specifier. If there is a typedef specifier present then set
28905 conflicting_specifiers_p which will trigger an error later
28906 on in grokdeclarator. */
28907 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28908 decl_specs->conflicting_specifiers_p = true;
28909 }
28910
28911 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28912 is true, the type is a class or enum definition. */
28913
28914 static void
28915 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28916 tree type_spec,
28917 cp_token *token,
28918 bool type_definition_p)
28919 {
28920 decl_specs->any_specifiers_p = true;
28921
28922 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28923 (with, for example, in "typedef int wchar_t;") we remember that
28924 this is what happened. In system headers, we ignore these
28925 declarations so that G++ can work with system headers that are not
28926 C++-safe. */
28927 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28928 && !type_definition_p
28929 && (type_spec == boolean_type_node
28930 || type_spec == char16_type_node
28931 || type_spec == char32_type_node
28932 || type_spec == wchar_type_node)
28933 && (decl_specs->type
28934 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28935 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28936 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28937 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28938 {
28939 decl_specs->redefined_builtin_type = type_spec;
28940 set_and_check_decl_spec_loc (decl_specs,
28941 ds_redefined_builtin_type_spec,
28942 token);
28943 if (!decl_specs->type)
28944 {
28945 decl_specs->type = type_spec;
28946 decl_specs->type_definition_p = false;
28947 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28948 }
28949 }
28950 else if (decl_specs->type)
28951 decl_specs->multiple_types_p = true;
28952 else
28953 {
28954 decl_specs->type = type_spec;
28955 decl_specs->type_definition_p = type_definition_p;
28956 decl_specs->redefined_builtin_type = NULL_TREE;
28957 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28958 }
28959 }
28960
28961 /* True iff TOKEN is the GNU keyword __thread. */
28962
28963 static bool
28964 token_is__thread (cp_token *token)
28965 {
28966 gcc_assert (token->keyword == RID_THREAD);
28967 return id_equal (token->u.value, "__thread");
28968 }
28969
28970 /* Set the location for a declarator specifier and check if it is
28971 duplicated.
28972
28973 DECL_SPECS is the sequence of declarator specifiers onto which to
28974 set the location.
28975
28976 DS is the single declarator specifier to set which location is to
28977 be set onto the existing sequence of declarators.
28978
28979 LOCATION is the location for the declarator specifier to
28980 consider. */
28981
28982 static void
28983 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28984 cp_decl_spec ds, cp_token *token)
28985 {
28986 gcc_assert (ds < ds_last);
28987
28988 if (decl_specs == NULL)
28989 return;
28990
28991 location_t location = token->location;
28992
28993 if (decl_specs->locations[ds] == 0)
28994 {
28995 decl_specs->locations[ds] = location;
28996 if (ds == ds_thread)
28997 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28998 }
28999 else
29000 {
29001 if (ds == ds_long)
29002 {
29003 if (decl_specs->locations[ds_long_long] != 0)
29004 error_at (location,
29005 "%<long long long%> is too long for GCC");
29006 else
29007 {
29008 decl_specs->locations[ds_long_long] = location;
29009 pedwarn_cxx98 (location,
29010 OPT_Wlong_long,
29011 "ISO C++ 1998 does not support %<long long%>");
29012 }
29013 }
29014 else if (ds == ds_thread)
29015 {
29016 bool gnu = token_is__thread (token);
29017 gcc_rich_location richloc (location);
29018 if (gnu != decl_specs->gnu_thread_keyword_p)
29019 {
29020 richloc.add_range (decl_specs->locations[ds_thread]);
29021 error_at (&richloc,
29022 "both %<__thread%> and %<thread_local%> specified");
29023 }
29024 else
29025 {
29026 richloc.add_fixit_remove ();
29027 error_at (&richloc, "duplicate %qD", token->u.value);
29028 }
29029 }
29030 else
29031 {
29032 static const char *const decl_spec_names[] = {
29033 "signed",
29034 "unsigned",
29035 "short",
29036 "long",
29037 "const",
29038 "volatile",
29039 "restrict",
29040 "inline",
29041 "virtual",
29042 "explicit",
29043 "friend",
29044 "typedef",
29045 "using",
29046 "constexpr",
29047 "__complex"
29048 };
29049 gcc_rich_location richloc (location);
29050 richloc.add_fixit_remove ();
29051 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
29052 }
29053 }
29054 }
29055
29056 /* Return true iff the declarator specifier DS is present in the
29057 sequence of declarator specifiers DECL_SPECS. */
29058
29059 bool
29060 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
29061 cp_decl_spec ds)
29062 {
29063 gcc_assert (ds < ds_last);
29064
29065 if (decl_specs == NULL)
29066 return false;
29067
29068 return decl_specs->locations[ds] != 0;
29069 }
29070
29071 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
29072 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
29073
29074 static bool
29075 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
29076 {
29077 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
29078 }
29079
29080 /* Issue an error message indicating that TOKEN_DESC was expected.
29081 If KEYWORD is true, it indicated this function is called by
29082 cp_parser_require_keword and the required token can only be
29083 a indicated keyword.
29084
29085 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29086 within any error as the location of an "opening" token matching
29087 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29088 RT_CLOSE_PAREN). */
29089
29090 static void
29091 cp_parser_required_error (cp_parser *parser,
29092 required_token token_desc,
29093 bool keyword,
29094 location_t matching_location)
29095 {
29096 if (cp_parser_simulate_error (parser))
29097 return;
29098
29099 const char *gmsgid = NULL;
29100 switch (token_desc)
29101 {
29102 case RT_NEW:
29103 gmsgid = G_("expected %<new%>");
29104 break;
29105 case RT_DELETE:
29106 gmsgid = G_("expected %<delete%>");
29107 break;
29108 case RT_RETURN:
29109 gmsgid = G_("expected %<return%>");
29110 break;
29111 case RT_WHILE:
29112 gmsgid = G_("expected %<while%>");
29113 break;
29114 case RT_EXTERN:
29115 gmsgid = G_("expected %<extern%>");
29116 break;
29117 case RT_STATIC_ASSERT:
29118 gmsgid = G_("expected %<static_assert%>");
29119 break;
29120 case RT_DECLTYPE:
29121 gmsgid = G_("expected %<decltype%>");
29122 break;
29123 case RT_OPERATOR:
29124 gmsgid = G_("expected %<operator%>");
29125 break;
29126 case RT_CLASS:
29127 gmsgid = G_("expected %<class%>");
29128 break;
29129 case RT_TEMPLATE:
29130 gmsgid = G_("expected %<template%>");
29131 break;
29132 case RT_NAMESPACE:
29133 gmsgid = G_("expected %<namespace%>");
29134 break;
29135 case RT_USING:
29136 gmsgid = G_("expected %<using%>");
29137 break;
29138 case RT_ASM:
29139 gmsgid = G_("expected %<asm%>");
29140 break;
29141 case RT_TRY:
29142 gmsgid = G_("expected %<try%>");
29143 break;
29144 case RT_CATCH:
29145 gmsgid = G_("expected %<catch%>");
29146 break;
29147 case RT_THROW:
29148 gmsgid = G_("expected %<throw%>");
29149 break;
29150 case RT_LABEL:
29151 gmsgid = G_("expected %<__label__%>");
29152 break;
29153 case RT_AT_TRY:
29154 gmsgid = G_("expected %<@try%>");
29155 break;
29156 case RT_AT_SYNCHRONIZED:
29157 gmsgid = G_("expected %<@synchronized%>");
29158 break;
29159 case RT_AT_THROW:
29160 gmsgid = G_("expected %<@throw%>");
29161 break;
29162 case RT_TRANSACTION_ATOMIC:
29163 gmsgid = G_("expected %<__transaction_atomic%>");
29164 break;
29165 case RT_TRANSACTION_RELAXED:
29166 gmsgid = G_("expected %<__transaction_relaxed%>");
29167 break;
29168 default:
29169 break;
29170 }
29171
29172 if (!gmsgid && !keyword)
29173 {
29174 switch (token_desc)
29175 {
29176 case RT_SEMICOLON:
29177 gmsgid = G_("expected %<;%>");
29178 break;
29179 case RT_OPEN_PAREN:
29180 gmsgid = G_("expected %<(%>");
29181 break;
29182 case RT_CLOSE_BRACE:
29183 gmsgid = G_("expected %<}%>");
29184 break;
29185 case RT_OPEN_BRACE:
29186 gmsgid = G_("expected %<{%>");
29187 break;
29188 case RT_CLOSE_SQUARE:
29189 gmsgid = G_("expected %<]%>");
29190 break;
29191 case RT_OPEN_SQUARE:
29192 gmsgid = G_("expected %<[%>");
29193 break;
29194 case RT_COMMA:
29195 gmsgid = G_("expected %<,%>");
29196 break;
29197 case RT_SCOPE:
29198 gmsgid = G_("expected %<::%>");
29199 break;
29200 case RT_LESS:
29201 gmsgid = G_("expected %<<%>");
29202 break;
29203 case RT_GREATER:
29204 gmsgid = G_("expected %<>%>");
29205 break;
29206 case RT_EQ:
29207 gmsgid = G_("expected %<=%>");
29208 break;
29209 case RT_ELLIPSIS:
29210 gmsgid = G_("expected %<...%>");
29211 break;
29212 case RT_MULT:
29213 gmsgid = G_("expected %<*%>");
29214 break;
29215 case RT_COMPL:
29216 gmsgid = G_("expected %<~%>");
29217 break;
29218 case RT_COLON:
29219 gmsgid = G_("expected %<:%>");
29220 break;
29221 case RT_COLON_SCOPE:
29222 gmsgid = G_("expected %<:%> or %<::%>");
29223 break;
29224 case RT_CLOSE_PAREN:
29225 gmsgid = G_("expected %<)%>");
29226 break;
29227 case RT_COMMA_CLOSE_PAREN:
29228 gmsgid = G_("expected %<,%> or %<)%>");
29229 break;
29230 case RT_PRAGMA_EOL:
29231 gmsgid = G_("expected end of line");
29232 break;
29233 case RT_NAME:
29234 gmsgid = G_("expected identifier");
29235 break;
29236 case RT_SELECT:
29237 gmsgid = G_("expected selection-statement");
29238 break;
29239 case RT_ITERATION:
29240 gmsgid = G_("expected iteration-statement");
29241 break;
29242 case RT_JUMP:
29243 gmsgid = G_("expected jump-statement");
29244 break;
29245 case RT_CLASS_KEY:
29246 gmsgid = G_("expected class-key");
29247 break;
29248 case RT_CLASS_TYPENAME_TEMPLATE:
29249 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
29250 break;
29251 default:
29252 gcc_unreachable ();
29253 }
29254 }
29255
29256 if (gmsgid)
29257 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
29258 }
29259
29260
29261 /* If the next token is of the indicated TYPE, consume it. Otherwise,
29262 issue an error message indicating that TOKEN_DESC was expected.
29263
29264 Returns the token consumed, if the token had the appropriate type.
29265 Otherwise, returns NULL.
29266
29267 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29268 within any error as the location of an "opening" token matching
29269 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29270 RT_CLOSE_PAREN). */
29271
29272 static cp_token *
29273 cp_parser_require (cp_parser* parser,
29274 enum cpp_ttype type,
29275 required_token token_desc,
29276 location_t matching_location)
29277 {
29278 if (cp_lexer_next_token_is (parser->lexer, type))
29279 return cp_lexer_consume_token (parser->lexer);
29280 else
29281 {
29282 /* Output the MESSAGE -- unless we're parsing tentatively. */
29283 if (!cp_parser_simulate_error (parser))
29284 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
29285 matching_location);
29286 return NULL;
29287 }
29288 }
29289
29290 /* An error message is produced if the next token is not '>'.
29291 All further tokens are skipped until the desired token is
29292 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29293
29294 static void
29295 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
29296 {
29297 /* Current level of '< ... >'. */
29298 unsigned level = 0;
29299 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29300 unsigned nesting_depth = 0;
29301
29302 /* Are we ready, yet? If not, issue error message. */
29303 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
29304 return;
29305
29306 /* Skip tokens until the desired token is found. */
29307 while (true)
29308 {
29309 /* Peek at the next token. */
29310 switch (cp_lexer_peek_token (parser->lexer)->type)
29311 {
29312 case CPP_LESS:
29313 if (!nesting_depth)
29314 ++level;
29315 break;
29316
29317 case CPP_RSHIFT:
29318 if (cxx_dialect == cxx98)
29319 /* C++0x views the `>>' operator as two `>' tokens, but
29320 C++98 does not. */
29321 break;
29322 else if (!nesting_depth && level-- == 0)
29323 {
29324 /* We've hit a `>>' where the first `>' closes the
29325 template argument list, and the second `>' is
29326 spurious. Just consume the `>>' and stop; we've
29327 already produced at least one error. */
29328 cp_lexer_consume_token (parser->lexer);
29329 return;
29330 }
29331 /* Fall through for C++0x, so we handle the second `>' in
29332 the `>>'. */
29333 gcc_fallthrough ();
29334
29335 case CPP_GREATER:
29336 if (!nesting_depth && level-- == 0)
29337 {
29338 /* We've reached the token we want, consume it and stop. */
29339 cp_lexer_consume_token (parser->lexer);
29340 return;
29341 }
29342 break;
29343
29344 case CPP_OPEN_PAREN:
29345 case CPP_OPEN_SQUARE:
29346 ++nesting_depth;
29347 break;
29348
29349 case CPP_CLOSE_PAREN:
29350 case CPP_CLOSE_SQUARE:
29351 if (nesting_depth-- == 0)
29352 return;
29353 break;
29354
29355 case CPP_EOF:
29356 case CPP_PRAGMA_EOL:
29357 case CPP_SEMICOLON:
29358 case CPP_OPEN_BRACE:
29359 case CPP_CLOSE_BRACE:
29360 /* The '>' was probably forgotten, don't look further. */
29361 return;
29362
29363 default:
29364 break;
29365 }
29366
29367 /* Consume this token. */
29368 cp_lexer_consume_token (parser->lexer);
29369 }
29370 }
29371
29372 /* If the next token is the indicated keyword, consume it. Otherwise,
29373 issue an error message indicating that TOKEN_DESC was expected.
29374
29375 Returns the token consumed, if the token had the appropriate type.
29376 Otherwise, returns NULL. */
29377
29378 static cp_token *
29379 cp_parser_require_keyword (cp_parser* parser,
29380 enum rid keyword,
29381 required_token token_desc)
29382 {
29383 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
29384
29385 if (token && token->keyword != keyword)
29386 {
29387 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
29388 UNKNOWN_LOCATION);
29389 return NULL;
29390 }
29391
29392 return token;
29393 }
29394
29395 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29396 function-definition. */
29397
29398 static bool
29399 cp_parser_token_starts_function_definition_p (cp_token* token)
29400 {
29401 return (/* An ordinary function-body begins with an `{'. */
29402 token->type == CPP_OPEN_BRACE
29403 /* A ctor-initializer begins with a `:'. */
29404 || token->type == CPP_COLON
29405 /* A function-try-block begins with `try'. */
29406 || token->keyword == RID_TRY
29407 /* A function-transaction-block begins with `__transaction_atomic'
29408 or `__transaction_relaxed'. */
29409 || token->keyword == RID_TRANSACTION_ATOMIC
29410 || token->keyword == RID_TRANSACTION_RELAXED
29411 /* The named return value extension begins with `return'. */
29412 || token->keyword == RID_RETURN);
29413 }
29414
29415 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29416 definition. */
29417
29418 static bool
29419 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
29420 {
29421 cp_token *token;
29422
29423 token = cp_lexer_peek_token (parser->lexer);
29424 return (token->type == CPP_OPEN_BRACE
29425 || (token->type == CPP_COLON
29426 && !parser->colon_doesnt_start_class_def_p));
29427 }
29428
29429 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29430 C++0x) ending a template-argument. */
29431
29432 static bool
29433 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
29434 {
29435 cp_token *token;
29436
29437 token = cp_lexer_peek_token (parser->lexer);
29438 return (token->type == CPP_COMMA
29439 || token->type == CPP_GREATER
29440 || token->type == CPP_ELLIPSIS
29441 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
29442 }
29443
29444 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29445 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29446
29447 static bool
29448 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
29449 size_t n)
29450 {
29451 cp_token *token;
29452
29453 token = cp_lexer_peek_nth_token (parser->lexer, n);
29454 if (token->type == CPP_LESS)
29455 return true;
29456 /* Check for the sequence `<::' in the original code. It would be lexed as
29457 `[:', where `[' is a digraph, and there is no whitespace before
29458 `:'. */
29459 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
29460 {
29461 cp_token *token2;
29462 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
29463 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
29464 return true;
29465 }
29466 return false;
29467 }
29468
29469 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29470 or none_type otherwise. */
29471
29472 static enum tag_types
29473 cp_parser_token_is_class_key (cp_token* token)
29474 {
29475 switch (token->keyword)
29476 {
29477 case RID_CLASS:
29478 return class_type;
29479 case RID_STRUCT:
29480 return record_type;
29481 case RID_UNION:
29482 return union_type;
29483
29484 default:
29485 return none_type;
29486 }
29487 }
29488
29489 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29490 or none_type otherwise or if the token is null. */
29491
29492 static enum tag_types
29493 cp_parser_token_is_type_parameter_key (cp_token* token)
29494 {
29495 if (!token)
29496 return none_type;
29497
29498 switch (token->keyword)
29499 {
29500 case RID_CLASS:
29501 return class_type;
29502 case RID_TYPENAME:
29503 return typename_type;
29504
29505 default:
29506 return none_type;
29507 }
29508 }
29509
29510 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29511
29512 static void
29513 cp_parser_check_class_key (enum tag_types class_key, tree type)
29514 {
29515 if (type == error_mark_node)
29516 return;
29517 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29518 {
29519 if (permerror (input_location, "%qs tag used in naming %q#T",
29520 class_key == union_type ? "union"
29521 : class_key == record_type ? "struct" : "class",
29522 type))
29523 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29524 "%q#T was previously declared here", type);
29525 }
29526 }
29527
29528 /* Issue an error message if DECL is redeclared with different
29529 access than its original declaration [class.access.spec/3].
29530 This applies to nested classes, nested class templates and
29531 enumerations [class.mem/1]. */
29532
29533 static void
29534 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29535 {
29536 if (!decl
29537 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29538 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29539 return;
29540
29541 if ((TREE_PRIVATE (decl)
29542 != (current_access_specifier == access_private_node))
29543 || (TREE_PROTECTED (decl)
29544 != (current_access_specifier == access_protected_node)))
29545 error_at (location, "%qD redeclared with different access", decl);
29546 }
29547
29548 /* Look for the `template' keyword, as a syntactic disambiguator.
29549 Return TRUE iff it is present, in which case it will be
29550 consumed. */
29551
29552 static bool
29553 cp_parser_optional_template_keyword (cp_parser *parser)
29554 {
29555 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29556 {
29557 /* In C++98 the `template' keyword can only be used within templates;
29558 outside templates the parser can always figure out what is a
29559 template and what is not. In C++11, per the resolution of DR 468,
29560 `template' is allowed in cases where it is not strictly necessary. */
29561 if (!processing_template_decl
29562 && pedantic && cxx_dialect == cxx98)
29563 {
29564 cp_token *token = cp_lexer_peek_token (parser->lexer);
29565 pedwarn (token->location, OPT_Wpedantic,
29566 "in C++98 %<template%> (as a disambiguator) is only "
29567 "allowed within templates");
29568 /* If this part of the token stream is rescanned, the same
29569 error message would be generated. So, we purge the token
29570 from the stream. */
29571 cp_lexer_purge_token (parser->lexer);
29572 return false;
29573 }
29574 else
29575 {
29576 /* Consume the `template' keyword. */
29577 cp_lexer_consume_token (parser->lexer);
29578 return true;
29579 }
29580 }
29581 return false;
29582 }
29583
29584 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29585 set PARSER->SCOPE, and perform other related actions. */
29586
29587 static void
29588 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29589 {
29590 struct tree_check *check_value;
29591
29592 /* Get the stored value. */
29593 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29594 /* Set the scope from the stored value. */
29595 parser->scope = saved_checks_value (check_value);
29596 parser->qualifying_scope = check_value->qualifying_scope;
29597 parser->object_scope = NULL_TREE;
29598 }
29599
29600 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29601 encounter the end of a block before what we were looking for. */
29602
29603 static bool
29604 cp_parser_cache_group (cp_parser *parser,
29605 enum cpp_ttype end,
29606 unsigned depth)
29607 {
29608 while (true)
29609 {
29610 cp_token *token = cp_lexer_peek_token (parser->lexer);
29611
29612 /* Abort a parenthesized expression if we encounter a semicolon. */
29613 if ((end == CPP_CLOSE_PAREN || depth == 0)
29614 && token->type == CPP_SEMICOLON)
29615 return true;
29616 /* If we've reached the end of the file, stop. */
29617 if (token->type == CPP_EOF
29618 || (end != CPP_PRAGMA_EOL
29619 && token->type == CPP_PRAGMA_EOL))
29620 return true;
29621 if (token->type == CPP_CLOSE_BRACE && depth == 0)
29622 /* We've hit the end of an enclosing block, so there's been some
29623 kind of syntax error. */
29624 return true;
29625
29626 /* Consume the token. */
29627 cp_lexer_consume_token (parser->lexer);
29628 /* See if it starts a new group. */
29629 if (token->type == CPP_OPEN_BRACE)
29630 {
29631 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29632 /* In theory this should probably check end == '}', but
29633 cp_parser_save_member_function_body needs it to exit
29634 after either '}' or ')' when called with ')'. */
29635 if (depth == 0)
29636 return false;
29637 }
29638 else if (token->type == CPP_OPEN_PAREN)
29639 {
29640 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29641 if (depth == 0 && end == CPP_CLOSE_PAREN)
29642 return false;
29643 }
29644 else if (token->type == CPP_PRAGMA)
29645 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29646 else if (token->type == end)
29647 return false;
29648 }
29649 }
29650
29651 /* Like above, for caching a default argument or NSDMI. Both of these are
29652 terminated by a non-nested comma, but it can be unclear whether or not a
29653 comma is nested in a template argument list unless we do more parsing.
29654 In order to handle this ambiguity, when we encounter a ',' after a '<'
29655 we try to parse what follows as a parameter-declaration-list (in the
29656 case of a default argument) or a member-declarator (in the case of an
29657 NSDMI). If that succeeds, then we stop caching. */
29658
29659 static tree
29660 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29661 {
29662 unsigned depth = 0;
29663 int maybe_template_id = 0;
29664 cp_token *first_token;
29665 cp_token *token;
29666 tree default_argument;
29667
29668 /* Add tokens until we have processed the entire default
29669 argument. We add the range [first_token, token). */
29670 first_token = cp_lexer_peek_token (parser->lexer);
29671 if (first_token->type == CPP_OPEN_BRACE)
29672 {
29673 /* For list-initialization, this is straightforward. */
29674 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29675 token = cp_lexer_peek_token (parser->lexer);
29676 }
29677 else while (true)
29678 {
29679 bool done = false;
29680
29681 /* Peek at the next token. */
29682 token = cp_lexer_peek_token (parser->lexer);
29683 /* What we do depends on what token we have. */
29684 switch (token->type)
29685 {
29686 /* In valid code, a default argument must be
29687 immediately followed by a `,' `)', or `...'. */
29688 case CPP_COMMA:
29689 if (depth == 0 && maybe_template_id)
29690 {
29691 /* If we've seen a '<', we might be in a
29692 template-argument-list. Until Core issue 325 is
29693 resolved, we don't know how this situation ought
29694 to be handled, so try to DTRT. We check whether
29695 what comes after the comma is a valid parameter
29696 declaration list. If it is, then the comma ends
29697 the default argument; otherwise the default
29698 argument continues. */
29699 bool error = false;
29700 cp_token *peek;
29701
29702 /* Set ITALP so cp_parser_parameter_declaration_list
29703 doesn't decide to commit to this parse. */
29704 bool saved_italp = parser->in_template_argument_list_p;
29705 parser->in_template_argument_list_p = true;
29706
29707 cp_parser_parse_tentatively (parser);
29708
29709 if (nsdmi)
29710 {
29711 /* Parse declarators until we reach a non-comma or
29712 somthing that cannot be an initializer.
29713 Just checking whether we're looking at a single
29714 declarator is insufficient. Consider:
29715 int var = tuple<T,U>::x;
29716 The template parameter 'U' looks exactly like a
29717 declarator. */
29718 do
29719 {
29720 int ctor_dtor_or_conv_p;
29721 cp_lexer_consume_token (parser->lexer);
29722 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29723 CP_PARSER_FLAGS_NONE,
29724 &ctor_dtor_or_conv_p,
29725 /*parenthesized_p=*/NULL,
29726 /*member_p=*/true,
29727 /*friend_p=*/false,
29728 /*static_p=*/false);
29729 peek = cp_lexer_peek_token (parser->lexer);
29730 if (cp_parser_error_occurred (parser))
29731 break;
29732 }
29733 while (peek->type == CPP_COMMA);
29734 /* If we met an '=' or ';' then the original comma
29735 was the end of the NSDMI. Otherwise assume
29736 we're still in the NSDMI. */
29737 error = (peek->type != CPP_EQ
29738 && peek->type != CPP_SEMICOLON);
29739 }
29740 else
29741 {
29742 cp_lexer_consume_token (parser->lexer);
29743 begin_scope (sk_function_parms, NULL_TREE);
29744 tree t = cp_parser_parameter_declaration_list
29745 (parser, CP_PARSER_FLAGS_NONE);
29746 if (t == error_mark_node)
29747 error = true;
29748 pop_bindings_and_leave_scope ();
29749 }
29750 if (!cp_parser_error_occurred (parser) && !error)
29751 done = true;
29752 cp_parser_abort_tentative_parse (parser);
29753
29754 parser->in_template_argument_list_p = saved_italp;
29755 break;
29756 }
29757 /* FALLTHRU */
29758 case CPP_CLOSE_PAREN:
29759 case CPP_ELLIPSIS:
29760 /* If we run into a non-nested `;', `}', or `]',
29761 then the code is invalid -- but the default
29762 argument is certainly over. */
29763 case CPP_SEMICOLON:
29764 case CPP_CLOSE_BRACE:
29765 case CPP_CLOSE_SQUARE:
29766 if (depth == 0
29767 /* Handle correctly int n = sizeof ... ( p ); */
29768 && token->type != CPP_ELLIPSIS)
29769 done = true;
29770 /* Update DEPTH, if necessary. */
29771 else if (token->type == CPP_CLOSE_PAREN
29772 || token->type == CPP_CLOSE_BRACE
29773 || token->type == CPP_CLOSE_SQUARE)
29774 --depth;
29775 break;
29776
29777 case CPP_OPEN_PAREN:
29778 case CPP_OPEN_SQUARE:
29779 case CPP_OPEN_BRACE:
29780 ++depth;
29781 break;
29782
29783 case CPP_LESS:
29784 if (depth == 0)
29785 /* This might be the comparison operator, or it might
29786 start a template argument list. */
29787 ++maybe_template_id;
29788 break;
29789
29790 case CPP_RSHIFT:
29791 if (cxx_dialect == cxx98)
29792 break;
29793 /* Fall through for C++0x, which treats the `>>'
29794 operator like two `>' tokens in certain
29795 cases. */
29796 gcc_fallthrough ();
29797
29798 case CPP_GREATER:
29799 if (depth == 0)
29800 {
29801 /* This might be an operator, or it might close a
29802 template argument list. But if a previous '<'
29803 started a template argument list, this will have
29804 closed it, so we can't be in one anymore. */
29805 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29806 if (maybe_template_id < 0)
29807 maybe_template_id = 0;
29808 }
29809 break;
29810
29811 /* If we run out of tokens, issue an error message. */
29812 case CPP_EOF:
29813 case CPP_PRAGMA_EOL:
29814 error_at (token->location, "file ends in default argument");
29815 return error_mark_node;
29816
29817 case CPP_NAME:
29818 case CPP_SCOPE:
29819 /* In these cases, we should look for template-ids.
29820 For example, if the default argument is
29821 `X<int, double>()', we need to do name lookup to
29822 figure out whether or not `X' is a template; if
29823 so, the `,' does not end the default argument.
29824
29825 That is not yet done. */
29826 break;
29827
29828 default:
29829 break;
29830 }
29831
29832 /* If we've reached the end, stop. */
29833 if (done)
29834 break;
29835
29836 /* Add the token to the token block. */
29837 token = cp_lexer_consume_token (parser->lexer);
29838 }
29839
29840 /* Create a DEFAULT_ARG to represent the unparsed default
29841 argument. */
29842 default_argument = make_node (DEFAULT_ARG);
29843 DEFARG_TOKENS (default_argument)
29844 = cp_token_cache_new (first_token, token);
29845 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29846
29847 return default_argument;
29848 }
29849
29850 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29851
29852 location_t
29853 defarg_location (tree default_argument)
29854 {
29855 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29856 location_t start = tokens->first->location;
29857 location_t end = tokens->last->location;
29858 return make_location (start, start, end);
29859 }
29860
29861 /* Begin parsing tentatively. We always save tokens while parsing
29862 tentatively so that if the tentative parsing fails we can restore the
29863 tokens. */
29864
29865 static void
29866 cp_parser_parse_tentatively (cp_parser* parser)
29867 {
29868 /* Enter a new parsing context. */
29869 parser->context = cp_parser_context_new (parser->context);
29870 /* Begin saving tokens. */
29871 cp_lexer_save_tokens (parser->lexer);
29872 /* In order to avoid repetitive access control error messages,
29873 access checks are queued up until we are no longer parsing
29874 tentatively. */
29875 push_deferring_access_checks (dk_deferred);
29876 }
29877
29878 /* Commit to the currently active tentative parse. */
29879
29880 static void
29881 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29882 {
29883 cp_parser_context *context;
29884 cp_lexer *lexer;
29885
29886 /* Mark all of the levels as committed. */
29887 lexer = parser->lexer;
29888 for (context = parser->context; context->next; context = context->next)
29889 {
29890 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29891 break;
29892 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29893 while (!cp_lexer_saving_tokens (lexer))
29894 lexer = lexer->next;
29895 cp_lexer_commit_tokens (lexer);
29896 }
29897 }
29898
29899 /* Commit to the topmost currently active tentative parse.
29900
29901 Note that this function shouldn't be called when there are
29902 irreversible side-effects while in a tentative state. For
29903 example, we shouldn't create a permanent entry in the symbol
29904 table, or issue an error message that might not apply if the
29905 tentative parse is aborted. */
29906
29907 static void
29908 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29909 {
29910 cp_parser_context *context = parser->context;
29911 cp_lexer *lexer = parser->lexer;
29912
29913 if (context)
29914 {
29915 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29916 return;
29917 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29918
29919 while (!cp_lexer_saving_tokens (lexer))
29920 lexer = lexer->next;
29921 cp_lexer_commit_tokens (lexer);
29922 }
29923 }
29924
29925 /* Abort the currently active tentative parse. All consumed tokens
29926 will be rolled back, and no diagnostics will be issued. */
29927
29928 static void
29929 cp_parser_abort_tentative_parse (cp_parser* parser)
29930 {
29931 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29932 || errorcount > 0);
29933 cp_parser_simulate_error (parser);
29934 /* Now, pretend that we want to see if the construct was
29935 successfully parsed. */
29936 cp_parser_parse_definitely (parser);
29937 }
29938
29939 /* Stop parsing tentatively. If a parse error has occurred, restore the
29940 token stream. Otherwise, commit to the tokens we have consumed.
29941 Returns true if no error occurred; false otherwise. */
29942
29943 static bool
29944 cp_parser_parse_definitely (cp_parser* parser)
29945 {
29946 bool error_occurred;
29947 cp_parser_context *context;
29948
29949 /* Remember whether or not an error occurred, since we are about to
29950 destroy that information. */
29951 error_occurred = cp_parser_error_occurred (parser);
29952 /* Remove the topmost context from the stack. */
29953 context = parser->context;
29954 parser->context = context->next;
29955 /* If no parse errors occurred, commit to the tentative parse. */
29956 if (!error_occurred)
29957 {
29958 /* Commit to the tokens read tentatively, unless that was
29959 already done. */
29960 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29961 cp_lexer_commit_tokens (parser->lexer);
29962
29963 pop_to_parent_deferring_access_checks ();
29964 }
29965 /* Otherwise, if errors occurred, roll back our state so that things
29966 are just as they were before we began the tentative parse. */
29967 else
29968 {
29969 cp_lexer_rollback_tokens (parser->lexer);
29970 pop_deferring_access_checks ();
29971 }
29972 /* Add the context to the front of the free list. */
29973 context->next = cp_parser_context_free_list;
29974 cp_parser_context_free_list = context;
29975
29976 return !error_occurred;
29977 }
29978
29979 /* Returns true if we are parsing tentatively and are not committed to
29980 this tentative parse. */
29981
29982 static bool
29983 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29984 {
29985 return (cp_parser_parsing_tentatively (parser)
29986 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29987 }
29988
29989 /* Returns nonzero iff an error has occurred during the most recent
29990 tentative parse. */
29991
29992 static bool
29993 cp_parser_error_occurred (cp_parser* parser)
29994 {
29995 return (cp_parser_parsing_tentatively (parser)
29996 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29997 }
29998
29999 /* Returns nonzero if GNU extensions are allowed. */
30000
30001 static bool
30002 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
30003 {
30004 return parser->allow_gnu_extensions_p;
30005 }
30006 \f
30007 /* Objective-C++ Productions */
30008
30009
30010 /* Parse an Objective-C expression, which feeds into a primary-expression
30011 above.
30012
30013 objc-expression:
30014 objc-message-expression
30015 objc-string-literal
30016 objc-encode-expression
30017 objc-protocol-expression
30018 objc-selector-expression
30019
30020 Returns a tree representation of the expression. */
30021
30022 static cp_expr
30023 cp_parser_objc_expression (cp_parser* parser)
30024 {
30025 /* Try to figure out what kind of declaration is present. */
30026 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30027
30028 switch (kwd->type)
30029 {
30030 case CPP_OPEN_SQUARE:
30031 return cp_parser_objc_message_expression (parser);
30032
30033 case CPP_OBJC_STRING:
30034 kwd = cp_lexer_consume_token (parser->lexer);
30035 return objc_build_string_object (kwd->u.value);
30036
30037 case CPP_KEYWORD:
30038 switch (kwd->keyword)
30039 {
30040 case RID_AT_ENCODE:
30041 return cp_parser_objc_encode_expression (parser);
30042
30043 case RID_AT_PROTOCOL:
30044 return cp_parser_objc_protocol_expression (parser);
30045
30046 case RID_AT_SELECTOR:
30047 return cp_parser_objc_selector_expression (parser);
30048
30049 default:
30050 break;
30051 }
30052 /* FALLTHRU */
30053 default:
30054 error_at (kwd->location,
30055 "misplaced %<@%D%> Objective-C++ construct",
30056 kwd->u.value);
30057 cp_parser_skip_to_end_of_block_or_statement (parser);
30058 }
30059
30060 return error_mark_node;
30061 }
30062
30063 /* Parse an Objective-C message expression.
30064
30065 objc-message-expression:
30066 [ objc-message-receiver objc-message-args ]
30067
30068 Returns a representation of an Objective-C message. */
30069
30070 static tree
30071 cp_parser_objc_message_expression (cp_parser* parser)
30072 {
30073 tree receiver, messageargs;
30074
30075 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30076 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
30077 receiver = cp_parser_objc_message_receiver (parser);
30078 messageargs = cp_parser_objc_message_args (parser);
30079 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
30080 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
30081
30082 tree result = objc_build_message_expr (receiver, messageargs);
30083
30084 /* Construct a location e.g.
30085 [self func1:5]
30086 ^~~~~~~~~~~~~~
30087 ranging from the '[' to the ']', with the caret at the start. */
30088 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
30089 protected_set_expr_location (result, combined_loc);
30090
30091 return result;
30092 }
30093
30094 /* Parse an objc-message-receiver.
30095
30096 objc-message-receiver:
30097 expression
30098 simple-type-specifier
30099
30100 Returns a representation of the type or expression. */
30101
30102 static tree
30103 cp_parser_objc_message_receiver (cp_parser* parser)
30104 {
30105 tree rcv;
30106
30107 /* An Objective-C message receiver may be either (1) a type
30108 or (2) an expression. */
30109 cp_parser_parse_tentatively (parser);
30110 rcv = cp_parser_expression (parser);
30111
30112 /* If that worked out, fine. */
30113 if (cp_parser_parse_definitely (parser))
30114 return rcv;
30115
30116 cp_parser_parse_tentatively (parser);
30117 rcv = cp_parser_simple_type_specifier (parser,
30118 /*decl_specs=*/NULL,
30119 CP_PARSER_FLAGS_NONE);
30120
30121 if (cp_parser_parse_definitely (parser))
30122 return objc_get_class_reference (rcv);
30123
30124 cp_parser_error (parser, "objective-c++ message receiver expected");
30125 return error_mark_node;
30126 }
30127
30128 /* Parse the arguments and selectors comprising an Objective-C message.
30129
30130 objc-message-args:
30131 objc-selector
30132 objc-selector-args
30133 objc-selector-args , objc-comma-args
30134
30135 objc-selector-args:
30136 objc-selector [opt] : assignment-expression
30137 objc-selector-args objc-selector [opt] : assignment-expression
30138
30139 objc-comma-args:
30140 assignment-expression
30141 objc-comma-args , assignment-expression
30142
30143 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
30144 selector arguments and TREE_VALUE containing a list of comma
30145 arguments. */
30146
30147 static tree
30148 cp_parser_objc_message_args (cp_parser* parser)
30149 {
30150 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
30151 bool maybe_unary_selector_p = true;
30152 cp_token *token = cp_lexer_peek_token (parser->lexer);
30153
30154 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30155 {
30156 tree selector = NULL_TREE, arg;
30157
30158 if (token->type != CPP_COLON)
30159 selector = cp_parser_objc_selector (parser);
30160
30161 /* Detect if we have a unary selector. */
30162 if (maybe_unary_selector_p
30163 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30164 return build_tree_list (selector, NULL_TREE);
30165
30166 maybe_unary_selector_p = false;
30167 cp_parser_require (parser, CPP_COLON, RT_COLON);
30168 arg = cp_parser_assignment_expression (parser);
30169
30170 sel_args
30171 = chainon (sel_args,
30172 build_tree_list (selector, arg));
30173
30174 token = cp_lexer_peek_token (parser->lexer);
30175 }
30176
30177 /* Handle non-selector arguments, if any. */
30178 while (token->type == CPP_COMMA)
30179 {
30180 tree arg;
30181
30182 cp_lexer_consume_token (parser->lexer);
30183 arg = cp_parser_assignment_expression (parser);
30184
30185 addl_args
30186 = chainon (addl_args,
30187 build_tree_list (NULL_TREE, arg));
30188
30189 token = cp_lexer_peek_token (parser->lexer);
30190 }
30191
30192 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
30193 {
30194 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
30195 return build_tree_list (error_mark_node, error_mark_node);
30196 }
30197
30198 return build_tree_list (sel_args, addl_args);
30199 }
30200
30201 /* Parse an Objective-C encode expression.
30202
30203 objc-encode-expression:
30204 @encode objc-typename
30205
30206 Returns an encoded representation of the type argument. */
30207
30208 static cp_expr
30209 cp_parser_objc_encode_expression (cp_parser* parser)
30210 {
30211 tree type;
30212 cp_token *token;
30213 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30214
30215 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
30216 matching_parens parens;
30217 parens.require_open (parser);
30218 token = cp_lexer_peek_token (parser->lexer);
30219 type = complete_type (cp_parser_type_id (parser));
30220 parens.require_close (parser);
30221
30222 if (!type)
30223 {
30224 error_at (token->location,
30225 "%<@encode%> must specify a type as an argument");
30226 return error_mark_node;
30227 }
30228
30229 /* This happens if we find @encode(T) (where T is a template
30230 typename or something dependent on a template typename) when
30231 parsing a template. In that case, we can't compile it
30232 immediately, but we rather create an AT_ENCODE_EXPR which will
30233 need to be instantiated when the template is used.
30234 */
30235 if (dependent_type_p (type))
30236 {
30237 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
30238 TREE_READONLY (value) = 1;
30239 return value;
30240 }
30241
30242
30243 /* Build a location of the form:
30244 @encode(int)
30245 ^~~~~~~~~~~~
30246 with caret==start at the @ token, finishing at the close paren. */
30247 location_t combined_loc
30248 = make_location (start_loc, start_loc,
30249 cp_lexer_previous_token (parser->lexer)->location);
30250
30251 return cp_expr (objc_build_encode_expr (type), combined_loc);
30252 }
30253
30254 /* Parse an Objective-C @defs expression. */
30255
30256 static tree
30257 cp_parser_objc_defs_expression (cp_parser *parser)
30258 {
30259 tree name;
30260
30261 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
30262 matching_parens parens;
30263 parens.require_open (parser);
30264 name = cp_parser_identifier (parser);
30265 parens.require_close (parser);
30266
30267 return objc_get_class_ivars (name);
30268 }
30269
30270 /* Parse an Objective-C protocol expression.
30271
30272 objc-protocol-expression:
30273 @protocol ( identifier )
30274
30275 Returns a representation of the protocol expression. */
30276
30277 static tree
30278 cp_parser_objc_protocol_expression (cp_parser* parser)
30279 {
30280 tree proto;
30281 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30282
30283 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30284 matching_parens parens;
30285 parens.require_open (parser);
30286 proto = cp_parser_identifier (parser);
30287 parens.require_close (parser);
30288
30289 /* Build a location of the form:
30290 @protocol(prot)
30291 ^~~~~~~~~~~~~~~
30292 with caret==start at the @ token, finishing at the close paren. */
30293 location_t combined_loc
30294 = make_location (start_loc, start_loc,
30295 cp_lexer_previous_token (parser->lexer)->location);
30296 tree result = objc_build_protocol_expr (proto);
30297 protected_set_expr_location (result, combined_loc);
30298 return result;
30299 }
30300
30301 /* Parse an Objective-C selector expression.
30302
30303 objc-selector-expression:
30304 @selector ( objc-method-signature )
30305
30306 objc-method-signature:
30307 objc-selector
30308 objc-selector-seq
30309
30310 objc-selector-seq:
30311 objc-selector :
30312 objc-selector-seq objc-selector :
30313
30314 Returns a representation of the method selector. */
30315
30316 static tree
30317 cp_parser_objc_selector_expression (cp_parser* parser)
30318 {
30319 tree sel_seq = NULL_TREE;
30320 bool maybe_unary_selector_p = true;
30321 cp_token *token;
30322 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30323
30324 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
30325 matching_parens parens;
30326 parens.require_open (parser);
30327 token = cp_lexer_peek_token (parser->lexer);
30328
30329 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
30330 || token->type == CPP_SCOPE)
30331 {
30332 tree selector = NULL_TREE;
30333
30334 if (token->type != CPP_COLON
30335 || token->type == CPP_SCOPE)
30336 selector = cp_parser_objc_selector (parser);
30337
30338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
30339 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
30340 {
30341 /* Detect if we have a unary selector. */
30342 if (maybe_unary_selector_p)
30343 {
30344 sel_seq = selector;
30345 goto finish_selector;
30346 }
30347 else
30348 {
30349 cp_parser_error (parser, "expected %<:%>");
30350 }
30351 }
30352 maybe_unary_selector_p = false;
30353 token = cp_lexer_consume_token (parser->lexer);
30354
30355 if (token->type == CPP_SCOPE)
30356 {
30357 sel_seq
30358 = chainon (sel_seq,
30359 build_tree_list (selector, NULL_TREE));
30360 sel_seq
30361 = chainon (sel_seq,
30362 build_tree_list (NULL_TREE, NULL_TREE));
30363 }
30364 else
30365 sel_seq
30366 = chainon (sel_seq,
30367 build_tree_list (selector, NULL_TREE));
30368
30369 token = cp_lexer_peek_token (parser->lexer);
30370 }
30371
30372 finish_selector:
30373 parens.require_close (parser);
30374
30375
30376 /* Build a location of the form:
30377 @selector(func)
30378 ^~~~~~~~~~~~~~~
30379 with caret==start at the @ token, finishing at the close paren. */
30380 location_t combined_loc
30381 = make_location (loc, loc,
30382 cp_lexer_previous_token (parser->lexer)->location);
30383 tree result = objc_build_selector_expr (combined_loc, sel_seq);
30384 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30385 protected_set_expr_location (result, combined_loc);
30386 return result;
30387 }
30388
30389 /* Parse a list of identifiers.
30390
30391 objc-identifier-list:
30392 identifier
30393 objc-identifier-list , identifier
30394
30395 Returns a TREE_LIST of identifier nodes. */
30396
30397 static tree
30398 cp_parser_objc_identifier_list (cp_parser* parser)
30399 {
30400 tree identifier;
30401 tree list;
30402 cp_token *sep;
30403
30404 identifier = cp_parser_identifier (parser);
30405 if (identifier == error_mark_node)
30406 return error_mark_node;
30407
30408 list = build_tree_list (NULL_TREE, identifier);
30409 sep = cp_lexer_peek_token (parser->lexer);
30410
30411 while (sep->type == CPP_COMMA)
30412 {
30413 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30414 identifier = cp_parser_identifier (parser);
30415 if (identifier == error_mark_node)
30416 return list;
30417
30418 list = chainon (list, build_tree_list (NULL_TREE,
30419 identifier));
30420 sep = cp_lexer_peek_token (parser->lexer);
30421 }
30422
30423 return list;
30424 }
30425
30426 /* Parse an Objective-C alias declaration.
30427
30428 objc-alias-declaration:
30429 @compatibility_alias identifier identifier ;
30430
30431 This function registers the alias mapping with the Objective-C front end.
30432 It returns nothing. */
30433
30434 static void
30435 cp_parser_objc_alias_declaration (cp_parser* parser)
30436 {
30437 tree alias, orig;
30438
30439 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
30440 alias = cp_parser_identifier (parser);
30441 orig = cp_parser_identifier (parser);
30442 objc_declare_alias (alias, orig);
30443 cp_parser_consume_semicolon_at_end_of_statement (parser);
30444 }
30445
30446 /* Parse an Objective-C class forward-declaration.
30447
30448 objc-class-declaration:
30449 @class objc-identifier-list ;
30450
30451 The function registers the forward declarations with the Objective-C
30452 front end. It returns nothing. */
30453
30454 static void
30455 cp_parser_objc_class_declaration (cp_parser* parser)
30456 {
30457 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
30458 while (true)
30459 {
30460 tree id;
30461
30462 id = cp_parser_identifier (parser);
30463 if (id == error_mark_node)
30464 break;
30465
30466 objc_declare_class (id);
30467
30468 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30469 cp_lexer_consume_token (parser->lexer);
30470 else
30471 break;
30472 }
30473 cp_parser_consume_semicolon_at_end_of_statement (parser);
30474 }
30475
30476 /* Parse a list of Objective-C protocol references.
30477
30478 objc-protocol-refs-opt:
30479 objc-protocol-refs [opt]
30480
30481 objc-protocol-refs:
30482 < objc-identifier-list >
30483
30484 Returns a TREE_LIST of identifiers, if any. */
30485
30486 static tree
30487 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30488 {
30489 tree protorefs = NULL_TREE;
30490
30491 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30492 {
30493 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
30494 protorefs = cp_parser_objc_identifier_list (parser);
30495 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30496 }
30497
30498 return protorefs;
30499 }
30500
30501 /* Parse a Objective-C visibility specification. */
30502
30503 static void
30504 cp_parser_objc_visibility_spec (cp_parser* parser)
30505 {
30506 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30507
30508 switch (vis->keyword)
30509 {
30510 case RID_AT_PRIVATE:
30511 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30512 break;
30513 case RID_AT_PROTECTED:
30514 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30515 break;
30516 case RID_AT_PUBLIC:
30517 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30518 break;
30519 case RID_AT_PACKAGE:
30520 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30521 break;
30522 default:
30523 return;
30524 }
30525
30526 /* Eat '@private'/'@protected'/'@public'. */
30527 cp_lexer_consume_token (parser->lexer);
30528 }
30529
30530 /* Parse an Objective-C method type. Return 'true' if it is a class
30531 (+) method, and 'false' if it is an instance (-) method. */
30532
30533 static inline bool
30534 cp_parser_objc_method_type (cp_parser* parser)
30535 {
30536 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30537 return true;
30538 else
30539 return false;
30540 }
30541
30542 /* Parse an Objective-C protocol qualifier. */
30543
30544 static tree
30545 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30546 {
30547 tree quals = NULL_TREE, node;
30548 cp_token *token = cp_lexer_peek_token (parser->lexer);
30549
30550 node = token->u.value;
30551
30552 while (node && identifier_p (node)
30553 && (node == ridpointers [(int) RID_IN]
30554 || node == ridpointers [(int) RID_OUT]
30555 || node == ridpointers [(int) RID_INOUT]
30556 || node == ridpointers [(int) RID_BYCOPY]
30557 || node == ridpointers [(int) RID_BYREF]
30558 || node == ridpointers [(int) RID_ONEWAY]))
30559 {
30560 quals = tree_cons (NULL_TREE, node, quals);
30561 cp_lexer_consume_token (parser->lexer);
30562 token = cp_lexer_peek_token (parser->lexer);
30563 node = token->u.value;
30564 }
30565
30566 return quals;
30567 }
30568
30569 /* Parse an Objective-C typename. */
30570
30571 static tree
30572 cp_parser_objc_typename (cp_parser* parser)
30573 {
30574 tree type_name = NULL_TREE;
30575
30576 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30577 {
30578 tree proto_quals, cp_type = NULL_TREE;
30579
30580 matching_parens parens;
30581 parens.consume_open (parser); /* Eat '('. */
30582 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30583
30584 /* An ObjC type name may consist of just protocol qualifiers, in which
30585 case the type shall default to 'id'. */
30586 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30587 {
30588 cp_type = cp_parser_type_id (parser);
30589
30590 /* If the type could not be parsed, an error has already
30591 been produced. For error recovery, behave as if it had
30592 not been specified, which will use the default type
30593 'id'. */
30594 if (cp_type == error_mark_node)
30595 {
30596 cp_type = NULL_TREE;
30597 /* We need to skip to the closing parenthesis as
30598 cp_parser_type_id() does not seem to do it for
30599 us. */
30600 cp_parser_skip_to_closing_parenthesis (parser,
30601 /*recovering=*/true,
30602 /*or_comma=*/false,
30603 /*consume_paren=*/false);
30604 }
30605 }
30606
30607 parens.require_close (parser);
30608 type_name = build_tree_list (proto_quals, cp_type);
30609 }
30610
30611 return type_name;
30612 }
30613
30614 /* Check to see if TYPE refers to an Objective-C selector name. */
30615
30616 static bool
30617 cp_parser_objc_selector_p (enum cpp_ttype type)
30618 {
30619 return (type == CPP_NAME || type == CPP_KEYWORD
30620 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30621 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30622 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30623 || type == CPP_XOR || type == CPP_XOR_EQ);
30624 }
30625
30626 /* Parse an Objective-C selector. */
30627
30628 static tree
30629 cp_parser_objc_selector (cp_parser* parser)
30630 {
30631 cp_token *token = cp_lexer_consume_token (parser->lexer);
30632
30633 if (!cp_parser_objc_selector_p (token->type))
30634 {
30635 error_at (token->location, "invalid Objective-C++ selector name");
30636 return error_mark_node;
30637 }
30638
30639 /* C++ operator names are allowed to appear in ObjC selectors. */
30640 switch (token->type)
30641 {
30642 case CPP_AND_AND: return get_identifier ("and");
30643 case CPP_AND_EQ: return get_identifier ("and_eq");
30644 case CPP_AND: return get_identifier ("bitand");
30645 case CPP_OR: return get_identifier ("bitor");
30646 case CPP_COMPL: return get_identifier ("compl");
30647 case CPP_NOT: return get_identifier ("not");
30648 case CPP_NOT_EQ: return get_identifier ("not_eq");
30649 case CPP_OR_OR: return get_identifier ("or");
30650 case CPP_OR_EQ: return get_identifier ("or_eq");
30651 case CPP_XOR: return get_identifier ("xor");
30652 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30653 default: return token->u.value;
30654 }
30655 }
30656
30657 /* Parse an Objective-C params list. */
30658
30659 static tree
30660 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30661 {
30662 tree params = NULL_TREE;
30663 bool maybe_unary_selector_p = true;
30664 cp_token *token = cp_lexer_peek_token (parser->lexer);
30665
30666 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30667 {
30668 tree selector = NULL_TREE, type_name, identifier;
30669 tree parm_attr = NULL_TREE;
30670
30671 if (token->keyword == RID_ATTRIBUTE)
30672 break;
30673
30674 if (token->type != CPP_COLON)
30675 selector = cp_parser_objc_selector (parser);
30676
30677 /* Detect if we have a unary selector. */
30678 if (maybe_unary_selector_p
30679 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30680 {
30681 params = selector; /* Might be followed by attributes. */
30682 break;
30683 }
30684
30685 maybe_unary_selector_p = false;
30686 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30687 {
30688 /* Something went quite wrong. There should be a colon
30689 here, but there is not. Stop parsing parameters. */
30690 break;
30691 }
30692 type_name = cp_parser_objc_typename (parser);
30693 /* New ObjC allows attributes on parameters too. */
30694 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30695 parm_attr = cp_parser_attributes_opt (parser);
30696 identifier = cp_parser_identifier (parser);
30697
30698 params
30699 = chainon (params,
30700 objc_build_keyword_decl (selector,
30701 type_name,
30702 identifier,
30703 parm_attr));
30704
30705 token = cp_lexer_peek_token (parser->lexer);
30706 }
30707
30708 if (params == NULL_TREE)
30709 {
30710 cp_parser_error (parser, "objective-c++ method declaration is expected");
30711 return error_mark_node;
30712 }
30713
30714 /* We allow tail attributes for the method. */
30715 if (token->keyword == RID_ATTRIBUTE)
30716 {
30717 *attributes = cp_parser_attributes_opt (parser);
30718 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30719 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30720 return params;
30721 cp_parser_error (parser,
30722 "method attributes must be specified at the end");
30723 return error_mark_node;
30724 }
30725
30726 if (params == NULL_TREE)
30727 {
30728 cp_parser_error (parser, "objective-c++ method declaration is expected");
30729 return error_mark_node;
30730 }
30731 return params;
30732 }
30733
30734 /* Parse the non-keyword Objective-C params. */
30735
30736 static tree
30737 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30738 tree* attributes)
30739 {
30740 tree params = make_node (TREE_LIST);
30741 cp_token *token = cp_lexer_peek_token (parser->lexer);
30742 *ellipsisp = false; /* Initially, assume no ellipsis. */
30743
30744 while (token->type == CPP_COMMA)
30745 {
30746 cp_parameter_declarator *parmdecl;
30747 tree parm;
30748
30749 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30750 token = cp_lexer_peek_token (parser->lexer);
30751
30752 if (token->type == CPP_ELLIPSIS)
30753 {
30754 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30755 *ellipsisp = true;
30756 token = cp_lexer_peek_token (parser->lexer);
30757 break;
30758 }
30759
30760 /* TODO: parse attributes for tail parameters. */
30761 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
30762 false, NULL);
30763 parm = grokdeclarator (parmdecl->declarator,
30764 &parmdecl->decl_specifiers,
30765 PARM, /*initialized=*/0,
30766 /*attrlist=*/NULL);
30767
30768 chainon (params, build_tree_list (NULL_TREE, parm));
30769 token = cp_lexer_peek_token (parser->lexer);
30770 }
30771
30772 /* We allow tail attributes for the method. */
30773 if (token->keyword == RID_ATTRIBUTE)
30774 {
30775 if (*attributes == NULL_TREE)
30776 {
30777 *attributes = cp_parser_attributes_opt (parser);
30778 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30779 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30780 return params;
30781 }
30782 else
30783 /* We have an error, but parse the attributes, so that we can
30784 carry on. */
30785 *attributes = cp_parser_attributes_opt (parser);
30786
30787 cp_parser_error (parser,
30788 "method attributes must be specified at the end");
30789 return error_mark_node;
30790 }
30791
30792 return params;
30793 }
30794
30795 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30796
30797 static void
30798 cp_parser_objc_interstitial_code (cp_parser* parser)
30799 {
30800 cp_token *token = cp_lexer_peek_token (parser->lexer);
30801
30802 /* If the next token is `extern' and the following token is a string
30803 literal, then we have a linkage specification. */
30804 if (token->keyword == RID_EXTERN
30805 && cp_parser_is_pure_string_literal
30806 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30807 cp_parser_linkage_specification (parser);
30808 /* Handle #pragma, if any. */
30809 else if (token->type == CPP_PRAGMA)
30810 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30811 /* Allow stray semicolons. */
30812 else if (token->type == CPP_SEMICOLON)
30813 cp_lexer_consume_token (parser->lexer);
30814 /* Mark methods as optional or required, when building protocols. */
30815 else if (token->keyword == RID_AT_OPTIONAL)
30816 {
30817 cp_lexer_consume_token (parser->lexer);
30818 objc_set_method_opt (true);
30819 }
30820 else if (token->keyword == RID_AT_REQUIRED)
30821 {
30822 cp_lexer_consume_token (parser->lexer);
30823 objc_set_method_opt (false);
30824 }
30825 else if (token->keyword == RID_NAMESPACE)
30826 cp_parser_namespace_definition (parser);
30827 /* Other stray characters must generate errors. */
30828 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30829 {
30830 cp_lexer_consume_token (parser->lexer);
30831 error ("stray %qs between Objective-C++ methods",
30832 token->type == CPP_OPEN_BRACE ? "{" : "}");
30833 }
30834 /* Finally, try to parse a block-declaration, or a function-definition. */
30835 else
30836 cp_parser_block_declaration (parser, /*statement_p=*/false);
30837 }
30838
30839 /* Parse a method signature. */
30840
30841 static tree
30842 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30843 {
30844 tree rettype, kwdparms, optparms;
30845 bool ellipsis = false;
30846 bool is_class_method;
30847
30848 is_class_method = cp_parser_objc_method_type (parser);
30849 rettype = cp_parser_objc_typename (parser);
30850 *attributes = NULL_TREE;
30851 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30852 if (kwdparms == error_mark_node)
30853 return error_mark_node;
30854 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30855 if (optparms == error_mark_node)
30856 return error_mark_node;
30857
30858 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30859 }
30860
30861 static bool
30862 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30863 {
30864 tree tattr;
30865 cp_lexer_save_tokens (parser->lexer);
30866 tattr = cp_parser_attributes_opt (parser);
30867 gcc_assert (tattr) ;
30868
30869 /* If the attributes are followed by a method introducer, this is not allowed.
30870 Dump the attributes and flag the situation. */
30871 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30872 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30873 return true;
30874
30875 /* Otherwise, the attributes introduce some interstitial code, possibly so
30876 rewind to allow that check. */
30877 cp_lexer_rollback_tokens (parser->lexer);
30878 return false;
30879 }
30880
30881 /* Parse an Objective-C method prototype list. */
30882
30883 static void
30884 cp_parser_objc_method_prototype_list (cp_parser* parser)
30885 {
30886 cp_token *token = cp_lexer_peek_token (parser->lexer);
30887
30888 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30889 {
30890 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30891 {
30892 tree attributes, sig;
30893 bool is_class_method;
30894 if (token->type == CPP_PLUS)
30895 is_class_method = true;
30896 else
30897 is_class_method = false;
30898 sig = cp_parser_objc_method_signature (parser, &attributes);
30899 if (sig == error_mark_node)
30900 {
30901 cp_parser_skip_to_end_of_block_or_statement (parser);
30902 token = cp_lexer_peek_token (parser->lexer);
30903 continue;
30904 }
30905 objc_add_method_declaration (is_class_method, sig, attributes);
30906 cp_parser_consume_semicolon_at_end_of_statement (parser);
30907 }
30908 else if (token->keyword == RID_AT_PROPERTY)
30909 cp_parser_objc_at_property_declaration (parser);
30910 else if (token->keyword == RID_ATTRIBUTE
30911 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30912 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30913 OPT_Wattributes,
30914 "prefix attributes are ignored for methods");
30915 else
30916 /* Allow for interspersed non-ObjC++ code. */
30917 cp_parser_objc_interstitial_code (parser);
30918
30919 token = cp_lexer_peek_token (parser->lexer);
30920 }
30921
30922 if (token->type != CPP_EOF)
30923 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30924 else
30925 cp_parser_error (parser, "expected %<@end%>");
30926
30927 objc_finish_interface ();
30928 }
30929
30930 /* Parse an Objective-C method definition list. */
30931
30932 static void
30933 cp_parser_objc_method_definition_list (cp_parser* parser)
30934 {
30935 cp_token *token = cp_lexer_peek_token (parser->lexer);
30936
30937 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30938 {
30939 tree meth;
30940
30941 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30942 {
30943 cp_token *ptk;
30944 tree sig, attribute;
30945 bool is_class_method;
30946 if (token->type == CPP_PLUS)
30947 is_class_method = true;
30948 else
30949 is_class_method = false;
30950 push_deferring_access_checks (dk_deferred);
30951 sig = cp_parser_objc_method_signature (parser, &attribute);
30952 if (sig == error_mark_node)
30953 {
30954 cp_parser_skip_to_end_of_block_or_statement (parser);
30955 token = cp_lexer_peek_token (parser->lexer);
30956 continue;
30957 }
30958 objc_start_method_definition (is_class_method, sig, attribute,
30959 NULL_TREE);
30960
30961 /* For historical reasons, we accept an optional semicolon. */
30962 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30963 cp_lexer_consume_token (parser->lexer);
30964
30965 ptk = cp_lexer_peek_token (parser->lexer);
30966 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30967 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30968 {
30969 perform_deferred_access_checks (tf_warning_or_error);
30970 stop_deferring_access_checks ();
30971 meth = cp_parser_function_definition_after_declarator (parser,
30972 false);
30973 pop_deferring_access_checks ();
30974 objc_finish_method_definition (meth);
30975 }
30976 }
30977 /* The following case will be removed once @synthesize is
30978 completely implemented. */
30979 else if (token->keyword == RID_AT_PROPERTY)
30980 cp_parser_objc_at_property_declaration (parser);
30981 else if (token->keyword == RID_AT_SYNTHESIZE)
30982 cp_parser_objc_at_synthesize_declaration (parser);
30983 else if (token->keyword == RID_AT_DYNAMIC)
30984 cp_parser_objc_at_dynamic_declaration (parser);
30985 else if (token->keyword == RID_ATTRIBUTE
30986 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30987 warning_at (token->location, OPT_Wattributes,
30988 "prefix attributes are ignored for methods");
30989 else
30990 /* Allow for interspersed non-ObjC++ code. */
30991 cp_parser_objc_interstitial_code (parser);
30992
30993 token = cp_lexer_peek_token (parser->lexer);
30994 }
30995
30996 if (token->type != CPP_EOF)
30997 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30998 else
30999 cp_parser_error (parser, "expected %<@end%>");
31000
31001 objc_finish_implementation ();
31002 }
31003
31004 /* Parse Objective-C ivars. */
31005
31006 static void
31007 cp_parser_objc_class_ivars (cp_parser* parser)
31008 {
31009 cp_token *token = cp_lexer_peek_token (parser->lexer);
31010
31011 if (token->type != CPP_OPEN_BRACE)
31012 return; /* No ivars specified. */
31013
31014 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
31015 token = cp_lexer_peek_token (parser->lexer);
31016
31017 while (token->type != CPP_CLOSE_BRACE
31018 && token->keyword != RID_AT_END && token->type != CPP_EOF)
31019 {
31020 cp_decl_specifier_seq declspecs;
31021 int decl_class_or_enum_p;
31022 tree prefix_attributes;
31023
31024 cp_parser_objc_visibility_spec (parser);
31025
31026 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31027 break;
31028
31029 cp_parser_decl_specifier_seq (parser,
31030 CP_PARSER_FLAGS_OPTIONAL,
31031 &declspecs,
31032 &decl_class_or_enum_p);
31033
31034 /* auto, register, static, extern, mutable. */
31035 if (declspecs.storage_class != sc_none)
31036 {
31037 cp_parser_error (parser, "invalid type for instance variable");
31038 declspecs.storage_class = sc_none;
31039 }
31040
31041 /* thread_local. */
31042 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31043 {
31044 cp_parser_error (parser, "invalid type for instance variable");
31045 declspecs.locations[ds_thread] = 0;
31046 }
31047
31048 /* typedef. */
31049 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31050 {
31051 cp_parser_error (parser, "invalid type for instance variable");
31052 declspecs.locations[ds_typedef] = 0;
31053 }
31054
31055 prefix_attributes = declspecs.attributes;
31056 declspecs.attributes = NULL_TREE;
31057
31058 /* Keep going until we hit the `;' at the end of the
31059 declaration. */
31060 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31061 {
31062 tree width = NULL_TREE, attributes, first_attribute, decl;
31063 cp_declarator *declarator = NULL;
31064 int ctor_dtor_or_conv_p;
31065
31066 /* Check for a (possibly unnamed) bitfield declaration. */
31067 token = cp_lexer_peek_token (parser->lexer);
31068 if (token->type == CPP_COLON)
31069 goto eat_colon;
31070
31071 if (token->type == CPP_NAME
31072 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
31073 == CPP_COLON))
31074 {
31075 /* Get the name of the bitfield. */
31076 declarator = make_id_declarator (NULL_TREE,
31077 cp_parser_identifier (parser),
31078 sfk_none, token->location);
31079
31080 eat_colon:
31081 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31082 /* Get the width of the bitfield. */
31083 width
31084 = cp_parser_constant_expression (parser);
31085 }
31086 else
31087 {
31088 /* Parse the declarator. */
31089 declarator
31090 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31091 CP_PARSER_FLAGS_NONE,
31092 &ctor_dtor_or_conv_p,
31093 /*parenthesized_p=*/NULL,
31094 /*member_p=*/false,
31095 /*friend_p=*/false,
31096 /*static_p=*/false);
31097 }
31098
31099 /* Look for attributes that apply to the ivar. */
31100 attributes = cp_parser_attributes_opt (parser);
31101 /* Remember which attributes are prefix attributes and
31102 which are not. */
31103 first_attribute = attributes;
31104 /* Combine the attributes. */
31105 attributes = attr_chainon (prefix_attributes, attributes);
31106
31107 if (width)
31108 /* Create the bitfield declaration. */
31109 decl = grokbitfield (declarator, &declspecs,
31110 width, NULL_TREE, attributes);
31111 else
31112 decl = grokfield (declarator, &declspecs,
31113 NULL_TREE, /*init_const_expr_p=*/false,
31114 NULL_TREE, attributes);
31115
31116 /* Add the instance variable. */
31117 if (decl != error_mark_node && decl != NULL_TREE)
31118 objc_add_instance_variable (decl);
31119
31120 /* Reset PREFIX_ATTRIBUTES. */
31121 if (attributes != error_mark_node)
31122 {
31123 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31124 attributes = TREE_CHAIN (attributes);
31125 if (attributes)
31126 TREE_CHAIN (attributes) = NULL_TREE;
31127 }
31128
31129 token = cp_lexer_peek_token (parser->lexer);
31130
31131 if (token->type == CPP_COMMA)
31132 {
31133 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31134 continue;
31135 }
31136 break;
31137 }
31138
31139 cp_parser_consume_semicolon_at_end_of_statement (parser);
31140 token = cp_lexer_peek_token (parser->lexer);
31141 }
31142
31143 if (token->keyword == RID_AT_END)
31144 cp_parser_error (parser, "expected %<}%>");
31145
31146 /* Do not consume the RID_AT_END, so it will be read again as terminating
31147 the @interface of @implementation. */
31148 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
31149 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
31150
31151 /* For historical reasons, we accept an optional semicolon. */
31152 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31153 cp_lexer_consume_token (parser->lexer);
31154 }
31155
31156 /* Parse an Objective-C protocol declaration. */
31157
31158 static void
31159 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
31160 {
31161 tree proto, protorefs;
31162 cp_token *tok;
31163
31164 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
31165 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31166 {
31167 tok = cp_lexer_peek_token (parser->lexer);
31168 error_at (tok->location, "identifier expected after %<@protocol%>");
31169 cp_parser_consume_semicolon_at_end_of_statement (parser);
31170 return;
31171 }
31172
31173 /* See if we have a forward declaration or a definition. */
31174 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
31175
31176 /* Try a forward declaration first. */
31177 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
31178 {
31179 while (true)
31180 {
31181 tree id;
31182
31183 id = cp_parser_identifier (parser);
31184 if (id == error_mark_node)
31185 break;
31186
31187 objc_declare_protocol (id, attributes);
31188
31189 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31190 cp_lexer_consume_token (parser->lexer);
31191 else
31192 break;
31193 }
31194 cp_parser_consume_semicolon_at_end_of_statement (parser);
31195 }
31196
31197 /* Ok, we got a full-fledged definition (or at least should). */
31198 else
31199 {
31200 proto = cp_parser_identifier (parser);
31201 protorefs = cp_parser_objc_protocol_refs_opt (parser);
31202 objc_start_protocol (proto, protorefs, attributes);
31203 cp_parser_objc_method_prototype_list (parser);
31204 }
31205 }
31206
31207 /* Parse an Objective-C superclass or category. */
31208
31209 static void
31210 cp_parser_objc_superclass_or_category (cp_parser *parser,
31211 bool iface_p,
31212 tree *super,
31213 tree *categ, bool *is_class_extension)
31214 {
31215 cp_token *next = cp_lexer_peek_token (parser->lexer);
31216
31217 *super = *categ = NULL_TREE;
31218 *is_class_extension = false;
31219 if (next->type == CPP_COLON)
31220 {
31221 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31222 *super = cp_parser_identifier (parser);
31223 }
31224 else if (next->type == CPP_OPEN_PAREN)
31225 {
31226 matching_parens parens;
31227 parens.consume_open (parser); /* Eat '('. */
31228
31229 /* If there is no category name, and this is an @interface, we
31230 have a class extension. */
31231 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31232 {
31233 *categ = NULL_TREE;
31234 *is_class_extension = true;
31235 }
31236 else
31237 *categ = cp_parser_identifier (parser);
31238
31239 parens.require_close (parser);
31240 }
31241 }
31242
31243 /* Parse an Objective-C class interface. */
31244
31245 static void
31246 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
31247 {
31248 tree name, super, categ, protos;
31249 bool is_class_extension;
31250
31251 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
31252 name = cp_parser_identifier (parser);
31253 if (name == error_mark_node)
31254 {
31255 /* It's hard to recover because even if valid @interface stuff
31256 is to follow, we can't compile it (or validate it) if we
31257 don't even know which class it refers to. Let's assume this
31258 was a stray '@interface' token in the stream and skip it.
31259 */
31260 return;
31261 }
31262 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
31263 &is_class_extension);
31264 protos = cp_parser_objc_protocol_refs_opt (parser);
31265
31266 /* We have either a class or a category on our hands. */
31267 if (categ || is_class_extension)
31268 objc_start_category_interface (name, categ, protos, attributes);
31269 else
31270 {
31271 objc_start_class_interface (name, super, protos, attributes);
31272 /* Handle instance variable declarations, if any. */
31273 cp_parser_objc_class_ivars (parser);
31274 objc_continue_interface ();
31275 }
31276
31277 cp_parser_objc_method_prototype_list (parser);
31278 }
31279
31280 /* Parse an Objective-C class implementation. */
31281
31282 static void
31283 cp_parser_objc_class_implementation (cp_parser* parser)
31284 {
31285 tree name, super, categ;
31286 bool is_class_extension;
31287
31288 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
31289 name = cp_parser_identifier (parser);
31290 if (name == error_mark_node)
31291 {
31292 /* It's hard to recover because even if valid @implementation
31293 stuff is to follow, we can't compile it (or validate it) if
31294 we don't even know which class it refers to. Let's assume
31295 this was a stray '@implementation' token in the stream and
31296 skip it.
31297 */
31298 return;
31299 }
31300 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
31301 &is_class_extension);
31302
31303 /* We have either a class or a category on our hands. */
31304 if (categ)
31305 objc_start_category_implementation (name, categ);
31306 else
31307 {
31308 objc_start_class_implementation (name, super);
31309 /* Handle instance variable declarations, if any. */
31310 cp_parser_objc_class_ivars (parser);
31311 objc_continue_implementation ();
31312 }
31313
31314 cp_parser_objc_method_definition_list (parser);
31315 }
31316
31317 /* Consume the @end token and finish off the implementation. */
31318
31319 static void
31320 cp_parser_objc_end_implementation (cp_parser* parser)
31321 {
31322 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31323 objc_finish_implementation ();
31324 }
31325
31326 /* Parse an Objective-C declaration. */
31327
31328 static void
31329 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
31330 {
31331 /* Try to figure out what kind of declaration is present. */
31332 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31333
31334 if (attributes)
31335 switch (kwd->keyword)
31336 {
31337 case RID_AT_ALIAS:
31338 case RID_AT_CLASS:
31339 case RID_AT_END:
31340 error_at (kwd->location, "attributes may not be specified before"
31341 " the %<@%D%> Objective-C++ keyword",
31342 kwd->u.value);
31343 attributes = NULL;
31344 break;
31345 case RID_AT_IMPLEMENTATION:
31346 warning_at (kwd->location, OPT_Wattributes,
31347 "prefix attributes are ignored before %<@%D%>",
31348 kwd->u.value);
31349 attributes = NULL;
31350 default:
31351 break;
31352 }
31353
31354 switch (kwd->keyword)
31355 {
31356 case RID_AT_ALIAS:
31357 cp_parser_objc_alias_declaration (parser);
31358 break;
31359 case RID_AT_CLASS:
31360 cp_parser_objc_class_declaration (parser);
31361 break;
31362 case RID_AT_PROTOCOL:
31363 cp_parser_objc_protocol_declaration (parser, attributes);
31364 break;
31365 case RID_AT_INTERFACE:
31366 cp_parser_objc_class_interface (parser, attributes);
31367 break;
31368 case RID_AT_IMPLEMENTATION:
31369 cp_parser_objc_class_implementation (parser);
31370 break;
31371 case RID_AT_END:
31372 cp_parser_objc_end_implementation (parser);
31373 break;
31374 default:
31375 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31376 kwd->u.value);
31377 cp_parser_skip_to_end_of_block_or_statement (parser);
31378 }
31379 }
31380
31381 /* Parse an Objective-C try-catch-finally statement.
31382
31383 objc-try-catch-finally-stmt:
31384 @try compound-statement objc-catch-clause-seq [opt]
31385 objc-finally-clause [opt]
31386
31387 objc-catch-clause-seq:
31388 objc-catch-clause objc-catch-clause-seq [opt]
31389
31390 objc-catch-clause:
31391 @catch ( objc-exception-declaration ) compound-statement
31392
31393 objc-finally-clause:
31394 @finally compound-statement
31395
31396 objc-exception-declaration:
31397 parameter-declaration
31398 '...'
31399
31400 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31401
31402 Returns NULL_TREE.
31403
31404 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31405 for C. Keep them in sync. */
31406
31407 static tree
31408 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
31409 {
31410 location_t location;
31411 tree stmt;
31412
31413 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
31414 location = cp_lexer_peek_token (parser->lexer)->location;
31415 objc_maybe_warn_exceptions (location);
31416 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31417 node, lest it get absorbed into the surrounding block. */
31418 stmt = push_stmt_list ();
31419 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31420 objc_begin_try_stmt (location, pop_stmt_list (stmt));
31421
31422 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
31423 {
31424 cp_parameter_declarator *parm;
31425 tree parameter_declaration = error_mark_node;
31426 bool seen_open_paren = false;
31427 matching_parens parens;
31428
31429 cp_lexer_consume_token (parser->lexer);
31430 if (parens.require_open (parser))
31431 seen_open_paren = true;
31432 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31433 {
31434 /* We have "@catch (...)" (where the '...' are literally
31435 what is in the code). Skip the '...'.
31436 parameter_declaration is set to NULL_TREE, and
31437 objc_being_catch_clauses() knows that that means
31438 '...'. */
31439 cp_lexer_consume_token (parser->lexer);
31440 parameter_declaration = NULL_TREE;
31441 }
31442 else
31443 {
31444 /* We have "@catch (NSException *exception)" or something
31445 like that. Parse the parameter declaration. */
31446 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
31447 false, NULL);
31448 if (parm == NULL)
31449 parameter_declaration = error_mark_node;
31450 else
31451 parameter_declaration = grokdeclarator (parm->declarator,
31452 &parm->decl_specifiers,
31453 PARM, /*initialized=*/0,
31454 /*attrlist=*/NULL);
31455 }
31456 if (seen_open_paren)
31457 parens.require_close (parser);
31458 else
31459 {
31460 /* If there was no open parenthesis, we are recovering from
31461 an error, and we are trying to figure out what mistake
31462 the user has made. */
31463
31464 /* If there is an immediate closing parenthesis, the user
31465 probably forgot the opening one (ie, they typed "@catch
31466 NSException *e)". Parse the closing parenthesis and keep
31467 going. */
31468 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31469 cp_lexer_consume_token (parser->lexer);
31470
31471 /* If these is no immediate closing parenthesis, the user
31472 probably doesn't know that parenthesis are required at
31473 all (ie, they typed "@catch NSException *e"). So, just
31474 forget about the closing parenthesis and keep going. */
31475 }
31476 objc_begin_catch_clause (parameter_declaration);
31477 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31478 objc_finish_catch_clause ();
31479 }
31480 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
31481 {
31482 cp_lexer_consume_token (parser->lexer);
31483 location = cp_lexer_peek_token (parser->lexer)->location;
31484 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31485 node, lest it get absorbed into the surrounding block. */
31486 stmt = push_stmt_list ();
31487 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31488 objc_build_finally_clause (location, pop_stmt_list (stmt));
31489 }
31490
31491 return objc_finish_try_stmt ();
31492 }
31493
31494 /* Parse an Objective-C synchronized statement.
31495
31496 objc-synchronized-stmt:
31497 @synchronized ( expression ) compound-statement
31498
31499 Returns NULL_TREE. */
31500
31501 static tree
31502 cp_parser_objc_synchronized_statement (cp_parser *parser)
31503 {
31504 location_t location;
31505 tree lock, stmt;
31506
31507 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31508
31509 location = cp_lexer_peek_token (parser->lexer)->location;
31510 objc_maybe_warn_exceptions (location);
31511 matching_parens parens;
31512 parens.require_open (parser);
31513 lock = cp_parser_expression (parser);
31514 parens.require_close (parser);
31515
31516 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31517 node, lest it get absorbed into the surrounding block. */
31518 stmt = push_stmt_list ();
31519 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31520
31521 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31522 }
31523
31524 /* Parse an Objective-C throw statement.
31525
31526 objc-throw-stmt:
31527 @throw assignment-expression [opt] ;
31528
31529 Returns a constructed '@throw' statement. */
31530
31531 static tree
31532 cp_parser_objc_throw_statement (cp_parser *parser)
31533 {
31534 tree expr = NULL_TREE;
31535 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31536
31537 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31538
31539 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31540 expr = cp_parser_expression (parser);
31541
31542 cp_parser_consume_semicolon_at_end_of_statement (parser);
31543
31544 return objc_build_throw_stmt (loc, expr);
31545 }
31546
31547 /* Parse an Objective-C statement. */
31548
31549 static tree
31550 cp_parser_objc_statement (cp_parser * parser)
31551 {
31552 /* Try to figure out what kind of declaration is present. */
31553 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31554
31555 switch (kwd->keyword)
31556 {
31557 case RID_AT_TRY:
31558 return cp_parser_objc_try_catch_finally_statement (parser);
31559 case RID_AT_SYNCHRONIZED:
31560 return cp_parser_objc_synchronized_statement (parser);
31561 case RID_AT_THROW:
31562 return cp_parser_objc_throw_statement (parser);
31563 default:
31564 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31565 kwd->u.value);
31566 cp_parser_skip_to_end_of_block_or_statement (parser);
31567 }
31568
31569 return error_mark_node;
31570 }
31571
31572 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31573 look ahead to see if an objc keyword follows the attributes. This
31574 is to detect the use of prefix attributes on ObjC @interface and
31575 @protocol. */
31576
31577 static bool
31578 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31579 {
31580 cp_lexer_save_tokens (parser->lexer);
31581 *attrib = cp_parser_attributes_opt (parser);
31582 gcc_assert (*attrib);
31583 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31584 {
31585 cp_lexer_commit_tokens (parser->lexer);
31586 return true;
31587 }
31588 cp_lexer_rollback_tokens (parser->lexer);
31589 return false;
31590 }
31591
31592 /* This routine is a minimal replacement for
31593 c_parser_struct_declaration () used when parsing the list of
31594 types/names or ObjC++ properties. For example, when parsing the
31595 code
31596
31597 @property (readonly) int a, b, c;
31598
31599 this function is responsible for parsing "int a, int b, int c" and
31600 returning the declarations as CHAIN of DECLs.
31601
31602 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31603 similar parsing. */
31604 static tree
31605 cp_parser_objc_struct_declaration (cp_parser *parser)
31606 {
31607 tree decls = NULL_TREE;
31608 cp_decl_specifier_seq declspecs;
31609 int decl_class_or_enum_p;
31610 tree prefix_attributes;
31611
31612 cp_parser_decl_specifier_seq (parser,
31613 CP_PARSER_FLAGS_NONE,
31614 &declspecs,
31615 &decl_class_or_enum_p);
31616
31617 if (declspecs.type == error_mark_node)
31618 return error_mark_node;
31619
31620 /* auto, register, static, extern, mutable. */
31621 if (declspecs.storage_class != sc_none)
31622 {
31623 cp_parser_error (parser, "invalid type for property");
31624 declspecs.storage_class = sc_none;
31625 }
31626
31627 /* thread_local. */
31628 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31629 {
31630 cp_parser_error (parser, "invalid type for property");
31631 declspecs.locations[ds_thread] = 0;
31632 }
31633
31634 /* typedef. */
31635 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31636 {
31637 cp_parser_error (parser, "invalid type for property");
31638 declspecs.locations[ds_typedef] = 0;
31639 }
31640
31641 prefix_attributes = declspecs.attributes;
31642 declspecs.attributes = NULL_TREE;
31643
31644 /* Keep going until we hit the `;' at the end of the declaration. */
31645 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31646 {
31647 tree attributes, first_attribute, decl;
31648 cp_declarator *declarator;
31649 cp_token *token;
31650
31651 /* Parse the declarator. */
31652 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31653 CP_PARSER_FLAGS_NONE,
31654 NULL, NULL, false, false, false);
31655
31656 /* Look for attributes that apply to the ivar. */
31657 attributes = cp_parser_attributes_opt (parser);
31658 /* Remember which attributes are prefix attributes and
31659 which are not. */
31660 first_attribute = attributes;
31661 /* Combine the attributes. */
31662 attributes = attr_chainon (prefix_attributes, attributes);
31663
31664 decl = grokfield (declarator, &declspecs,
31665 NULL_TREE, /*init_const_expr_p=*/false,
31666 NULL_TREE, attributes);
31667
31668 if (decl == error_mark_node || decl == NULL_TREE)
31669 return error_mark_node;
31670
31671 /* Reset PREFIX_ATTRIBUTES. */
31672 if (attributes != error_mark_node)
31673 {
31674 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31675 attributes = TREE_CHAIN (attributes);
31676 if (attributes)
31677 TREE_CHAIN (attributes) = NULL_TREE;
31678 }
31679
31680 DECL_CHAIN (decl) = decls;
31681 decls = decl;
31682
31683 token = cp_lexer_peek_token (parser->lexer);
31684 if (token->type == CPP_COMMA)
31685 {
31686 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31687 continue;
31688 }
31689 else
31690 break;
31691 }
31692 return decls;
31693 }
31694
31695 /* Parse an Objective-C @property declaration. The syntax is:
31696
31697 objc-property-declaration:
31698 '@property' objc-property-attributes[opt] struct-declaration ;
31699
31700 objc-property-attributes:
31701 '(' objc-property-attribute-list ')'
31702
31703 objc-property-attribute-list:
31704 objc-property-attribute
31705 objc-property-attribute-list, objc-property-attribute
31706
31707 objc-property-attribute
31708 'getter' = identifier
31709 'setter' = identifier
31710 'readonly'
31711 'readwrite'
31712 'assign'
31713 'retain'
31714 'copy'
31715 'nonatomic'
31716
31717 For example:
31718 @property NSString *name;
31719 @property (readonly) id object;
31720 @property (retain, nonatomic, getter=getTheName) id name;
31721 @property int a, b, c;
31722
31723 PS: This function is identical to
31724 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31725 static void
31726 cp_parser_objc_at_property_declaration (cp_parser *parser)
31727 {
31728 /* The following variables hold the attributes of the properties as
31729 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31730 seen. When we see an attribute, we set them to 'true' (if they
31731 are boolean properties) or to the identifier (if they have an
31732 argument, ie, for getter and setter). Note that here we only
31733 parse the list of attributes, check the syntax and accumulate the
31734 attributes that we find. objc_add_property_declaration() will
31735 then process the information. */
31736 bool property_assign = false;
31737 bool property_copy = false;
31738 tree property_getter_ident = NULL_TREE;
31739 bool property_nonatomic = false;
31740 bool property_readonly = false;
31741 bool property_readwrite = false;
31742 bool property_retain = false;
31743 tree property_setter_ident = NULL_TREE;
31744
31745 /* 'properties' is the list of properties that we read. Usually a
31746 single one, but maybe more (eg, in "@property int a, b, c;" there
31747 are three). */
31748 tree properties;
31749 location_t loc;
31750
31751 loc = cp_lexer_peek_token (parser->lexer)->location;
31752
31753 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31754
31755 /* Parse the optional attribute list... */
31756 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31757 {
31758 /* Eat the '('. */
31759 matching_parens parens;
31760 parens.consume_open (parser);
31761
31762 while (true)
31763 {
31764 bool syntax_error = false;
31765 cp_token *token = cp_lexer_peek_token (parser->lexer);
31766 enum rid keyword;
31767
31768 if (token->type != CPP_NAME)
31769 {
31770 cp_parser_error (parser, "expected identifier");
31771 break;
31772 }
31773 keyword = C_RID_CODE (token->u.value);
31774 cp_lexer_consume_token (parser->lexer);
31775 switch (keyword)
31776 {
31777 case RID_ASSIGN: property_assign = true; break;
31778 case RID_COPY: property_copy = true; break;
31779 case RID_NONATOMIC: property_nonatomic = true; break;
31780 case RID_READONLY: property_readonly = true; break;
31781 case RID_READWRITE: property_readwrite = true; break;
31782 case RID_RETAIN: property_retain = true; break;
31783
31784 case RID_GETTER:
31785 case RID_SETTER:
31786 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31787 {
31788 if (keyword == RID_GETTER)
31789 cp_parser_error (parser,
31790 "missing %<=%> (after %<getter%> attribute)");
31791 else
31792 cp_parser_error (parser,
31793 "missing %<=%> (after %<setter%> attribute)");
31794 syntax_error = true;
31795 break;
31796 }
31797 cp_lexer_consume_token (parser->lexer); /* eat the = */
31798 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31799 {
31800 cp_parser_error (parser, "expected identifier");
31801 syntax_error = true;
31802 break;
31803 }
31804 if (keyword == RID_SETTER)
31805 {
31806 if (property_setter_ident != NULL_TREE)
31807 {
31808 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31809 cp_lexer_consume_token (parser->lexer);
31810 }
31811 else
31812 property_setter_ident = cp_parser_objc_selector (parser);
31813 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31814 cp_parser_error (parser, "setter name must terminate with %<:%>");
31815 else
31816 cp_lexer_consume_token (parser->lexer);
31817 }
31818 else
31819 {
31820 if (property_getter_ident != NULL_TREE)
31821 {
31822 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31823 cp_lexer_consume_token (parser->lexer);
31824 }
31825 else
31826 property_getter_ident = cp_parser_objc_selector (parser);
31827 }
31828 break;
31829 default:
31830 cp_parser_error (parser, "unknown property attribute");
31831 syntax_error = true;
31832 break;
31833 }
31834
31835 if (syntax_error)
31836 break;
31837
31838 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31839 cp_lexer_consume_token (parser->lexer);
31840 else
31841 break;
31842 }
31843
31844 /* FIXME: "@property (setter, assign);" will generate a spurious
31845 "error: expected ‘)’ before ‘,’ token". This is because
31846 cp_parser_require, unlike the C counterpart, will produce an
31847 error even if we are in error recovery. */
31848 if (!parens.require_close (parser))
31849 {
31850 cp_parser_skip_to_closing_parenthesis (parser,
31851 /*recovering=*/true,
31852 /*or_comma=*/false,
31853 /*consume_paren=*/true);
31854 }
31855 }
31856
31857 /* ... and the property declaration(s). */
31858 properties = cp_parser_objc_struct_declaration (parser);
31859
31860 if (properties == error_mark_node)
31861 {
31862 cp_parser_skip_to_end_of_statement (parser);
31863 /* If the next token is now a `;', consume it. */
31864 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31865 cp_lexer_consume_token (parser->lexer);
31866 return;
31867 }
31868
31869 if (properties == NULL_TREE)
31870 cp_parser_error (parser, "expected identifier");
31871 else
31872 {
31873 /* Comma-separated properties are chained together in
31874 reverse order; add them one by one. */
31875 properties = nreverse (properties);
31876
31877 for (; properties; properties = TREE_CHAIN (properties))
31878 objc_add_property_declaration (loc, copy_node (properties),
31879 property_readonly, property_readwrite,
31880 property_assign, property_retain,
31881 property_copy, property_nonatomic,
31882 property_getter_ident, property_setter_ident);
31883 }
31884
31885 cp_parser_consume_semicolon_at_end_of_statement (parser);
31886 }
31887
31888 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31889
31890 objc-synthesize-declaration:
31891 @synthesize objc-synthesize-identifier-list ;
31892
31893 objc-synthesize-identifier-list:
31894 objc-synthesize-identifier
31895 objc-synthesize-identifier-list, objc-synthesize-identifier
31896
31897 objc-synthesize-identifier
31898 identifier
31899 identifier = identifier
31900
31901 For example:
31902 @synthesize MyProperty;
31903 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31904
31905 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31906 for C. Keep them in sync.
31907 */
31908 static void
31909 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31910 {
31911 tree list = NULL_TREE;
31912 location_t loc;
31913 loc = cp_lexer_peek_token (parser->lexer)->location;
31914
31915 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31916 while (true)
31917 {
31918 tree property, ivar;
31919 property = cp_parser_identifier (parser);
31920 if (property == error_mark_node)
31921 {
31922 cp_parser_consume_semicolon_at_end_of_statement (parser);
31923 return;
31924 }
31925 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31926 {
31927 cp_lexer_consume_token (parser->lexer);
31928 ivar = cp_parser_identifier (parser);
31929 if (ivar == error_mark_node)
31930 {
31931 cp_parser_consume_semicolon_at_end_of_statement (parser);
31932 return;
31933 }
31934 }
31935 else
31936 ivar = NULL_TREE;
31937 list = chainon (list, build_tree_list (ivar, property));
31938 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31939 cp_lexer_consume_token (parser->lexer);
31940 else
31941 break;
31942 }
31943 cp_parser_consume_semicolon_at_end_of_statement (parser);
31944 objc_add_synthesize_declaration (loc, list);
31945 }
31946
31947 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31948
31949 objc-dynamic-declaration:
31950 @dynamic identifier-list ;
31951
31952 For example:
31953 @dynamic MyProperty;
31954 @dynamic MyProperty, AnotherProperty;
31955
31956 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31957 for C. Keep them in sync.
31958 */
31959 static void
31960 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31961 {
31962 tree list = NULL_TREE;
31963 location_t loc;
31964 loc = cp_lexer_peek_token (parser->lexer)->location;
31965
31966 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31967 while (true)
31968 {
31969 tree property;
31970 property = cp_parser_identifier (parser);
31971 if (property == error_mark_node)
31972 {
31973 cp_parser_consume_semicolon_at_end_of_statement (parser);
31974 return;
31975 }
31976 list = chainon (list, build_tree_list (NULL, property));
31977 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31978 cp_lexer_consume_token (parser->lexer);
31979 else
31980 break;
31981 }
31982 cp_parser_consume_semicolon_at_end_of_statement (parser);
31983 objc_add_dynamic_declaration (loc, list);
31984 }
31985
31986 \f
31987 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
31988
31989 /* Returns name of the next clause.
31990 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31991 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31992 returned and the token is consumed. */
31993
31994 static pragma_omp_clause
31995 cp_parser_omp_clause_name (cp_parser *parser)
31996 {
31997 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31998
31999 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32000 result = PRAGMA_OACC_CLAUSE_AUTO;
32001 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
32002 result = PRAGMA_OMP_CLAUSE_IF;
32003 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
32004 result = PRAGMA_OMP_CLAUSE_DEFAULT;
32005 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
32006 result = PRAGMA_OACC_CLAUSE_DELETE;
32007 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
32008 result = PRAGMA_OMP_CLAUSE_PRIVATE;
32009 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
32010 result = PRAGMA_OMP_CLAUSE_FOR;
32011 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32012 {
32013 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32014 const char *p = IDENTIFIER_POINTER (id);
32015
32016 switch (p[0])
32017 {
32018 case 'a':
32019 if (!strcmp ("aligned", p))
32020 result = PRAGMA_OMP_CLAUSE_ALIGNED;
32021 else if (!strcmp ("async", p))
32022 result = PRAGMA_OACC_CLAUSE_ASYNC;
32023 break;
32024 case 'c':
32025 if (!strcmp ("collapse", p))
32026 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
32027 else if (!strcmp ("copy", p))
32028 result = PRAGMA_OACC_CLAUSE_COPY;
32029 else if (!strcmp ("copyin", p))
32030 result = PRAGMA_OMP_CLAUSE_COPYIN;
32031 else if (!strcmp ("copyout", p))
32032 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32033 else if (!strcmp ("copyprivate", p))
32034 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
32035 else if (!strcmp ("create", p))
32036 result = PRAGMA_OACC_CLAUSE_CREATE;
32037 break;
32038 case 'd':
32039 if (!strcmp ("defaultmap", p))
32040 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
32041 else if (!strcmp ("depend", p))
32042 result = PRAGMA_OMP_CLAUSE_DEPEND;
32043 else if (!strcmp ("device", p))
32044 result = PRAGMA_OMP_CLAUSE_DEVICE;
32045 else if (!strcmp ("deviceptr", p))
32046 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
32047 else if (!strcmp ("device_resident", p))
32048 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
32049 else if (!strcmp ("dist_schedule", p))
32050 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
32051 break;
32052 case 'f':
32053 if (!strcmp ("final", p))
32054 result = PRAGMA_OMP_CLAUSE_FINAL;
32055 else if (!strcmp ("finalize", p))
32056 result = PRAGMA_OACC_CLAUSE_FINALIZE;
32057 else if (!strcmp ("firstprivate", p))
32058 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
32059 else if (!strcmp ("from", p))
32060 result = PRAGMA_OMP_CLAUSE_FROM;
32061 break;
32062 case 'g':
32063 if (!strcmp ("gang", p))
32064 result = PRAGMA_OACC_CLAUSE_GANG;
32065 else if (!strcmp ("grainsize", p))
32066 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
32067 break;
32068 case 'h':
32069 if (!strcmp ("hint", p))
32070 result = PRAGMA_OMP_CLAUSE_HINT;
32071 else if (!strcmp ("host", p))
32072 result = PRAGMA_OACC_CLAUSE_HOST;
32073 break;
32074 case 'i':
32075 if (!strcmp ("if_present", p))
32076 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
32077 else if (!strcmp ("in_reduction", p))
32078 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
32079 else if (!strcmp ("inbranch", p))
32080 result = PRAGMA_OMP_CLAUSE_INBRANCH;
32081 else if (!strcmp ("independent", p))
32082 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
32083 else if (!strcmp ("is_device_ptr", p))
32084 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
32085 break;
32086 case 'l':
32087 if (!strcmp ("lastprivate", p))
32088 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
32089 else if (!strcmp ("linear", p))
32090 result = PRAGMA_OMP_CLAUSE_LINEAR;
32091 else if (!strcmp ("link", p))
32092 result = PRAGMA_OMP_CLAUSE_LINK;
32093 break;
32094 case 'm':
32095 if (!strcmp ("map", p))
32096 result = PRAGMA_OMP_CLAUSE_MAP;
32097 else if (!strcmp ("mergeable", p))
32098 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
32099 break;
32100 case 'n':
32101 if (!strcmp ("nogroup", p))
32102 result = PRAGMA_OMP_CLAUSE_NOGROUP;
32103 else if (!strcmp ("nontemporal", p))
32104 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
32105 else if (!strcmp ("notinbranch", p))
32106 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
32107 else if (!strcmp ("nowait", p))
32108 result = PRAGMA_OMP_CLAUSE_NOWAIT;
32109 else if (!strcmp ("num_gangs", p))
32110 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
32111 else if (!strcmp ("num_tasks", p))
32112 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
32113 else if (!strcmp ("num_teams", p))
32114 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
32115 else if (!strcmp ("num_threads", p))
32116 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
32117 else if (!strcmp ("num_workers", p))
32118 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
32119 break;
32120 case 'o':
32121 if (!strcmp ("ordered", p))
32122 result = PRAGMA_OMP_CLAUSE_ORDERED;
32123 break;
32124 case 'p':
32125 if (!strcmp ("parallel", p))
32126 result = PRAGMA_OMP_CLAUSE_PARALLEL;
32127 else if (!strcmp ("present", p))
32128 result = PRAGMA_OACC_CLAUSE_PRESENT;
32129 else if (!strcmp ("present_or_copy", p)
32130 || !strcmp ("pcopy", p))
32131 result = PRAGMA_OACC_CLAUSE_COPY;
32132 else if (!strcmp ("present_or_copyin", p)
32133 || !strcmp ("pcopyin", p))
32134 result = PRAGMA_OACC_CLAUSE_COPYIN;
32135 else if (!strcmp ("present_or_copyout", p)
32136 || !strcmp ("pcopyout", p))
32137 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32138 else if (!strcmp ("present_or_create", p)
32139 || !strcmp ("pcreate", p))
32140 result = PRAGMA_OACC_CLAUSE_CREATE;
32141 else if (!strcmp ("priority", p))
32142 result = PRAGMA_OMP_CLAUSE_PRIORITY;
32143 else if (!strcmp ("proc_bind", p))
32144 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
32145 break;
32146 case 'r':
32147 if (!strcmp ("reduction", p))
32148 result = PRAGMA_OMP_CLAUSE_REDUCTION;
32149 break;
32150 case 's':
32151 if (!strcmp ("safelen", p))
32152 result = PRAGMA_OMP_CLAUSE_SAFELEN;
32153 else if (!strcmp ("schedule", p))
32154 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
32155 else if (!strcmp ("sections", p))
32156 result = PRAGMA_OMP_CLAUSE_SECTIONS;
32157 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
32158 result = PRAGMA_OACC_CLAUSE_HOST;
32159 else if (!strcmp ("seq", p))
32160 result = PRAGMA_OACC_CLAUSE_SEQ;
32161 else if (!strcmp ("shared", p))
32162 result = PRAGMA_OMP_CLAUSE_SHARED;
32163 else if (!strcmp ("simd", p))
32164 result = PRAGMA_OMP_CLAUSE_SIMD;
32165 else if (!strcmp ("simdlen", p))
32166 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
32167 break;
32168 case 't':
32169 if (!strcmp ("task_reduction", p))
32170 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
32171 else if (!strcmp ("taskgroup", p))
32172 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
32173 else if (!strcmp ("thread_limit", p))
32174 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
32175 else if (!strcmp ("threads", p))
32176 result = PRAGMA_OMP_CLAUSE_THREADS;
32177 else if (!strcmp ("tile", p))
32178 result = PRAGMA_OACC_CLAUSE_TILE;
32179 else if (!strcmp ("to", p))
32180 result = PRAGMA_OMP_CLAUSE_TO;
32181 break;
32182 case 'u':
32183 if (!strcmp ("uniform", p))
32184 result = PRAGMA_OMP_CLAUSE_UNIFORM;
32185 else if (!strcmp ("untied", p))
32186 result = PRAGMA_OMP_CLAUSE_UNTIED;
32187 else if (!strcmp ("use_device", p))
32188 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
32189 else if (!strcmp ("use_device_ptr", p))
32190 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
32191 break;
32192 case 'v':
32193 if (!strcmp ("vector", p))
32194 result = PRAGMA_OACC_CLAUSE_VECTOR;
32195 else if (!strcmp ("vector_length", p))
32196 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
32197 break;
32198 case 'w':
32199 if (!strcmp ("wait", p))
32200 result = PRAGMA_OACC_CLAUSE_WAIT;
32201 else if (!strcmp ("worker", p))
32202 result = PRAGMA_OACC_CLAUSE_WORKER;
32203 break;
32204 }
32205 }
32206
32207 if (result != PRAGMA_OMP_CLAUSE_NONE)
32208 cp_lexer_consume_token (parser->lexer);
32209
32210 return result;
32211 }
32212
32213 /* Validate that a clause of the given type does not already exist. */
32214
32215 static void
32216 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
32217 const char *name, location_t location)
32218 {
32219 tree c;
32220
32221 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
32222 if (OMP_CLAUSE_CODE (c) == code)
32223 {
32224 error_at (location, "too many %qs clauses", name);
32225 break;
32226 }
32227 }
32228
32229 /* OpenMP 2.5:
32230 variable-list:
32231 identifier
32232 variable-list , identifier
32233
32234 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
32235 colon). An opening parenthesis will have been consumed by the caller.
32236
32237 If KIND is nonzero, create the appropriate node and install the decl
32238 in OMP_CLAUSE_DECL and add the node to the head of the list.
32239
32240 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
32241 return the list created.
32242
32243 COLON can be NULL if only closing parenthesis should end the list,
32244 or pointer to bool which will receive false if the list is terminated
32245 by closing parenthesis or true if the list is terminated by colon. */
32246
32247 static tree
32248 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
32249 tree list, bool *colon)
32250 {
32251 cp_token *token;
32252 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32253 if (colon)
32254 {
32255 parser->colon_corrects_to_scope_p = false;
32256 *colon = false;
32257 }
32258 while (1)
32259 {
32260 tree name, decl;
32261
32262 if (kind == OMP_CLAUSE_DEPEND)
32263 cp_parser_parse_tentatively (parser);
32264 token = cp_lexer_peek_token (parser->lexer);
32265 if (kind != 0
32266 && current_class_ptr
32267 && cp_parser_is_keyword (token, RID_THIS))
32268 {
32269 decl = finish_this_expr ();
32270 if (TREE_CODE (decl) == NON_LVALUE_EXPR
32271 || CONVERT_EXPR_P (decl))
32272 decl = TREE_OPERAND (decl, 0);
32273 cp_lexer_consume_token (parser->lexer);
32274 }
32275 else
32276 {
32277 name = cp_parser_id_expression (parser, /*template_p=*/false,
32278 /*check_dependency_p=*/true,
32279 /*template_p=*/NULL,
32280 /*declarator_p=*/false,
32281 /*optional_p=*/false);
32282 if (name == error_mark_node)
32283 {
32284 if (kind == OMP_CLAUSE_DEPEND
32285 && cp_parser_simulate_error (parser))
32286 goto depend_lvalue;
32287 goto skip_comma;
32288 }
32289
32290 if (identifier_p (name))
32291 decl = cp_parser_lookup_name_simple (parser, name, token->location);
32292 else
32293 decl = name;
32294 if (decl == error_mark_node)
32295 {
32296 if (kind == OMP_CLAUSE_DEPEND
32297 && cp_parser_simulate_error (parser))
32298 goto depend_lvalue;
32299 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
32300 token->location);
32301 }
32302 }
32303 if (decl == error_mark_node)
32304 ;
32305 else if (kind != 0)
32306 {
32307 switch (kind)
32308 {
32309 case OMP_CLAUSE__CACHE_:
32310 /* The OpenACC cache directive explicitly only allows "array
32311 elements or subarrays". */
32312 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
32313 {
32314 error_at (token->location, "expected %<[%>");
32315 decl = error_mark_node;
32316 break;
32317 }
32318 /* FALLTHROUGH. */
32319 case OMP_CLAUSE_MAP:
32320 case OMP_CLAUSE_FROM:
32321 case OMP_CLAUSE_TO:
32322 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
32323 {
32324 location_t loc
32325 = cp_lexer_peek_token (parser->lexer)->location;
32326 cp_id_kind idk = CP_ID_KIND_NONE;
32327 cp_lexer_consume_token (parser->lexer);
32328 decl = convert_from_reference (decl);
32329 decl
32330 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
32331 decl, false,
32332 &idk, loc);
32333 }
32334 /* FALLTHROUGH. */
32335 case OMP_CLAUSE_DEPEND:
32336 case OMP_CLAUSE_REDUCTION:
32337 case OMP_CLAUSE_IN_REDUCTION:
32338 case OMP_CLAUSE_TASK_REDUCTION:
32339 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
32340 {
32341 tree low_bound = NULL_TREE, length = NULL_TREE;
32342
32343 parser->colon_corrects_to_scope_p = false;
32344 cp_lexer_consume_token (parser->lexer);
32345 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32346 low_bound = cp_parser_expression (parser);
32347 if (!colon)
32348 parser->colon_corrects_to_scope_p
32349 = saved_colon_corrects_to_scope_p;
32350 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
32351 length = integer_one_node;
32352 else
32353 {
32354 /* Look for `:'. */
32355 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32356 {
32357 if (kind == OMP_CLAUSE_DEPEND
32358 && cp_parser_simulate_error (parser))
32359 goto depend_lvalue;
32360 goto skip_comma;
32361 }
32362 if (kind == OMP_CLAUSE_DEPEND)
32363 cp_parser_commit_to_tentative_parse (parser);
32364 if (!cp_lexer_next_token_is (parser->lexer,
32365 CPP_CLOSE_SQUARE))
32366 length = cp_parser_expression (parser);
32367 }
32368 /* Look for the closing `]'. */
32369 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
32370 RT_CLOSE_SQUARE))
32371 {
32372 if (kind == OMP_CLAUSE_DEPEND
32373 && cp_parser_simulate_error (parser))
32374 goto depend_lvalue;
32375 goto skip_comma;
32376 }
32377
32378 decl = tree_cons (low_bound, length, decl);
32379 }
32380 break;
32381 default:
32382 break;
32383 }
32384
32385 if (kind == OMP_CLAUSE_DEPEND)
32386 {
32387 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
32388 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32389 && cp_parser_simulate_error (parser))
32390 {
32391 depend_lvalue:
32392 cp_parser_abort_tentative_parse (parser);
32393 decl = cp_parser_assignment_expression (parser, NULL,
32394 false, false);
32395 }
32396 else
32397 cp_parser_parse_definitely (parser);
32398 }
32399
32400 tree u = build_omp_clause (token->location, kind);
32401 OMP_CLAUSE_DECL (u) = decl;
32402 OMP_CLAUSE_CHAIN (u) = list;
32403 list = u;
32404 }
32405 else
32406 list = tree_cons (decl, NULL_TREE, list);
32407
32408 get_comma:
32409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32410 break;
32411 cp_lexer_consume_token (parser->lexer);
32412 }
32413
32414 if (colon)
32415 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32416
32417 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32418 {
32419 *colon = true;
32420 cp_parser_require (parser, CPP_COLON, RT_COLON);
32421 return list;
32422 }
32423
32424 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32425 {
32426 int ending;
32427
32428 /* Try to resync to an unnested comma. Copied from
32429 cp_parser_parenthesized_expression_list. */
32430 skip_comma:
32431 if (colon)
32432 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32433 ending = cp_parser_skip_to_closing_parenthesis (parser,
32434 /*recovering=*/true,
32435 /*or_comma=*/true,
32436 /*consume_paren=*/true);
32437 if (ending < 0)
32438 goto get_comma;
32439 }
32440
32441 return list;
32442 }
32443
32444 /* Similarly, but expect leading and trailing parenthesis. This is a very
32445 common case for omp clauses. */
32446
32447 static tree
32448 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
32449 {
32450 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32451 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
32452 return list;
32453 }
32454
32455 /* OpenACC 2.0:
32456 copy ( variable-list )
32457 copyin ( variable-list )
32458 copyout ( variable-list )
32459 create ( variable-list )
32460 delete ( variable-list )
32461 present ( variable-list ) */
32462
32463 static tree
32464 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
32465 tree list)
32466 {
32467 enum gomp_map_kind kind;
32468 switch (c_kind)
32469 {
32470 case PRAGMA_OACC_CLAUSE_COPY:
32471 kind = GOMP_MAP_TOFROM;
32472 break;
32473 case PRAGMA_OACC_CLAUSE_COPYIN:
32474 kind = GOMP_MAP_TO;
32475 break;
32476 case PRAGMA_OACC_CLAUSE_COPYOUT:
32477 kind = GOMP_MAP_FROM;
32478 break;
32479 case PRAGMA_OACC_CLAUSE_CREATE:
32480 kind = GOMP_MAP_ALLOC;
32481 break;
32482 case PRAGMA_OACC_CLAUSE_DELETE:
32483 kind = GOMP_MAP_RELEASE;
32484 break;
32485 case PRAGMA_OACC_CLAUSE_DEVICE:
32486 kind = GOMP_MAP_FORCE_TO;
32487 break;
32488 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32489 kind = GOMP_MAP_DEVICE_RESIDENT;
32490 break;
32491 case PRAGMA_OACC_CLAUSE_HOST:
32492 kind = GOMP_MAP_FORCE_FROM;
32493 break;
32494 case PRAGMA_OACC_CLAUSE_LINK:
32495 kind = GOMP_MAP_LINK;
32496 break;
32497 case PRAGMA_OACC_CLAUSE_PRESENT:
32498 kind = GOMP_MAP_FORCE_PRESENT;
32499 break;
32500 default:
32501 gcc_unreachable ();
32502 }
32503 tree nl, c;
32504 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32505
32506 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32507 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32508
32509 return nl;
32510 }
32511
32512 /* OpenACC 2.0:
32513 deviceptr ( variable-list ) */
32514
32515 static tree
32516 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32517 {
32518 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32519 tree vars, t;
32520
32521 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32522 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32523 variable-list must only allow for pointer variables. */
32524 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32525 for (t = vars; t; t = TREE_CHAIN (t))
32526 {
32527 tree v = TREE_PURPOSE (t);
32528 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32529 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32530 OMP_CLAUSE_DECL (u) = v;
32531 OMP_CLAUSE_CHAIN (u) = list;
32532 list = u;
32533 }
32534
32535 return list;
32536 }
32537
32538 /* OpenACC 2.5:
32539 auto
32540 finalize
32541 independent
32542 nohost
32543 seq */
32544
32545 static tree
32546 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
32547 enum omp_clause_code code,
32548 tree list, location_t location)
32549 {
32550 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32551 tree c = build_omp_clause (location, code);
32552 OMP_CLAUSE_CHAIN (c) = list;
32553 return c;
32554 }
32555
32556 /* OpenACC:
32557 num_gangs ( expression )
32558 num_workers ( expression )
32559 vector_length ( expression ) */
32560
32561 static tree
32562 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32563 const char *str, tree list)
32564 {
32565 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32566
32567 matching_parens parens;
32568 if (!parens.require_open (parser))
32569 return list;
32570
32571 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32572
32573 if (t == error_mark_node
32574 || !parens.require_close (parser))
32575 {
32576 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32577 /*or_comma=*/false,
32578 /*consume_paren=*/true);
32579 return list;
32580 }
32581
32582 check_no_duplicate_clause (list, code, str, loc);
32583
32584 tree c = build_omp_clause (loc, code);
32585 OMP_CLAUSE_OPERAND (c, 0) = t;
32586 OMP_CLAUSE_CHAIN (c) = list;
32587 return c;
32588 }
32589
32590 /* OpenACC:
32591
32592 gang [( gang-arg-list )]
32593 worker [( [num:] int-expr )]
32594 vector [( [length:] int-expr )]
32595
32596 where gang-arg is one of:
32597
32598 [num:] int-expr
32599 static: size-expr
32600
32601 and size-expr may be:
32602
32603 *
32604 int-expr
32605 */
32606
32607 static tree
32608 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
32609 const char *str, tree list)
32610 {
32611 const char *id = "num";
32612 cp_lexer *lexer = parser->lexer;
32613 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
32614 location_t loc = cp_lexer_peek_token (lexer)->location;
32615
32616 if (kind == OMP_CLAUSE_VECTOR)
32617 id = "length";
32618
32619 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32620 {
32621 matching_parens parens;
32622 parens.consume_open (parser);
32623
32624 do
32625 {
32626 cp_token *next = cp_lexer_peek_token (lexer);
32627 int idx = 0;
32628
32629 /* Gang static argument. */
32630 if (kind == OMP_CLAUSE_GANG
32631 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32632 {
32633 cp_lexer_consume_token (lexer);
32634
32635 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32636 goto cleanup_error;
32637
32638 idx = 1;
32639 if (ops[idx] != NULL)
32640 {
32641 cp_parser_error (parser, "too many %<static%> arguments");
32642 goto cleanup_error;
32643 }
32644
32645 /* Check for the '*' argument. */
32646 if (cp_lexer_next_token_is (lexer, CPP_MULT)
32647 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32648 || cp_lexer_nth_token_is (parser->lexer, 2,
32649 CPP_CLOSE_PAREN)))
32650 {
32651 cp_lexer_consume_token (lexer);
32652 ops[idx] = integer_minus_one_node;
32653
32654 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32655 {
32656 cp_lexer_consume_token (lexer);
32657 continue;
32658 }
32659 else break;
32660 }
32661 }
32662 /* Worker num: argument and vector length: arguments. */
32663 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32664 && id_equal (next->u.value, id)
32665 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32666 {
32667 cp_lexer_consume_token (lexer); /* id */
32668 cp_lexer_consume_token (lexer); /* ':' */
32669 }
32670
32671 /* Now collect the actual argument. */
32672 if (ops[idx] != NULL_TREE)
32673 {
32674 cp_parser_error (parser, "unexpected argument");
32675 goto cleanup_error;
32676 }
32677
32678 tree expr = cp_parser_assignment_expression (parser, NULL, false,
32679 false);
32680 if (expr == error_mark_node)
32681 goto cleanup_error;
32682
32683 mark_exp_read (expr);
32684 ops[idx] = expr;
32685
32686 if (kind == OMP_CLAUSE_GANG
32687 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32688 {
32689 cp_lexer_consume_token (lexer);
32690 continue;
32691 }
32692 break;
32693 }
32694 while (1);
32695
32696 if (!parens.require_close (parser))
32697 goto cleanup_error;
32698 }
32699
32700 check_no_duplicate_clause (list, kind, str, loc);
32701
32702 c = build_omp_clause (loc, kind);
32703
32704 if (ops[1])
32705 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32706
32707 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32708 OMP_CLAUSE_CHAIN (c) = list;
32709
32710 return c;
32711
32712 cleanup_error:
32713 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32714 return list;
32715 }
32716
32717 /* OpenACC 2.0:
32718 tile ( size-expr-list ) */
32719
32720 static tree
32721 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32722 {
32723 tree c, expr = error_mark_node;
32724 tree tile = NULL_TREE;
32725
32726 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32727 so, but the spec authors never considered such a case and have
32728 differing opinions on what it might mean, including 'not
32729 allowed'.) */
32730 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32731 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32732 clause_loc);
32733
32734 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32735 return list;
32736
32737 do
32738 {
32739 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32740 return list;
32741
32742 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32743 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32744 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32745 {
32746 cp_lexer_consume_token (parser->lexer);
32747 expr = integer_zero_node;
32748 }
32749 else
32750 expr = cp_parser_constant_expression (parser);
32751
32752 tile = tree_cons (NULL_TREE, expr, tile);
32753 }
32754 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32755
32756 /* Consume the trailing ')'. */
32757 cp_lexer_consume_token (parser->lexer);
32758
32759 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32760 tile = nreverse (tile);
32761 OMP_CLAUSE_TILE_LIST (c) = tile;
32762 OMP_CLAUSE_CHAIN (c) = list;
32763 return c;
32764 }
32765
32766 /* OpenACC 2.0
32767 Parse wait clause or directive parameters. */
32768
32769 static tree
32770 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32771 {
32772 vec<tree, va_gc> *args;
32773 tree t, args_tree;
32774
32775 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32776 /*cast_p=*/false,
32777 /*allow_expansion_p=*/true,
32778 /*non_constant_p=*/NULL);
32779
32780 if (args == NULL || args->length () == 0)
32781 {
32782 if (args != NULL)
32783 {
32784 cp_parser_error (parser, "expected integer expression list");
32785 release_tree_vector (args);
32786 }
32787 return list;
32788 }
32789
32790 args_tree = build_tree_list_vec (args);
32791
32792 release_tree_vector (args);
32793
32794 for (t = args_tree; t; t = TREE_CHAIN (t))
32795 {
32796 tree targ = TREE_VALUE (t);
32797
32798 if (targ != error_mark_node)
32799 {
32800 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32801 error ("%<wait%> expression must be integral");
32802 else
32803 {
32804 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32805
32806 targ = mark_rvalue_use (targ);
32807 OMP_CLAUSE_DECL (c) = targ;
32808 OMP_CLAUSE_CHAIN (c) = list;
32809 list = c;
32810 }
32811 }
32812 }
32813
32814 return list;
32815 }
32816
32817 /* OpenACC:
32818 wait ( int-expr-list ) */
32819
32820 static tree
32821 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32822 {
32823 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32824
32825 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32826 return list;
32827
32828 list = cp_parser_oacc_wait_list (parser, location, list);
32829
32830 return list;
32831 }
32832
32833 /* OpenMP 3.0:
32834 collapse ( constant-expression ) */
32835
32836 static tree
32837 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32838 {
32839 tree c, num;
32840 location_t loc;
32841 HOST_WIDE_INT n;
32842
32843 loc = cp_lexer_peek_token (parser->lexer)->location;
32844 matching_parens parens;
32845 if (!parens.require_open (parser))
32846 return list;
32847
32848 num = cp_parser_constant_expression (parser);
32849
32850 if (!parens.require_close (parser))
32851 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32852 /*or_comma=*/false,
32853 /*consume_paren=*/true);
32854
32855 if (num == error_mark_node)
32856 return list;
32857 num = fold_non_dependent_expr (num);
32858 if (!tree_fits_shwi_p (num)
32859 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32860 || (n = tree_to_shwi (num)) <= 0
32861 || (int) n != n)
32862 {
32863 error_at (loc, "collapse argument needs positive constant integer expression");
32864 return list;
32865 }
32866
32867 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32868 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32869 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32870 OMP_CLAUSE_CHAIN (c) = list;
32871 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32872
32873 return c;
32874 }
32875
32876 /* OpenMP 2.5:
32877 default ( none | shared )
32878
32879 OpenACC:
32880 default ( none | present ) */
32881
32882 static tree
32883 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32884 location_t location, bool is_oacc)
32885 {
32886 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32887 tree c;
32888
32889 matching_parens parens;
32890 if (!parens.require_open (parser))
32891 return list;
32892 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32893 {
32894 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32895 const char *p = IDENTIFIER_POINTER (id);
32896
32897 switch (p[0])
32898 {
32899 case 'n':
32900 if (strcmp ("none", p) != 0)
32901 goto invalid_kind;
32902 kind = OMP_CLAUSE_DEFAULT_NONE;
32903 break;
32904
32905 case 'p':
32906 if (strcmp ("present", p) != 0 || !is_oacc)
32907 goto invalid_kind;
32908 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32909 break;
32910
32911 case 's':
32912 if (strcmp ("shared", p) != 0 || is_oacc)
32913 goto invalid_kind;
32914 kind = OMP_CLAUSE_DEFAULT_SHARED;
32915 break;
32916
32917 default:
32918 goto invalid_kind;
32919 }
32920
32921 cp_lexer_consume_token (parser->lexer);
32922 }
32923 else
32924 {
32925 invalid_kind:
32926 if (is_oacc)
32927 cp_parser_error (parser, "expected %<none%> or %<present%>");
32928 else
32929 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32930 }
32931
32932 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32933 || !parens.require_close (parser))
32934 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32935 /*or_comma=*/false,
32936 /*consume_paren=*/true);
32937
32938 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32939 return list;
32940
32941 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32942 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32943 OMP_CLAUSE_CHAIN (c) = list;
32944 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32945
32946 return c;
32947 }
32948
32949 /* OpenMP 3.1:
32950 final ( expression ) */
32951
32952 static tree
32953 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32954 {
32955 tree t, c;
32956
32957 matching_parens parens;
32958 if (!parens.require_open (parser))
32959 return list;
32960
32961 t = cp_parser_assignment_expression (parser);
32962
32963 if (t == error_mark_node
32964 || !parens.require_close (parser))
32965 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32966 /*or_comma=*/false,
32967 /*consume_paren=*/true);
32968
32969 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32970
32971 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32972 OMP_CLAUSE_FINAL_EXPR (c) = t;
32973 OMP_CLAUSE_CHAIN (c) = list;
32974
32975 return c;
32976 }
32977
32978 /* OpenMP 2.5:
32979 if ( expression )
32980
32981 OpenMP 4.5:
32982 if ( directive-name-modifier : expression )
32983
32984 directive-name-modifier:
32985 parallel | task | taskloop | target data | target | target update
32986 | target enter data | target exit data
32987
32988 OpenMP 5.0:
32989 directive-name-modifier:
32990 ... | simd | cancel */
32991
32992 static tree
32993 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32994 bool is_omp)
32995 {
32996 tree t, c;
32997 enum tree_code if_modifier = ERROR_MARK;
32998
32999 matching_parens parens;
33000 if (!parens.require_open (parser))
33001 return list;
33002
33003 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33004 {
33005 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33006 const char *p = IDENTIFIER_POINTER (id);
33007 int n = 2;
33008
33009 if (strcmp ("cancel", p) == 0)
33010 if_modifier = VOID_CST;
33011 else if (strcmp ("parallel", p) == 0)
33012 if_modifier = OMP_PARALLEL;
33013 else if (strcmp ("simd", p) == 0)
33014 if_modifier = OMP_SIMD;
33015 else if (strcmp ("task", p) == 0)
33016 if_modifier = OMP_TASK;
33017 else if (strcmp ("taskloop", p) == 0)
33018 if_modifier = OMP_TASKLOOP;
33019 else if (strcmp ("target", p) == 0)
33020 {
33021 if_modifier = OMP_TARGET;
33022 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
33023 {
33024 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
33025 p = IDENTIFIER_POINTER (id);
33026 if (strcmp ("data", p) == 0)
33027 if_modifier = OMP_TARGET_DATA;
33028 else if (strcmp ("update", p) == 0)
33029 if_modifier = OMP_TARGET_UPDATE;
33030 else if (strcmp ("enter", p) == 0)
33031 if_modifier = OMP_TARGET_ENTER_DATA;
33032 else if (strcmp ("exit", p) == 0)
33033 if_modifier = OMP_TARGET_EXIT_DATA;
33034 if (if_modifier != OMP_TARGET)
33035 n = 3;
33036 else
33037 {
33038 location_t loc
33039 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
33040 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
33041 "or %<exit%>");
33042 if_modifier = ERROR_MARK;
33043 }
33044 if (if_modifier == OMP_TARGET_ENTER_DATA
33045 || if_modifier == OMP_TARGET_EXIT_DATA)
33046 {
33047 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
33048 {
33049 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
33050 p = IDENTIFIER_POINTER (id);
33051 if (strcmp ("data", p) == 0)
33052 n = 4;
33053 }
33054 if (n != 4)
33055 {
33056 location_t loc
33057 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
33058 error_at (loc, "expected %<data%>");
33059 if_modifier = ERROR_MARK;
33060 }
33061 }
33062 }
33063 }
33064 if (if_modifier != ERROR_MARK)
33065 {
33066 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
33067 {
33068 while (n-- > 0)
33069 cp_lexer_consume_token (parser->lexer);
33070 }
33071 else
33072 {
33073 if (n > 2)
33074 {
33075 location_t loc
33076 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
33077 error_at (loc, "expected %<:%>");
33078 }
33079 if_modifier = ERROR_MARK;
33080 }
33081 }
33082 }
33083
33084 t = cp_parser_assignment_expression (parser);
33085
33086 if (t == error_mark_node
33087 || !parens.require_close (parser))
33088 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33089 /*or_comma=*/false,
33090 /*consume_paren=*/true);
33091
33092 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33093 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
33094 {
33095 if (if_modifier != ERROR_MARK
33096 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33097 {
33098 const char *p = NULL;
33099 switch (if_modifier)
33100 {
33101 case VOID_CST: p = "cancel"; break;
33102 case OMP_PARALLEL: p = "parallel"; break;
33103 case OMP_SIMD: p = "simd"; break;
33104 case OMP_TASK: p = "task"; break;
33105 case OMP_TASKLOOP: p = "taskloop"; break;
33106 case OMP_TARGET_DATA: p = "target data"; break;
33107 case OMP_TARGET: p = "target"; break;
33108 case OMP_TARGET_UPDATE: p = "target update"; break;
33109 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
33110 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
33111 default: gcc_unreachable ();
33112 }
33113 error_at (location, "too many %<if%> clauses with %qs modifier",
33114 p);
33115 return list;
33116 }
33117 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33118 {
33119 if (!is_omp)
33120 error_at (location, "too many %<if%> clauses");
33121 else
33122 error_at (location, "too many %<if%> clauses without modifier");
33123 return list;
33124 }
33125 else if (if_modifier == ERROR_MARK
33126 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
33127 {
33128 error_at (location, "if any %<if%> clause has modifier, then all "
33129 "%<if%> clauses have to use modifier");
33130 return list;
33131 }
33132 }
33133
33134 c = build_omp_clause (location, OMP_CLAUSE_IF);
33135 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
33136 OMP_CLAUSE_IF_EXPR (c) = t;
33137 OMP_CLAUSE_CHAIN (c) = list;
33138
33139 return c;
33140 }
33141
33142 /* OpenMP 3.1:
33143 mergeable */
33144
33145 static tree
33146 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
33147 tree list, location_t location)
33148 {
33149 tree c;
33150
33151 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
33152 location);
33153
33154 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
33155 OMP_CLAUSE_CHAIN (c) = list;
33156 return c;
33157 }
33158
33159 /* OpenMP 2.5:
33160 nowait */
33161
33162 static tree
33163 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
33164 tree list, location_t location)
33165 {
33166 tree c;
33167
33168 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
33169
33170 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
33171 OMP_CLAUSE_CHAIN (c) = list;
33172 return c;
33173 }
33174
33175 /* OpenMP 2.5:
33176 num_threads ( expression ) */
33177
33178 static tree
33179 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
33180 location_t location)
33181 {
33182 tree t, c;
33183
33184 matching_parens parens;
33185 if (!parens.require_open (parser))
33186 return list;
33187
33188 t = cp_parser_assignment_expression (parser);
33189
33190 if (t == error_mark_node
33191 || !parens.require_close (parser))
33192 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33193 /*or_comma=*/false,
33194 /*consume_paren=*/true);
33195
33196 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
33197 "num_threads", location);
33198
33199 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
33200 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
33201 OMP_CLAUSE_CHAIN (c) = list;
33202
33203 return c;
33204 }
33205
33206 /* OpenMP 4.5:
33207 num_tasks ( expression ) */
33208
33209 static tree
33210 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
33211 location_t location)
33212 {
33213 tree t, c;
33214
33215 matching_parens parens;
33216 if (!parens.require_open (parser))
33217 return list;
33218
33219 t = cp_parser_assignment_expression (parser);
33220
33221 if (t == error_mark_node
33222 || !parens.require_close (parser))
33223 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33224 /*or_comma=*/false,
33225 /*consume_paren=*/true);
33226
33227 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
33228 "num_tasks", location);
33229
33230 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
33231 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
33232 OMP_CLAUSE_CHAIN (c) = list;
33233
33234 return c;
33235 }
33236
33237 /* OpenMP 4.5:
33238 grainsize ( expression ) */
33239
33240 static tree
33241 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
33242 location_t location)
33243 {
33244 tree t, c;
33245
33246 matching_parens parens;
33247 if (!parens.require_open (parser))
33248 return list;
33249
33250 t = cp_parser_assignment_expression (parser);
33251
33252 if (t == error_mark_node
33253 || !parens.require_close (parser))
33254 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33255 /*or_comma=*/false,
33256 /*consume_paren=*/true);
33257
33258 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
33259 "grainsize", location);
33260
33261 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
33262 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
33263 OMP_CLAUSE_CHAIN (c) = list;
33264
33265 return c;
33266 }
33267
33268 /* OpenMP 4.5:
33269 priority ( expression ) */
33270
33271 static tree
33272 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
33273 location_t location)
33274 {
33275 tree t, c;
33276
33277 matching_parens parens;
33278 if (!parens.require_open (parser))
33279 return list;
33280
33281 t = cp_parser_assignment_expression (parser);
33282
33283 if (t == error_mark_node
33284 || !parens.require_close (parser))
33285 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33286 /*or_comma=*/false,
33287 /*consume_paren=*/true);
33288
33289 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
33290 "priority", location);
33291
33292 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
33293 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
33294 OMP_CLAUSE_CHAIN (c) = list;
33295
33296 return c;
33297 }
33298
33299 /* OpenMP 4.5:
33300 hint ( expression ) */
33301
33302 static tree
33303 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
33304 {
33305 tree t, c;
33306
33307 matching_parens parens;
33308 if (!parens.require_open (parser))
33309 return list;
33310
33311 t = cp_parser_assignment_expression (parser);
33312
33313 if (t == error_mark_node
33314 || !parens.require_close (parser))
33315 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33316 /*or_comma=*/false,
33317 /*consume_paren=*/true);
33318
33319 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
33320
33321 c = build_omp_clause (location, OMP_CLAUSE_HINT);
33322 OMP_CLAUSE_HINT_EXPR (c) = t;
33323 OMP_CLAUSE_CHAIN (c) = list;
33324
33325 return c;
33326 }
33327
33328 /* OpenMP 4.5:
33329 defaultmap ( tofrom : scalar )
33330
33331 OpenMP 5.0:
33332 defaultmap ( implicit-behavior [ : variable-category ] ) */
33333
33334 static tree
33335 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
33336 location_t location)
33337 {
33338 tree c, id;
33339 const char *p;
33340 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33341 enum omp_clause_defaultmap_kind category
33342 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
33343
33344 matching_parens parens;
33345 if (!parens.require_open (parser))
33346 return list;
33347
33348 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
33349 p = "default";
33350 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33351 {
33352 invalid_behavior:
33353 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
33354 "%<tofrom%>, %<firstprivate%>, %<none%> "
33355 "or %<default%>");
33356 goto out_err;
33357 }
33358 else
33359 {
33360 id = cp_lexer_peek_token (parser->lexer)->u.value;
33361 p = IDENTIFIER_POINTER (id);
33362 }
33363
33364 switch (p[0])
33365 {
33366 case 'a':
33367 if (strcmp ("alloc", p) == 0)
33368 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
33369 else
33370 goto invalid_behavior;
33371 break;
33372
33373 case 'd':
33374 if (strcmp ("default", p) == 0)
33375 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33376 else
33377 goto invalid_behavior;
33378 break;
33379
33380 case 'f':
33381 if (strcmp ("firstprivate", p) == 0)
33382 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
33383 else if (strcmp ("from", p) == 0)
33384 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
33385 else
33386 goto invalid_behavior;
33387 break;
33388
33389 case 'n':
33390 if (strcmp ("none", p) == 0)
33391 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
33392 else
33393 goto invalid_behavior;
33394 break;
33395
33396 case 't':
33397 if (strcmp ("tofrom", p) == 0)
33398 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
33399 else if (strcmp ("to", p) == 0)
33400 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
33401 else
33402 goto invalid_behavior;
33403 break;
33404
33405 default:
33406 goto invalid_behavior;
33407 }
33408 cp_lexer_consume_token (parser->lexer);
33409
33410 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33411 {
33412 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33413 goto out_err;
33414
33415 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33416 {
33417 invalid_category:
33418 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
33419 "%<pointer%>");
33420 goto out_err;
33421 }
33422 id = cp_lexer_peek_token (parser->lexer)->u.value;
33423 p = IDENTIFIER_POINTER (id);
33424
33425 switch (p[0])
33426 {
33427 case 'a':
33428 if (strcmp ("aggregate", p) == 0)
33429 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
33430 else
33431 goto invalid_category;
33432 break;
33433
33434 case 'p':
33435 if (strcmp ("pointer", p) == 0)
33436 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
33437 else
33438 goto invalid_category;
33439 break;
33440
33441 case 's':
33442 if (strcmp ("scalar", p) == 0)
33443 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
33444 else
33445 goto invalid_category;
33446 break;
33447
33448 default:
33449 goto invalid_category;
33450 }
33451
33452 cp_lexer_consume_token (parser->lexer);
33453 }
33454 if (!parens.require_close (parser))
33455 goto out_err;
33456
33457 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33458 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
33459 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33460 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
33461 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
33462 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
33463 {
33464 enum omp_clause_defaultmap_kind cat = category;
33465 location_t loc = OMP_CLAUSE_LOCATION (c);
33466 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
33467 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
33468 p = NULL;
33469 switch (cat)
33470 {
33471 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
33472 p = NULL;
33473 break;
33474 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
33475 p = "aggregate";
33476 break;
33477 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
33478 p = "pointer";
33479 break;
33480 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
33481 p = "scalar";
33482 break;
33483 default:
33484 gcc_unreachable ();
33485 }
33486 if (p)
33487 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
33488 p);
33489 else
33490 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
33491 "category");
33492 break;
33493 }
33494
33495 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33496 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33497 OMP_CLAUSE_CHAIN (c) = list;
33498 return c;
33499
33500 out_err:
33501 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33502 /*or_comma=*/false,
33503 /*consume_paren=*/true);
33504 return list;
33505 }
33506
33507 /* OpenMP 2.5:
33508 ordered
33509
33510 OpenMP 4.5:
33511 ordered ( constant-expression ) */
33512
33513 static tree
33514 cp_parser_omp_clause_ordered (cp_parser *parser,
33515 tree list, location_t location)
33516 {
33517 tree c, num = NULL_TREE;
33518 HOST_WIDE_INT n;
33519
33520 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
33521 "ordered", location);
33522
33523 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33524 {
33525 matching_parens parens;
33526 parens.consume_open (parser);
33527
33528 num = cp_parser_constant_expression (parser);
33529
33530 if (!parens.require_close (parser))
33531 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33532 /*or_comma=*/false,
33533 /*consume_paren=*/true);
33534
33535 if (num == error_mark_node)
33536 return list;
33537 num = fold_non_dependent_expr (num);
33538 if (!tree_fits_shwi_p (num)
33539 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33540 || (n = tree_to_shwi (num)) <= 0
33541 || (int) n != n)
33542 {
33543 error_at (location,
33544 "ordered argument needs positive constant integer "
33545 "expression");
33546 return list;
33547 }
33548 }
33549
33550 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
33551 OMP_CLAUSE_ORDERED_EXPR (c) = num;
33552 OMP_CLAUSE_CHAIN (c) = list;
33553 return c;
33554 }
33555
33556 /* OpenMP 2.5:
33557 reduction ( reduction-operator : variable-list )
33558
33559 reduction-operator:
33560 One of: + * - & ^ | && ||
33561
33562 OpenMP 3.1:
33563
33564 reduction-operator:
33565 One of: + * - & ^ | && || min max
33566
33567 OpenMP 4.0:
33568
33569 reduction-operator:
33570 One of: + * - & ^ | && ||
33571 id-expression
33572
33573 OpenMP 5.0:
33574 reduction ( reduction-modifier, reduction-operator : variable-list )
33575 in_reduction ( reduction-operator : variable-list )
33576 task_reduction ( reduction-operator : variable-list ) */
33577
33578 static tree
33579 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
33580 bool is_omp, tree list)
33581 {
33582 enum tree_code code = ERROR_MARK;
33583 tree nlist, c, id = NULL_TREE;
33584 bool task = false;
33585 bool inscan = false;
33586
33587 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33588 return list;
33589
33590 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
33591 {
33592 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
33593 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33594 {
33595 cp_lexer_consume_token (parser->lexer);
33596 cp_lexer_consume_token (parser->lexer);
33597 }
33598 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33599 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33600 {
33601 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33602 const char *p = IDENTIFIER_POINTER (id);
33603 if (strcmp (p, "task") == 0)
33604 task = true;
33605 else if (strcmp (p, "inscan") == 0)
33606 {
33607 inscan = true;
33608 sorry ("%<inscan%> modifier on %<reduction%> clause "
33609 "not supported yet");
33610 }
33611 if (task || inscan)
33612 {
33613 cp_lexer_consume_token (parser->lexer);
33614 cp_lexer_consume_token (parser->lexer);
33615 }
33616 }
33617 }
33618
33619 switch (cp_lexer_peek_token (parser->lexer)->type)
33620 {
33621 case CPP_PLUS: code = PLUS_EXPR; break;
33622 case CPP_MULT: code = MULT_EXPR; break;
33623 case CPP_MINUS: code = MINUS_EXPR; break;
33624 case CPP_AND: code = BIT_AND_EXPR; break;
33625 case CPP_XOR: code = BIT_XOR_EXPR; break;
33626 case CPP_OR: code = BIT_IOR_EXPR; break;
33627 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
33628 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
33629 default: break;
33630 }
33631
33632 if (code != ERROR_MARK)
33633 cp_lexer_consume_token (parser->lexer);
33634 else
33635 {
33636 bool saved_colon_corrects_to_scope_p;
33637 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33638 parser->colon_corrects_to_scope_p = false;
33639 id = cp_parser_id_expression (parser, /*template_p=*/false,
33640 /*check_dependency_p=*/true,
33641 /*template_p=*/NULL,
33642 /*declarator_p=*/false,
33643 /*optional_p=*/false);
33644 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33645 if (identifier_p (id))
33646 {
33647 const char *p = IDENTIFIER_POINTER (id);
33648
33649 if (strcmp (p, "min") == 0)
33650 code = MIN_EXPR;
33651 else if (strcmp (p, "max") == 0)
33652 code = MAX_EXPR;
33653 else if (id == ovl_op_identifier (false, PLUS_EXPR))
33654 code = PLUS_EXPR;
33655 else if (id == ovl_op_identifier (false, MULT_EXPR))
33656 code = MULT_EXPR;
33657 else if (id == ovl_op_identifier (false, MINUS_EXPR))
33658 code = MINUS_EXPR;
33659 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
33660 code = BIT_AND_EXPR;
33661 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
33662 code = BIT_IOR_EXPR;
33663 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
33664 code = BIT_XOR_EXPR;
33665 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
33666 code = TRUTH_ANDIF_EXPR;
33667 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
33668 code = TRUTH_ORIF_EXPR;
33669 id = omp_reduction_id (code, id, NULL_TREE);
33670 tree scope = parser->scope;
33671 if (scope)
33672 id = build_qualified_name (NULL_TREE, scope, id, false);
33673 parser->scope = NULL_TREE;
33674 parser->qualifying_scope = NULL_TREE;
33675 parser->object_scope = NULL_TREE;
33676 }
33677 else
33678 {
33679 error ("invalid reduction-identifier");
33680 resync_fail:
33681 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33682 /*or_comma=*/false,
33683 /*consume_paren=*/true);
33684 return list;
33685 }
33686 }
33687
33688 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33689 goto resync_fail;
33690
33691 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
33692 NULL);
33693 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33694 {
33695 OMP_CLAUSE_REDUCTION_CODE (c) = code;
33696 if (task)
33697 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
33698 else if (inscan)
33699 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
33700 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
33701 }
33702
33703 return nlist;
33704 }
33705
33706 /* OpenMP 2.5:
33707 schedule ( schedule-kind )
33708 schedule ( schedule-kind , expression )
33709
33710 schedule-kind:
33711 static | dynamic | guided | runtime | auto
33712
33713 OpenMP 4.5:
33714 schedule ( schedule-modifier : schedule-kind )
33715 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33716
33717 schedule-modifier:
33718 simd
33719 monotonic
33720 nonmonotonic */
33721
33722 static tree
33723 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
33724 {
33725 tree c, t;
33726 int modifiers = 0, nmodifiers = 0;
33727
33728 matching_parens parens;
33729 if (!parens.require_open (parser))
33730 return list;
33731
33732 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
33733
33734 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33735 {
33736 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33737 const char *p = IDENTIFIER_POINTER (id);
33738 if (strcmp ("simd", p) == 0)
33739 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
33740 else if (strcmp ("monotonic", p) == 0)
33741 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
33742 else if (strcmp ("nonmonotonic", p) == 0)
33743 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
33744 else
33745 break;
33746 cp_lexer_consume_token (parser->lexer);
33747 if (nmodifiers++ == 0
33748 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33749 cp_lexer_consume_token (parser->lexer);
33750 else
33751 {
33752 cp_parser_require (parser, CPP_COLON, RT_COLON);
33753 break;
33754 }
33755 }
33756
33757 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33758 {
33759 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33760 const char *p = IDENTIFIER_POINTER (id);
33761
33762 switch (p[0])
33763 {
33764 case 'd':
33765 if (strcmp ("dynamic", p) != 0)
33766 goto invalid_kind;
33767 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
33768 break;
33769
33770 case 'g':
33771 if (strcmp ("guided", p) != 0)
33772 goto invalid_kind;
33773 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
33774 break;
33775
33776 case 'r':
33777 if (strcmp ("runtime", p) != 0)
33778 goto invalid_kind;
33779 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
33780 break;
33781
33782 default:
33783 goto invalid_kind;
33784 }
33785 }
33786 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33787 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
33788 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33789 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
33790 else
33791 goto invalid_kind;
33792 cp_lexer_consume_token (parser->lexer);
33793
33794 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
33795 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33796 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33797 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33798 {
33799 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33800 "specified");
33801 modifiers = 0;
33802 }
33803
33804 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33805 {
33806 cp_token *token;
33807 cp_lexer_consume_token (parser->lexer);
33808
33809 token = cp_lexer_peek_token (parser->lexer);
33810 t = cp_parser_assignment_expression (parser);
33811
33812 if (t == error_mark_node)
33813 goto resync_fail;
33814 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
33815 error_at (token->location, "schedule %<runtime%> does not take "
33816 "a %<chunk_size%> parameter");
33817 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
33818 error_at (token->location, "schedule %<auto%> does not take "
33819 "a %<chunk_size%> parameter");
33820 else
33821 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
33822
33823 if (!parens.require_close (parser))
33824 goto resync_fail;
33825 }
33826 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33827 goto resync_fail;
33828
33829 OMP_CLAUSE_SCHEDULE_KIND (c)
33830 = (enum omp_clause_schedule_kind)
33831 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
33832
33833 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
33834 OMP_CLAUSE_CHAIN (c) = list;
33835 return c;
33836
33837 invalid_kind:
33838 cp_parser_error (parser, "invalid schedule kind");
33839 resync_fail:
33840 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33841 /*or_comma=*/false,
33842 /*consume_paren=*/true);
33843 return list;
33844 }
33845
33846 /* OpenMP 3.0:
33847 untied */
33848
33849 static tree
33850 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
33851 tree list, location_t location)
33852 {
33853 tree c;
33854
33855 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
33856
33857 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
33858 OMP_CLAUSE_CHAIN (c) = list;
33859 return c;
33860 }
33861
33862 /* OpenMP 4.0:
33863 inbranch
33864 notinbranch */
33865
33866 static tree
33867 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33868 tree list, location_t location)
33869 {
33870 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33871 tree c = build_omp_clause (location, code);
33872 OMP_CLAUSE_CHAIN (c) = list;
33873 return c;
33874 }
33875
33876 /* OpenMP 4.0:
33877 parallel
33878 for
33879 sections
33880 taskgroup */
33881
33882 static tree
33883 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33884 enum omp_clause_code code,
33885 tree list, location_t location)
33886 {
33887 tree c = build_omp_clause (location, code);
33888 OMP_CLAUSE_CHAIN (c) = list;
33889 return c;
33890 }
33891
33892 /* OpenMP 4.5:
33893 nogroup */
33894
33895 static tree
33896 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33897 tree list, location_t location)
33898 {
33899 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33900 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33901 OMP_CLAUSE_CHAIN (c) = list;
33902 return c;
33903 }
33904
33905 /* OpenMP 4.5:
33906 simd
33907 threads */
33908
33909 static tree
33910 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33911 enum omp_clause_code code,
33912 tree list, location_t location)
33913 {
33914 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33915 tree c = build_omp_clause (location, code);
33916 OMP_CLAUSE_CHAIN (c) = list;
33917 return c;
33918 }
33919
33920 /* OpenMP 4.0:
33921 num_teams ( expression ) */
33922
33923 static tree
33924 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33925 location_t location)
33926 {
33927 tree t, c;
33928
33929 matching_parens parens;
33930 if (!parens.require_open (parser))
33931 return list;
33932
33933 t = cp_parser_assignment_expression (parser);
33934
33935 if (t == error_mark_node
33936 || !parens.require_close (parser))
33937 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33938 /*or_comma=*/false,
33939 /*consume_paren=*/true);
33940
33941 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33942 "num_teams", location);
33943
33944 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33945 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33946 OMP_CLAUSE_CHAIN (c) = list;
33947
33948 return c;
33949 }
33950
33951 /* OpenMP 4.0:
33952 thread_limit ( expression ) */
33953
33954 static tree
33955 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33956 location_t location)
33957 {
33958 tree t, c;
33959
33960 matching_parens parens;
33961 if (!parens.require_open (parser))
33962 return list;
33963
33964 t = cp_parser_assignment_expression (parser);
33965
33966 if (t == error_mark_node
33967 || !parens.require_close (parser))
33968 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33969 /*or_comma=*/false,
33970 /*consume_paren=*/true);
33971
33972 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33973 "thread_limit", location);
33974
33975 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33976 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33977 OMP_CLAUSE_CHAIN (c) = list;
33978
33979 return c;
33980 }
33981
33982 /* OpenMP 4.0:
33983 aligned ( variable-list )
33984 aligned ( variable-list : constant-expression ) */
33985
33986 static tree
33987 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33988 {
33989 tree nlist, c, alignment = NULL_TREE;
33990 bool colon;
33991
33992 matching_parens parens;
33993 if (!parens.require_open (parser))
33994 return list;
33995
33996 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33997 &colon);
33998
33999 if (colon)
34000 {
34001 alignment = cp_parser_constant_expression (parser);
34002
34003 if (!parens.require_close (parser))
34004 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34005 /*or_comma=*/false,
34006 /*consume_paren=*/true);
34007
34008 if (alignment == error_mark_node)
34009 alignment = NULL_TREE;
34010 }
34011
34012 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34013 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
34014
34015 return nlist;
34016 }
34017
34018 /* OpenMP 2.5:
34019 lastprivate ( variable-list )
34020
34021 OpenMP 5.0:
34022 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
34023
34024 static tree
34025 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
34026 {
34027 bool conditional = false;
34028
34029 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34030 return list;
34031
34032 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34033 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
34034 {
34035 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34036 const char *p = IDENTIFIER_POINTER (id);
34037
34038 if (strcmp ("conditional", p) == 0)
34039 {
34040 conditional = true;
34041 cp_lexer_consume_token (parser->lexer);
34042 cp_lexer_consume_token (parser->lexer);
34043 }
34044 }
34045
34046 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
34047 list, NULL);
34048
34049 if (conditional)
34050 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34051 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
34052 return nlist;
34053 }
34054
34055 /* OpenMP 4.0:
34056 linear ( variable-list )
34057 linear ( variable-list : expression )
34058
34059 OpenMP 4.5:
34060 linear ( modifier ( variable-list ) )
34061 linear ( modifier ( variable-list ) : expression ) */
34062
34063 static tree
34064 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
34065 bool declare_simd)
34066 {
34067 tree nlist, c, step = integer_one_node;
34068 bool colon;
34069 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
34070
34071 matching_parens parens;
34072 if (!parens.require_open (parser))
34073 return list;
34074
34075 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34076 {
34077 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34078 const char *p = IDENTIFIER_POINTER (id);
34079
34080 if (strcmp ("ref", p) == 0)
34081 kind = OMP_CLAUSE_LINEAR_REF;
34082 else if (strcmp ("val", p) == 0)
34083 kind = OMP_CLAUSE_LINEAR_VAL;
34084 else if (strcmp ("uval", p) == 0)
34085 kind = OMP_CLAUSE_LINEAR_UVAL;
34086 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
34087 cp_lexer_consume_token (parser->lexer);
34088 else
34089 kind = OMP_CLAUSE_LINEAR_DEFAULT;
34090 }
34091
34092 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
34093 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
34094 &colon);
34095 else
34096 {
34097 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
34098 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
34099 if (colon)
34100 cp_parser_require (parser, CPP_COLON, RT_COLON);
34101 else if (!parens.require_close (parser))
34102 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34103 /*or_comma=*/false,
34104 /*consume_paren=*/true);
34105 }
34106
34107 if (colon)
34108 {
34109 step = NULL_TREE;
34110 if (declare_simd
34111 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34112 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
34113 {
34114 cp_token *token = cp_lexer_peek_token (parser->lexer);
34115 cp_parser_parse_tentatively (parser);
34116 step = cp_parser_id_expression (parser, /*template_p=*/false,
34117 /*check_dependency_p=*/true,
34118 /*template_p=*/NULL,
34119 /*declarator_p=*/false,
34120 /*optional_p=*/false);
34121 if (step != error_mark_node)
34122 step = cp_parser_lookup_name_simple (parser, step, token->location);
34123 if (step == error_mark_node)
34124 {
34125 step = NULL_TREE;
34126 cp_parser_abort_tentative_parse (parser);
34127 }
34128 else if (!cp_parser_parse_definitely (parser))
34129 step = NULL_TREE;
34130 }
34131 if (!step)
34132 step = cp_parser_assignment_expression (parser);
34133
34134 if (!parens.require_close (parser))
34135 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34136 /*or_comma=*/false,
34137 /*consume_paren=*/true);
34138
34139 if (step == error_mark_node)
34140 return list;
34141 }
34142
34143 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34144 {
34145 OMP_CLAUSE_LINEAR_STEP (c) = step;
34146 OMP_CLAUSE_LINEAR_KIND (c) = kind;
34147 }
34148
34149 return nlist;
34150 }
34151
34152 /* OpenMP 4.0:
34153 safelen ( constant-expression ) */
34154
34155 static tree
34156 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
34157 location_t location)
34158 {
34159 tree t, c;
34160
34161 matching_parens parens;
34162 if (!parens.require_open (parser))
34163 return list;
34164
34165 t = cp_parser_constant_expression (parser);
34166
34167 if (t == error_mark_node
34168 || !parens.require_close (parser))
34169 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34170 /*or_comma=*/false,
34171 /*consume_paren=*/true);
34172
34173 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
34174
34175 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
34176 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
34177 OMP_CLAUSE_CHAIN (c) = list;
34178
34179 return c;
34180 }
34181
34182 /* OpenMP 4.0:
34183 simdlen ( constant-expression ) */
34184
34185 static tree
34186 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
34187 location_t location)
34188 {
34189 tree t, c;
34190
34191 matching_parens parens;
34192 if (!parens.require_open (parser))
34193 return list;
34194
34195 t = cp_parser_constant_expression (parser);
34196
34197 if (t == error_mark_node
34198 || !parens.require_close (parser))
34199 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34200 /*or_comma=*/false,
34201 /*consume_paren=*/true);
34202
34203 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
34204
34205 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
34206 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
34207 OMP_CLAUSE_CHAIN (c) = list;
34208
34209 return c;
34210 }
34211
34212 /* OpenMP 4.5:
34213 vec:
34214 identifier [+/- integer]
34215 vec , identifier [+/- integer]
34216 */
34217
34218 static tree
34219 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
34220 tree list)
34221 {
34222 tree vec = NULL;
34223
34224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34225 {
34226 cp_parser_error (parser, "expected identifier");
34227 return list;
34228 }
34229
34230 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34231 {
34232 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
34233 tree t, identifier = cp_parser_identifier (parser);
34234 tree addend = NULL;
34235
34236 if (identifier == error_mark_node)
34237 t = error_mark_node;
34238 else
34239 {
34240 t = cp_parser_lookup_name_simple
34241 (parser, identifier,
34242 cp_lexer_peek_token (parser->lexer)->location);
34243 if (t == error_mark_node)
34244 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
34245 id_loc);
34246 }
34247
34248 bool neg = false;
34249 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
34250 neg = true;
34251 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
34252 {
34253 addend = integer_zero_node;
34254 goto add_to_vector;
34255 }
34256 cp_lexer_consume_token (parser->lexer);
34257
34258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
34259 {
34260 cp_parser_error (parser, "expected integer");
34261 return list;
34262 }
34263
34264 addend = cp_lexer_peek_token (parser->lexer)->u.value;
34265 if (TREE_CODE (addend) != INTEGER_CST)
34266 {
34267 cp_parser_error (parser, "expected integer");
34268 return list;
34269 }
34270 cp_lexer_consume_token (parser->lexer);
34271
34272 add_to_vector:
34273 if (t != error_mark_node)
34274 {
34275 vec = tree_cons (addend, t, vec);
34276 if (neg)
34277 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
34278 }
34279
34280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
34281 break;
34282
34283 cp_lexer_consume_token (parser->lexer);
34284 }
34285
34286 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
34287 {
34288 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
34289 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
34290 OMP_CLAUSE_DECL (u) = nreverse (vec);
34291 OMP_CLAUSE_CHAIN (u) = list;
34292 return u;
34293 }
34294 return list;
34295 }
34296
34297 /* OpenMP 5.0:
34298 iterators ( iterators-definition )
34299
34300 iterators-definition:
34301 iterator-specifier
34302 iterator-specifier , iterators-definition
34303
34304 iterator-specifier:
34305 identifier = range-specification
34306 iterator-type identifier = range-specification
34307
34308 range-specification:
34309 begin : end
34310 begin : end : step */
34311
34312 static tree
34313 cp_parser_omp_iterators (cp_parser *parser)
34314 {
34315 tree ret = NULL_TREE, *last = &ret;
34316 cp_lexer_consume_token (parser->lexer);
34317
34318 matching_parens parens;
34319 if (!parens.require_open (parser))
34320 return error_mark_node;
34321
34322 bool saved_colon_corrects_to_scope_p
34323 = parser->colon_corrects_to_scope_p;
34324 bool saved_colon_doesnt_start_class_def_p
34325 = parser->colon_doesnt_start_class_def_p;
34326
34327 do
34328 {
34329 tree iter_type;
34330 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34331 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
34332 iter_type = integer_type_node;
34333 else
34334 {
34335 const char *saved_message
34336 = parser->type_definition_forbidden_message;
34337 parser->type_definition_forbidden_message
34338 = G_("types may not be defined in iterator type");
34339
34340 iter_type = cp_parser_type_id (parser);
34341
34342 parser->type_definition_forbidden_message = saved_message;
34343 }
34344
34345 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34346 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34347 {
34348 cp_parser_error (parser, "expected identifier");
34349 break;
34350 }
34351
34352 tree id = cp_parser_identifier (parser);
34353 if (id == error_mark_node)
34354 break;
34355
34356 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34357 break;
34358
34359 parser->colon_corrects_to_scope_p = false;
34360 parser->colon_doesnt_start_class_def_p = true;
34361 tree begin = cp_parser_assignment_expression (parser);
34362
34363 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34364 break;
34365
34366 tree end = cp_parser_assignment_expression (parser);
34367
34368 tree step = integer_one_node;
34369 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34370 {
34371 cp_lexer_consume_token (parser->lexer);
34372 step = cp_parser_assignment_expression (parser);
34373 }
34374
34375 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
34376 DECL_ARTIFICIAL (iter_var) = 1;
34377 DECL_CONTEXT (iter_var) = current_function_decl;
34378 pushdecl (iter_var);
34379
34380 *last = make_tree_vec (6);
34381 TREE_VEC_ELT (*last, 0) = iter_var;
34382 TREE_VEC_ELT (*last, 1) = begin;
34383 TREE_VEC_ELT (*last, 2) = end;
34384 TREE_VEC_ELT (*last, 3) = step;
34385 last = &TREE_CHAIN (*last);
34386
34387 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34388 {
34389 cp_lexer_consume_token (parser->lexer);
34390 continue;
34391 }
34392 break;
34393 }
34394 while (1);
34395
34396 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34397 parser->colon_doesnt_start_class_def_p
34398 = saved_colon_doesnt_start_class_def_p;
34399
34400 if (!parens.require_close (parser))
34401 cp_parser_skip_to_closing_parenthesis (parser,
34402 /*recovering=*/true,
34403 /*or_comma=*/false,
34404 /*consume_paren=*/true);
34405
34406 return ret ? ret : error_mark_node;
34407 }
34408
34409 /* OpenMP 4.0:
34410 depend ( depend-kind : variable-list )
34411
34412 depend-kind:
34413 in | out | inout
34414
34415 OpenMP 4.5:
34416 depend ( source )
34417
34418 depend ( sink : vec )
34419
34420 OpenMP 5.0:
34421 depend ( depend-modifier , depend-kind: variable-list )
34422
34423 depend-kind:
34424 in | out | inout | mutexinoutset | depobj
34425
34426 depend-modifier:
34427 iterator ( iterators-definition ) */
34428
34429 static tree
34430 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
34431 {
34432 tree nlist, c, iterators = NULL_TREE;
34433 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
34434
34435 matching_parens parens;
34436 if (!parens.require_open (parser))
34437 return list;
34438
34439 do
34440 {
34441 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34442 goto invalid_kind;
34443
34444 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34445 const char *p = IDENTIFIER_POINTER (id);
34446
34447 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
34448 {
34449 begin_scope (sk_omp, NULL);
34450 iterators = cp_parser_omp_iterators (parser);
34451 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
34452 continue;
34453 }
34454 if (strcmp ("in", p) == 0)
34455 kind = OMP_CLAUSE_DEPEND_IN;
34456 else if (strcmp ("inout", p) == 0)
34457 kind = OMP_CLAUSE_DEPEND_INOUT;
34458 else if (strcmp ("mutexinoutset", p) == 0)
34459 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
34460 else if (strcmp ("out", p) == 0)
34461 kind = OMP_CLAUSE_DEPEND_OUT;
34462 else if (strcmp ("depobj", p) == 0)
34463 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
34464 else if (strcmp ("sink", p) == 0)
34465 kind = OMP_CLAUSE_DEPEND_SINK;
34466 else if (strcmp ("source", p) == 0)
34467 kind = OMP_CLAUSE_DEPEND_SOURCE;
34468 else
34469 goto invalid_kind;
34470 break;
34471 }
34472 while (1);
34473
34474 cp_lexer_consume_token (parser->lexer);
34475
34476 if (iterators
34477 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
34478 {
34479 poplevel (0, 1, 0);
34480 error_at (loc, "%<iterator%> modifier incompatible with %qs",
34481 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
34482 iterators = NULL_TREE;
34483 }
34484
34485 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
34486 {
34487 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
34488 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34489 OMP_CLAUSE_DECL (c) = NULL_TREE;
34490 OMP_CLAUSE_CHAIN (c) = list;
34491 if (!parens.require_close (parser))
34492 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34493 /*or_comma=*/false,
34494 /*consume_paren=*/true);
34495 return c;
34496 }
34497
34498 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34499 goto resync_fail;
34500
34501 if (kind == OMP_CLAUSE_DEPEND_SINK)
34502 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
34503 else
34504 {
34505 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
34506 list, NULL);
34507
34508 if (iterators)
34509 {
34510 tree block = poplevel (1, 1, 0);
34511 if (iterators == error_mark_node)
34512 iterators = NULL_TREE;
34513 else
34514 TREE_VEC_ELT (iterators, 5) = block;
34515 }
34516
34517 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34518 {
34519 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34520 if (iterators)
34521 OMP_CLAUSE_DECL (c)
34522 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
34523 }
34524 }
34525 return nlist;
34526
34527 invalid_kind:
34528 cp_parser_error (parser, "invalid depend kind");
34529 resync_fail:
34530 if (iterators)
34531 poplevel (0, 1, 0);
34532 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34533 /*or_comma=*/false,
34534 /*consume_paren=*/true);
34535 return list;
34536 }
34537
34538 /* OpenMP 4.0:
34539 map ( map-kind : variable-list )
34540 map ( variable-list )
34541
34542 map-kind:
34543 alloc | to | from | tofrom
34544
34545 OpenMP 4.5:
34546 map-kind:
34547 alloc | to | from | tofrom | release | delete
34548
34549 map ( always [,] map-kind: variable-list ) */
34550
34551 static tree
34552 cp_parser_omp_clause_map (cp_parser *parser, tree list)
34553 {
34554 tree nlist, c;
34555 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
34556 bool always = false;
34557
34558 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34559 return list;
34560
34561 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34562 {
34563 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34564 const char *p = IDENTIFIER_POINTER (id);
34565
34566 if (strcmp ("always", p) == 0)
34567 {
34568 int nth = 2;
34569 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
34570 nth++;
34571 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
34572 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
34573 == RID_DELETE))
34574 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
34575 == CPP_COLON))
34576 {
34577 always = true;
34578 cp_lexer_consume_token (parser->lexer);
34579 if (nth == 3)
34580 cp_lexer_consume_token (parser->lexer);
34581 }
34582 }
34583 }
34584
34585 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34586 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34587 {
34588 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34589 const char *p = IDENTIFIER_POINTER (id);
34590
34591 if (strcmp ("alloc", p) == 0)
34592 kind = GOMP_MAP_ALLOC;
34593 else if (strcmp ("to", p) == 0)
34594 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
34595 else if (strcmp ("from", p) == 0)
34596 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
34597 else if (strcmp ("tofrom", p) == 0)
34598 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
34599 else if (strcmp ("release", p) == 0)
34600 kind = GOMP_MAP_RELEASE;
34601 else
34602 {
34603 cp_parser_error (parser, "invalid map kind");
34604 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34605 /*or_comma=*/false,
34606 /*consume_paren=*/true);
34607 return list;
34608 }
34609 cp_lexer_consume_token (parser->lexer);
34610 cp_lexer_consume_token (parser->lexer);
34611 }
34612 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
34613 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34614 {
34615 kind = GOMP_MAP_DELETE;
34616 cp_lexer_consume_token (parser->lexer);
34617 cp_lexer_consume_token (parser->lexer);
34618 }
34619
34620 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
34621 NULL);
34622
34623 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34624 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34625
34626 return nlist;
34627 }
34628
34629 /* OpenMP 4.0:
34630 device ( expression ) */
34631
34632 static tree
34633 cp_parser_omp_clause_device (cp_parser *parser, tree list,
34634 location_t location)
34635 {
34636 tree t, c;
34637
34638 matching_parens parens;
34639 if (!parens.require_open (parser))
34640 return list;
34641
34642 t = cp_parser_assignment_expression (parser);
34643
34644 if (t == error_mark_node
34645 || !parens.require_close (parser))
34646 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34647 /*or_comma=*/false,
34648 /*consume_paren=*/true);
34649
34650 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
34651 "device", location);
34652
34653 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
34654 OMP_CLAUSE_DEVICE_ID (c) = t;
34655 OMP_CLAUSE_CHAIN (c) = list;
34656
34657 return c;
34658 }
34659
34660 /* OpenMP 4.0:
34661 dist_schedule ( static )
34662 dist_schedule ( static , expression ) */
34663
34664 static tree
34665 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
34666 location_t location)
34667 {
34668 tree c, t;
34669
34670 matching_parens parens;
34671 if (!parens.require_open (parser))
34672 return list;
34673
34674 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
34675
34676 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34677 goto invalid_kind;
34678 cp_lexer_consume_token (parser->lexer);
34679
34680 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34681 {
34682 cp_lexer_consume_token (parser->lexer);
34683
34684 t = cp_parser_assignment_expression (parser);
34685
34686 if (t == error_mark_node)
34687 goto resync_fail;
34688 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
34689
34690 if (!parens.require_close (parser))
34691 goto resync_fail;
34692 }
34693 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34694 goto resync_fail;
34695
34696 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
34697 location);
34698 OMP_CLAUSE_CHAIN (c) = list;
34699 return c;
34700
34701 invalid_kind:
34702 cp_parser_error (parser, "invalid dist_schedule kind");
34703 resync_fail:
34704 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34705 /*or_comma=*/false,
34706 /*consume_paren=*/true);
34707 return list;
34708 }
34709
34710 /* OpenMP 4.0:
34711 proc_bind ( proc-bind-kind )
34712
34713 proc-bind-kind:
34714 master | close | spread */
34715
34716 static tree
34717 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
34718 location_t location)
34719 {
34720 tree c;
34721 enum omp_clause_proc_bind_kind kind;
34722
34723 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34724 return list;
34725
34726 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34727 {
34728 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34729 const char *p = IDENTIFIER_POINTER (id);
34730
34731 if (strcmp ("master", p) == 0)
34732 kind = OMP_CLAUSE_PROC_BIND_MASTER;
34733 else if (strcmp ("close", p) == 0)
34734 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
34735 else if (strcmp ("spread", p) == 0)
34736 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
34737 else
34738 goto invalid_kind;
34739 }
34740 else
34741 goto invalid_kind;
34742
34743 cp_lexer_consume_token (parser->lexer);
34744 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34745 goto resync_fail;
34746
34747 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
34748 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
34749 location);
34750 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
34751 OMP_CLAUSE_CHAIN (c) = list;
34752 return c;
34753
34754 invalid_kind:
34755 cp_parser_error (parser, "invalid depend kind");
34756 resync_fail:
34757 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34758 /*or_comma=*/false,
34759 /*consume_paren=*/true);
34760 return list;
34761 }
34762
34763 /* OpenACC:
34764 async [( int-expr )] */
34765
34766 static tree
34767 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
34768 {
34769 tree c, t;
34770 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34771
34772 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34773
34774 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34775 {
34776 matching_parens parens;
34777 parens.consume_open (parser);
34778
34779 t = cp_parser_expression (parser);
34780 if (t == error_mark_node
34781 || !parens.require_close (parser))
34782 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34783 /*or_comma=*/false,
34784 /*consume_paren=*/true);
34785 }
34786
34787 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
34788
34789 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
34790 OMP_CLAUSE_ASYNC_EXPR (c) = t;
34791 OMP_CLAUSE_CHAIN (c) = list;
34792 list = c;
34793
34794 return list;
34795 }
34796
34797 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34798 is a bitmask in MASK. Return the list of clauses found. */
34799
34800 static tree
34801 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
34802 const char *where, cp_token *pragma_tok,
34803 bool finish_p = true)
34804 {
34805 tree clauses = NULL;
34806 bool first = true;
34807
34808 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34809 {
34810 location_t here;
34811 pragma_omp_clause c_kind;
34812 omp_clause_code code;
34813 const char *c_name;
34814 tree prev = clauses;
34815
34816 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34817 cp_lexer_consume_token (parser->lexer);
34818
34819 here = cp_lexer_peek_token (parser->lexer)->location;
34820 c_kind = cp_parser_omp_clause_name (parser);
34821
34822 switch (c_kind)
34823 {
34824 case PRAGMA_OACC_CLAUSE_ASYNC:
34825 clauses = cp_parser_oacc_clause_async (parser, clauses);
34826 c_name = "async";
34827 break;
34828 case PRAGMA_OACC_CLAUSE_AUTO:
34829 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
34830 clauses, here);
34831 c_name = "auto";
34832 break;
34833 case PRAGMA_OACC_CLAUSE_COLLAPSE:
34834 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
34835 c_name = "collapse";
34836 break;
34837 case PRAGMA_OACC_CLAUSE_COPY:
34838 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34839 c_name = "copy";
34840 break;
34841 case PRAGMA_OACC_CLAUSE_COPYIN:
34842 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34843 c_name = "copyin";
34844 break;
34845 case PRAGMA_OACC_CLAUSE_COPYOUT:
34846 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34847 c_name = "copyout";
34848 break;
34849 case PRAGMA_OACC_CLAUSE_CREATE:
34850 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34851 c_name = "create";
34852 break;
34853 case PRAGMA_OACC_CLAUSE_DELETE:
34854 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34855 c_name = "delete";
34856 break;
34857 case PRAGMA_OMP_CLAUSE_DEFAULT:
34858 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
34859 c_name = "default";
34860 break;
34861 case PRAGMA_OACC_CLAUSE_DEVICE:
34862 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34863 c_name = "device";
34864 break;
34865 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
34866 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
34867 c_name = "deviceptr";
34868 break;
34869 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
34870 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34871 c_name = "device_resident";
34872 break;
34873 case PRAGMA_OACC_CLAUSE_FINALIZE:
34874 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
34875 clauses, here);
34876 c_name = "finalize";
34877 break;
34878 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
34879 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34880 clauses);
34881 c_name = "firstprivate";
34882 break;
34883 case PRAGMA_OACC_CLAUSE_GANG:
34884 c_name = "gang";
34885 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
34886 c_name, clauses);
34887 break;
34888 case PRAGMA_OACC_CLAUSE_HOST:
34889 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34890 c_name = "host";
34891 break;
34892 case PRAGMA_OACC_CLAUSE_IF:
34893 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
34894 c_name = "if";
34895 break;
34896 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
34897 clauses = cp_parser_oacc_simple_clause (parser,
34898 OMP_CLAUSE_IF_PRESENT,
34899 clauses, here);
34900 c_name = "if_present";
34901 break;
34902 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
34903 clauses = cp_parser_oacc_simple_clause (parser,
34904 OMP_CLAUSE_INDEPENDENT,
34905 clauses, here);
34906 c_name = "independent";
34907 break;
34908 case PRAGMA_OACC_CLAUSE_LINK:
34909 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34910 c_name = "link";
34911 break;
34912 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
34913 code = OMP_CLAUSE_NUM_GANGS;
34914 c_name = "num_gangs";
34915 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34916 clauses);
34917 break;
34918 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
34919 c_name = "num_workers";
34920 code = OMP_CLAUSE_NUM_WORKERS;
34921 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34922 clauses);
34923 break;
34924 case PRAGMA_OACC_CLAUSE_PRESENT:
34925 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34926 c_name = "present";
34927 break;
34928 case PRAGMA_OACC_CLAUSE_PRIVATE:
34929 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34930 clauses);
34931 c_name = "private";
34932 break;
34933 case PRAGMA_OACC_CLAUSE_REDUCTION:
34934 clauses
34935 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34936 false, clauses);
34937 c_name = "reduction";
34938 break;
34939 case PRAGMA_OACC_CLAUSE_SEQ:
34940 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
34941 clauses, here);
34942 c_name = "seq";
34943 break;
34944 case PRAGMA_OACC_CLAUSE_TILE:
34945 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
34946 c_name = "tile";
34947 break;
34948 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
34949 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34950 clauses);
34951 c_name = "use_device";
34952 break;
34953 case PRAGMA_OACC_CLAUSE_VECTOR:
34954 c_name = "vector";
34955 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
34956 c_name, clauses);
34957 break;
34958 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
34959 c_name = "vector_length";
34960 code = OMP_CLAUSE_VECTOR_LENGTH;
34961 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34962 clauses);
34963 break;
34964 case PRAGMA_OACC_CLAUSE_WAIT:
34965 clauses = cp_parser_oacc_clause_wait (parser, clauses);
34966 c_name = "wait";
34967 break;
34968 case PRAGMA_OACC_CLAUSE_WORKER:
34969 c_name = "worker";
34970 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
34971 c_name, clauses);
34972 break;
34973 default:
34974 cp_parser_error (parser, "expected %<#pragma acc%> clause");
34975 goto saw_error;
34976 }
34977
34978 first = false;
34979
34980 if (((mask >> c_kind) & 1) == 0)
34981 {
34982 /* Remove the invalid clause(s) from the list to avoid
34983 confusing the rest of the compiler. */
34984 clauses = prev;
34985 error_at (here, "%qs is not valid for %qs", c_name, where);
34986 }
34987 }
34988
34989 saw_error:
34990 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34991
34992 if (finish_p)
34993 return finish_omp_clauses (clauses, C_ORT_ACC);
34994
34995 return clauses;
34996 }
34997
34998 /* Parse all OpenMP clauses. The set clauses allowed by the directive
34999 is a bitmask in MASK. Return the list of clauses found; the result
35000 of clause default goes in *pdefault. */
35001
35002 static tree
35003 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
35004 const char *where, cp_token *pragma_tok,
35005 bool finish_p = true)
35006 {
35007 tree clauses = NULL;
35008 bool first = true;
35009 cp_token *token = NULL;
35010
35011 /* Don't create location wrapper nodes within OpenMP clauses. */
35012 auto_suppress_location_wrappers sentinel;
35013
35014 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35015 {
35016 pragma_omp_clause c_kind;
35017 const char *c_name;
35018 tree prev = clauses;
35019
35020 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35021 cp_lexer_consume_token (parser->lexer);
35022
35023 token = cp_lexer_peek_token (parser->lexer);
35024 c_kind = cp_parser_omp_clause_name (parser);
35025
35026 switch (c_kind)
35027 {
35028 case PRAGMA_OMP_CLAUSE_COLLAPSE:
35029 clauses = cp_parser_omp_clause_collapse (parser, clauses,
35030 token->location);
35031 c_name = "collapse";
35032 break;
35033 case PRAGMA_OMP_CLAUSE_COPYIN:
35034 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
35035 c_name = "copyin";
35036 break;
35037 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
35038 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
35039 clauses);
35040 c_name = "copyprivate";
35041 break;
35042 case PRAGMA_OMP_CLAUSE_DEFAULT:
35043 clauses = cp_parser_omp_clause_default (parser, clauses,
35044 token->location, false);
35045 c_name = "default";
35046 break;
35047 case PRAGMA_OMP_CLAUSE_FINAL:
35048 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
35049 c_name = "final";
35050 break;
35051 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
35052 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
35053 clauses);
35054 c_name = "firstprivate";
35055 break;
35056 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
35057 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
35058 token->location);
35059 c_name = "grainsize";
35060 break;
35061 case PRAGMA_OMP_CLAUSE_HINT:
35062 clauses = cp_parser_omp_clause_hint (parser, clauses,
35063 token->location);
35064 c_name = "hint";
35065 break;
35066 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
35067 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
35068 token->location);
35069 c_name = "defaultmap";
35070 break;
35071 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
35072 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35073 clauses);
35074 c_name = "use_device_ptr";
35075 break;
35076 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
35077 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
35078 clauses);
35079 c_name = "is_device_ptr";
35080 break;
35081 case PRAGMA_OMP_CLAUSE_IF:
35082 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
35083 true);
35084 c_name = "if";
35085 break;
35086 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
35087 clauses
35088 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
35089 true, clauses);
35090 c_name = "in_reduction";
35091 break;
35092 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
35093 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
35094 c_name = "lastprivate";
35095 break;
35096 case PRAGMA_OMP_CLAUSE_MERGEABLE:
35097 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
35098 token->location);
35099 c_name = "mergeable";
35100 break;
35101 case PRAGMA_OMP_CLAUSE_NOWAIT:
35102 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
35103 c_name = "nowait";
35104 break;
35105 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
35106 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
35107 token->location);
35108 c_name = "num_tasks";
35109 break;
35110 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
35111 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
35112 token->location);
35113 c_name = "num_threads";
35114 break;
35115 case PRAGMA_OMP_CLAUSE_ORDERED:
35116 clauses = cp_parser_omp_clause_ordered (parser, clauses,
35117 token->location);
35118 c_name = "ordered";
35119 break;
35120 case PRAGMA_OMP_CLAUSE_PRIORITY:
35121 clauses = cp_parser_omp_clause_priority (parser, clauses,
35122 token->location);
35123 c_name = "priority";
35124 break;
35125 case PRAGMA_OMP_CLAUSE_PRIVATE:
35126 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35127 clauses);
35128 c_name = "private";
35129 break;
35130 case PRAGMA_OMP_CLAUSE_REDUCTION:
35131 clauses
35132 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35133 true, clauses);
35134 c_name = "reduction";
35135 break;
35136 case PRAGMA_OMP_CLAUSE_SCHEDULE:
35137 clauses = cp_parser_omp_clause_schedule (parser, clauses,
35138 token->location);
35139 c_name = "schedule";
35140 break;
35141 case PRAGMA_OMP_CLAUSE_SHARED:
35142 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
35143 clauses);
35144 c_name = "shared";
35145 break;
35146 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
35147 clauses
35148 = cp_parser_omp_clause_reduction (parser,
35149 OMP_CLAUSE_TASK_REDUCTION,
35150 true, clauses);
35151 c_name = "task_reduction";
35152 break;
35153 case PRAGMA_OMP_CLAUSE_UNTIED:
35154 clauses = cp_parser_omp_clause_untied (parser, clauses,
35155 token->location);
35156 c_name = "untied";
35157 break;
35158 case PRAGMA_OMP_CLAUSE_INBRANCH:
35159 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
35160 clauses, token->location);
35161 c_name = "inbranch";
35162 break;
35163 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
35164 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
35165 clauses);
35166 c_name = "nontemporal";
35167 break;
35168 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
35169 clauses = cp_parser_omp_clause_branch (parser,
35170 OMP_CLAUSE_NOTINBRANCH,
35171 clauses, token->location);
35172 c_name = "notinbranch";
35173 break;
35174 case PRAGMA_OMP_CLAUSE_PARALLEL:
35175 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
35176 clauses, token->location);
35177 c_name = "parallel";
35178 if (!first)
35179 {
35180 clause_not_first:
35181 error_at (token->location, "%qs must be the first clause of %qs",
35182 c_name, where);
35183 clauses = prev;
35184 }
35185 break;
35186 case PRAGMA_OMP_CLAUSE_FOR:
35187 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
35188 clauses, token->location);
35189 c_name = "for";
35190 if (!first)
35191 goto clause_not_first;
35192 break;
35193 case PRAGMA_OMP_CLAUSE_SECTIONS:
35194 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
35195 clauses, token->location);
35196 c_name = "sections";
35197 if (!first)
35198 goto clause_not_first;
35199 break;
35200 case PRAGMA_OMP_CLAUSE_TASKGROUP:
35201 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
35202 clauses, token->location);
35203 c_name = "taskgroup";
35204 if (!first)
35205 goto clause_not_first;
35206 break;
35207 case PRAGMA_OMP_CLAUSE_LINK:
35208 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
35209 c_name = "to";
35210 break;
35211 case PRAGMA_OMP_CLAUSE_TO:
35212 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
35213 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35214 clauses);
35215 else
35216 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
35217 c_name = "to";
35218 break;
35219 case PRAGMA_OMP_CLAUSE_FROM:
35220 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
35221 c_name = "from";
35222 break;
35223 case PRAGMA_OMP_CLAUSE_UNIFORM:
35224 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
35225 clauses);
35226 c_name = "uniform";
35227 break;
35228 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
35229 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
35230 token->location);
35231 c_name = "num_teams";
35232 break;
35233 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
35234 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
35235 token->location);
35236 c_name = "thread_limit";
35237 break;
35238 case PRAGMA_OMP_CLAUSE_ALIGNED:
35239 clauses = cp_parser_omp_clause_aligned (parser, clauses);
35240 c_name = "aligned";
35241 break;
35242 case PRAGMA_OMP_CLAUSE_LINEAR:
35243 {
35244 bool declare_simd = false;
35245 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
35246 declare_simd = true;
35247 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
35248 }
35249 c_name = "linear";
35250 break;
35251 case PRAGMA_OMP_CLAUSE_DEPEND:
35252 clauses = cp_parser_omp_clause_depend (parser, clauses,
35253 token->location);
35254 c_name = "depend";
35255 break;
35256 case PRAGMA_OMP_CLAUSE_MAP:
35257 clauses = cp_parser_omp_clause_map (parser, clauses);
35258 c_name = "map";
35259 break;
35260 case PRAGMA_OMP_CLAUSE_DEVICE:
35261 clauses = cp_parser_omp_clause_device (parser, clauses,
35262 token->location);
35263 c_name = "device";
35264 break;
35265 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
35266 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
35267 token->location);
35268 c_name = "dist_schedule";
35269 break;
35270 case PRAGMA_OMP_CLAUSE_PROC_BIND:
35271 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
35272 token->location);
35273 c_name = "proc_bind";
35274 break;
35275 case PRAGMA_OMP_CLAUSE_SAFELEN:
35276 clauses = cp_parser_omp_clause_safelen (parser, clauses,
35277 token->location);
35278 c_name = "safelen";
35279 break;
35280 case PRAGMA_OMP_CLAUSE_SIMDLEN:
35281 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
35282 token->location);
35283 c_name = "simdlen";
35284 break;
35285 case PRAGMA_OMP_CLAUSE_NOGROUP:
35286 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
35287 token->location);
35288 c_name = "nogroup";
35289 break;
35290 case PRAGMA_OMP_CLAUSE_THREADS:
35291 clauses
35292 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
35293 clauses, token->location);
35294 c_name = "threads";
35295 break;
35296 case PRAGMA_OMP_CLAUSE_SIMD:
35297 clauses
35298 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
35299 clauses, token->location);
35300 c_name = "simd";
35301 break;
35302 default:
35303 cp_parser_error (parser, "expected %<#pragma omp%> clause");
35304 goto saw_error;
35305 }
35306
35307 first = false;
35308
35309 if (((mask >> c_kind) & 1) == 0)
35310 {
35311 /* Remove the invalid clause(s) from the list to avoid
35312 confusing the rest of the compiler. */
35313 clauses = prev;
35314 error_at (token->location, "%qs is not valid for %qs", c_name, where);
35315 }
35316 }
35317 saw_error:
35318 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35319 if (finish_p)
35320 {
35321 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
35322 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
35323 else
35324 return finish_omp_clauses (clauses, C_ORT_OMP);
35325 }
35326 return clauses;
35327 }
35328
35329 /* OpenMP 2.5:
35330 structured-block:
35331 statement
35332
35333 In practice, we're also interested in adding the statement to an
35334 outer node. So it is convenient if we work around the fact that
35335 cp_parser_statement calls add_stmt. */
35336
35337 static unsigned
35338 cp_parser_begin_omp_structured_block (cp_parser *parser)
35339 {
35340 unsigned save = parser->in_statement;
35341
35342 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35343 This preserves the "not within loop or switch" style error messages
35344 for nonsense cases like
35345 void foo() {
35346 #pragma omp single
35347 break;
35348 }
35349 */
35350 if (parser->in_statement)
35351 parser->in_statement = IN_OMP_BLOCK;
35352
35353 return save;
35354 }
35355
35356 static void
35357 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
35358 {
35359 parser->in_statement = save;
35360 }
35361
35362 static tree
35363 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
35364 {
35365 tree stmt = begin_omp_structured_block ();
35366 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35367
35368 cp_parser_statement (parser, NULL_TREE, false, if_p);
35369
35370 cp_parser_end_omp_structured_block (parser, save);
35371 return finish_omp_structured_block (stmt);
35372 }
35373
35374 /* OpenMP 2.5:
35375 # pragma omp atomic new-line
35376 expression-stmt
35377
35378 expression-stmt:
35379 x binop= expr | x++ | ++x | x-- | --x
35380 binop:
35381 +, *, -, /, &, ^, |, <<, >>
35382
35383 where x is an lvalue expression with scalar type.
35384
35385 OpenMP 3.1:
35386 # pragma omp atomic new-line
35387 update-stmt
35388
35389 # pragma omp atomic read new-line
35390 read-stmt
35391
35392 # pragma omp atomic write new-line
35393 write-stmt
35394
35395 # pragma omp atomic update new-line
35396 update-stmt
35397
35398 # pragma omp atomic capture new-line
35399 capture-stmt
35400
35401 # pragma omp atomic capture new-line
35402 capture-block
35403
35404 read-stmt:
35405 v = x
35406 write-stmt:
35407 x = expr
35408 update-stmt:
35409 expression-stmt | x = x binop expr
35410 capture-stmt:
35411 v = expression-stmt
35412 capture-block:
35413 { v = x; update-stmt; } | { update-stmt; v = x; }
35414
35415 OpenMP 4.0:
35416 update-stmt:
35417 expression-stmt | x = x binop expr | x = expr binop x
35418 capture-stmt:
35419 v = update-stmt
35420 capture-block:
35421 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35422
35423 where x and v are lvalue expressions with scalar type. */
35424
35425 static void
35426 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
35427 {
35428 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
35429 tree rhs1 = NULL_TREE, orig_lhs;
35430 location_t loc = pragma_tok->location;
35431 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
35432 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
35433 bool structured_block = false;
35434 bool first = true;
35435 tree clauses = NULL_TREE;
35436
35437 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35438 {
35439 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35440 cp_lexer_consume_token (parser->lexer);
35441
35442 first = false;
35443
35444 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35445 {
35446 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35447 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
35448 const char *p = IDENTIFIER_POINTER (id);
35449 enum tree_code new_code = ERROR_MARK;
35450 enum omp_memory_order new_memory_order
35451 = OMP_MEMORY_ORDER_UNSPECIFIED;
35452
35453 if (!strcmp (p, "read"))
35454 new_code = OMP_ATOMIC_READ;
35455 else if (!strcmp (p, "write"))
35456 new_code = NOP_EXPR;
35457 else if (!strcmp (p, "update"))
35458 new_code = OMP_ATOMIC;
35459 else if (!strcmp (p, "capture"))
35460 new_code = OMP_ATOMIC_CAPTURE_NEW;
35461 else if (!strcmp (p, "seq_cst"))
35462 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35463 else if (!strcmp (p, "acq_rel"))
35464 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35465 else if (!strcmp (p, "release"))
35466 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
35467 else if (!strcmp (p, "acquire"))
35468 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35469 else if (!strcmp (p, "relaxed"))
35470 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
35471 else if (!strcmp (p, "hint"))
35472 {
35473 cp_lexer_consume_token (parser->lexer);
35474 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
35475 continue;
35476 }
35477 else
35478 {
35479 p = NULL;
35480 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
35481 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35482 "%<release%>, %<relaxed%> or %<hint%> clause");
35483 }
35484 if (p)
35485 {
35486 if (new_code != ERROR_MARK)
35487 {
35488 if (code != ERROR_MARK)
35489 error_at (cloc, "too many atomic clauses");
35490 else
35491 code = new_code;
35492 }
35493 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35494 {
35495 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35496 error_at (cloc, "too many memory order clauses");
35497 else
35498 memory_order = new_memory_order;
35499 }
35500 cp_lexer_consume_token (parser->lexer);
35501 continue;
35502 }
35503 }
35504 break;
35505 }
35506 cp_parser_require_pragma_eol (parser, pragma_tok);
35507
35508 if (code == ERROR_MARK)
35509 code = OMP_ATOMIC;
35510 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
35511 {
35512 omp_requires_mask
35513 = (enum omp_requires) (omp_requires_mask
35514 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
35515 switch ((enum omp_memory_order)
35516 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
35517 {
35518 case OMP_MEMORY_ORDER_UNSPECIFIED:
35519 case OMP_MEMORY_ORDER_RELAXED:
35520 memory_order = OMP_MEMORY_ORDER_RELAXED;
35521 break;
35522 case OMP_MEMORY_ORDER_SEQ_CST:
35523 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35524 break;
35525 case OMP_MEMORY_ORDER_ACQ_REL:
35526 switch (code)
35527 {
35528 case OMP_ATOMIC_READ:
35529 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35530 break;
35531 case NOP_EXPR: /* atomic write */
35532 case OMP_ATOMIC:
35533 memory_order = OMP_MEMORY_ORDER_RELEASE;
35534 break;
35535 default:
35536 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35537 break;
35538 }
35539 break;
35540 default:
35541 gcc_unreachable ();
35542 }
35543 }
35544 else
35545 switch (code)
35546 {
35547 case OMP_ATOMIC_READ:
35548 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35549 || memory_order == OMP_MEMORY_ORDER_RELEASE)
35550 {
35551 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
35552 "%<acq_rel%> or %<release%> clauses");
35553 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35554 }
35555 break;
35556 case NOP_EXPR: /* atomic write */
35557 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35558 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35559 {
35560 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
35561 "%<acq_rel%> or %<acquire%> clauses");
35562 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35563 }
35564 break;
35565 case OMP_ATOMIC:
35566 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35567 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35568 {
35569 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
35570 "%<acq_rel%> or %<acquire%> clauses");
35571 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35572 }
35573 break;
35574 default:
35575 break;
35576 }
35577
35578 switch (code)
35579 {
35580 case OMP_ATOMIC_READ:
35581 case NOP_EXPR: /* atomic write */
35582 v = cp_parser_unary_expression (parser);
35583 if (v == error_mark_node)
35584 goto saw_error;
35585 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35586 goto saw_error;
35587 if (code == NOP_EXPR)
35588 lhs = cp_parser_expression (parser);
35589 else
35590 lhs = cp_parser_unary_expression (parser);
35591 if (lhs == error_mark_node)
35592 goto saw_error;
35593 if (code == NOP_EXPR)
35594 {
35595 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35596 opcode. */
35597 code = OMP_ATOMIC;
35598 rhs = lhs;
35599 lhs = v;
35600 v = NULL_TREE;
35601 }
35602 goto done;
35603 case OMP_ATOMIC_CAPTURE_NEW:
35604 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35605 {
35606 cp_lexer_consume_token (parser->lexer);
35607 structured_block = true;
35608 }
35609 else
35610 {
35611 v = cp_parser_unary_expression (parser);
35612 if (v == error_mark_node)
35613 goto saw_error;
35614 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35615 goto saw_error;
35616 }
35617 default:
35618 break;
35619 }
35620
35621 restart:
35622 lhs = cp_parser_unary_expression (parser);
35623 orig_lhs = lhs;
35624 switch (TREE_CODE (lhs))
35625 {
35626 case ERROR_MARK:
35627 goto saw_error;
35628
35629 case POSTINCREMENT_EXPR:
35630 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35631 code = OMP_ATOMIC_CAPTURE_OLD;
35632 /* FALLTHROUGH */
35633 case PREINCREMENT_EXPR:
35634 lhs = TREE_OPERAND (lhs, 0);
35635 opcode = PLUS_EXPR;
35636 rhs = integer_one_node;
35637 break;
35638
35639 case POSTDECREMENT_EXPR:
35640 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35641 code = OMP_ATOMIC_CAPTURE_OLD;
35642 /* FALLTHROUGH */
35643 case PREDECREMENT_EXPR:
35644 lhs = TREE_OPERAND (lhs, 0);
35645 opcode = MINUS_EXPR;
35646 rhs = integer_one_node;
35647 break;
35648
35649 case COMPOUND_EXPR:
35650 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
35651 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
35652 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
35653 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
35654 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35655 (TREE_OPERAND (lhs, 1), 0), 0)))
35656 == BOOLEAN_TYPE)
35657 /* Undo effects of boolean_increment for post {in,de}crement. */
35658 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
35659 /* FALLTHRU */
35660 case MODIFY_EXPR:
35661 if (TREE_CODE (lhs) == MODIFY_EXPR
35662 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
35663 {
35664 /* Undo effects of boolean_increment. */
35665 if (integer_onep (TREE_OPERAND (lhs, 1)))
35666 {
35667 /* This is pre or post increment. */
35668 rhs = TREE_OPERAND (lhs, 1);
35669 lhs = TREE_OPERAND (lhs, 0);
35670 opcode = NOP_EXPR;
35671 if (code == OMP_ATOMIC_CAPTURE_NEW
35672 && !structured_block
35673 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
35674 code = OMP_ATOMIC_CAPTURE_OLD;
35675 break;
35676 }
35677 }
35678 /* FALLTHRU */
35679 default:
35680 switch (cp_lexer_peek_token (parser->lexer)->type)
35681 {
35682 case CPP_MULT_EQ:
35683 opcode = MULT_EXPR;
35684 break;
35685 case CPP_DIV_EQ:
35686 opcode = TRUNC_DIV_EXPR;
35687 break;
35688 case CPP_PLUS_EQ:
35689 opcode = PLUS_EXPR;
35690 break;
35691 case CPP_MINUS_EQ:
35692 opcode = MINUS_EXPR;
35693 break;
35694 case CPP_LSHIFT_EQ:
35695 opcode = LSHIFT_EXPR;
35696 break;
35697 case CPP_RSHIFT_EQ:
35698 opcode = RSHIFT_EXPR;
35699 break;
35700 case CPP_AND_EQ:
35701 opcode = BIT_AND_EXPR;
35702 break;
35703 case CPP_OR_EQ:
35704 opcode = BIT_IOR_EXPR;
35705 break;
35706 case CPP_XOR_EQ:
35707 opcode = BIT_XOR_EXPR;
35708 break;
35709 case CPP_EQ:
35710 enum cp_parser_prec oprec;
35711 cp_token *token;
35712 cp_lexer_consume_token (parser->lexer);
35713 cp_parser_parse_tentatively (parser);
35714 rhs1 = cp_parser_simple_cast_expression (parser);
35715 if (rhs1 == error_mark_node)
35716 {
35717 cp_parser_abort_tentative_parse (parser);
35718 cp_parser_simple_cast_expression (parser);
35719 goto saw_error;
35720 }
35721 token = cp_lexer_peek_token (parser->lexer);
35722 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
35723 {
35724 cp_parser_abort_tentative_parse (parser);
35725 cp_parser_parse_tentatively (parser);
35726 rhs = cp_parser_binary_expression (parser, false, true,
35727 PREC_NOT_OPERATOR, NULL);
35728 if (rhs == error_mark_node)
35729 {
35730 cp_parser_abort_tentative_parse (parser);
35731 cp_parser_binary_expression (parser, false, true,
35732 PREC_NOT_OPERATOR, NULL);
35733 goto saw_error;
35734 }
35735 switch (TREE_CODE (rhs))
35736 {
35737 case MULT_EXPR:
35738 case TRUNC_DIV_EXPR:
35739 case RDIV_EXPR:
35740 case PLUS_EXPR:
35741 case MINUS_EXPR:
35742 case LSHIFT_EXPR:
35743 case RSHIFT_EXPR:
35744 case BIT_AND_EXPR:
35745 case BIT_IOR_EXPR:
35746 case BIT_XOR_EXPR:
35747 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
35748 {
35749 if (cp_parser_parse_definitely (parser))
35750 {
35751 opcode = TREE_CODE (rhs);
35752 rhs1 = TREE_OPERAND (rhs, 0);
35753 rhs = TREE_OPERAND (rhs, 1);
35754 goto stmt_done;
35755 }
35756 else
35757 goto saw_error;
35758 }
35759 break;
35760 default:
35761 break;
35762 }
35763 cp_parser_abort_tentative_parse (parser);
35764 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
35765 {
35766 rhs = cp_parser_expression (parser);
35767 if (rhs == error_mark_node)
35768 goto saw_error;
35769 opcode = NOP_EXPR;
35770 rhs1 = NULL_TREE;
35771 goto stmt_done;
35772 }
35773 cp_parser_error (parser,
35774 "invalid form of %<#pragma omp atomic%>");
35775 goto saw_error;
35776 }
35777 if (!cp_parser_parse_definitely (parser))
35778 goto saw_error;
35779 switch (token->type)
35780 {
35781 case CPP_SEMICOLON:
35782 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35783 {
35784 code = OMP_ATOMIC_CAPTURE_OLD;
35785 v = lhs;
35786 lhs = NULL_TREE;
35787 lhs1 = rhs1;
35788 rhs1 = NULL_TREE;
35789 cp_lexer_consume_token (parser->lexer);
35790 goto restart;
35791 }
35792 else if (structured_block)
35793 {
35794 opcode = NOP_EXPR;
35795 rhs = rhs1;
35796 rhs1 = NULL_TREE;
35797 goto stmt_done;
35798 }
35799 cp_parser_error (parser,
35800 "invalid form of %<#pragma omp atomic%>");
35801 goto saw_error;
35802 case CPP_MULT:
35803 opcode = MULT_EXPR;
35804 break;
35805 case CPP_DIV:
35806 opcode = TRUNC_DIV_EXPR;
35807 break;
35808 case CPP_PLUS:
35809 opcode = PLUS_EXPR;
35810 break;
35811 case CPP_MINUS:
35812 opcode = MINUS_EXPR;
35813 break;
35814 case CPP_LSHIFT:
35815 opcode = LSHIFT_EXPR;
35816 break;
35817 case CPP_RSHIFT:
35818 opcode = RSHIFT_EXPR;
35819 break;
35820 case CPP_AND:
35821 opcode = BIT_AND_EXPR;
35822 break;
35823 case CPP_OR:
35824 opcode = BIT_IOR_EXPR;
35825 break;
35826 case CPP_XOR:
35827 opcode = BIT_XOR_EXPR;
35828 break;
35829 default:
35830 cp_parser_error (parser,
35831 "invalid operator for %<#pragma omp atomic%>");
35832 goto saw_error;
35833 }
35834 oprec = TOKEN_PRECEDENCE (token);
35835 gcc_assert (oprec != PREC_NOT_OPERATOR);
35836 if (commutative_tree_code (opcode))
35837 oprec = (enum cp_parser_prec) (oprec - 1);
35838 cp_lexer_consume_token (parser->lexer);
35839 rhs = cp_parser_binary_expression (parser, false, false,
35840 oprec, NULL);
35841 if (rhs == error_mark_node)
35842 goto saw_error;
35843 goto stmt_done;
35844 /* FALLTHROUGH */
35845 default:
35846 cp_parser_error (parser,
35847 "invalid operator for %<#pragma omp atomic%>");
35848 goto saw_error;
35849 }
35850 cp_lexer_consume_token (parser->lexer);
35851
35852 rhs = cp_parser_expression (parser);
35853 if (rhs == error_mark_node)
35854 goto saw_error;
35855 break;
35856 }
35857 stmt_done:
35858 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35859 {
35860 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
35861 goto saw_error;
35862 v = cp_parser_unary_expression (parser);
35863 if (v == error_mark_node)
35864 goto saw_error;
35865 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35866 goto saw_error;
35867 lhs1 = cp_parser_unary_expression (parser);
35868 if (lhs1 == error_mark_node)
35869 goto saw_error;
35870 }
35871 if (structured_block)
35872 {
35873 cp_parser_consume_semicolon_at_end_of_statement (parser);
35874 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35875 }
35876 done:
35877 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
35878 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
35879 rhs1, clauses, memory_order);
35880 if (!structured_block)
35881 cp_parser_consume_semicolon_at_end_of_statement (parser);
35882 return;
35883
35884 saw_error:
35885 cp_parser_skip_to_end_of_block_or_statement (parser);
35886 if (structured_block)
35887 {
35888 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35889 cp_lexer_consume_token (parser->lexer);
35890 else if (code == OMP_ATOMIC_CAPTURE_NEW)
35891 {
35892 cp_parser_skip_to_end_of_block_or_statement (parser);
35893 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35894 cp_lexer_consume_token (parser->lexer);
35895 }
35896 }
35897 }
35898
35899
35900 /* OpenMP 2.5:
35901 # pragma omp barrier new-line */
35902
35903 static void
35904 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
35905 {
35906 cp_parser_require_pragma_eol (parser, pragma_tok);
35907 finish_omp_barrier ();
35908 }
35909
35910 /* OpenMP 2.5:
35911 # pragma omp critical [(name)] new-line
35912 structured-block
35913
35914 OpenMP 4.5:
35915 # pragma omp critical [(name) [hint(expression)]] new-line
35916 structured-block */
35917
35918 #define OMP_CRITICAL_CLAUSE_MASK \
35919 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35920
35921 static tree
35922 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35923 {
35924 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
35925
35926 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35927 {
35928 matching_parens parens;
35929 parens.consume_open (parser);
35930
35931 name = cp_parser_identifier (parser);
35932
35933 if (name == error_mark_node
35934 || !parens.require_close (parser))
35935 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35936 /*or_comma=*/false,
35937 /*consume_paren=*/true);
35938 if (name == error_mark_node)
35939 name = NULL;
35940
35941 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
35942 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
35943 cp_lexer_consume_token (parser->lexer);
35944
35945 clauses = cp_parser_omp_all_clauses (parser,
35946 OMP_CRITICAL_CLAUSE_MASK,
35947 "#pragma omp critical", pragma_tok);
35948 }
35949 else
35950 cp_parser_require_pragma_eol (parser, pragma_tok);
35951
35952 stmt = cp_parser_omp_structured_block (parser, if_p);
35953 return c_finish_omp_critical (input_location, stmt, name, clauses);
35954 }
35955
35956 /* OpenMP 5.0:
35957 # pragma omp depobj ( depobj ) depobj-clause new-line
35958
35959 depobj-clause:
35960 depend (dependence-type : locator)
35961 destroy
35962 update (dependence-type)
35963
35964 dependence-type:
35965 in
35966 out
35967 inout
35968 mutexinout */
35969
35970 static void
35971 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
35972 {
35973 location_t loc = pragma_tok->location;
35974 matching_parens parens;
35975 if (!parens.require_open (parser))
35976 {
35977 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35978 return;
35979 }
35980
35981 tree depobj = cp_parser_assignment_expression (parser);
35982
35983 if (!parens.require_close (parser))
35984 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35985 /*or_comma=*/false,
35986 /*consume_paren=*/true);
35987
35988 tree clause = NULL_TREE;
35989 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
35990 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
35991 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35992 {
35993 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35994 const char *p = IDENTIFIER_POINTER (id);
35995
35996 cp_lexer_consume_token (parser->lexer);
35997 if (!strcmp ("depend", p))
35998 {
35999 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
36000 if (clause)
36001 clause = finish_omp_clauses (clause, C_ORT_OMP);
36002 if (!clause)
36003 clause = error_mark_node;
36004 }
36005 else if (!strcmp ("destroy", p))
36006 kind = OMP_CLAUSE_DEPEND_LAST;
36007 else if (!strcmp ("update", p))
36008 {
36009 matching_parens c_parens;
36010 if (c_parens.require_open (parser))
36011 {
36012 location_t c2_loc
36013 = cp_lexer_peek_token (parser->lexer)->location;
36014 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36015 {
36016 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
36017 const char *p2 = IDENTIFIER_POINTER (id2);
36018
36019 cp_lexer_consume_token (parser->lexer);
36020 if (!strcmp ("in", p2))
36021 kind = OMP_CLAUSE_DEPEND_IN;
36022 else if (!strcmp ("out", p2))
36023 kind = OMP_CLAUSE_DEPEND_OUT;
36024 else if (!strcmp ("inout", p2))
36025 kind = OMP_CLAUSE_DEPEND_INOUT;
36026 else if (!strcmp ("mutexinoutset", p2))
36027 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
36028 }
36029 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
36030 {
36031 clause = error_mark_node;
36032 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
36033 "%<mutexinoutset%>");
36034 }
36035 if (!c_parens.require_close (parser))
36036 cp_parser_skip_to_closing_parenthesis (parser,
36037 /*recovering=*/true,
36038 /*or_comma=*/false,
36039 /*consume_paren=*/true);
36040 }
36041 else
36042 clause = error_mark_node;
36043 }
36044 }
36045 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
36046 {
36047 clause = error_mark_node;
36048 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
36049 }
36050 cp_parser_require_pragma_eol (parser, pragma_tok);
36051
36052 finish_omp_depobj (loc, depobj, kind, clause);
36053 }
36054
36055
36056 /* OpenMP 2.5:
36057 # pragma omp flush flush-vars[opt] new-line
36058
36059 flush-vars:
36060 ( variable-list )
36061
36062 OpenMP 5.0:
36063 # pragma omp flush memory-order-clause new-line */
36064
36065 static void
36066 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
36067 {
36068 enum memmodel mo = MEMMODEL_LAST;
36069 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36070 {
36071 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36072 const char *p = IDENTIFIER_POINTER (id);
36073 if (!strcmp (p, "acq_rel"))
36074 mo = MEMMODEL_ACQ_REL;
36075 else if (!strcmp (p, "release"))
36076 mo = MEMMODEL_RELEASE;
36077 else if (!strcmp (p, "acquire"))
36078 mo = MEMMODEL_ACQUIRE;
36079 else
36080 error_at (cp_lexer_peek_token (parser->lexer)->location,
36081 "expected %<acq_rel%>, %<release%> or %<acquire%>");
36082 cp_lexer_consume_token (parser->lexer);
36083 }
36084 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36085 {
36086 if (mo != MEMMODEL_LAST)
36087 error_at (cp_lexer_peek_token (parser->lexer)->location,
36088 "%<flush%> list specified together with memory order "
36089 "clause");
36090 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36091 }
36092 cp_parser_require_pragma_eol (parser, pragma_tok);
36093
36094 finish_omp_flush (mo);
36095 }
36096
36097 /* Helper function, to parse omp for increment expression. */
36098
36099 static tree
36100 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
36101 {
36102 tree cond = cp_parser_binary_expression (parser, false, true,
36103 PREC_NOT_OPERATOR, NULL);
36104 if (cond == error_mark_node
36105 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36106 {
36107 cp_parser_skip_to_end_of_statement (parser);
36108 return error_mark_node;
36109 }
36110
36111 switch (TREE_CODE (cond))
36112 {
36113 case GT_EXPR:
36114 case GE_EXPR:
36115 case LT_EXPR:
36116 case LE_EXPR:
36117 break;
36118 case NE_EXPR:
36119 if (code != OACC_LOOP)
36120 break;
36121 gcc_fallthrough ();
36122 default:
36123 return error_mark_node;
36124 }
36125
36126 /* If decl is an iterator, preserve LHS and RHS of the relational
36127 expr until finish_omp_for. */
36128 if (decl
36129 && (type_dependent_expression_p (decl)
36130 || CLASS_TYPE_P (TREE_TYPE (decl))))
36131 return cond;
36132
36133 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
36134 TREE_CODE (cond),
36135 TREE_OPERAND (cond, 0), ERROR_MARK,
36136 TREE_OPERAND (cond, 1), ERROR_MARK,
36137 /*overload=*/NULL, tf_warning_or_error);
36138 }
36139
36140 /* Helper function, to parse omp for increment expression. */
36141
36142 static tree
36143 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
36144 {
36145 cp_token *token = cp_lexer_peek_token (parser->lexer);
36146 enum tree_code op;
36147 tree lhs, rhs;
36148 cp_id_kind idk;
36149 bool decl_first;
36150
36151 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36152 {
36153 op = (token->type == CPP_PLUS_PLUS
36154 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
36155 cp_lexer_consume_token (parser->lexer);
36156 lhs = cp_parser_simple_cast_expression (parser);
36157 if (lhs != decl
36158 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36159 return error_mark_node;
36160 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36161 }
36162
36163 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
36164 if (lhs != decl
36165 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36166 return error_mark_node;
36167
36168 token = cp_lexer_peek_token (parser->lexer);
36169 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36170 {
36171 op = (token->type == CPP_PLUS_PLUS
36172 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
36173 cp_lexer_consume_token (parser->lexer);
36174 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36175 }
36176
36177 op = cp_parser_assignment_operator_opt (parser);
36178 if (op == ERROR_MARK)
36179 return error_mark_node;
36180
36181 if (op != NOP_EXPR)
36182 {
36183 rhs = cp_parser_assignment_expression (parser);
36184 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
36185 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36186 }
36187
36188 lhs = cp_parser_binary_expression (parser, false, false,
36189 PREC_ADDITIVE_EXPRESSION, NULL);
36190 token = cp_lexer_peek_token (parser->lexer);
36191 decl_first = (lhs == decl
36192 || (processing_template_decl && cp_tree_equal (lhs, decl)));
36193 if (decl_first)
36194 lhs = NULL_TREE;
36195 if (token->type != CPP_PLUS
36196 && token->type != CPP_MINUS)
36197 return error_mark_node;
36198
36199 do
36200 {
36201 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
36202 cp_lexer_consume_token (parser->lexer);
36203 rhs = cp_parser_binary_expression (parser, false, false,
36204 PREC_ADDITIVE_EXPRESSION, NULL);
36205 token = cp_lexer_peek_token (parser->lexer);
36206 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
36207 {
36208 if (lhs == NULL_TREE)
36209 {
36210 if (op == PLUS_EXPR)
36211 lhs = rhs;
36212 else
36213 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
36214 tf_warning_or_error);
36215 }
36216 else
36217 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
36218 ERROR_MARK, NULL, tf_warning_or_error);
36219 }
36220 }
36221 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
36222
36223 if (!decl_first)
36224 {
36225 if ((rhs != decl
36226 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
36227 || op == MINUS_EXPR)
36228 return error_mark_node;
36229 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
36230 }
36231 else
36232 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
36233
36234 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36235 }
36236
36237 /* Parse the initialization statement of an OpenMP for loop.
36238
36239 Return true if the resulting construct should have an
36240 OMP_CLAUSE_PRIVATE added to it. */
36241
36242 static tree
36243 cp_parser_omp_for_loop_init (cp_parser *parser,
36244 tree &this_pre_body,
36245 vec<tree, va_gc> *&for_block,
36246 tree &init,
36247 tree &orig_init,
36248 tree &decl,
36249 tree &real_decl)
36250 {
36251 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36252 return NULL_TREE;
36253
36254 tree add_private_clause = NULL_TREE;
36255
36256 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
36257
36258 init-expr:
36259 var = lb
36260 integer-type var = lb
36261 random-access-iterator-type var = lb
36262 pointer-type var = lb
36263 */
36264 cp_decl_specifier_seq type_specifiers;
36265
36266 /* First, try to parse as an initialized declaration. See
36267 cp_parser_condition, from whence the bulk of this is copied. */
36268
36269 cp_parser_parse_tentatively (parser);
36270 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
36271 /*is_declaration=*/true,
36272 /*is_trailing_return=*/false,
36273 &type_specifiers);
36274 if (cp_parser_parse_definitely (parser))
36275 {
36276 /* If parsing a type specifier seq succeeded, then this
36277 MUST be a initialized declaration. */
36278 tree asm_specification, attributes;
36279 cp_declarator *declarator;
36280
36281 declarator = cp_parser_declarator (parser,
36282 CP_PARSER_DECLARATOR_NAMED,
36283 CP_PARSER_FLAGS_NONE,
36284 /*ctor_dtor_or_conv_p=*/NULL,
36285 /*parenthesized_p=*/NULL,
36286 /*member_p=*/false,
36287 /*friend_p=*/false,
36288 /*static_p=*/false);
36289 attributes = cp_parser_attributes_opt (parser);
36290 asm_specification = cp_parser_asm_specification_opt (parser);
36291
36292 if (declarator == cp_error_declarator)
36293 cp_parser_skip_to_end_of_statement (parser);
36294
36295 else
36296 {
36297 tree pushed_scope, auto_node;
36298
36299 decl = start_decl (declarator, &type_specifiers,
36300 SD_INITIALIZED, attributes,
36301 /*prefix_attributes=*/NULL_TREE,
36302 &pushed_scope);
36303
36304 auto_node = type_uses_auto (TREE_TYPE (decl));
36305 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36306 {
36307 if (cp_lexer_next_token_is (parser->lexer,
36308 CPP_OPEN_PAREN))
36309 error ("parenthesized initialization is not allowed in "
36310 "OpenMP %<for%> loop");
36311 else
36312 /* Trigger an error. */
36313 cp_parser_require (parser, CPP_EQ, RT_EQ);
36314
36315 init = error_mark_node;
36316 cp_parser_skip_to_end_of_statement (parser);
36317 }
36318 else if (CLASS_TYPE_P (TREE_TYPE (decl))
36319 || type_dependent_expression_p (decl)
36320 || auto_node)
36321 {
36322 bool is_direct_init, is_non_constant_init;
36323
36324 init = cp_parser_initializer (parser,
36325 &is_direct_init,
36326 &is_non_constant_init);
36327
36328 if (auto_node)
36329 {
36330 TREE_TYPE (decl)
36331 = do_auto_deduction (TREE_TYPE (decl), init,
36332 auto_node);
36333
36334 if (!CLASS_TYPE_P (TREE_TYPE (decl))
36335 && !type_dependent_expression_p (decl))
36336 goto non_class;
36337 }
36338
36339 cp_finish_decl (decl, init, !is_non_constant_init,
36340 asm_specification,
36341 LOOKUP_ONLYCONVERTING);
36342 orig_init = init;
36343 if (CLASS_TYPE_P (TREE_TYPE (decl)))
36344 {
36345 vec_safe_push (for_block, this_pre_body);
36346 init = NULL_TREE;
36347 }
36348 else
36349 {
36350 init = pop_stmt_list (this_pre_body);
36351 if (init && TREE_CODE (init) == STATEMENT_LIST)
36352 {
36353 tree_stmt_iterator i = tsi_start (init);
36354 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36355 while (!tsi_end_p (i))
36356 {
36357 tree t = tsi_stmt (i);
36358 if (TREE_CODE (t) == DECL_EXPR
36359 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
36360 {
36361 tsi_delink (&i);
36362 vec_safe_push (for_block, t);
36363 continue;
36364 }
36365 break;
36366 }
36367 if (tsi_one_before_end_p (i))
36368 {
36369 tree t = tsi_stmt (i);
36370 tsi_delink (&i);
36371 free_stmt_list (init);
36372 init = t;
36373 }
36374 }
36375 }
36376 this_pre_body = NULL_TREE;
36377 }
36378 else
36379 {
36380 /* Consume '='. */
36381 cp_lexer_consume_token (parser->lexer);
36382 init = cp_parser_assignment_expression (parser);
36383
36384 non_class:
36385 if (TYPE_REF_P (TREE_TYPE (decl)))
36386 init = error_mark_node;
36387 else
36388 cp_finish_decl (decl, NULL_TREE,
36389 /*init_const_expr_p=*/false,
36390 asm_specification,
36391 LOOKUP_ONLYCONVERTING);
36392 }
36393
36394 if (pushed_scope)
36395 pop_scope (pushed_scope);
36396 }
36397 }
36398 else
36399 {
36400 cp_id_kind idk;
36401 /* If parsing a type specifier sequence failed, then
36402 this MUST be a simple expression. */
36403 cp_parser_parse_tentatively (parser);
36404 decl = cp_parser_primary_expression (parser, false, false,
36405 false, &idk);
36406 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
36407 if (!cp_parser_error_occurred (parser)
36408 && decl
36409 && (TREE_CODE (decl) == COMPONENT_REF
36410 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
36411 {
36412 cp_parser_abort_tentative_parse (parser);
36413 cp_parser_parse_tentatively (parser);
36414 cp_token *token = cp_lexer_peek_token (parser->lexer);
36415 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
36416 /*check_dependency_p=*/true,
36417 /*template_p=*/NULL,
36418 /*declarator_p=*/false,
36419 /*optional_p=*/false);
36420 if (name != error_mark_node
36421 && last_tok == cp_lexer_peek_token (parser->lexer))
36422 {
36423 decl = cp_parser_lookup_name_simple (parser, name,
36424 token->location);
36425 if (TREE_CODE (decl) == FIELD_DECL)
36426 add_private_clause = omp_privatize_field (decl, false);
36427 }
36428 cp_parser_abort_tentative_parse (parser);
36429 cp_parser_parse_tentatively (parser);
36430 decl = cp_parser_primary_expression (parser, false, false,
36431 false, &idk);
36432 }
36433 if (!cp_parser_error_occurred (parser)
36434 && decl
36435 && DECL_P (decl)
36436 && CLASS_TYPE_P (TREE_TYPE (decl)))
36437 {
36438 tree rhs;
36439
36440 cp_parser_parse_definitely (parser);
36441 cp_parser_require (parser, CPP_EQ, RT_EQ);
36442 rhs = cp_parser_assignment_expression (parser);
36443 orig_init = rhs;
36444 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
36445 decl, NOP_EXPR,
36446 rhs,
36447 tf_warning_or_error));
36448 if (!add_private_clause)
36449 add_private_clause = decl;
36450 }
36451 else
36452 {
36453 decl = NULL;
36454 cp_parser_abort_tentative_parse (parser);
36455 init = cp_parser_expression (parser);
36456 if (init)
36457 {
36458 if (TREE_CODE (init) == MODIFY_EXPR
36459 || TREE_CODE (init) == MODOP_EXPR)
36460 real_decl = TREE_OPERAND (init, 0);
36461 }
36462 }
36463 }
36464 return add_private_clause;
36465 }
36466
36467 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36468
36469 void
36470 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
36471 tree &decl, tree &orig_decl, tree &init,
36472 tree &orig_init, tree &cond, tree &incr)
36473 {
36474 tree begin, end, range_temp_decl = NULL_TREE;
36475 tree iter_type, begin_expr, end_expr;
36476
36477 if (processing_template_decl)
36478 {
36479 if (check_for_bare_parameter_packs (init))
36480 init = error_mark_node;
36481 if (!type_dependent_expression_p (init)
36482 /* do_auto_deduction doesn't mess with template init-lists. */
36483 && !BRACE_ENCLOSED_INITIALIZER_P (init))
36484 {
36485 tree d = decl;
36486 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
36487 {
36488 tree v = DECL_VALUE_EXPR (decl);
36489 if (TREE_CODE (v) == ARRAY_REF
36490 && VAR_P (TREE_OPERAND (v, 0))
36491 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36492 d = TREE_OPERAND (v, 0);
36493 }
36494 do_range_for_auto_deduction (d, init);
36495 }
36496 cond = global_namespace;
36497 incr = NULL_TREE;
36498 orig_init = init;
36499 if (this_pre_body)
36500 this_pre_body = pop_stmt_list (this_pre_body);
36501 return;
36502 }
36503
36504 init = mark_lvalue_use (init);
36505
36506 if (decl == error_mark_node || init == error_mark_node)
36507 /* If an error happened previously do nothing or else a lot of
36508 unhelpful errors would be issued. */
36509 begin_expr = end_expr = iter_type = error_mark_node;
36510 else
36511 {
36512 tree range_temp;
36513
36514 if (VAR_P (init)
36515 && array_of_runtime_bound_p (TREE_TYPE (init)))
36516 /* Can't bind a reference to an array of runtime bound. */
36517 range_temp = init;
36518 else
36519 {
36520 range_temp = build_range_temp (init);
36521 DECL_NAME (range_temp) = NULL_TREE;
36522 pushdecl (range_temp);
36523 cp_finish_decl (range_temp, init,
36524 /*is_constant_init*/false, NULL_TREE,
36525 LOOKUP_ONLYCONVERTING);
36526 range_temp_decl = range_temp;
36527 range_temp = convert_from_reference (range_temp);
36528 }
36529 iter_type = cp_parser_perform_range_for_lookup (range_temp,
36530 &begin_expr, &end_expr);
36531 }
36532
36533 tree end_iter_type = iter_type;
36534 if (cxx_dialect >= cxx17)
36535 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
36536 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
36537 TREE_USED (end) = 1;
36538 DECL_ARTIFICIAL (end) = 1;
36539 pushdecl (end);
36540 cp_finish_decl (end, end_expr,
36541 /*is_constant_init*/false, NULL_TREE,
36542 LOOKUP_ONLYCONVERTING);
36543
36544 /* The new for initialization statement. */
36545 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
36546 TREE_USED (begin) = 1;
36547 DECL_ARTIFICIAL (begin) = 1;
36548 pushdecl (begin);
36549 orig_init = init;
36550 if (CLASS_TYPE_P (iter_type))
36551 init = NULL_TREE;
36552 else
36553 {
36554 init = begin_expr;
36555 begin_expr = NULL_TREE;
36556 }
36557 cp_finish_decl (begin, begin_expr,
36558 /*is_constant_init*/false, NULL_TREE,
36559 LOOKUP_ONLYCONVERTING);
36560
36561 /* The new for condition. */
36562 if (CLASS_TYPE_P (iter_type))
36563 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
36564 else
36565 cond = build_x_binary_op (input_location, NE_EXPR,
36566 begin, ERROR_MARK,
36567 end, ERROR_MARK,
36568 NULL, tf_warning_or_error);
36569
36570 /* The new increment expression. */
36571 if (CLASS_TYPE_P (iter_type))
36572 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
36573 else
36574 incr = finish_unary_op_expr (input_location,
36575 PREINCREMENT_EXPR, begin,
36576 tf_warning_or_error);
36577
36578 orig_decl = decl;
36579 decl = begin;
36580 if (for_block)
36581 {
36582 vec_safe_push (for_block, this_pre_body);
36583 this_pre_body = NULL_TREE;
36584 }
36585
36586 tree decomp_first_name = NULL_TREE;
36587 unsigned decomp_cnt = 0;
36588 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
36589 {
36590 tree v = DECL_VALUE_EXPR (orig_decl);
36591 if (TREE_CODE (v) == ARRAY_REF
36592 && VAR_P (TREE_OPERAND (v, 0))
36593 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36594 {
36595 tree d = orig_decl;
36596 orig_decl = TREE_OPERAND (v, 0);
36597 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
36598 decomp_first_name = d;
36599 }
36600 }
36601
36602 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
36603 if (auto_node)
36604 {
36605 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36606 tf_none);
36607 if (!error_operand_p (t))
36608 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
36609 t, auto_node);
36610 }
36611
36612 tree v = make_tree_vec (decomp_cnt + 3);
36613 TREE_VEC_ELT (v, 0) = range_temp_decl;
36614 TREE_VEC_ELT (v, 1) = end;
36615 TREE_VEC_ELT (v, 2) = orig_decl;
36616 for (unsigned i = 0; i < decomp_cnt; i++)
36617 {
36618 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
36619 decomp_first_name = DECL_CHAIN (decomp_first_name);
36620 }
36621 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
36622 }
36623
36624 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36625 inside of the collapsed body. */
36626
36627 void
36628 cp_finish_omp_range_for (tree orig, tree begin)
36629 {
36630 gcc_assert (TREE_CODE (orig) == TREE_LIST
36631 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
36632 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
36633 tree decomp_first_name = NULL_TREE;
36634 unsigned int decomp_cnt = 0;
36635
36636 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36637 {
36638 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
36639 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
36640 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
36641 }
36642
36643 /* The declaration is initialized with *__begin inside the loop body. */
36644 cp_finish_decl (decl,
36645 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36646 tf_warning_or_error),
36647 /*is_constant_init*/false, NULL_TREE,
36648 LOOKUP_ONLYCONVERTING);
36649 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36650 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
36651 }
36652
36653 /* Parse the restricted form of the for statement allowed by OpenMP. */
36654
36655 static tree
36656 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
36657 tree *cclauses, bool *if_p)
36658 {
36659 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
36660 tree orig_decl;
36661 tree real_decl, initv, condv, incrv, declv, orig_declv;
36662 tree this_pre_body, cl, ordered_cl = NULL_TREE;
36663 location_t loc_first;
36664 bool collapse_err = false;
36665 int i, collapse = 1, ordered = 0, count, nbraces = 0;
36666 vec<tree, va_gc> *for_block = make_tree_vector ();
36667 auto_vec<tree, 4> orig_inits;
36668 bool tiling = false;
36669
36670 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
36671 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
36672 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
36673 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
36674 {
36675 tiling = true;
36676 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
36677 }
36678 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
36679 && OMP_CLAUSE_ORDERED_EXPR (cl))
36680 {
36681 ordered_cl = cl;
36682 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
36683 }
36684
36685 if (ordered && ordered < collapse)
36686 {
36687 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36688 "%<ordered%> clause parameter is less than %<collapse%>");
36689 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
36690 = build_int_cst (NULL_TREE, collapse);
36691 ordered = collapse;
36692 }
36693 if (ordered)
36694 {
36695 for (tree *pc = &clauses; *pc; )
36696 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
36697 {
36698 error_at (OMP_CLAUSE_LOCATION (*pc),
36699 "%<linear%> clause may not be specified together "
36700 "with %<ordered%> clause with a parameter");
36701 *pc = OMP_CLAUSE_CHAIN (*pc);
36702 }
36703 else
36704 pc = &OMP_CLAUSE_CHAIN (*pc);
36705 }
36706
36707 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
36708 count = ordered ? ordered : collapse;
36709
36710 declv = make_tree_vec (count);
36711 initv = make_tree_vec (count);
36712 condv = make_tree_vec (count);
36713 incrv = make_tree_vec (count);
36714 orig_declv = NULL_TREE;
36715
36716 loc_first = cp_lexer_peek_token (parser->lexer)->location;
36717
36718 for (i = 0; i < count; i++)
36719 {
36720 int bracecount = 0;
36721 tree add_private_clause = NULL_TREE;
36722 location_t loc;
36723
36724 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36725 {
36726 if (!collapse_err)
36727 cp_parser_error (parser, "for statement expected");
36728 return NULL;
36729 }
36730 loc = cp_lexer_consume_token (parser->lexer)->location;
36731
36732 /* Don't create location wrapper nodes within an OpenMP "for"
36733 statement. */
36734 auto_suppress_location_wrappers sentinel;
36735
36736 matching_parens parens;
36737 if (!parens.require_open (parser))
36738 return NULL;
36739
36740 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
36741 this_pre_body = push_stmt_list ();
36742
36743 if (code != OACC_LOOP && cxx_dialect >= cxx11)
36744 {
36745 /* Save tokens so that we can put them back. */
36746 cp_lexer_save_tokens (parser->lexer);
36747
36748 /* Look for ':' that is not nested in () or {}. */
36749 bool is_range_for
36750 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
36751 /*recovering=*/false,
36752 CPP_COLON,
36753 /*consume_paren=*/
36754 false) == -1);
36755
36756 /* Roll back the tokens we skipped. */
36757 cp_lexer_rollback_tokens (parser->lexer);
36758
36759 if (is_range_for)
36760 {
36761 bool saved_colon_corrects_to_scope_p
36762 = parser->colon_corrects_to_scope_p;
36763
36764 /* A colon is used in range-based for. */
36765 parser->colon_corrects_to_scope_p = false;
36766
36767 /* Parse the declaration. */
36768 cp_parser_simple_declaration (parser,
36769 /*function_definition_allowed_p=*/
36770 false, &decl);
36771 parser->colon_corrects_to_scope_p
36772 = saved_colon_corrects_to_scope_p;
36773
36774 cp_parser_require (parser, CPP_COLON, RT_COLON);
36775
36776 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
36777 false, 0, true);
36778
36779 cp_convert_omp_range_for (this_pre_body, for_block, decl,
36780 orig_decl, init, orig_init,
36781 cond, incr);
36782 if (this_pre_body)
36783 {
36784 if (pre_body)
36785 {
36786 tree t = pre_body;
36787 pre_body = push_stmt_list ();
36788 add_stmt (t);
36789 add_stmt (this_pre_body);
36790 pre_body = pop_stmt_list (pre_body);
36791 }
36792 else
36793 pre_body = this_pre_body;
36794 }
36795
36796 if (ordered_cl)
36797 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36798 "%<ordered%> clause with parameter on "
36799 "range-based %<for%> loop");
36800
36801 goto parse_close_paren;
36802 }
36803 }
36804
36805 add_private_clause
36806 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
36807 init, orig_init, decl, real_decl);
36808
36809 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36810 if (this_pre_body)
36811 {
36812 this_pre_body = pop_stmt_list (this_pre_body);
36813 if (pre_body)
36814 {
36815 tree t = pre_body;
36816 pre_body = push_stmt_list ();
36817 add_stmt (t);
36818 add_stmt (this_pre_body);
36819 pre_body = pop_stmt_list (pre_body);
36820 }
36821 else
36822 pre_body = this_pre_body;
36823 }
36824
36825 if (decl)
36826 real_decl = decl;
36827 if (cclauses != NULL
36828 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
36829 && real_decl != NULL_TREE)
36830 {
36831 tree *c;
36832 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
36833 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
36834 && OMP_CLAUSE_DECL (*c) == real_decl)
36835 {
36836 error_at (loc, "iteration variable %qD"
36837 " should not be firstprivate", real_decl);
36838 *c = OMP_CLAUSE_CHAIN (*c);
36839 }
36840 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
36841 && OMP_CLAUSE_DECL (*c) == real_decl)
36842 {
36843 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36844 tree l = *c;
36845 *c = OMP_CLAUSE_CHAIN (*c);
36846 if (code == OMP_SIMD)
36847 {
36848 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36849 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
36850 }
36851 else
36852 {
36853 OMP_CLAUSE_CHAIN (l) = clauses;
36854 clauses = l;
36855 }
36856 add_private_clause = NULL_TREE;
36857 }
36858 else
36859 {
36860 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
36861 && OMP_CLAUSE_DECL (*c) == real_decl)
36862 add_private_clause = NULL_TREE;
36863 c = &OMP_CLAUSE_CHAIN (*c);
36864 }
36865 }
36866
36867 if (add_private_clause)
36868 {
36869 tree c;
36870 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
36871 {
36872 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
36873 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
36874 && OMP_CLAUSE_DECL (c) == decl)
36875 break;
36876 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
36877 && OMP_CLAUSE_DECL (c) == decl)
36878 error_at (loc, "iteration variable %qD "
36879 "should not be firstprivate",
36880 decl);
36881 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
36882 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
36883 && OMP_CLAUSE_DECL (c) == decl)
36884 error_at (loc, "iteration variable %qD should not be reduction",
36885 decl);
36886 }
36887 if (c == NULL)
36888 {
36889 if (code != OMP_SIMD)
36890 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
36891 else if (collapse == 1)
36892 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
36893 else
36894 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
36895 OMP_CLAUSE_DECL (c) = add_private_clause;
36896 c = finish_omp_clauses (c, C_ORT_OMP);
36897 if (c)
36898 {
36899 OMP_CLAUSE_CHAIN (c) = clauses;
36900 clauses = c;
36901 /* For linear, signal that we need to fill up
36902 the so far unknown linear step. */
36903 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
36904 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
36905 }
36906 }
36907 }
36908
36909 cond = NULL;
36910 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36911 cond = cp_parser_omp_for_cond (parser, decl, code);
36912 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36913
36914 incr = NULL;
36915 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
36916 {
36917 /* If decl is an iterator, preserve the operator on decl
36918 until finish_omp_for. */
36919 if (real_decl
36920 && ((processing_template_decl
36921 && (TREE_TYPE (real_decl) == NULL_TREE
36922 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
36923 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
36924 incr = cp_parser_omp_for_incr (parser, real_decl);
36925 else
36926 incr = cp_parser_expression (parser);
36927 if (!EXPR_HAS_LOCATION (incr))
36928 protected_set_expr_location (incr, input_location);
36929 }
36930
36931 parse_close_paren:
36932 if (!parens.require_close (parser))
36933 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36934 /*or_comma=*/false,
36935 /*consume_paren=*/true);
36936
36937 TREE_VEC_ELT (declv, i) = decl;
36938 TREE_VEC_ELT (initv, i) = init;
36939 TREE_VEC_ELT (condv, i) = cond;
36940 TREE_VEC_ELT (incrv, i) = incr;
36941 if (orig_init)
36942 {
36943 orig_inits.safe_grow_cleared (i + 1);
36944 orig_inits[i] = orig_init;
36945 }
36946 if (orig_decl)
36947 {
36948 if (!orig_declv)
36949 orig_declv = copy_node (declv);
36950 TREE_VEC_ELT (orig_declv, i) = orig_decl;
36951 }
36952 else if (orig_declv)
36953 TREE_VEC_ELT (orig_declv, i) = decl;
36954
36955 if (i == count - 1)
36956 break;
36957
36958 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
36959 in between the collapsed for loops to be still considered perfectly
36960 nested. Hopefully the final version clarifies this.
36961 For now handle (multiple) {'s and empty statements. */
36962 cp_parser_parse_tentatively (parser);
36963 for (;;)
36964 {
36965 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36966 break;
36967 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36968 {
36969 cp_lexer_consume_token (parser->lexer);
36970 bracecount++;
36971 }
36972 else if (bracecount
36973 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36974 cp_lexer_consume_token (parser->lexer);
36975 else
36976 {
36977 loc = cp_lexer_peek_token (parser->lexer)->location;
36978 error_at (loc, "not enough for loops to collapse");
36979 collapse_err = true;
36980 cp_parser_abort_tentative_parse (parser);
36981 declv = NULL_TREE;
36982 break;
36983 }
36984 }
36985
36986 if (declv)
36987 {
36988 cp_parser_parse_definitely (parser);
36989 nbraces += bracecount;
36990 }
36991 }
36992
36993 if (nbraces)
36994 if_p = NULL;
36995
36996 /* Note that we saved the original contents of this flag when we entered
36997 the structured block, and so we don't need to re-save it here. */
36998 parser->in_statement = IN_OMP_FOR;
36999
37000 /* Note that the grammar doesn't call for a structured block here,
37001 though the loop as a whole is a structured block. */
37002 if (orig_declv)
37003 {
37004 body = begin_omp_structured_block ();
37005 for (i = 0; i < count; i++)
37006 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
37007 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
37008 TREE_VEC_ELT (declv, i));
37009 }
37010 else
37011 body = push_stmt_list ();
37012 cp_parser_statement (parser, NULL_TREE, false, if_p);
37013 if (orig_declv)
37014 body = finish_omp_structured_block (body);
37015 else
37016 body = pop_stmt_list (body);
37017
37018 if (declv == NULL_TREE)
37019 ret = NULL_TREE;
37020 else
37021 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
37022 incrv, body, pre_body, &orig_inits, clauses);
37023
37024 while (nbraces)
37025 {
37026 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
37027 {
37028 cp_lexer_consume_token (parser->lexer);
37029 nbraces--;
37030 }
37031 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37032 cp_lexer_consume_token (parser->lexer);
37033 else
37034 {
37035 if (!collapse_err)
37036 {
37037 error_at (cp_lexer_peek_token (parser->lexer)->location,
37038 "collapsed loops not perfectly nested");
37039 }
37040 collapse_err = true;
37041 cp_parser_statement_seq_opt (parser, NULL);
37042 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
37043 break;
37044 }
37045 }
37046
37047 while (!for_block->is_empty ())
37048 {
37049 tree t = for_block->pop ();
37050 if (TREE_CODE (t) == STATEMENT_LIST)
37051 add_stmt (pop_stmt_list (t));
37052 else
37053 add_stmt (t);
37054 }
37055 release_tree_vector (for_block);
37056
37057 return ret;
37058 }
37059
37060 /* Helper function for OpenMP parsing, split clauses and call
37061 finish_omp_clauses on each of the set of clauses afterwards. */
37062
37063 static void
37064 cp_omp_split_clauses (location_t loc, enum tree_code code,
37065 omp_clause_mask mask, tree clauses, tree *cclauses)
37066 {
37067 int i;
37068 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
37069 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
37070 if (cclauses[i])
37071 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
37072 }
37073
37074 /* OpenMP 4.0:
37075 #pragma omp simd simd-clause[optseq] new-line
37076 for-loop */
37077
37078 #define OMP_SIMD_CLAUSE_MASK \
37079 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
37080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
37089
37090 static tree
37091 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
37092 char *p_name, omp_clause_mask mask, tree *cclauses,
37093 bool *if_p)
37094 {
37095 tree clauses, sb, ret;
37096 unsigned int save;
37097 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37098
37099 strcat (p_name, " simd");
37100 mask |= OMP_SIMD_CLAUSE_MASK;
37101
37102 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37103 cclauses == NULL);
37104 if (cclauses)
37105 {
37106 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
37107 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
37108 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
37109 OMP_CLAUSE_ORDERED);
37110 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
37111 {
37112 error_at (OMP_CLAUSE_LOCATION (c),
37113 "%<ordered%> clause with parameter may not be specified "
37114 "on %qs construct", p_name);
37115 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
37116 }
37117 }
37118
37119 keep_next_level (true);
37120 sb = begin_omp_structured_block ();
37121 save = cp_parser_begin_omp_structured_block (parser);
37122
37123 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
37124
37125 cp_parser_end_omp_structured_block (parser, save);
37126 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37127
37128 return ret;
37129 }
37130
37131 /* OpenMP 2.5:
37132 #pragma omp for for-clause[optseq] new-line
37133 for-loop
37134
37135 OpenMP 4.0:
37136 #pragma omp for simd for-simd-clause[optseq] new-line
37137 for-loop */
37138
37139 #define OMP_FOR_CLAUSE_MASK \
37140 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37149
37150 static tree
37151 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
37152 char *p_name, omp_clause_mask mask, tree *cclauses,
37153 bool *if_p)
37154 {
37155 tree clauses, sb, ret;
37156 unsigned int save;
37157 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37158
37159 strcat (p_name, " for");
37160 mask |= OMP_FOR_CLAUSE_MASK;
37161 /* parallel for{, simd} disallows nowait clause, but for
37162 target {teams distribute ,}parallel for{, simd} it should be accepted. */
37163 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
37164 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37165 /* Composite distribute parallel for{, simd} disallows ordered clause. */
37166 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37167 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
37168
37169 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37170 {
37171 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37172 const char *p = IDENTIFIER_POINTER (id);
37173
37174 if (strcmp (p, "simd") == 0)
37175 {
37176 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37177 if (cclauses == NULL)
37178 cclauses = cclauses_buf;
37179
37180 cp_lexer_consume_token (parser->lexer);
37181 if (!flag_openmp) /* flag_openmp_simd */
37182 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37183 cclauses, if_p);
37184 sb = begin_omp_structured_block ();
37185 save = cp_parser_begin_omp_structured_block (parser);
37186 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37187 cclauses, if_p);
37188 cp_parser_end_omp_structured_block (parser, save);
37189 tree body = finish_omp_structured_block (sb);
37190 if (ret == NULL)
37191 return ret;
37192 ret = make_node (OMP_FOR);
37193 TREE_TYPE (ret) = void_type_node;
37194 OMP_FOR_BODY (ret) = body;
37195 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37196 SET_EXPR_LOCATION (ret, loc);
37197 add_stmt (ret);
37198 return ret;
37199 }
37200 }
37201 if (!flag_openmp) /* flag_openmp_simd */
37202 {
37203 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37204 return NULL_TREE;
37205 }
37206
37207 /* Composite distribute parallel for disallows linear clause. */
37208 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37209 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
37210
37211 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37212 cclauses == NULL);
37213 if (cclauses)
37214 {
37215 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
37216 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37217 }
37218
37219 keep_next_level (true);
37220 sb = begin_omp_structured_block ();
37221 save = cp_parser_begin_omp_structured_block (parser);
37222
37223 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
37224
37225 cp_parser_end_omp_structured_block (parser, save);
37226 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37227
37228 return ret;
37229 }
37230
37231 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
37232 omp_clause_mask, tree *, bool *);
37233
37234 /* OpenMP 2.5:
37235 # pragma omp master new-line
37236 structured-block */
37237
37238 static tree
37239 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
37240 char *p_name, omp_clause_mask mask, tree *cclauses,
37241 bool *if_p)
37242 {
37243 tree clauses, sb, ret;
37244 unsigned int save;
37245 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37246
37247 strcat (p_name, " master");
37248
37249 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37250 {
37251 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37252 const char *p = IDENTIFIER_POINTER (id);
37253
37254 if (strcmp (p, "taskloop") == 0)
37255 {
37256 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37257 if (cclauses == NULL)
37258 cclauses = cclauses_buf;
37259
37260 cp_lexer_consume_token (parser->lexer);
37261 if (!flag_openmp) /* flag_openmp_simd */
37262 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37263 cclauses, if_p);
37264 sb = begin_omp_structured_block ();
37265 save = cp_parser_begin_omp_structured_block (parser);
37266 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37267 cclauses, if_p);
37268 cp_parser_end_omp_structured_block (parser, save);
37269 tree body = finish_omp_structured_block (sb);
37270 if (ret == NULL)
37271 return ret;
37272 return c_finish_omp_master (loc, body);
37273 }
37274 }
37275 if (!flag_openmp) /* flag_openmp_simd */
37276 {
37277 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37278 return NULL_TREE;
37279 }
37280
37281 if (cclauses)
37282 {
37283 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37284 false);
37285 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
37286 }
37287 else
37288 cp_parser_require_pragma_eol (parser, pragma_tok);
37289
37290 return c_finish_omp_master (loc,
37291 cp_parser_omp_structured_block (parser, if_p));
37292 }
37293
37294 /* OpenMP 2.5:
37295 # pragma omp ordered new-line
37296 structured-block
37297
37298 OpenMP 4.5:
37299 # pragma omp ordered ordered-clauses new-line
37300 structured-block */
37301
37302 #define OMP_ORDERED_CLAUSE_MASK \
37303 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
37304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37305
37306 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
37307 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37308
37309 static bool
37310 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
37311 enum pragma_context context, bool *if_p)
37312 {
37313 location_t loc = pragma_tok->location;
37314
37315 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37316 {
37317 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37318 const char *p = IDENTIFIER_POINTER (id);
37319
37320 if (strcmp (p, "depend") == 0)
37321 {
37322 if (!flag_openmp) /* flag_openmp_simd */
37323 {
37324 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37325 return false;
37326 }
37327 if (context == pragma_stmt)
37328 {
37329 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
37330 "%<depend%> clause may only be used in compound "
37331 "statements");
37332 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37333 return false;
37334 }
37335 tree clauses
37336 = cp_parser_omp_all_clauses (parser,
37337 OMP_ORDERED_DEPEND_CLAUSE_MASK,
37338 "#pragma omp ordered", pragma_tok);
37339 c_finish_omp_ordered (loc, clauses, NULL_TREE);
37340 return false;
37341 }
37342 }
37343
37344 tree clauses
37345 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
37346 "#pragma omp ordered", pragma_tok);
37347
37348 if (!flag_openmp /* flag_openmp_simd */
37349 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
37350 return false;
37351
37352 c_finish_omp_ordered (loc, clauses,
37353 cp_parser_omp_structured_block (parser, if_p));
37354 return true;
37355 }
37356
37357 /* OpenMP 2.5:
37358
37359 section-scope:
37360 { section-sequence }
37361
37362 section-sequence:
37363 section-directive[opt] structured-block
37364 section-sequence section-directive structured-block */
37365
37366 static tree
37367 cp_parser_omp_sections_scope (cp_parser *parser)
37368 {
37369 tree stmt, substmt;
37370 bool error_suppress = false;
37371 cp_token *tok;
37372
37373 matching_braces braces;
37374 if (!braces.require_open (parser))
37375 return NULL_TREE;
37376
37377 stmt = push_stmt_list ();
37378
37379 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
37380 != PRAGMA_OMP_SECTION)
37381 {
37382 substmt = cp_parser_omp_structured_block (parser, NULL);
37383 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37384 add_stmt (substmt);
37385 }
37386
37387 while (1)
37388 {
37389 tok = cp_lexer_peek_token (parser->lexer);
37390 if (tok->type == CPP_CLOSE_BRACE)
37391 break;
37392 if (tok->type == CPP_EOF)
37393 break;
37394
37395 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
37396 {
37397 cp_lexer_consume_token (parser->lexer);
37398 cp_parser_require_pragma_eol (parser, tok);
37399 error_suppress = false;
37400 }
37401 else if (!error_suppress)
37402 {
37403 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
37404 error_suppress = true;
37405 }
37406
37407 substmt = cp_parser_omp_structured_block (parser, NULL);
37408 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37409 add_stmt (substmt);
37410 }
37411 braces.require_close (parser);
37412
37413 substmt = pop_stmt_list (stmt);
37414
37415 stmt = make_node (OMP_SECTIONS);
37416 TREE_TYPE (stmt) = void_type_node;
37417 OMP_SECTIONS_BODY (stmt) = substmt;
37418
37419 add_stmt (stmt);
37420 return stmt;
37421 }
37422
37423 /* OpenMP 2.5:
37424 # pragma omp sections sections-clause[optseq] newline
37425 sections-scope */
37426
37427 #define OMP_SECTIONS_CLAUSE_MASK \
37428 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37433
37434 static tree
37435 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
37436 char *p_name, omp_clause_mask mask, tree *cclauses)
37437 {
37438 tree clauses, ret;
37439 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37440
37441 strcat (p_name, " sections");
37442 mask |= OMP_SECTIONS_CLAUSE_MASK;
37443 if (cclauses)
37444 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37445
37446 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37447 cclauses == NULL);
37448 if (cclauses)
37449 {
37450 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
37451 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
37452 }
37453
37454 ret = cp_parser_omp_sections_scope (parser);
37455 if (ret)
37456 OMP_SECTIONS_CLAUSES (ret) = clauses;
37457
37458 return ret;
37459 }
37460
37461 /* OpenMP 2.5:
37462 # pragma omp parallel parallel-clause[optseq] new-line
37463 structured-block
37464 # pragma omp parallel for parallel-for-clause[optseq] new-line
37465 structured-block
37466 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37467 structured-block
37468
37469 OpenMP 4.0:
37470 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37471 structured-block */
37472
37473 #define OMP_PARALLEL_CLAUSE_MASK \
37474 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37483
37484 static tree
37485 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
37486 char *p_name, omp_clause_mask mask, tree *cclauses,
37487 bool *if_p)
37488 {
37489 tree stmt, clauses, block;
37490 unsigned int save;
37491 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37492
37493 strcat (p_name, " parallel");
37494 mask |= OMP_PARALLEL_CLAUSE_MASK;
37495 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37496 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
37497 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
37498 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
37499
37500 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37501 {
37502 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37503 if (cclauses == NULL)
37504 cclauses = cclauses_buf;
37505
37506 cp_lexer_consume_token (parser->lexer);
37507 if (!flag_openmp) /* flag_openmp_simd */
37508 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37509 if_p);
37510 block = begin_omp_parallel ();
37511 save = cp_parser_begin_omp_structured_block (parser);
37512 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37513 if_p);
37514 cp_parser_end_omp_structured_block (parser, save);
37515 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37516 block);
37517 if (ret == NULL_TREE)
37518 return ret;
37519 OMP_PARALLEL_COMBINED (stmt) = 1;
37520 return stmt;
37521 }
37522 /* When combined with distribute, parallel has to be followed by for.
37523 #pragma omp target parallel is allowed though. */
37524 else if (cclauses
37525 && (mask & (OMP_CLAUSE_MASK_1
37526 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37527 {
37528 error_at (loc, "expected %<for%> after %qs", p_name);
37529 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37530 return NULL_TREE;
37531 }
37532 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37533 {
37534 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37535 const char *p = IDENTIFIER_POINTER (id);
37536 if (strcmp (p, "master") == 0)
37537 {
37538 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37539 cclauses = cclauses_buf;
37540
37541 cp_lexer_consume_token (parser->lexer);
37542 block = begin_omp_parallel ();
37543 save = cp_parser_begin_omp_structured_block (parser);
37544 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
37545 cclauses, if_p);
37546 cp_parser_end_omp_structured_block (parser, save);
37547 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37548 block);
37549 OMP_PARALLEL_COMBINED (stmt) = 1;
37550 if (ret == NULL_TREE)
37551 return ret;
37552 return stmt;
37553 }
37554 else if (!flag_openmp) /* flag_openmp_simd */
37555 {
37556 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37557 return NULL_TREE;
37558 }
37559 else if (strcmp (p, "sections") == 0)
37560 {
37561 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37562 cclauses = cclauses_buf;
37563
37564 cp_lexer_consume_token (parser->lexer);
37565 block = begin_omp_parallel ();
37566 save = cp_parser_begin_omp_structured_block (parser);
37567 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
37568 cp_parser_end_omp_structured_block (parser, save);
37569 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37570 block);
37571 OMP_PARALLEL_COMBINED (stmt) = 1;
37572 return stmt;
37573 }
37574 }
37575 else if (!flag_openmp) /* flag_openmp_simd */
37576 {
37577 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37578 return NULL_TREE;
37579 }
37580
37581 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37582 cclauses == NULL);
37583 if (cclauses)
37584 {
37585 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
37586 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
37587 }
37588
37589 block = begin_omp_parallel ();
37590 save = cp_parser_begin_omp_structured_block (parser);
37591 cp_parser_statement (parser, NULL_TREE, false, if_p);
37592 cp_parser_end_omp_structured_block (parser, save);
37593 stmt = finish_omp_parallel (clauses, block);
37594 return stmt;
37595 }
37596
37597 /* OpenMP 2.5:
37598 # pragma omp single single-clause[optseq] new-line
37599 structured-block */
37600
37601 #define OMP_SINGLE_CLAUSE_MASK \
37602 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37606
37607 static tree
37608 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37609 {
37610 tree stmt = make_node (OMP_SINGLE);
37611 TREE_TYPE (stmt) = void_type_node;
37612 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37613
37614 OMP_SINGLE_CLAUSES (stmt)
37615 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
37616 "#pragma omp single", pragma_tok);
37617 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37618
37619 return add_stmt (stmt);
37620 }
37621
37622 /* OpenMP 3.0:
37623 # pragma omp task task-clause[optseq] new-line
37624 structured-block */
37625
37626 #define OMP_TASK_CLAUSE_MASK \
37627 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37638
37639 static tree
37640 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37641 {
37642 tree clauses, block;
37643 unsigned int save;
37644
37645 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
37646 "#pragma omp task", pragma_tok);
37647 block = begin_omp_task ();
37648 save = cp_parser_begin_omp_structured_block (parser);
37649 cp_parser_statement (parser, NULL_TREE, false, if_p);
37650 cp_parser_end_omp_structured_block (parser, save);
37651 return finish_omp_task (clauses, block);
37652 }
37653
37654 /* OpenMP 3.0:
37655 # pragma omp taskwait new-line
37656
37657 OpenMP 5.0:
37658 # pragma omp taskwait taskwait-clause[opt] new-line */
37659
37660 #define OMP_TASKWAIT_CLAUSE_MASK \
37661 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37662
37663 static void
37664 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
37665 {
37666 tree clauses
37667 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
37668 "#pragma omp taskwait", pragma_tok);
37669
37670 if (clauses)
37671 {
37672 tree stmt = make_node (OMP_TASK);
37673 TREE_TYPE (stmt) = void_node;
37674 OMP_TASK_CLAUSES (stmt) = clauses;
37675 OMP_TASK_BODY (stmt) = NULL_TREE;
37676 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37677 add_stmt (stmt);
37678 }
37679 else
37680 finish_omp_taskwait ();
37681 }
37682
37683 /* OpenMP 3.1:
37684 # pragma omp taskyield new-line */
37685
37686 static void
37687 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
37688 {
37689 cp_parser_require_pragma_eol (parser, pragma_tok);
37690 finish_omp_taskyield ();
37691 }
37692
37693 /* OpenMP 4.0:
37694 # pragma omp taskgroup new-line
37695 structured-block
37696
37697 OpenMP 5.0:
37698 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37699
37700 #define OMP_TASKGROUP_CLAUSE_MASK \
37701 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37702
37703 static tree
37704 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37705 {
37706 tree clauses
37707 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
37708 "#pragma omp taskgroup", pragma_tok);
37709 return c_finish_omp_taskgroup (input_location,
37710 cp_parser_omp_structured_block (parser,
37711 if_p),
37712 clauses);
37713 }
37714
37715
37716 /* OpenMP 2.5:
37717 # pragma omp threadprivate (variable-list) */
37718
37719 static void
37720 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
37721 {
37722 tree vars;
37723
37724 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37725 cp_parser_require_pragma_eol (parser, pragma_tok);
37726
37727 finish_omp_threadprivate (vars);
37728 }
37729
37730 /* OpenMP 4.0:
37731 # pragma omp cancel cancel-clause[optseq] new-line */
37732
37733 #define OMP_CANCEL_CLAUSE_MASK \
37734 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37739
37740 static void
37741 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
37742 {
37743 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
37744 "#pragma omp cancel", pragma_tok);
37745 finish_omp_cancel (clauses);
37746 }
37747
37748 /* OpenMP 4.0:
37749 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37750
37751 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37752 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37756
37757 static void
37758 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
37759 enum pragma_context context)
37760 {
37761 tree clauses;
37762 bool point_seen = false;
37763
37764 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37765 {
37766 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37767 const char *p = IDENTIFIER_POINTER (id);
37768
37769 if (strcmp (p, "point") == 0)
37770 {
37771 cp_lexer_consume_token (parser->lexer);
37772 point_seen = true;
37773 }
37774 }
37775 if (!point_seen)
37776 {
37777 cp_parser_error (parser, "expected %<point%>");
37778 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37779 return;
37780 }
37781
37782 if (context != pragma_compound)
37783 {
37784 if (context == pragma_stmt)
37785 error_at (pragma_tok->location,
37786 "%<#pragma %s%> may only be used in compound statements",
37787 "omp cancellation point");
37788 else
37789 cp_parser_error (parser, "expected declaration specifiers");
37790 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37791 return;
37792 }
37793
37794 clauses = cp_parser_omp_all_clauses (parser,
37795 OMP_CANCELLATION_POINT_CLAUSE_MASK,
37796 "#pragma omp cancellation point",
37797 pragma_tok);
37798 finish_omp_cancellation_point (clauses);
37799 }
37800
37801 /* OpenMP 4.0:
37802 #pragma omp distribute distribute-clause[optseq] new-line
37803 for-loop */
37804
37805 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37806 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37811
37812 static tree
37813 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
37814 char *p_name, omp_clause_mask mask, tree *cclauses,
37815 bool *if_p)
37816 {
37817 tree clauses, sb, ret;
37818 unsigned int save;
37819 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37820
37821 strcat (p_name, " distribute");
37822 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
37823
37824 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37825 {
37826 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37827 const char *p = IDENTIFIER_POINTER (id);
37828 bool simd = false;
37829 bool parallel = false;
37830
37831 if (strcmp (p, "simd") == 0)
37832 simd = true;
37833 else
37834 parallel = strcmp (p, "parallel") == 0;
37835 if (parallel || simd)
37836 {
37837 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37838 if (cclauses == NULL)
37839 cclauses = cclauses_buf;
37840 cp_lexer_consume_token (parser->lexer);
37841 if (!flag_openmp) /* flag_openmp_simd */
37842 {
37843 if (simd)
37844 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37845 cclauses, if_p);
37846 else
37847 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37848 cclauses, if_p);
37849 }
37850 sb = begin_omp_structured_block ();
37851 save = cp_parser_begin_omp_structured_block (parser);
37852 if (simd)
37853 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37854 cclauses, if_p);
37855 else
37856 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37857 cclauses, if_p);
37858 cp_parser_end_omp_structured_block (parser, save);
37859 tree body = finish_omp_structured_block (sb);
37860 if (ret == NULL)
37861 return ret;
37862 ret = make_node (OMP_DISTRIBUTE);
37863 TREE_TYPE (ret) = void_type_node;
37864 OMP_FOR_BODY (ret) = body;
37865 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37866 SET_EXPR_LOCATION (ret, loc);
37867 add_stmt (ret);
37868 return ret;
37869 }
37870 }
37871 if (!flag_openmp) /* flag_openmp_simd */
37872 {
37873 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37874 return NULL_TREE;
37875 }
37876
37877 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37878 cclauses == NULL);
37879 if (cclauses)
37880 {
37881 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
37882 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37883 }
37884
37885 keep_next_level (true);
37886 sb = begin_omp_structured_block ();
37887 save = cp_parser_begin_omp_structured_block (parser);
37888
37889 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
37890
37891 cp_parser_end_omp_structured_block (parser, save);
37892 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37893
37894 return ret;
37895 }
37896
37897 /* OpenMP 4.0:
37898 # pragma omp teams teams-clause[optseq] new-line
37899 structured-block */
37900
37901 #define OMP_TEAMS_CLAUSE_MASK \
37902 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37909
37910 static tree
37911 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
37912 char *p_name, omp_clause_mask mask, tree *cclauses,
37913 bool *if_p)
37914 {
37915 tree clauses, sb, ret;
37916 unsigned int save;
37917 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37918
37919 strcat (p_name, " teams");
37920 mask |= OMP_TEAMS_CLAUSE_MASK;
37921
37922 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37923 {
37924 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37925 const char *p = IDENTIFIER_POINTER (id);
37926 if (strcmp (p, "distribute") == 0)
37927 {
37928 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37929 if (cclauses == NULL)
37930 cclauses = cclauses_buf;
37931
37932 cp_lexer_consume_token (parser->lexer);
37933 if (!flag_openmp) /* flag_openmp_simd */
37934 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37935 cclauses, if_p);
37936 keep_next_level (true);
37937 sb = begin_omp_structured_block ();
37938 save = cp_parser_begin_omp_structured_block (parser);
37939 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37940 cclauses, if_p);
37941 cp_parser_end_omp_structured_block (parser, save);
37942 tree body = finish_omp_structured_block (sb);
37943 if (ret == NULL)
37944 return ret;
37945 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37946 ret = make_node (OMP_TEAMS);
37947 TREE_TYPE (ret) = void_type_node;
37948 OMP_TEAMS_CLAUSES (ret) = clauses;
37949 OMP_TEAMS_BODY (ret) = body;
37950 OMP_TEAMS_COMBINED (ret) = 1;
37951 SET_EXPR_LOCATION (ret, loc);
37952 return add_stmt (ret);
37953 }
37954 }
37955 if (!flag_openmp) /* flag_openmp_simd */
37956 {
37957 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37958 return NULL_TREE;
37959 }
37960
37961 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37962 cclauses == NULL);
37963 if (cclauses)
37964 {
37965 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
37966 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37967 }
37968
37969 tree stmt = make_node (OMP_TEAMS);
37970 TREE_TYPE (stmt) = void_type_node;
37971 OMP_TEAMS_CLAUSES (stmt) = clauses;
37972 keep_next_level (true);
37973 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37974 SET_EXPR_LOCATION (stmt, loc);
37975
37976 return add_stmt (stmt);
37977 }
37978
37979 /* OpenMP 4.0:
37980 # pragma omp target data target-data-clause[optseq] new-line
37981 structured-block */
37982
37983 #define OMP_TARGET_DATA_CLAUSE_MASK \
37984 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
37988
37989 static tree
37990 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37991 {
37992 tree clauses
37993 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
37994 "#pragma omp target data", pragma_tok);
37995 int map_seen = 0;
37996 for (tree *pc = &clauses; *pc;)
37997 {
37998 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37999 switch (OMP_CLAUSE_MAP_KIND (*pc))
38000 {
38001 case GOMP_MAP_TO:
38002 case GOMP_MAP_ALWAYS_TO:
38003 case GOMP_MAP_FROM:
38004 case GOMP_MAP_ALWAYS_FROM:
38005 case GOMP_MAP_TOFROM:
38006 case GOMP_MAP_ALWAYS_TOFROM:
38007 case GOMP_MAP_ALLOC:
38008 map_seen = 3;
38009 break;
38010 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38011 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38012 case GOMP_MAP_ALWAYS_POINTER:
38013 break;
38014 default:
38015 map_seen |= 1;
38016 error_at (OMP_CLAUSE_LOCATION (*pc),
38017 "%<#pragma omp target data%> with map-type other "
38018 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38019 "on %<map%> clause");
38020 *pc = OMP_CLAUSE_CHAIN (*pc);
38021 continue;
38022 }
38023 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
38024 map_seen = 3;
38025 pc = &OMP_CLAUSE_CHAIN (*pc);
38026 }
38027
38028 if (map_seen != 3)
38029 {
38030 if (map_seen == 0)
38031 error_at (pragma_tok->location,
38032 "%<#pragma omp target data%> must contain at least "
38033 "one %<map%> or %<use_device_ptr%> clause");
38034 return NULL_TREE;
38035 }
38036
38037 tree stmt = make_node (OMP_TARGET_DATA);
38038 TREE_TYPE (stmt) = void_type_node;
38039 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
38040
38041 keep_next_level (true);
38042 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38043
38044 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38045 return add_stmt (stmt);
38046 }
38047
38048 /* OpenMP 4.5:
38049 # pragma omp target enter data target-enter-data-clause[optseq] new-line
38050 structured-block */
38051
38052 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
38053 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38058
38059 static tree
38060 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
38061 enum pragma_context context)
38062 {
38063 bool data_seen = false;
38064 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38065 {
38066 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38067 const char *p = IDENTIFIER_POINTER (id);
38068
38069 if (strcmp (p, "data") == 0)
38070 {
38071 cp_lexer_consume_token (parser->lexer);
38072 data_seen = true;
38073 }
38074 }
38075 if (!data_seen)
38076 {
38077 cp_parser_error (parser, "expected %<data%>");
38078 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38079 return NULL_TREE;
38080 }
38081
38082 if (context == pragma_stmt)
38083 {
38084 error_at (pragma_tok->location,
38085 "%<#pragma %s%> may only be used in compound statements",
38086 "omp target enter data");
38087 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38088 return NULL_TREE;
38089 }
38090
38091 tree clauses
38092 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
38093 "#pragma omp target enter data", pragma_tok);
38094 int map_seen = 0;
38095 for (tree *pc = &clauses; *pc;)
38096 {
38097 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38098 switch (OMP_CLAUSE_MAP_KIND (*pc))
38099 {
38100 case GOMP_MAP_TO:
38101 case GOMP_MAP_ALWAYS_TO:
38102 case GOMP_MAP_ALLOC:
38103 map_seen = 3;
38104 break;
38105 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38106 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38107 case GOMP_MAP_ALWAYS_POINTER:
38108 break;
38109 default:
38110 map_seen |= 1;
38111 error_at (OMP_CLAUSE_LOCATION (*pc),
38112 "%<#pragma omp target enter data%> with map-type other "
38113 "than %<to%> or %<alloc%> on %<map%> clause");
38114 *pc = OMP_CLAUSE_CHAIN (*pc);
38115 continue;
38116 }
38117 pc = &OMP_CLAUSE_CHAIN (*pc);
38118 }
38119
38120 if (map_seen != 3)
38121 {
38122 if (map_seen == 0)
38123 error_at (pragma_tok->location,
38124 "%<#pragma omp target enter data%> must contain at least "
38125 "one %<map%> clause");
38126 return NULL_TREE;
38127 }
38128
38129 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
38130 TREE_TYPE (stmt) = void_type_node;
38131 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
38132 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38133 return add_stmt (stmt);
38134 }
38135
38136 /* OpenMP 4.5:
38137 # pragma omp target exit data target-enter-data-clause[optseq] new-line
38138 structured-block */
38139
38140 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
38141 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38146
38147 static tree
38148 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
38149 enum pragma_context context)
38150 {
38151 bool data_seen = false;
38152 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38153 {
38154 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38155 const char *p = IDENTIFIER_POINTER (id);
38156
38157 if (strcmp (p, "data") == 0)
38158 {
38159 cp_lexer_consume_token (parser->lexer);
38160 data_seen = true;
38161 }
38162 }
38163 if (!data_seen)
38164 {
38165 cp_parser_error (parser, "expected %<data%>");
38166 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38167 return NULL_TREE;
38168 }
38169
38170 if (context == pragma_stmt)
38171 {
38172 error_at (pragma_tok->location,
38173 "%<#pragma %s%> may only be used in compound statements",
38174 "omp target exit data");
38175 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38176 return NULL_TREE;
38177 }
38178
38179 tree clauses
38180 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
38181 "#pragma omp target exit data", pragma_tok);
38182 int map_seen = 0;
38183 for (tree *pc = &clauses; *pc;)
38184 {
38185 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38186 switch (OMP_CLAUSE_MAP_KIND (*pc))
38187 {
38188 case GOMP_MAP_FROM:
38189 case GOMP_MAP_ALWAYS_FROM:
38190 case GOMP_MAP_RELEASE:
38191 case GOMP_MAP_DELETE:
38192 map_seen = 3;
38193 break;
38194 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38195 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38196 case GOMP_MAP_ALWAYS_POINTER:
38197 break;
38198 default:
38199 map_seen |= 1;
38200 error_at (OMP_CLAUSE_LOCATION (*pc),
38201 "%<#pragma omp target exit data%> with map-type other "
38202 "than %<from%>, %<release%> or %<delete%> on %<map%>"
38203 " clause");
38204 *pc = OMP_CLAUSE_CHAIN (*pc);
38205 continue;
38206 }
38207 pc = &OMP_CLAUSE_CHAIN (*pc);
38208 }
38209
38210 if (map_seen != 3)
38211 {
38212 if (map_seen == 0)
38213 error_at (pragma_tok->location,
38214 "%<#pragma omp target exit data%> must contain at least "
38215 "one %<map%> clause");
38216 return NULL_TREE;
38217 }
38218
38219 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
38220 TREE_TYPE (stmt) = void_type_node;
38221 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
38222 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38223 return add_stmt (stmt);
38224 }
38225
38226 /* OpenMP 4.0:
38227 # pragma omp target update target-update-clause[optseq] new-line */
38228
38229 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
38230 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
38231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38236
38237 static bool
38238 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
38239 enum pragma_context context)
38240 {
38241 if (context == pragma_stmt)
38242 {
38243 error_at (pragma_tok->location,
38244 "%<#pragma %s%> may only be used in compound statements",
38245 "omp target update");
38246 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38247 return false;
38248 }
38249
38250 tree clauses
38251 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
38252 "#pragma omp target update", pragma_tok);
38253 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
38254 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
38255 {
38256 error_at (pragma_tok->location,
38257 "%<#pragma omp target update%> must contain at least one "
38258 "%<from%> or %<to%> clauses");
38259 return false;
38260 }
38261
38262 tree stmt = make_node (OMP_TARGET_UPDATE);
38263 TREE_TYPE (stmt) = void_type_node;
38264 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
38265 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38266 add_stmt (stmt);
38267 return false;
38268 }
38269
38270 /* OpenMP 4.0:
38271 # pragma omp target target-clause[optseq] new-line
38272 structured-block */
38273
38274 #define OMP_TARGET_CLAUSE_MASK \
38275 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
38283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
38284
38285 static bool
38286 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
38287 enum pragma_context context, bool *if_p)
38288 {
38289 tree *pc = NULL, stmt;
38290
38291 if (flag_openmp)
38292 omp_requires_mask
38293 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
38294
38295 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38296 {
38297 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38298 const char *p = IDENTIFIER_POINTER (id);
38299 enum tree_code ccode = ERROR_MARK;
38300
38301 if (strcmp (p, "teams") == 0)
38302 ccode = OMP_TEAMS;
38303 else if (strcmp (p, "parallel") == 0)
38304 ccode = OMP_PARALLEL;
38305 else if (strcmp (p, "simd") == 0)
38306 ccode = OMP_SIMD;
38307 if (ccode != ERROR_MARK)
38308 {
38309 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
38310 char p_name[sizeof ("#pragma omp target teams distribute "
38311 "parallel for simd")];
38312
38313 cp_lexer_consume_token (parser->lexer);
38314 strcpy (p_name, "#pragma omp target");
38315 if (!flag_openmp) /* flag_openmp_simd */
38316 {
38317 tree stmt;
38318 switch (ccode)
38319 {
38320 case OMP_TEAMS:
38321 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
38322 OMP_TARGET_CLAUSE_MASK,
38323 cclauses, if_p);
38324 break;
38325 case OMP_PARALLEL:
38326 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38327 OMP_TARGET_CLAUSE_MASK,
38328 cclauses, if_p);
38329 break;
38330 case OMP_SIMD:
38331 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
38332 OMP_TARGET_CLAUSE_MASK,
38333 cclauses, if_p);
38334 break;
38335 default:
38336 gcc_unreachable ();
38337 }
38338 return stmt != NULL_TREE;
38339 }
38340 keep_next_level (true);
38341 tree sb = begin_omp_structured_block (), ret;
38342 unsigned save = cp_parser_begin_omp_structured_block (parser);
38343 switch (ccode)
38344 {
38345 case OMP_TEAMS:
38346 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
38347 OMP_TARGET_CLAUSE_MASK, cclauses,
38348 if_p);
38349 break;
38350 case OMP_PARALLEL:
38351 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38352 OMP_TARGET_CLAUSE_MASK, cclauses,
38353 if_p);
38354 break;
38355 case OMP_SIMD:
38356 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
38357 OMP_TARGET_CLAUSE_MASK, cclauses,
38358 if_p);
38359 break;
38360 default:
38361 gcc_unreachable ();
38362 }
38363 cp_parser_end_omp_structured_block (parser, save);
38364 tree body = finish_omp_structured_block (sb);
38365 if (ret == NULL_TREE)
38366 return false;
38367 if (ccode == OMP_TEAMS && !processing_template_decl)
38368 {
38369 /* For combined target teams, ensure the num_teams and
38370 thread_limit clause expressions are evaluated on the host,
38371 before entering the target construct. */
38372 tree c;
38373 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38374 c; c = OMP_CLAUSE_CHAIN (c))
38375 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
38376 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
38377 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
38378 {
38379 tree expr = OMP_CLAUSE_OPERAND (c, 0);
38380 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
38381 if (expr == error_mark_node)
38382 continue;
38383 tree tmp = TARGET_EXPR_SLOT (expr);
38384 add_stmt (expr);
38385 OMP_CLAUSE_OPERAND (c, 0) = expr;
38386 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
38387 OMP_CLAUSE_FIRSTPRIVATE);
38388 OMP_CLAUSE_DECL (tc) = tmp;
38389 OMP_CLAUSE_CHAIN (tc)
38390 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38391 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
38392 }
38393 }
38394 tree stmt = make_node (OMP_TARGET);
38395 TREE_TYPE (stmt) = void_type_node;
38396 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38397 OMP_TARGET_BODY (stmt) = body;
38398 OMP_TARGET_COMBINED (stmt) = 1;
38399 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38400 add_stmt (stmt);
38401 pc = &OMP_TARGET_CLAUSES (stmt);
38402 goto check_clauses;
38403 }
38404 else if (!flag_openmp) /* flag_openmp_simd */
38405 {
38406 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38407 return false;
38408 }
38409 else if (strcmp (p, "data") == 0)
38410 {
38411 cp_lexer_consume_token (parser->lexer);
38412 cp_parser_omp_target_data (parser, pragma_tok, if_p);
38413 return true;
38414 }
38415 else if (strcmp (p, "enter") == 0)
38416 {
38417 cp_lexer_consume_token (parser->lexer);
38418 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
38419 return false;
38420 }
38421 else if (strcmp (p, "exit") == 0)
38422 {
38423 cp_lexer_consume_token (parser->lexer);
38424 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
38425 return false;
38426 }
38427 else if (strcmp (p, "update") == 0)
38428 {
38429 cp_lexer_consume_token (parser->lexer);
38430 return cp_parser_omp_target_update (parser, pragma_tok, context);
38431 }
38432 }
38433 if (!flag_openmp) /* flag_openmp_simd */
38434 {
38435 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38436 return false;
38437 }
38438
38439 stmt = make_node (OMP_TARGET);
38440 TREE_TYPE (stmt) = void_type_node;
38441
38442 OMP_TARGET_CLAUSES (stmt)
38443 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
38444 "#pragma omp target", pragma_tok);
38445 pc = &OMP_TARGET_CLAUSES (stmt);
38446 keep_next_level (true);
38447 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38448
38449 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38450 add_stmt (stmt);
38451
38452 check_clauses:
38453 while (*pc)
38454 {
38455 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38456 switch (OMP_CLAUSE_MAP_KIND (*pc))
38457 {
38458 case GOMP_MAP_TO:
38459 case GOMP_MAP_ALWAYS_TO:
38460 case GOMP_MAP_FROM:
38461 case GOMP_MAP_ALWAYS_FROM:
38462 case GOMP_MAP_TOFROM:
38463 case GOMP_MAP_ALWAYS_TOFROM:
38464 case GOMP_MAP_ALLOC:
38465 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38466 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38467 case GOMP_MAP_ALWAYS_POINTER:
38468 break;
38469 default:
38470 error_at (OMP_CLAUSE_LOCATION (*pc),
38471 "%<#pragma omp target%> with map-type other "
38472 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38473 "on %<map%> clause");
38474 *pc = OMP_CLAUSE_CHAIN (*pc);
38475 continue;
38476 }
38477 pc = &OMP_CLAUSE_CHAIN (*pc);
38478 }
38479 return true;
38480 }
38481
38482 /* OpenACC 2.0:
38483 # pragma acc cache (variable-list) new-line
38484 */
38485
38486 static tree
38487 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
38488 {
38489 tree stmt, clauses;
38490
38491 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
38492 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38493
38494 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
38495
38496 stmt = make_node (OACC_CACHE);
38497 TREE_TYPE (stmt) = void_type_node;
38498 OACC_CACHE_CLAUSES (stmt) = clauses;
38499 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38500 add_stmt (stmt);
38501
38502 return stmt;
38503 }
38504
38505 /* OpenACC 2.0:
38506 # pragma acc data oacc-data-clause[optseq] new-line
38507 structured-block */
38508
38509 #define OACC_DATA_CLAUSE_MASK \
38510 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38517
38518 static tree
38519 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38520 {
38521 tree stmt, clauses, block;
38522 unsigned int save;
38523
38524 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
38525 "#pragma acc data", pragma_tok);
38526
38527 block = begin_omp_parallel ();
38528 save = cp_parser_begin_omp_structured_block (parser);
38529 cp_parser_statement (parser, NULL_TREE, false, if_p);
38530 cp_parser_end_omp_structured_block (parser, save);
38531 stmt = finish_oacc_data (clauses, block);
38532 return stmt;
38533 }
38534
38535 /* OpenACC 2.0:
38536 # pragma acc host_data <clauses> new-line
38537 structured-block */
38538
38539 #define OACC_HOST_DATA_CLAUSE_MASK \
38540 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38541
38542 static tree
38543 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38544 {
38545 tree stmt, clauses, block;
38546 unsigned int save;
38547
38548 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
38549 "#pragma acc host_data", pragma_tok);
38550
38551 block = begin_omp_parallel ();
38552 save = cp_parser_begin_omp_structured_block (parser);
38553 cp_parser_statement (parser, NULL_TREE, false, if_p);
38554 cp_parser_end_omp_structured_block (parser, save);
38555 stmt = finish_oacc_host_data (clauses, block);
38556 return stmt;
38557 }
38558
38559 /* OpenACC 2.0:
38560 # pragma acc declare oacc-data-clause[optseq] new-line
38561 */
38562
38563 #define OACC_DECLARE_CLAUSE_MASK \
38564 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38572
38573 static tree
38574 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
38575 {
38576 tree clauses, stmt;
38577 bool error = false;
38578
38579 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
38580 "#pragma acc declare", pragma_tok, true);
38581
38582
38583 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38584 {
38585 error_at (pragma_tok->location,
38586 "no valid clauses specified in %<#pragma acc declare%>");
38587 return NULL_TREE;
38588 }
38589
38590 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
38591 {
38592 location_t loc = OMP_CLAUSE_LOCATION (t);
38593 tree decl = OMP_CLAUSE_DECL (t);
38594 if (!DECL_P (decl))
38595 {
38596 error_at (loc, "array section in %<#pragma acc declare%>");
38597 error = true;
38598 continue;
38599 }
38600 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
38601 switch (OMP_CLAUSE_MAP_KIND (t))
38602 {
38603 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38604 case GOMP_MAP_ALLOC:
38605 case GOMP_MAP_TO:
38606 case GOMP_MAP_FORCE_DEVICEPTR:
38607 case GOMP_MAP_DEVICE_RESIDENT:
38608 break;
38609
38610 case GOMP_MAP_LINK:
38611 if (!global_bindings_p ()
38612 && (TREE_STATIC (decl)
38613 || !DECL_EXTERNAL (decl)))
38614 {
38615 error_at (loc,
38616 "%qD must be a global variable in "
38617 "%<#pragma acc declare link%>",
38618 decl);
38619 error = true;
38620 continue;
38621 }
38622 break;
38623
38624 default:
38625 if (global_bindings_p ())
38626 {
38627 error_at (loc, "invalid OpenACC clause at file scope");
38628 error = true;
38629 continue;
38630 }
38631 if (DECL_EXTERNAL (decl))
38632 {
38633 error_at (loc,
38634 "invalid use of %<extern%> variable %qD "
38635 "in %<#pragma acc declare%>", decl);
38636 error = true;
38637 continue;
38638 }
38639 else if (TREE_PUBLIC (decl))
38640 {
38641 error_at (loc,
38642 "invalid use of %<global%> variable %qD "
38643 "in %<#pragma acc declare%>", decl);
38644 error = true;
38645 continue;
38646 }
38647 break;
38648 }
38649
38650 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
38651 || lookup_attribute ("omp declare target link",
38652 DECL_ATTRIBUTES (decl)))
38653 {
38654 error_at (loc, "variable %qD used more than once with "
38655 "%<#pragma acc declare%>", decl);
38656 error = true;
38657 continue;
38658 }
38659
38660 if (!error)
38661 {
38662 tree id;
38663
38664 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
38665 id = get_identifier ("omp declare target link");
38666 else
38667 id = get_identifier ("omp declare target");
38668
38669 DECL_ATTRIBUTES (decl)
38670 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
38671 if (global_bindings_p ())
38672 {
38673 symtab_node *node = symtab_node::get (decl);
38674 if (node != NULL)
38675 {
38676 node->offloadable = 1;
38677 if (ENABLE_OFFLOADING)
38678 {
38679 g->have_offload = true;
38680 if (is_a <varpool_node *> (node))
38681 vec_safe_push (offload_vars, decl);
38682 }
38683 }
38684 }
38685 }
38686 }
38687
38688 if (error || global_bindings_p ())
38689 return NULL_TREE;
38690
38691 stmt = make_node (OACC_DECLARE);
38692 TREE_TYPE (stmt) = void_type_node;
38693 OACC_DECLARE_CLAUSES (stmt) = clauses;
38694 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38695
38696 add_stmt (stmt);
38697
38698 return NULL_TREE;
38699 }
38700
38701 /* OpenACC 2.0:
38702 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38703
38704 or
38705
38706 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38707
38708 LOC is the location of the #pragma token.
38709 */
38710
38711 #define OACC_ENTER_DATA_CLAUSE_MASK \
38712 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38717
38718 #define OACC_EXIT_DATA_CLAUSE_MASK \
38719 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38725
38726 static tree
38727 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
38728 bool enter)
38729 {
38730 location_t loc = pragma_tok->location;
38731 tree stmt, clauses;
38732 const char *p = "";
38733
38734 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38735 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38736
38737 if (strcmp (p, "data") != 0)
38738 {
38739 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
38740 enter ? "enter" : "exit");
38741 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38742 return NULL_TREE;
38743 }
38744
38745 cp_lexer_consume_token (parser->lexer);
38746
38747 if (enter)
38748 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
38749 "#pragma acc enter data", pragma_tok);
38750 else
38751 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
38752 "#pragma acc exit data", pragma_tok);
38753
38754 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38755 {
38756 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
38757 enter ? "enter" : "exit");
38758 return NULL_TREE;
38759 }
38760
38761 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
38762 TREE_TYPE (stmt) = void_type_node;
38763 OMP_STANDALONE_CLAUSES (stmt) = clauses;
38764 SET_EXPR_LOCATION (stmt, loc);
38765 add_stmt (stmt);
38766 return stmt;
38767 }
38768
38769 /* OpenACC 2.0:
38770 # pragma acc loop oacc-loop-clause[optseq] new-line
38771 structured-block */
38772
38773 #define OACC_LOOP_CLAUSE_MASK \
38774 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38784
38785 static tree
38786 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
38787 omp_clause_mask mask, tree *cclauses, bool *if_p)
38788 {
38789 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
38790
38791 strcat (p_name, " loop");
38792 mask |= OACC_LOOP_CLAUSE_MASK;
38793
38794 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
38795 cclauses == NULL);
38796 if (cclauses)
38797 {
38798 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
38799 if (*cclauses)
38800 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
38801 if (clauses)
38802 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38803 }
38804
38805 tree block = begin_omp_structured_block ();
38806 int save = cp_parser_begin_omp_structured_block (parser);
38807 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
38808 cp_parser_end_omp_structured_block (parser, save);
38809 add_stmt (finish_omp_structured_block (block));
38810
38811 return stmt;
38812 }
38813
38814 /* OpenACC 2.0:
38815 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38816 structured-block
38817
38818 or
38819
38820 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38821 structured-block
38822 */
38823
38824 #define OACC_KERNELS_CLAUSE_MASK \
38825 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38838
38839 #define OACC_PARALLEL_CLAUSE_MASK \
38840 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38856
38857 static tree
38858 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
38859 char *p_name, bool *if_p)
38860 {
38861 omp_clause_mask mask;
38862 enum tree_code code;
38863 switch (cp_parser_pragma_kind (pragma_tok))
38864 {
38865 case PRAGMA_OACC_KERNELS:
38866 strcat (p_name, " kernels");
38867 mask = OACC_KERNELS_CLAUSE_MASK;
38868 code = OACC_KERNELS;
38869 break;
38870 case PRAGMA_OACC_PARALLEL:
38871 strcat (p_name, " parallel");
38872 mask = OACC_PARALLEL_CLAUSE_MASK;
38873 code = OACC_PARALLEL;
38874 break;
38875 default:
38876 gcc_unreachable ();
38877 }
38878
38879 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38880 {
38881 const char *p
38882 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38883 if (strcmp (p, "loop") == 0)
38884 {
38885 cp_lexer_consume_token (parser->lexer);
38886 tree block = begin_omp_parallel ();
38887 tree clauses;
38888 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
38889 &clauses, if_p);
38890 protected_set_expr_location (stmt, pragma_tok->location);
38891 return finish_omp_construct (code, block, clauses);
38892 }
38893 }
38894
38895 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
38896
38897 tree block = begin_omp_parallel ();
38898 unsigned int save = cp_parser_begin_omp_structured_block (parser);
38899 cp_parser_statement (parser, NULL_TREE, false, if_p);
38900 cp_parser_end_omp_structured_block (parser, save);
38901 return finish_omp_construct (code, block, clauses);
38902 }
38903
38904 /* OpenACC 2.0:
38905 # pragma acc update oacc-update-clause[optseq] new-line
38906 */
38907
38908 #define OACC_UPDATE_CLAUSE_MASK \
38909 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38915
38916 static tree
38917 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
38918 {
38919 tree stmt, clauses;
38920
38921 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
38922 "#pragma acc update", pragma_tok);
38923
38924 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38925 {
38926 error_at (pragma_tok->location,
38927 "%<#pragma acc update%> must contain at least one "
38928 "%<device%> or %<host%> or %<self%> clause");
38929 return NULL_TREE;
38930 }
38931
38932 stmt = make_node (OACC_UPDATE);
38933 TREE_TYPE (stmt) = void_type_node;
38934 OACC_UPDATE_CLAUSES (stmt) = clauses;
38935 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38936 add_stmt (stmt);
38937 return stmt;
38938 }
38939
38940 /* OpenACC 2.0:
38941 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
38942
38943 LOC is the location of the #pragma token.
38944 */
38945
38946 #define OACC_WAIT_CLAUSE_MASK \
38947 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
38948
38949 static tree
38950 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
38951 {
38952 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
38953 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38954
38955 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38956 list = cp_parser_oacc_wait_list (parser, loc, list);
38957
38958 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
38959 "#pragma acc wait", pragma_tok);
38960
38961 stmt = c_finish_oacc_wait (loc, list, clauses);
38962 stmt = finish_expr_stmt (stmt);
38963
38964 return stmt;
38965 }
38966
38967 /* OpenMP 4.0:
38968 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
38969
38970 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
38971 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
38972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
38974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
38975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
38976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
38977
38978 static void
38979 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
38980 enum pragma_context context)
38981 {
38982 bool first_p = parser->omp_declare_simd == NULL;
38983 cp_omp_declare_simd_data data;
38984 if (first_p)
38985 {
38986 data.error_seen = false;
38987 data.fndecl_seen = false;
38988 data.tokens = vNULL;
38989 data.clauses = NULL_TREE;
38990 /* It is safe to take the address of a local variable; it will only be
38991 used while this scope is live. */
38992 parser->omp_declare_simd = &data;
38993 }
38994
38995 /* Store away all pragma tokens. */
38996 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38997 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38998 cp_lexer_consume_token (parser->lexer);
38999 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39000 parser->omp_declare_simd->error_seen = true;
39001 cp_parser_require_pragma_eol (parser, pragma_tok);
39002 struct cp_token_cache *cp
39003 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39004 parser->omp_declare_simd->tokens.safe_push (cp);
39005
39006 if (first_p)
39007 {
39008 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39009 cp_parser_pragma (parser, context, NULL);
39010 switch (context)
39011 {
39012 case pragma_external:
39013 cp_parser_declaration (parser);
39014 break;
39015 case pragma_member:
39016 cp_parser_member_declaration (parser);
39017 break;
39018 case pragma_objc_icode:
39019 cp_parser_block_declaration (parser, /*statement_p=*/false);
39020 break;
39021 default:
39022 cp_parser_declaration_statement (parser);
39023 break;
39024 }
39025 if (parser->omp_declare_simd
39026 && !parser->omp_declare_simd->error_seen
39027 && !parser->omp_declare_simd->fndecl_seen)
39028 error_at (pragma_tok->location,
39029 "%<#pragma omp declare simd%> not immediately followed by "
39030 "function declaration or definition");
39031 data.tokens.release ();
39032 parser->omp_declare_simd = NULL;
39033 }
39034 }
39035
39036 /* Finalize #pragma omp declare simd clauses after direct declarator has
39037 been parsed, and put that into "omp declare simd" attribute. */
39038
39039 static tree
39040 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
39041 {
39042 struct cp_token_cache *ce;
39043 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
39044 int i;
39045
39046 if (!data->error_seen && data->fndecl_seen)
39047 {
39048 error ("%<#pragma omp declare simd%> not immediately followed by "
39049 "a single function declaration or definition");
39050 data->error_seen = true;
39051 }
39052 if (data->error_seen)
39053 return attrs;
39054
39055 FOR_EACH_VEC_ELT (data->tokens, i, ce)
39056 {
39057 tree c, cl;
39058
39059 cp_parser_push_lexer_for_tokens (parser, ce);
39060 parser->lexer->in_pragma = true;
39061 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39062 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39063 cp_lexer_consume_token (parser->lexer);
39064 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
39065 "#pragma omp declare simd", pragma_tok);
39066 cp_parser_pop_lexer (parser);
39067 if (cl)
39068 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
39069 c = build_tree_list (get_identifier ("omp declare simd"), cl);
39070 TREE_CHAIN (c) = attrs;
39071 if (processing_template_decl)
39072 ATTR_IS_DEPENDENT (c) = 1;
39073 attrs = c;
39074 }
39075
39076 data->fndecl_seen = true;
39077 return attrs;
39078 }
39079
39080
39081 /* OpenMP 4.0:
39082 # pragma omp declare target new-line
39083 declarations and definitions
39084 # pragma omp end declare target new-line
39085
39086 OpenMP 4.5:
39087 # pragma omp declare target ( extended-list ) new-line
39088
39089 # pragma omp declare target declare-target-clauses[seq] new-line */
39090
39091 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
39092 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
39093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
39094
39095 static void
39096 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
39097 {
39098 tree clauses = NULL_TREE;
39099 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39100 clauses
39101 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
39102 "#pragma omp declare target", pragma_tok);
39103 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39104 {
39105 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
39106 clauses);
39107 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
39108 cp_parser_require_pragma_eol (parser, pragma_tok);
39109 }
39110 else
39111 {
39112 cp_parser_require_pragma_eol (parser, pragma_tok);
39113 scope_chain->omp_declare_target_attribute++;
39114 return;
39115 }
39116 if (scope_chain->omp_declare_target_attribute)
39117 error_at (pragma_tok->location,
39118 "%<#pragma omp declare target%> with clauses in between "
39119 "%<#pragma omp declare target%> without clauses and "
39120 "%<#pragma omp end declare target%>");
39121 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
39122 {
39123 tree t = OMP_CLAUSE_DECL (c), id;
39124 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
39125 tree at2 = lookup_attribute ("omp declare target link",
39126 DECL_ATTRIBUTES (t));
39127 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
39128 {
39129 id = get_identifier ("omp declare target link");
39130 std::swap (at1, at2);
39131 }
39132 else
39133 id = get_identifier ("omp declare target");
39134 if (at2)
39135 {
39136 error_at (OMP_CLAUSE_LOCATION (c),
39137 "%qD specified both in declare target %<link%> and %<to%>"
39138 " clauses", t);
39139 continue;
39140 }
39141 if (!at1)
39142 {
39143 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
39144 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
39145 continue;
39146
39147 symtab_node *node = symtab_node::get (t);
39148 if (node != NULL)
39149 {
39150 node->offloadable = 1;
39151 if (ENABLE_OFFLOADING)
39152 {
39153 g->have_offload = true;
39154 if (is_a <varpool_node *> (node))
39155 vec_safe_push (offload_vars, t);
39156 }
39157 }
39158 }
39159 }
39160 }
39161
39162 static void
39163 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
39164 {
39165 const char *p = "";
39166 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39167 {
39168 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39169 p = IDENTIFIER_POINTER (id);
39170 }
39171 if (strcmp (p, "declare") == 0)
39172 {
39173 cp_lexer_consume_token (parser->lexer);
39174 p = "";
39175 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39176 {
39177 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39178 p = IDENTIFIER_POINTER (id);
39179 }
39180 if (strcmp (p, "target") == 0)
39181 cp_lexer_consume_token (parser->lexer);
39182 else
39183 {
39184 cp_parser_error (parser, "expected %<target%>");
39185 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39186 return;
39187 }
39188 }
39189 else
39190 {
39191 cp_parser_error (parser, "expected %<declare%>");
39192 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39193 return;
39194 }
39195 cp_parser_require_pragma_eol (parser, pragma_tok);
39196 if (!scope_chain->omp_declare_target_attribute)
39197 error_at (pragma_tok->location,
39198 "%<#pragma omp end declare target%> without corresponding "
39199 "%<#pragma omp declare target%>");
39200 else
39201 scope_chain->omp_declare_target_attribute--;
39202 }
39203
39204 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
39205 expression and optional initializer clause of
39206 #pragma omp declare reduction. We store the expression(s) as
39207 either 3, 6 or 7 special statements inside of the artificial function's
39208 body. The first two statements are DECL_EXPRs for the artificial
39209 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
39210 expression that uses those variables.
39211 If there was any INITIALIZER clause, this is followed by further statements,
39212 the fourth and fifth statements are DECL_EXPRs for the artificial
39213 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
39214 constructor variant (first token after open paren is not omp_priv),
39215 then the sixth statement is a statement with the function call expression
39216 that uses the OMP_PRIV and optionally OMP_ORIG variable.
39217 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
39218 to initialize the OMP_PRIV artificial variable and there is seventh
39219 statement, a DECL_EXPR of the OMP_PRIV statement again. */
39220
39221 static bool
39222 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
39223 {
39224 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
39225 gcc_assert (TYPE_REF_P (type));
39226 type = TREE_TYPE (type);
39227 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
39228 DECL_ARTIFICIAL (omp_out) = 1;
39229 pushdecl (omp_out);
39230 add_decl_expr (omp_out);
39231 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
39232 DECL_ARTIFICIAL (omp_in) = 1;
39233 pushdecl (omp_in);
39234 add_decl_expr (omp_in);
39235 tree combiner;
39236 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
39237
39238 keep_next_level (true);
39239 tree block = begin_omp_structured_block ();
39240 combiner = cp_parser_expression (parser);
39241 finish_expr_stmt (combiner);
39242 block = finish_omp_structured_block (block);
39243 add_stmt (block);
39244
39245 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
39246 return false;
39247
39248 const char *p = "";
39249 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39250 {
39251 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39252 p = IDENTIFIER_POINTER (id);
39253 }
39254
39255 if (strcmp (p, "initializer") == 0)
39256 {
39257 cp_lexer_consume_token (parser->lexer);
39258 matching_parens parens;
39259 if (!parens.require_open (parser))
39260 return false;
39261
39262 p = "";
39263 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39264 {
39265 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39266 p = IDENTIFIER_POINTER (id);
39267 }
39268
39269 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
39270 DECL_ARTIFICIAL (omp_priv) = 1;
39271 pushdecl (omp_priv);
39272 add_decl_expr (omp_priv);
39273 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
39274 DECL_ARTIFICIAL (omp_orig) = 1;
39275 pushdecl (omp_orig);
39276 add_decl_expr (omp_orig);
39277
39278 keep_next_level (true);
39279 block = begin_omp_structured_block ();
39280
39281 bool ctor = false;
39282 if (strcmp (p, "omp_priv") == 0)
39283 {
39284 bool is_direct_init, is_non_constant_init;
39285 ctor = true;
39286 cp_lexer_consume_token (parser->lexer);
39287 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
39288 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
39289 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39290 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
39291 == CPP_CLOSE_PAREN
39292 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
39293 == CPP_CLOSE_PAREN))
39294 {
39295 finish_omp_structured_block (block);
39296 error ("invalid initializer clause");
39297 return false;
39298 }
39299 initializer = cp_parser_initializer (parser, &is_direct_init,
39300 &is_non_constant_init);
39301 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
39302 NULL_TREE, LOOKUP_ONLYCONVERTING);
39303 }
39304 else
39305 {
39306 cp_parser_parse_tentatively (parser);
39307 /* Don't create location wrapper nodes here. */
39308 auto_suppress_location_wrappers sentinel;
39309 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
39310 /*check_dependency_p=*/true,
39311 /*template_p=*/NULL,
39312 /*declarator_p=*/false,
39313 /*optional_p=*/false);
39314 vec<tree, va_gc> *args;
39315 if (fn_name == error_mark_node
39316 || cp_parser_error_occurred (parser)
39317 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39318 || ((args = cp_parser_parenthesized_expression_list
39319 (parser, non_attr, /*cast_p=*/false,
39320 /*allow_expansion_p=*/true,
39321 /*non_constant_p=*/NULL)),
39322 cp_parser_error_occurred (parser)))
39323 {
39324 finish_omp_structured_block (block);
39325 cp_parser_abort_tentative_parse (parser);
39326 cp_parser_error (parser, "expected id-expression (arguments)");
39327 return false;
39328 }
39329 unsigned int i;
39330 tree arg;
39331 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
39332 if (arg == omp_priv
39333 || (TREE_CODE (arg) == ADDR_EXPR
39334 && TREE_OPERAND (arg, 0) == omp_priv))
39335 break;
39336 cp_parser_abort_tentative_parse (parser);
39337 if (arg == NULL_TREE)
39338 error ("one of the initializer call arguments should be %<omp_priv%>"
39339 " or %<&omp_priv%>");
39340 initializer = cp_parser_postfix_expression (parser, false, false, false,
39341 false, NULL);
39342 finish_expr_stmt (initializer);
39343 }
39344
39345 block = finish_omp_structured_block (block);
39346 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
39347 add_stmt (block);
39348
39349 if (ctor)
39350 add_decl_expr (omp_orig);
39351
39352 if (!parens.require_close (parser))
39353 return false;
39354 }
39355
39356 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
39357 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
39358 UNKNOWN_LOCATION);
39359
39360 return true;
39361 }
39362
39363 /* OpenMP 4.0
39364 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39365 initializer-clause[opt] new-line
39366
39367 initializer-clause:
39368 initializer (omp_priv initializer)
39369 initializer (function-name (argument-list)) */
39370
39371 static void
39372 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
39373 enum pragma_context)
39374 {
39375 auto_vec<tree> types;
39376 enum tree_code reduc_code = ERROR_MARK;
39377 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
39378 unsigned int i;
39379 cp_token *first_token;
39380 cp_token_cache *cp;
39381 int errs;
39382 void *p;
39383
39384 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39385 p = obstack_alloc (&declarator_obstack, 0);
39386
39387 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39388 goto fail;
39389
39390 switch (cp_lexer_peek_token (parser->lexer)->type)
39391 {
39392 case CPP_PLUS:
39393 reduc_code = PLUS_EXPR;
39394 break;
39395 case CPP_MULT:
39396 reduc_code = MULT_EXPR;
39397 break;
39398 case CPP_MINUS:
39399 reduc_code = MINUS_EXPR;
39400 break;
39401 case CPP_AND:
39402 reduc_code = BIT_AND_EXPR;
39403 break;
39404 case CPP_XOR:
39405 reduc_code = BIT_XOR_EXPR;
39406 break;
39407 case CPP_OR:
39408 reduc_code = BIT_IOR_EXPR;
39409 break;
39410 case CPP_AND_AND:
39411 reduc_code = TRUTH_ANDIF_EXPR;
39412 break;
39413 case CPP_OR_OR:
39414 reduc_code = TRUTH_ORIF_EXPR;
39415 break;
39416 case CPP_NAME:
39417 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
39418 break;
39419 default:
39420 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39421 "%<|%>, %<&&%>, %<||%> or identifier");
39422 goto fail;
39423 }
39424
39425 if (reduc_code != ERROR_MARK)
39426 cp_lexer_consume_token (parser->lexer);
39427
39428 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
39429 if (reduc_id == error_mark_node)
39430 goto fail;
39431
39432 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39433 goto fail;
39434
39435 /* Types may not be defined in declare reduction type list. */
39436 const char *saved_message;
39437 saved_message = parser->type_definition_forbidden_message;
39438 parser->type_definition_forbidden_message
39439 = G_("types may not be defined in declare reduction type list");
39440 bool saved_colon_corrects_to_scope_p;
39441 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39442 parser->colon_corrects_to_scope_p = false;
39443 bool saved_colon_doesnt_start_class_def_p;
39444 saved_colon_doesnt_start_class_def_p
39445 = parser->colon_doesnt_start_class_def_p;
39446 parser->colon_doesnt_start_class_def_p = true;
39447
39448 while (true)
39449 {
39450 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39451 type = cp_parser_type_id (parser);
39452 if (type == error_mark_node)
39453 ;
39454 else if (ARITHMETIC_TYPE_P (type)
39455 && (orig_reduc_id == NULL_TREE
39456 || (TREE_CODE (type) != COMPLEX_TYPE
39457 && (id_equal (orig_reduc_id, "min")
39458 || id_equal (orig_reduc_id, "max")))))
39459 error_at (loc, "predeclared arithmetic type %qT in "
39460 "%<#pragma omp declare reduction%>", type);
39461 else if (TREE_CODE (type) == FUNCTION_TYPE
39462 || TREE_CODE (type) == METHOD_TYPE
39463 || TREE_CODE (type) == ARRAY_TYPE)
39464 error_at (loc, "function or array type %qT in "
39465 "%<#pragma omp declare reduction%>", type);
39466 else if (TYPE_REF_P (type))
39467 error_at (loc, "reference type %qT in "
39468 "%<#pragma omp declare reduction%>", type);
39469 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
39470 error_at (loc, "const, volatile or __restrict qualified type %qT in "
39471 "%<#pragma omp declare reduction%>", type);
39472 else
39473 types.safe_push (type);
39474
39475 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39476 cp_lexer_consume_token (parser->lexer);
39477 else
39478 break;
39479 }
39480
39481 /* Restore the saved message. */
39482 parser->type_definition_forbidden_message = saved_message;
39483 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39484 parser->colon_doesnt_start_class_def_p
39485 = saved_colon_doesnt_start_class_def_p;
39486
39487 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
39488 || types.is_empty ())
39489 {
39490 fail:
39491 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39492 goto done;
39493 }
39494
39495 first_token = cp_lexer_peek_token (parser->lexer);
39496 cp = NULL;
39497 errs = errorcount;
39498 FOR_EACH_VEC_ELT (types, i, type)
39499 {
39500 tree fntype
39501 = build_function_type_list (void_type_node,
39502 cp_build_reference_type (type, false),
39503 NULL_TREE);
39504 tree this_reduc_id = reduc_id;
39505 if (!dependent_type_p (type))
39506 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
39507 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
39508 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
39509 DECL_ARTIFICIAL (fndecl) = 1;
39510 DECL_EXTERNAL (fndecl) = 1;
39511 DECL_DECLARED_INLINE_P (fndecl) = 1;
39512 DECL_IGNORED_P (fndecl) = 1;
39513 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
39514 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
39515 DECL_ATTRIBUTES (fndecl)
39516 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
39517 DECL_ATTRIBUTES (fndecl));
39518 if (processing_template_decl)
39519 fndecl = push_template_decl (fndecl);
39520 bool block_scope = false;
39521 tree block = NULL_TREE;
39522 if (current_function_decl)
39523 {
39524 block_scope = true;
39525 DECL_CONTEXT (fndecl) = global_namespace;
39526 if (!processing_template_decl)
39527 pushdecl (fndecl);
39528 }
39529 else if (current_class_type)
39530 {
39531 if (cp == NULL)
39532 {
39533 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39534 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39535 cp_lexer_consume_token (parser->lexer);
39536 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39537 goto fail;
39538 cp = cp_token_cache_new (first_token,
39539 cp_lexer_peek_nth_token (parser->lexer,
39540 2));
39541 }
39542 DECL_STATIC_FUNCTION_P (fndecl) = 1;
39543 finish_member_declaration (fndecl);
39544 DECL_PENDING_INLINE_INFO (fndecl) = cp;
39545 DECL_PENDING_INLINE_P (fndecl) = 1;
39546 vec_safe_push (unparsed_funs_with_definitions, fndecl);
39547 continue;
39548 }
39549 else
39550 {
39551 DECL_CONTEXT (fndecl) = current_namespace;
39552 pushdecl (fndecl);
39553 }
39554 if (!block_scope)
39555 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
39556 else
39557 block = begin_omp_structured_block ();
39558 if (cp)
39559 {
39560 cp_parser_push_lexer_for_tokens (parser, cp);
39561 parser->lexer->in_pragma = true;
39562 }
39563 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
39564 {
39565 if (!block_scope)
39566 finish_function (/*inline_p=*/false);
39567 else
39568 DECL_CONTEXT (fndecl) = current_function_decl;
39569 if (cp)
39570 cp_parser_pop_lexer (parser);
39571 goto fail;
39572 }
39573 if (cp)
39574 cp_parser_pop_lexer (parser);
39575 if (!block_scope)
39576 finish_function (/*inline_p=*/false);
39577 else
39578 {
39579 DECL_CONTEXT (fndecl) = current_function_decl;
39580 block = finish_omp_structured_block (block);
39581 if (TREE_CODE (block) == BIND_EXPR)
39582 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
39583 else if (TREE_CODE (block) == STATEMENT_LIST)
39584 DECL_SAVED_TREE (fndecl) = block;
39585 if (processing_template_decl)
39586 add_decl_expr (fndecl);
39587 }
39588 cp_check_omp_declare_reduction (fndecl);
39589 if (cp == NULL && types.length () > 1)
39590 cp = cp_token_cache_new (first_token,
39591 cp_lexer_peek_nth_token (parser->lexer, 2));
39592 if (errs != errorcount)
39593 break;
39594 }
39595
39596 cp_parser_require_pragma_eol (parser, pragma_tok);
39597
39598 done:
39599 /* Free any declarators allocated. */
39600 obstack_free (&declarator_obstack, p);
39601 }
39602
39603 /* OpenMP 4.0
39604 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39605 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39606 initializer-clause[opt] new-line
39607 #pragma omp declare target new-line */
39608
39609 static bool
39610 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
39611 enum pragma_context context)
39612 {
39613 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39614 {
39615 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39616 const char *p = IDENTIFIER_POINTER (id);
39617
39618 if (strcmp (p, "simd") == 0)
39619 {
39620 cp_lexer_consume_token (parser->lexer);
39621 cp_parser_omp_declare_simd (parser, pragma_tok,
39622 context);
39623 return true;
39624 }
39625 cp_ensure_no_omp_declare_simd (parser);
39626 if (strcmp (p, "reduction") == 0)
39627 {
39628 cp_lexer_consume_token (parser->lexer);
39629 cp_parser_omp_declare_reduction (parser, pragma_tok,
39630 context);
39631 return false;
39632 }
39633 if (!flag_openmp) /* flag_openmp_simd */
39634 {
39635 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39636 return false;
39637 }
39638 if (strcmp (p, "target") == 0)
39639 {
39640 cp_lexer_consume_token (parser->lexer);
39641 cp_parser_omp_declare_target (parser, pragma_tok);
39642 return false;
39643 }
39644 }
39645 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
39646 "or %<target%>");
39647 cp_parser_require_pragma_eol (parser, pragma_tok);
39648 return false;
39649 }
39650
39651 /* OpenMP 5.0
39652 #pragma omp requires clauses[optseq] new-line */
39653
39654 static bool
39655 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
39656 {
39657 bool first = true;
39658 enum omp_requires new_req = (enum omp_requires) 0;
39659
39660 location_t loc = pragma_tok->location;
39661 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39662 {
39663 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39664 cp_lexer_consume_token (parser->lexer);
39665
39666 first = false;
39667
39668 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39669 {
39670 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39671 const char *p = IDENTIFIER_POINTER (id);
39672 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
39673 enum omp_requires this_req = (enum omp_requires) 0;
39674
39675 if (!strcmp (p, "unified_address"))
39676 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
39677 else if (!strcmp (p, "unified_shared_memory"))
39678 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
39679 else if (!strcmp (p, "dynamic_allocators"))
39680 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
39681 else if (!strcmp (p, "reverse_offload"))
39682 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
39683 else if (!strcmp (p, "atomic_default_mem_order"))
39684 {
39685 cp_lexer_consume_token (parser->lexer);
39686
39687 matching_parens parens;
39688 if (parens.require_open (parser))
39689 {
39690 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39691 {
39692 id = cp_lexer_peek_token (parser->lexer)->u.value;
39693 p = IDENTIFIER_POINTER (id);
39694
39695 if (!strcmp (p, "seq_cst"))
39696 this_req
39697 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
39698 else if (!strcmp (p, "relaxed"))
39699 this_req
39700 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
39701 else if (!strcmp (p, "acq_rel"))
39702 this_req
39703 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
39704 }
39705 if (this_req == 0)
39706 {
39707 error_at (cp_lexer_peek_token (parser->lexer)->location,
39708 "expected %<seq_cst%>, %<relaxed%> or "
39709 "%<acq_rel%>");
39710 if (cp_lexer_nth_token_is (parser->lexer, 2,
39711 CPP_CLOSE_PAREN))
39712 cp_lexer_consume_token (parser->lexer);
39713 }
39714 else
39715 cp_lexer_consume_token (parser->lexer);
39716
39717 if (!parens.require_close (parser))
39718 cp_parser_skip_to_closing_parenthesis (parser,
39719 /*recovering=*/true,
39720 /*or_comma=*/false,
39721 /*consume_paren=*/
39722 true);
39723
39724 if (this_req == 0)
39725 {
39726 cp_parser_require_pragma_eol (parser, pragma_tok);
39727 return false;
39728 }
39729 }
39730 p = NULL;
39731 }
39732 else
39733 {
39734 error_at (cloc, "expected %<unified_address%>, "
39735 "%<unified_shared_memory%>, "
39736 "%<dynamic_allocators%>, "
39737 "%<reverse_offload%> "
39738 "or %<atomic_default_mem_order%> clause");
39739 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39740 return false;
39741 }
39742 if (p)
39743 sorry_at (cloc, "%qs clause on %<requires%> directive not "
39744 "supported yet", p);
39745 if (p)
39746 cp_lexer_consume_token (parser->lexer);
39747 if (this_req)
39748 {
39749 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39750 {
39751 if ((this_req & new_req) != 0)
39752 error_at (cloc, "too many %qs clauses", p);
39753 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
39754 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
39755 error_at (cloc, "%qs clause used lexically after first "
39756 "target construct or offloading API", p);
39757 }
39758 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39759 {
39760 error_at (cloc, "too many %qs clauses",
39761 "atomic_default_mem_order");
39762 this_req = (enum omp_requires) 0;
39763 }
39764 else if ((omp_requires_mask
39765 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39766 {
39767 error_at (cloc, "more than one %<atomic_default_mem_order%>"
39768 " clause in a single compilation unit");
39769 this_req
39770 = (enum omp_requires)
39771 (omp_requires_mask
39772 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
39773 }
39774 else if ((omp_requires_mask
39775 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
39776 error_at (cloc, "%<atomic_default_mem_order%> clause used "
39777 "lexically after first %<atomic%> construct "
39778 "without memory order clause");
39779 new_req = (enum omp_requires) (new_req | this_req);
39780 omp_requires_mask
39781 = (enum omp_requires) (omp_requires_mask | this_req);
39782 continue;
39783 }
39784 }
39785 break;
39786 }
39787 cp_parser_require_pragma_eol (parser, pragma_tok);
39788
39789 if (new_req == 0)
39790 error_at (loc, "%<pragma omp requires%> requires at least one clause");
39791 return false;
39792 }
39793
39794
39795 /* OpenMP 4.5:
39796 #pragma omp taskloop taskloop-clause[optseq] new-line
39797 for-loop
39798
39799 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39800 for-loop */
39801
39802 #define OMP_TASKLOOP_CLAUSE_MASK \
39803 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39819
39820 static tree
39821 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
39822 char *p_name, omp_clause_mask mask, tree *cclauses,
39823 bool *if_p)
39824 {
39825 tree clauses, sb, ret;
39826 unsigned int save;
39827 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39828
39829 strcat (p_name, " taskloop");
39830 mask |= OMP_TASKLOOP_CLAUSE_MASK;
39831 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39832 clause. */
39833 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
39834 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
39835
39836 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39837 {
39838 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39839 const char *p = IDENTIFIER_POINTER (id);
39840
39841 if (strcmp (p, "simd") == 0)
39842 {
39843 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39844 if (cclauses == NULL)
39845 cclauses = cclauses_buf;
39846
39847 cp_lexer_consume_token (parser->lexer);
39848 if (!flag_openmp) /* flag_openmp_simd */
39849 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39850 cclauses, if_p);
39851 sb = begin_omp_structured_block ();
39852 save = cp_parser_begin_omp_structured_block (parser);
39853 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39854 cclauses, if_p);
39855 cp_parser_end_omp_structured_block (parser, save);
39856 tree body = finish_omp_structured_block (sb);
39857 if (ret == NULL)
39858 return ret;
39859 ret = make_node (OMP_TASKLOOP);
39860 TREE_TYPE (ret) = void_type_node;
39861 OMP_FOR_BODY (ret) = body;
39862 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39863 SET_EXPR_LOCATION (ret, loc);
39864 add_stmt (ret);
39865 return ret;
39866 }
39867 }
39868 if (!flag_openmp) /* flag_openmp_simd */
39869 {
39870 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39871 return NULL_TREE;
39872 }
39873
39874 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39875 cclauses == NULL);
39876 if (cclauses)
39877 {
39878 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
39879 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39880 }
39881
39882 keep_next_level (true);
39883 sb = begin_omp_structured_block ();
39884 save = cp_parser_begin_omp_structured_block (parser);
39885
39886 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
39887 if_p);
39888
39889 cp_parser_end_omp_structured_block (parser, save);
39890 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39891
39892 return ret;
39893 }
39894
39895
39896 /* OpenACC 2.0:
39897 # pragma acc routine oacc-routine-clause[optseq] new-line
39898 function-definition
39899
39900 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39901 */
39902
39903 #define OACC_ROUTINE_CLAUSE_MASK \
39904 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39908
39909
39910 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39911 component, which must resolve to a declared namespace-scope
39912 function. The clauses are either processed directly (for a named
39913 function), or defered until the immediatley following declaration
39914 is parsed. */
39915
39916 static void
39917 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
39918 enum pragma_context context)
39919 {
39920 gcc_checking_assert (context == pragma_external);
39921 /* The checking for "another pragma following this one" in the "no optional
39922 '( name )'" case makes sure that we dont re-enter. */
39923 gcc_checking_assert (parser->oacc_routine == NULL);
39924
39925 cp_oacc_routine_data data;
39926 data.error_seen = false;
39927 data.fndecl_seen = false;
39928 data.tokens = vNULL;
39929 data.clauses = NULL_TREE;
39930 data.loc = pragma_tok->location;
39931 /* It is safe to take the address of a local variable; it will only be
39932 used while this scope is live. */
39933 parser->oacc_routine = &data;
39934
39935 /* Look for optional '( name )'. */
39936 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39937 {
39938 matching_parens parens;
39939 parens.consume_open (parser); /* '(' */
39940
39941 /* We parse the name as an id-expression. If it resolves to
39942 anything other than a non-overloaded function at namespace
39943 scope, it's an error. */
39944 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
39945 tree name = cp_parser_id_expression (parser,
39946 /*template_keyword_p=*/false,
39947 /*check_dependency_p=*/false,
39948 /*template_p=*/NULL,
39949 /*declarator_p=*/false,
39950 /*optional_p=*/false);
39951 tree decl = (identifier_p (name)
39952 ? cp_parser_lookup_name_simple (parser, name, name_loc)
39953 : name);
39954 if (name != error_mark_node && decl == error_mark_node)
39955 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
39956
39957 if (decl == error_mark_node
39958 || !parens.require_close (parser))
39959 {
39960 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39961 parser->oacc_routine = NULL;
39962 return;
39963 }
39964
39965 data.clauses
39966 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
39967 "#pragma acc routine",
39968 cp_lexer_peek_token (parser->lexer));
39969
39970 if (decl && is_overloaded_fn (decl)
39971 && (TREE_CODE (decl) != FUNCTION_DECL
39972 || DECL_FUNCTION_TEMPLATE_P (decl)))
39973 {
39974 error_at (name_loc,
39975 "%<#pragma acc routine%> names a set of overloads");
39976 parser->oacc_routine = NULL;
39977 return;
39978 }
39979
39980 /* Perhaps we should use the same rule as declarations in different
39981 namespaces? */
39982 if (!DECL_NAMESPACE_SCOPE_P (decl))
39983 {
39984 error_at (name_loc,
39985 "%qD does not refer to a namespace scope function", decl);
39986 parser->oacc_routine = NULL;
39987 return;
39988 }
39989
39990 if (TREE_CODE (decl) != FUNCTION_DECL)
39991 {
39992 error_at (name_loc, "%qD does not refer to a function", decl);
39993 parser->oacc_routine = NULL;
39994 return;
39995 }
39996
39997 cp_finalize_oacc_routine (parser, decl, false);
39998 parser->oacc_routine = NULL;
39999 }
40000 else /* No optional '( name )'. */
40001 {
40002 /* Store away all pragma tokens. */
40003 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
40004 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
40005 cp_lexer_consume_token (parser->lexer);
40006 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40007 parser->oacc_routine->error_seen = true;
40008 cp_parser_require_pragma_eol (parser, pragma_tok);
40009 struct cp_token_cache *cp
40010 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
40011 parser->oacc_routine->tokens.safe_push (cp);
40012
40013 /* Emit a helpful diagnostic if there's another pragma following this
40014 one. */
40015 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
40016 {
40017 cp_ensure_no_oacc_routine (parser);
40018 data.tokens.release ();
40019 /* ..., and then just keep going. */
40020 return;
40021 }
40022
40023 /* We only have to consider the pragma_external case here. */
40024 cp_parser_declaration (parser);
40025 if (parser->oacc_routine
40026 && !parser->oacc_routine->fndecl_seen)
40027 cp_ensure_no_oacc_routine (parser);
40028 else
40029 parser->oacc_routine = NULL;
40030 data.tokens.release ();
40031 }
40032 }
40033
40034 /* Finalize #pragma acc routine clauses after direct declarator has
40035 been parsed. */
40036
40037 static tree
40038 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
40039 {
40040 struct cp_token_cache *ce;
40041 cp_oacc_routine_data *data = parser->oacc_routine;
40042
40043 if (!data->error_seen && data->fndecl_seen)
40044 {
40045 error_at (data->loc,
40046 "%<#pragma acc routine%> not immediately followed by "
40047 "a single function declaration or definition");
40048 data->error_seen = true;
40049 }
40050 if (data->error_seen)
40051 return attrs;
40052
40053 gcc_checking_assert (data->tokens.length () == 1);
40054 ce = data->tokens[0];
40055
40056 cp_parser_push_lexer_for_tokens (parser, ce);
40057 parser->lexer->in_pragma = true;
40058 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
40059
40060 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
40061 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
40062 parser->oacc_routine->clauses
40063 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40064 "#pragma acc routine", pragma_tok);
40065 cp_parser_pop_lexer (parser);
40066 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
40067 fndecl_seen. */
40068
40069 return attrs;
40070 }
40071
40072 /* Apply any saved OpenACC routine clauses to a just-parsed
40073 declaration. */
40074
40075 static void
40076 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
40077 {
40078 if (__builtin_expect (parser->oacc_routine != NULL, 0))
40079 {
40080 /* Keep going if we're in error reporting mode. */
40081 if (parser->oacc_routine->error_seen
40082 || fndecl == error_mark_node)
40083 return;
40084
40085 if (parser->oacc_routine->fndecl_seen)
40086 {
40087 error_at (parser->oacc_routine->loc,
40088 "%<#pragma acc routine%> not immediately followed by"
40089 " a single function declaration or definition");
40090 parser->oacc_routine = NULL;
40091 return;
40092 }
40093 if (TREE_CODE (fndecl) != FUNCTION_DECL)
40094 {
40095 cp_ensure_no_oacc_routine (parser);
40096 return;
40097 }
40098
40099 if (oacc_get_fn_attrib (fndecl))
40100 {
40101 error_at (parser->oacc_routine->loc,
40102 "%<#pragma acc routine%> already applied to %qD", fndecl);
40103 parser->oacc_routine = NULL;
40104 return;
40105 }
40106
40107 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
40108 {
40109 error_at (parser->oacc_routine->loc,
40110 TREE_USED (fndecl)
40111 ? G_("%<#pragma acc routine%> must be applied before use")
40112 : G_("%<#pragma acc routine%> must be applied before "
40113 "definition"));
40114 parser->oacc_routine = NULL;
40115 return;
40116 }
40117
40118 /* Process the routine's dimension clauses. */
40119 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
40120 oacc_replace_fn_attrib (fndecl, dims);
40121
40122 /* Add an "omp declare target" attribute. */
40123 DECL_ATTRIBUTES (fndecl)
40124 = tree_cons (get_identifier ("omp declare target"),
40125 NULL_TREE, DECL_ATTRIBUTES (fndecl));
40126
40127 /* Don't unset parser->oacc_routine here: we may still need it to
40128 diagnose wrong usage. But, remember that we've used this "#pragma acc
40129 routine". */
40130 parser->oacc_routine->fndecl_seen = true;
40131 }
40132 }
40133
40134 /* Main entry point to OpenMP statement pragmas. */
40135
40136 static void
40137 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40138 {
40139 tree stmt;
40140 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
40141 omp_clause_mask mask (0);
40142
40143 switch (cp_parser_pragma_kind (pragma_tok))
40144 {
40145 case PRAGMA_OACC_ATOMIC:
40146 cp_parser_omp_atomic (parser, pragma_tok);
40147 return;
40148 case PRAGMA_OACC_CACHE:
40149 stmt = cp_parser_oacc_cache (parser, pragma_tok);
40150 break;
40151 case PRAGMA_OACC_DATA:
40152 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
40153 break;
40154 case PRAGMA_OACC_ENTER_DATA:
40155 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
40156 break;
40157 case PRAGMA_OACC_EXIT_DATA:
40158 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
40159 break;
40160 case PRAGMA_OACC_HOST_DATA:
40161 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
40162 break;
40163 case PRAGMA_OACC_KERNELS:
40164 case PRAGMA_OACC_PARALLEL:
40165 strcpy (p_name, "#pragma acc");
40166 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
40167 if_p);
40168 break;
40169 case PRAGMA_OACC_LOOP:
40170 strcpy (p_name, "#pragma acc");
40171 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
40172 if_p);
40173 break;
40174 case PRAGMA_OACC_UPDATE:
40175 stmt = cp_parser_oacc_update (parser, pragma_tok);
40176 break;
40177 case PRAGMA_OACC_WAIT:
40178 stmt = cp_parser_oacc_wait (parser, pragma_tok);
40179 break;
40180 case PRAGMA_OMP_ATOMIC:
40181 cp_parser_omp_atomic (parser, pragma_tok);
40182 return;
40183 case PRAGMA_OMP_CRITICAL:
40184 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
40185 break;
40186 case PRAGMA_OMP_DISTRIBUTE:
40187 strcpy (p_name, "#pragma omp");
40188 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
40189 if_p);
40190 break;
40191 case PRAGMA_OMP_FOR:
40192 strcpy (p_name, "#pragma omp");
40193 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
40194 if_p);
40195 break;
40196 case PRAGMA_OMP_MASTER:
40197 strcpy (p_name, "#pragma omp");
40198 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
40199 if_p);
40200 break;
40201 case PRAGMA_OMP_PARALLEL:
40202 strcpy (p_name, "#pragma omp");
40203 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
40204 if_p);
40205 break;
40206 case PRAGMA_OMP_SECTIONS:
40207 strcpy (p_name, "#pragma omp");
40208 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
40209 break;
40210 case PRAGMA_OMP_SIMD:
40211 strcpy (p_name, "#pragma omp");
40212 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
40213 if_p);
40214 break;
40215 case PRAGMA_OMP_SINGLE:
40216 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
40217 break;
40218 case PRAGMA_OMP_TASK:
40219 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
40220 break;
40221 case PRAGMA_OMP_TASKGROUP:
40222 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
40223 break;
40224 case PRAGMA_OMP_TASKLOOP:
40225 strcpy (p_name, "#pragma omp");
40226 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
40227 if_p);
40228 break;
40229 case PRAGMA_OMP_TEAMS:
40230 strcpy (p_name, "#pragma omp");
40231 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
40232 if_p);
40233 break;
40234 default:
40235 gcc_unreachable ();
40236 }
40237
40238 protected_set_expr_location (stmt, pragma_tok->location);
40239 }
40240 \f
40241 /* Transactional Memory parsing routines. */
40242
40243 /* Parse a transaction attribute.
40244
40245 txn-attribute:
40246 attribute
40247 [ [ identifier ] ]
40248
40249 We use this instead of cp_parser_attributes_opt for transactions to avoid
40250 the pedwarn in C++98 mode. */
40251
40252 static tree
40253 cp_parser_txn_attribute_opt (cp_parser *parser)
40254 {
40255 cp_token *token;
40256 tree attr_name, attr = NULL;
40257
40258 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
40259 return cp_parser_attributes_opt (parser);
40260
40261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
40262 return NULL_TREE;
40263 cp_lexer_consume_token (parser->lexer);
40264 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
40265 goto error1;
40266
40267 token = cp_lexer_peek_token (parser->lexer);
40268 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
40269 {
40270 token = cp_lexer_consume_token (parser->lexer);
40271
40272 attr_name = (token->type == CPP_KEYWORD
40273 /* For keywords, use the canonical spelling,
40274 not the parsed identifier. */
40275 ? ridpointers[(int) token->keyword]
40276 : token->u.value);
40277 attr = build_tree_list (attr_name, NULL_TREE);
40278 }
40279 else
40280 cp_parser_error (parser, "expected identifier");
40281
40282 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40283 error1:
40284 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40285 return attr;
40286 }
40287
40288 /* Parse a __transaction_atomic or __transaction_relaxed statement.
40289
40290 transaction-statement:
40291 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40292 compound-statement
40293 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40294 */
40295
40296 static tree
40297 cp_parser_transaction (cp_parser *parser, cp_token *token)
40298 {
40299 unsigned char old_in = parser->in_transaction;
40300 unsigned char this_in = 1, new_in;
40301 enum rid keyword = token->keyword;
40302 tree stmt, attrs, noex;
40303
40304 cp_lexer_consume_token (parser->lexer);
40305
40306 if (keyword == RID_TRANSACTION_RELAXED
40307 || keyword == RID_SYNCHRONIZED)
40308 this_in |= TM_STMT_ATTR_RELAXED;
40309 else
40310 {
40311 attrs = cp_parser_txn_attribute_opt (parser);
40312 if (attrs)
40313 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40314 }
40315
40316 /* Parse a noexcept specification. */
40317 if (keyword == RID_ATOMIC_NOEXCEPT)
40318 noex = boolean_true_node;
40319 else if (keyword == RID_ATOMIC_CANCEL)
40320 {
40321 /* cancel-and-throw is unimplemented. */
40322 sorry ("atomic_cancel");
40323 noex = NULL_TREE;
40324 }
40325 else
40326 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
40327
40328 /* Keep track if we're in the lexical scope of an outer transaction. */
40329 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
40330
40331 stmt = begin_transaction_stmt (token->location, NULL, this_in);
40332
40333 parser->in_transaction = new_in;
40334 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
40335 parser->in_transaction = old_in;
40336
40337 finish_transaction_stmt (stmt, NULL, this_in, noex);
40338
40339 return stmt;
40340 }
40341
40342 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40343
40344 transaction-expression:
40345 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40346 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40347 */
40348
40349 static tree
40350 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
40351 {
40352 unsigned char old_in = parser->in_transaction;
40353 unsigned char this_in = 1;
40354 cp_token *token;
40355 tree expr, noex;
40356 bool noex_expr;
40357 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40358
40359 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40360 || keyword == RID_TRANSACTION_RELAXED);
40361
40362 if (!flag_tm)
40363 error_at (loc,
40364 keyword == RID_TRANSACTION_RELAXED
40365 ? G_("%<__transaction_relaxed%> without transactional memory "
40366 "support enabled")
40367 : G_("%<__transaction_atomic%> without transactional memory "
40368 "support enabled"));
40369
40370 token = cp_parser_require_keyword (parser, keyword,
40371 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40372 : RT_TRANSACTION_RELAXED));
40373 gcc_assert (token != NULL);
40374
40375 if (keyword == RID_TRANSACTION_RELAXED)
40376 this_in |= TM_STMT_ATTR_RELAXED;
40377
40378 /* Set this early. This might mean that we allow transaction_cancel in
40379 an expression that we find out later actually has to be a constexpr.
40380 However, we expect that cxx_constant_value will be able to deal with
40381 this; also, if the noexcept has no constexpr, then what we parse next
40382 really is a transaction's body. */
40383 parser->in_transaction = this_in;
40384
40385 /* Parse a noexcept specification. */
40386 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
40387 true);
40388
40389 if (!noex || !noex_expr
40390 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
40391 {
40392 matching_parens parens;
40393 parens.require_open (parser);
40394
40395 expr = cp_parser_expression (parser);
40396 expr = finish_parenthesized_expr (expr);
40397
40398 parens.require_close (parser);
40399 }
40400 else
40401 {
40402 /* The only expression that is available got parsed for the noexcept
40403 already. noexcept is true then. */
40404 expr = noex;
40405 noex = boolean_true_node;
40406 }
40407
40408 expr = build_transaction_expr (token->location, expr, this_in, noex);
40409 parser->in_transaction = old_in;
40410
40411 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
40412 return error_mark_node;
40413
40414 return (flag_tm ? expr : error_mark_node);
40415 }
40416
40417 /* Parse a function-transaction-block.
40418
40419 function-transaction-block:
40420 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40421 function-body
40422 __transaction_atomic txn-attribute[opt] function-try-block
40423 __transaction_relaxed ctor-initializer[opt] function-body
40424 __transaction_relaxed function-try-block
40425 */
40426
40427 static void
40428 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
40429 {
40430 unsigned char old_in = parser->in_transaction;
40431 unsigned char new_in = 1;
40432 tree compound_stmt, stmt, attrs;
40433 cp_token *token;
40434
40435 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40436 || keyword == RID_TRANSACTION_RELAXED);
40437 token = cp_parser_require_keyword (parser, keyword,
40438 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40439 : RT_TRANSACTION_RELAXED));
40440 gcc_assert (token != NULL);
40441
40442 if (keyword == RID_TRANSACTION_RELAXED)
40443 new_in |= TM_STMT_ATTR_RELAXED;
40444 else
40445 {
40446 attrs = cp_parser_txn_attribute_opt (parser);
40447 if (attrs)
40448 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40449 }
40450
40451 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
40452
40453 parser->in_transaction = new_in;
40454
40455 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
40456 cp_parser_function_try_block (parser);
40457 else
40458 cp_parser_ctor_initializer_opt_and_function_body
40459 (parser, /*in_function_try_block=*/false);
40460
40461 parser->in_transaction = old_in;
40462
40463 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
40464 }
40465
40466 /* Parse a __transaction_cancel statement.
40467
40468 cancel-statement:
40469 __transaction_cancel txn-attribute[opt] ;
40470 __transaction_cancel txn-attribute[opt] throw-expression ;
40471
40472 ??? Cancel and throw is not yet implemented. */
40473
40474 static tree
40475 cp_parser_transaction_cancel (cp_parser *parser)
40476 {
40477 cp_token *token;
40478 bool is_outer = false;
40479 tree stmt, attrs;
40480
40481 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
40482 RT_TRANSACTION_CANCEL);
40483 gcc_assert (token != NULL);
40484
40485 attrs = cp_parser_txn_attribute_opt (parser);
40486 if (attrs)
40487 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
40488
40489 /* ??? Parse cancel-and-throw here. */
40490
40491 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40492
40493 if (!flag_tm)
40494 {
40495 error_at (token->location, "%<__transaction_cancel%> without "
40496 "transactional memory support enabled");
40497 return error_mark_node;
40498 }
40499 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
40500 {
40501 error_at (token->location, "%<__transaction_cancel%> within a "
40502 "%<__transaction_relaxed%>");
40503 return error_mark_node;
40504 }
40505 else if (is_outer)
40506 {
40507 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
40508 && !is_tm_may_cancel_outer (current_function_decl))
40509 {
40510 error_at (token->location, "outer %<__transaction_cancel%> not "
40511 "within outer %<__transaction_atomic%>");
40512 error_at (token->location,
40513 " or a %<transaction_may_cancel_outer%> function");
40514 return error_mark_node;
40515 }
40516 }
40517 else if (parser->in_transaction == 0)
40518 {
40519 error_at (token->location, "%<__transaction_cancel%> not within "
40520 "%<__transaction_atomic%>");
40521 return error_mark_node;
40522 }
40523
40524 stmt = build_tm_abort_call (token->location, is_outer);
40525 add_stmt (stmt);
40526
40527 return stmt;
40528 }
40529 \f
40530 /* The parser. */
40531
40532 static GTY (()) cp_parser *the_parser;
40533
40534 \f
40535 /* Special handling for the first token or line in the file. The first
40536 thing in the file might be #pragma GCC pch_preprocess, which loads a
40537 PCH file, which is a GC collection point. So we need to handle this
40538 first pragma without benefit of an existing lexer structure.
40539
40540 Always returns one token to the caller in *FIRST_TOKEN. This is
40541 either the true first token of the file, or the first token after
40542 the initial pragma. */
40543
40544 static void
40545 cp_parser_initial_pragma (cp_token *first_token)
40546 {
40547 tree name = NULL;
40548
40549 cp_lexer_get_preprocessor_token (NULL, first_token);
40550 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
40551 return;
40552
40553 cp_lexer_get_preprocessor_token (NULL, first_token);
40554 if (first_token->type == CPP_STRING)
40555 {
40556 name = first_token->u.value;
40557
40558 cp_lexer_get_preprocessor_token (NULL, first_token);
40559 if (first_token->type != CPP_PRAGMA_EOL)
40560 error_at (first_token->location,
40561 "junk at end of %<#pragma GCC pch_preprocess%>");
40562 }
40563 else
40564 error_at (first_token->location, "expected string literal");
40565
40566 /* Skip to the end of the pragma. */
40567 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
40568 cp_lexer_get_preprocessor_token (NULL, first_token);
40569
40570 /* Now actually load the PCH file. */
40571 if (name)
40572 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
40573
40574 /* Read one more token to return to our caller. We have to do this
40575 after reading the PCH file in, since its pointers have to be
40576 live. */
40577 cp_lexer_get_preprocessor_token (NULL, first_token);
40578 }
40579
40580 /* Parse a pragma GCC ivdep. */
40581
40582 static bool
40583 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
40584 {
40585 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40586 return true;
40587 }
40588
40589 /* Parse a pragma GCC unroll. */
40590
40591 static unsigned short
40592 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
40593 {
40594 location_t location = cp_lexer_peek_token (parser->lexer)->location;
40595 tree expr = cp_parser_constant_expression (parser);
40596 unsigned short unroll;
40597 expr = maybe_constant_value (expr);
40598 HOST_WIDE_INT lunroll = 0;
40599 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
40600 || TREE_CODE (expr) != INTEGER_CST
40601 || (lunroll = tree_to_shwi (expr)) < 0
40602 || lunroll >= USHRT_MAX)
40603 {
40604 error_at (location, "%<#pragma GCC unroll%> requires an"
40605 " assignment-expression that evaluates to a non-negative"
40606 " integral constant less than %u", USHRT_MAX);
40607 unroll = 0;
40608 }
40609 else
40610 {
40611 unroll = (unsigned short)lunroll;
40612 if (unroll == 0)
40613 unroll = 1;
40614 }
40615 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40616 return unroll;
40617 }
40618
40619 /* Normal parsing of a pragma token. Here we can (and must) use the
40620 regular lexer. */
40621
40622 static bool
40623 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
40624 {
40625 cp_token *pragma_tok;
40626 unsigned int id;
40627 tree stmt;
40628 bool ret;
40629
40630 pragma_tok = cp_lexer_consume_token (parser->lexer);
40631 gcc_assert (pragma_tok->type == CPP_PRAGMA);
40632 parser->lexer->in_pragma = true;
40633
40634 id = cp_parser_pragma_kind (pragma_tok);
40635 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
40636 cp_ensure_no_omp_declare_simd (parser);
40637 switch (id)
40638 {
40639 case PRAGMA_GCC_PCH_PREPROCESS:
40640 error_at (pragma_tok->location,
40641 "%<#pragma GCC pch_preprocess%> must be first");
40642 break;
40643
40644 case PRAGMA_OMP_BARRIER:
40645 switch (context)
40646 {
40647 case pragma_compound:
40648 cp_parser_omp_barrier (parser, pragma_tok);
40649 return false;
40650 case pragma_stmt:
40651 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40652 "used in compound statements", "omp barrier");
40653 break;
40654 default:
40655 goto bad_stmt;
40656 }
40657 break;
40658
40659 case PRAGMA_OMP_DEPOBJ:
40660 switch (context)
40661 {
40662 case pragma_compound:
40663 cp_parser_omp_depobj (parser, pragma_tok);
40664 return false;
40665 case pragma_stmt:
40666 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40667 "used in compound statements", "omp depobj");
40668 break;
40669 default:
40670 goto bad_stmt;
40671 }
40672 break;
40673
40674 case PRAGMA_OMP_FLUSH:
40675 switch (context)
40676 {
40677 case pragma_compound:
40678 cp_parser_omp_flush (parser, pragma_tok);
40679 return false;
40680 case pragma_stmt:
40681 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40682 "used in compound statements", "omp flush");
40683 break;
40684 default:
40685 goto bad_stmt;
40686 }
40687 break;
40688
40689 case PRAGMA_OMP_TASKWAIT:
40690 switch (context)
40691 {
40692 case pragma_compound:
40693 cp_parser_omp_taskwait (parser, pragma_tok);
40694 return false;
40695 case pragma_stmt:
40696 error_at (pragma_tok->location,
40697 "%<#pragma %s%> may only be used in compound statements",
40698 "omp taskwait");
40699 break;
40700 default:
40701 goto bad_stmt;
40702 }
40703 break;
40704
40705 case PRAGMA_OMP_TASKYIELD:
40706 switch (context)
40707 {
40708 case pragma_compound:
40709 cp_parser_omp_taskyield (parser, pragma_tok);
40710 return false;
40711 case pragma_stmt:
40712 error_at (pragma_tok->location,
40713 "%<#pragma %s%> may only be used in compound statements",
40714 "omp taskyield");
40715 break;
40716 default:
40717 goto bad_stmt;
40718 }
40719 break;
40720
40721 case PRAGMA_OMP_CANCEL:
40722 switch (context)
40723 {
40724 case pragma_compound:
40725 cp_parser_omp_cancel (parser, pragma_tok);
40726 return false;
40727 case pragma_stmt:
40728 error_at (pragma_tok->location,
40729 "%<#pragma %s%> may only be used in compound statements",
40730 "omp cancel");
40731 break;
40732 default:
40733 goto bad_stmt;
40734 }
40735 break;
40736
40737 case PRAGMA_OMP_CANCELLATION_POINT:
40738 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
40739 return false;
40740
40741 case PRAGMA_OMP_THREADPRIVATE:
40742 cp_parser_omp_threadprivate (parser, pragma_tok);
40743 return false;
40744
40745 case PRAGMA_OMP_DECLARE:
40746 return cp_parser_omp_declare (parser, pragma_tok, context);
40747
40748 case PRAGMA_OACC_DECLARE:
40749 cp_parser_oacc_declare (parser, pragma_tok);
40750 return false;
40751
40752 case PRAGMA_OACC_ENTER_DATA:
40753 if (context == pragma_stmt)
40754 {
40755 error_at (pragma_tok->location,
40756 "%<#pragma %s%> may only be used in compound statements",
40757 "acc enter data");
40758 break;
40759 }
40760 else if (context != pragma_compound)
40761 goto bad_stmt;
40762 cp_parser_omp_construct (parser, pragma_tok, if_p);
40763 return true;
40764
40765 case PRAGMA_OACC_EXIT_DATA:
40766 if (context == pragma_stmt)
40767 {
40768 error_at (pragma_tok->location,
40769 "%<#pragma %s%> may only be used in compound statements",
40770 "acc exit data");
40771 break;
40772 }
40773 else if (context != pragma_compound)
40774 goto bad_stmt;
40775 cp_parser_omp_construct (parser, pragma_tok, if_p);
40776 return true;
40777
40778 case PRAGMA_OACC_ROUTINE:
40779 if (context != pragma_external)
40780 {
40781 error_at (pragma_tok->location,
40782 "%<#pragma acc routine%> must be at file scope");
40783 break;
40784 }
40785 cp_parser_oacc_routine (parser, pragma_tok, context);
40786 return false;
40787
40788 case PRAGMA_OACC_UPDATE:
40789 if (context == pragma_stmt)
40790 {
40791 error_at (pragma_tok->location,
40792 "%<#pragma %s%> may only be used in compound statements",
40793 "acc update");
40794 break;
40795 }
40796 else if (context != pragma_compound)
40797 goto bad_stmt;
40798 cp_parser_omp_construct (parser, pragma_tok, if_p);
40799 return true;
40800
40801 case PRAGMA_OACC_WAIT:
40802 if (context == pragma_stmt)
40803 {
40804 error_at (pragma_tok->location,
40805 "%<#pragma %s%> may only be used in compound statements",
40806 "acc wait");
40807 break;
40808 }
40809 else if (context != pragma_compound)
40810 goto bad_stmt;
40811 cp_parser_omp_construct (parser, pragma_tok, if_p);
40812 return true;
40813
40814 case PRAGMA_OACC_ATOMIC:
40815 case PRAGMA_OACC_CACHE:
40816 case PRAGMA_OACC_DATA:
40817 case PRAGMA_OACC_HOST_DATA:
40818 case PRAGMA_OACC_KERNELS:
40819 case PRAGMA_OACC_PARALLEL:
40820 case PRAGMA_OACC_LOOP:
40821 case PRAGMA_OMP_ATOMIC:
40822 case PRAGMA_OMP_CRITICAL:
40823 case PRAGMA_OMP_DISTRIBUTE:
40824 case PRAGMA_OMP_FOR:
40825 case PRAGMA_OMP_MASTER:
40826 case PRAGMA_OMP_PARALLEL:
40827 case PRAGMA_OMP_SECTIONS:
40828 case PRAGMA_OMP_SIMD:
40829 case PRAGMA_OMP_SINGLE:
40830 case PRAGMA_OMP_TASK:
40831 case PRAGMA_OMP_TASKGROUP:
40832 case PRAGMA_OMP_TASKLOOP:
40833 case PRAGMA_OMP_TEAMS:
40834 if (context != pragma_stmt && context != pragma_compound)
40835 goto bad_stmt;
40836 stmt = push_omp_privatization_clauses (false);
40837 cp_parser_omp_construct (parser, pragma_tok, if_p);
40838 pop_omp_privatization_clauses (stmt);
40839 return true;
40840
40841 case PRAGMA_OMP_REQUIRES:
40842 return cp_parser_omp_requires (parser, pragma_tok);
40843
40844 case PRAGMA_OMP_ORDERED:
40845 if (context != pragma_stmt && context != pragma_compound)
40846 goto bad_stmt;
40847 stmt = push_omp_privatization_clauses (false);
40848 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
40849 pop_omp_privatization_clauses (stmt);
40850 return ret;
40851
40852 case PRAGMA_OMP_TARGET:
40853 if (context != pragma_stmt && context != pragma_compound)
40854 goto bad_stmt;
40855 stmt = push_omp_privatization_clauses (false);
40856 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
40857 pop_omp_privatization_clauses (stmt);
40858 return ret;
40859
40860 case PRAGMA_OMP_END_DECLARE_TARGET:
40861 cp_parser_omp_end_declare_target (parser, pragma_tok);
40862 return false;
40863
40864 case PRAGMA_OMP_SECTION:
40865 error_at (pragma_tok->location,
40866 "%<#pragma omp section%> may only be used in "
40867 "%<#pragma omp sections%> construct");
40868 break;
40869
40870 case PRAGMA_IVDEP:
40871 {
40872 if (context == pragma_external)
40873 {
40874 error_at (pragma_tok->location,
40875 "%<#pragma GCC ivdep%> must be inside a function");
40876 break;
40877 }
40878 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
40879 unsigned short unroll;
40880 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40881 if (tok->type == CPP_PRAGMA
40882 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
40883 {
40884 tok = cp_lexer_consume_token (parser->lexer);
40885 unroll = cp_parser_pragma_unroll (parser, tok);
40886 tok = cp_lexer_peek_token (the_parser->lexer);
40887 }
40888 else
40889 unroll = 0;
40890 if (tok->type != CPP_KEYWORD
40891 || (tok->keyword != RID_FOR
40892 && tok->keyword != RID_WHILE
40893 && tok->keyword != RID_DO))
40894 {
40895 cp_parser_error (parser, "for, while or do statement expected");
40896 return false;
40897 }
40898 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40899 return true;
40900 }
40901
40902 case PRAGMA_UNROLL:
40903 {
40904 if (context == pragma_external)
40905 {
40906 error_at (pragma_tok->location,
40907 "%<#pragma GCC unroll%> must be inside a function");
40908 break;
40909 }
40910 const unsigned short unroll
40911 = cp_parser_pragma_unroll (parser, pragma_tok);
40912 bool ivdep;
40913 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40914 if (tok->type == CPP_PRAGMA
40915 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
40916 {
40917 tok = cp_lexer_consume_token (parser->lexer);
40918 ivdep = cp_parser_pragma_ivdep (parser, tok);
40919 tok = cp_lexer_peek_token (the_parser->lexer);
40920 }
40921 else
40922 ivdep = false;
40923 if (tok->type != CPP_KEYWORD
40924 || (tok->keyword != RID_FOR
40925 && tok->keyword != RID_WHILE
40926 && tok->keyword != RID_DO))
40927 {
40928 cp_parser_error (parser, "for, while or do statement expected");
40929 return false;
40930 }
40931 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40932 return true;
40933 }
40934
40935 default:
40936 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
40937 c_invoke_pragma_handler (id);
40938 break;
40939
40940 bad_stmt:
40941 cp_parser_error (parser, "expected declaration specifiers");
40942 break;
40943 }
40944
40945 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40946 return false;
40947 }
40948
40949 /* The interface the pragma parsers have to the lexer. */
40950
40951 enum cpp_ttype
40952 pragma_lex (tree *value, location_t *loc)
40953 {
40954 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40955 enum cpp_ttype ret = tok->type;
40956
40957 *value = tok->u.value;
40958 if (loc)
40959 *loc = tok->location;
40960
40961 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
40962 ret = CPP_EOF;
40963 else if (ret == CPP_STRING)
40964 *value = cp_parser_string_literal (the_parser, false, false);
40965 else
40966 {
40967 if (ret == CPP_KEYWORD)
40968 ret = CPP_NAME;
40969 cp_lexer_consume_token (the_parser->lexer);
40970 }
40971
40972 return ret;
40973 }
40974
40975 \f
40976 /* External interface. */
40977
40978 /* Parse one entire translation unit. */
40979
40980 void
40981 c_parse_file (void)
40982 {
40983 static bool already_called = false;
40984
40985 if (already_called)
40986 fatal_error (input_location,
40987 "inter-module optimizations not implemented for C++");
40988 already_called = true;
40989
40990 the_parser = cp_parser_new ();
40991 push_deferring_access_checks (flag_access_control
40992 ? dk_no_deferred : dk_no_check);
40993 cp_parser_translation_unit (the_parser);
40994 the_parser = NULL;
40995
40996 finish_translation_unit ();
40997 }
40998
40999 /* Create an identifier for a generic parameter type (a synthesized
41000 template parameter implied by `auto' or a concept identifier). */
41001
41002 static GTY(()) int generic_parm_count;
41003 static tree
41004 make_generic_type_name ()
41005 {
41006 char buf[32];
41007 sprintf (buf, "auto:%d", ++generic_parm_count);
41008 return get_identifier (buf);
41009 }
41010
41011 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
41012 (creating a new template parameter list if necessary). Returns the newly
41013 created template type parm. */
41014
41015 static tree
41016 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
41017 {
41018 gcc_assert (current_binding_level->kind == sk_function_parms);
41019
41020 /* Before committing to modifying any scope, if we're in an
41021 implicit template scope, and we're trying to synthesize a
41022 constrained parameter, try to find a previous parameter with
41023 the same name. This is the same-type rule for abbreviated
41024 function templates.
41025
41026 NOTE: We can generate implicit parameters when tentatively
41027 parsing a nested name specifier, only to reject that parse
41028 later. However, matching the same template-id as part of a
41029 direct-declarator should generate an identical template
41030 parameter, so this rule will merge them. */
41031 if (parser->implicit_template_scope && constr)
41032 {
41033 tree t = parser->implicit_template_parms;
41034 while (t)
41035 {
41036 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
41037 {
41038 tree d = TREE_VALUE (t);
41039 if (TREE_CODE (d) == PARM_DECL)
41040 /* Return the TEMPLATE_PARM_INDEX. */
41041 d = DECL_INITIAL (d);
41042 return d;
41043 }
41044 t = TREE_CHAIN (t);
41045 }
41046 }
41047
41048 /* We are either continuing a function template that already contains implicit
41049 template parameters, creating a new fully-implicit function template, or
41050 extending an existing explicit function template with implicit template
41051 parameters. */
41052
41053 cp_binding_level *const entry_scope = current_binding_level;
41054
41055 bool become_template = false;
41056 cp_binding_level *parent_scope = 0;
41057
41058 if (parser->implicit_template_scope)
41059 {
41060 gcc_assert (parser->implicit_template_parms);
41061
41062 current_binding_level = parser->implicit_template_scope;
41063 }
41064 else
41065 {
41066 /* Roll back to the existing template parameter scope (in the case of
41067 extending an explicit function template) or introduce a new template
41068 parameter scope ahead of the function parameter scope (or class scope
41069 in the case of out-of-line member definitions). The function scope is
41070 added back after template parameter synthesis below. */
41071
41072 cp_binding_level *scope = entry_scope;
41073
41074 while (scope->kind == sk_function_parms)
41075 {
41076 parent_scope = scope;
41077 scope = scope->level_chain;
41078 }
41079 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
41080 {
41081 /* If not defining a class, then any class scope is a scope level in
41082 an out-of-line member definition. In this case simply wind back
41083 beyond the first such scope to inject the template parameter list.
41084 Otherwise wind back to the class being defined. The latter can
41085 occur in class member friend declarations such as:
41086
41087 class A {
41088 void foo (auto);
41089 };
41090 class B {
41091 friend void A::foo (auto);
41092 };
41093
41094 The template parameter list synthesized for the friend declaration
41095 must be injected in the scope of 'B'. This can also occur in
41096 erroneous cases such as:
41097
41098 struct A {
41099 struct B {
41100 void foo (auto);
41101 };
41102 void B::foo (auto) {}
41103 };
41104
41105 Here the attempted definition of 'B::foo' within 'A' is ill-formed
41106 but, nevertheless, the template parameter list synthesized for the
41107 declarator should be injected into the scope of 'A' as if the
41108 ill-formed template was specified explicitly. */
41109
41110 while (scope->kind == sk_class && !scope->defining_class_p)
41111 {
41112 parent_scope = scope;
41113 scope = scope->level_chain;
41114 }
41115 }
41116
41117 current_binding_level = scope;
41118
41119 if (scope->kind != sk_template_parms
41120 || !function_being_declared_is_template_p (parser))
41121 {
41122 /* Introduce a new template parameter list for implicit template
41123 parameters. */
41124
41125 become_template = true;
41126
41127 parser->implicit_template_scope
41128 = begin_scope (sk_template_parms, NULL);
41129
41130 ++processing_template_decl;
41131
41132 parser->fully_implicit_function_template_p = true;
41133 ++parser->num_template_parameter_lists;
41134 }
41135 else
41136 {
41137 /* Synthesize implicit template parameters at the end of the explicit
41138 template parameter list. */
41139
41140 gcc_assert (current_template_parms);
41141
41142 parser->implicit_template_scope = scope;
41143
41144 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41145 parser->implicit_template_parms
41146 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
41147 }
41148 }
41149
41150 /* Synthesize a new template parameter and track the current template
41151 parameter chain with implicit_template_parms. */
41152
41153 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
41154 tree synth_id = make_generic_type_name ();
41155 tree synth_tmpl_parm;
41156 bool non_type = false;
41157
41158 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
41159 synth_tmpl_parm
41160 = finish_template_type_parm (class_type_node, synth_id);
41161 else if (TREE_CODE (proto) == TEMPLATE_DECL)
41162 synth_tmpl_parm
41163 = finish_constrained_template_template_parm (proto, synth_id);
41164 else
41165 {
41166 synth_tmpl_parm = copy_decl (proto);
41167 DECL_NAME (synth_tmpl_parm) = synth_id;
41168 non_type = true;
41169 }
41170
41171 // Attach the constraint to the parm before processing.
41172 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
41173 TREE_TYPE (node) = constr;
41174 tree new_parm
41175 = process_template_parm (parser->implicit_template_parms,
41176 input_location,
41177 node,
41178 /*non_type=*/non_type,
41179 /*param_pack=*/false);
41180
41181 // Chain the new parameter to the list of implicit parameters.
41182 if (parser->implicit_template_parms)
41183 parser->implicit_template_parms
41184 = TREE_CHAIN (parser->implicit_template_parms);
41185 else
41186 parser->implicit_template_parms = new_parm;
41187
41188 tree new_decl = get_local_decls ();
41189 if (non_type)
41190 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
41191 new_decl = DECL_INITIAL (new_decl);
41192
41193 /* If creating a fully implicit function template, start the new implicit
41194 template parameter list with this synthesized type, otherwise grow the
41195 current template parameter list. */
41196
41197 if (become_template)
41198 {
41199 parent_scope->level_chain = current_binding_level;
41200
41201 tree new_parms = make_tree_vec (1);
41202 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
41203 current_template_parms = tree_cons (size_int (processing_template_decl),
41204 new_parms, current_template_parms);
41205 }
41206 else
41207 {
41208 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41209 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
41210 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
41211 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
41212 }
41213
41214 // If the new parameter was constrained, we need to add that to the
41215 // constraints in the template parameter list.
41216 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
41217 {
41218 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
41219 reqs = conjoin_constraints (reqs, req);
41220 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
41221 }
41222
41223 current_binding_level = entry_scope;
41224
41225 return new_decl;
41226 }
41227
41228 /* Finish the declaration of a fully implicit function template. Such a
41229 template has no explicit template parameter list so has not been through the
41230 normal template head and tail processing. synthesize_implicit_template_parm
41231 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
41232 provided if the declaration is a class member such that its template
41233 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
41234 form is returned. Otherwise NULL_TREE is returned. */
41235
41236 static tree
41237 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
41238 {
41239 gcc_assert (parser->fully_implicit_function_template_p);
41240
41241 if (member_decl_opt && member_decl_opt != error_mark_node
41242 && DECL_VIRTUAL_P (member_decl_opt))
41243 {
41244 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
41245 "implicit templates may not be %<virtual%>");
41246 DECL_VIRTUAL_P (member_decl_opt) = false;
41247 }
41248
41249 if (member_decl_opt)
41250 member_decl_opt = finish_member_template_decl (member_decl_opt);
41251 end_template_decl ();
41252
41253 parser->fully_implicit_function_template_p = false;
41254 parser->implicit_template_parms = 0;
41255 parser->implicit_template_scope = 0;
41256 --parser->num_template_parameter_lists;
41257
41258 return member_decl_opt;
41259 }
41260
41261 /* Like finish_fully_implicit_template, but to be used in error
41262 recovery, rearranging scopes so that we restore the state we had
41263 before synthesize_implicit_template_parm inserted the implement
41264 template parms scope. */
41265
41266 static void
41267 abort_fully_implicit_template (cp_parser *parser)
41268 {
41269 cp_binding_level *return_to_scope = current_binding_level;
41270
41271 if (parser->implicit_template_scope
41272 && return_to_scope != parser->implicit_template_scope)
41273 {
41274 cp_binding_level *child = return_to_scope;
41275 for (cp_binding_level *scope = child->level_chain;
41276 scope != parser->implicit_template_scope;
41277 scope = child->level_chain)
41278 child = scope;
41279 child->level_chain = parser->implicit_template_scope->level_chain;
41280 parser->implicit_template_scope->level_chain = return_to_scope;
41281 current_binding_level = parser->implicit_template_scope;
41282 }
41283 else
41284 return_to_scope = return_to_scope->level_chain;
41285
41286 finish_fully_implicit_template (parser, NULL);
41287
41288 gcc_assert (current_binding_level == return_to_scope);
41289 }
41290
41291 /* Helper function for diagnostics that have complained about things
41292 being used with 'extern "C"' linkage.
41293
41294 Attempt to issue a note showing where the 'extern "C"' linkage began. */
41295
41296 void
41297 maybe_show_extern_c_location (void)
41298 {
41299 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
41300 inform (the_parser->innermost_linkage_specification_location,
41301 "%<extern \"C\"%> linkage started here");
41302 }
41303
41304 #include "gt-cp-parser.h"