]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
PR c++/88358 - name wrongly treated as type.
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
48
49 \f
50 /* The lexer. */
51
52 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53 and c-lex.c) and the C++ parser. */
54
55 static cp_token eof_token =
56 {
57 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
58 };
59
60 /* The various kinds of non integral constant we encounter. */
61 enum non_integral_constant {
62 NIC_NONE,
63 /* floating-point literal */
64 NIC_FLOAT,
65 /* %<this%> */
66 NIC_THIS,
67 /* %<__FUNCTION__%> */
68 NIC_FUNC_NAME,
69 /* %<__PRETTY_FUNCTION__%> */
70 NIC_PRETTY_FUNC,
71 /* %<__func__%> */
72 NIC_C99_FUNC,
73 /* "%<va_arg%> */
74 NIC_VA_ARG,
75 /* a cast */
76 NIC_CAST,
77 /* %<typeid%> operator */
78 NIC_TYPEID,
79 /* non-constant compound literals */
80 NIC_NCC,
81 /* a function call */
82 NIC_FUNC_CALL,
83 /* an increment */
84 NIC_INC,
85 /* an decrement */
86 NIC_DEC,
87 /* an array reference */
88 NIC_ARRAY_REF,
89 /* %<->%> */
90 NIC_ARROW,
91 /* %<.%> */
92 NIC_POINT,
93 /* the address of a label */
94 NIC_ADDR_LABEL,
95 /* %<*%> */
96 NIC_STAR,
97 /* %<&%> */
98 NIC_ADDR,
99 /* %<++%> */
100 NIC_PREINCREMENT,
101 /* %<--%> */
102 NIC_PREDECREMENT,
103 /* %<new%> */
104 NIC_NEW,
105 /* %<delete%> */
106 NIC_DEL,
107 /* calls to overloaded operators */
108 NIC_OVERLOADED,
109 /* an assignment */
110 NIC_ASSIGNMENT,
111 /* a comma operator */
112 NIC_COMMA,
113 /* a call to a constructor */
114 NIC_CONSTRUCTOR,
115 /* a transaction expression */
116 NIC_TRANSACTION
117 };
118
119 /* The various kinds of errors about name-lookup failing. */
120 enum name_lookup_error {
121 /* NULL */
122 NLE_NULL,
123 /* is not a type */
124 NLE_TYPE,
125 /* is not a class or namespace */
126 NLE_CXX98,
127 /* is not a class, namespace, or enumeration */
128 NLE_NOT_CXX98
129 };
130
131 /* The various kinds of required token */
132 enum required_token {
133 RT_NONE,
134 RT_SEMICOLON, /* ';' */
135 RT_OPEN_PAREN, /* '(' */
136 RT_CLOSE_BRACE, /* '}' */
137 RT_OPEN_BRACE, /* '{' */
138 RT_CLOSE_SQUARE, /* ']' */
139 RT_OPEN_SQUARE, /* '[' */
140 RT_COMMA, /* ',' */
141 RT_SCOPE, /* '::' */
142 RT_LESS, /* '<' */
143 RT_GREATER, /* '>' */
144 RT_EQ, /* '=' */
145 RT_ELLIPSIS, /* '...' */
146 RT_MULT, /* '*' */
147 RT_COMPL, /* '~' */
148 RT_COLON, /* ':' */
149 RT_COLON_SCOPE, /* ':' or '::' */
150 RT_CLOSE_PAREN, /* ')' */
151 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
152 RT_PRAGMA_EOL, /* end of line */
153 RT_NAME, /* identifier */
154
155 /* The type is CPP_KEYWORD */
156 RT_NEW, /* new */
157 RT_DELETE, /* delete */
158 RT_RETURN, /* return */
159 RT_WHILE, /* while */
160 RT_EXTERN, /* extern */
161 RT_STATIC_ASSERT, /* static_assert */
162 RT_DECLTYPE, /* decltype */
163 RT_OPERATOR, /* operator */
164 RT_CLASS, /* class */
165 RT_TEMPLATE, /* template */
166 RT_NAMESPACE, /* namespace */
167 RT_USING, /* using */
168 RT_ASM, /* asm */
169 RT_TRY, /* try */
170 RT_CATCH, /* catch */
171 RT_THROW, /* throw */
172 RT_LABEL, /* __label__ */
173 RT_AT_TRY, /* @try */
174 RT_AT_SYNCHRONIZED, /* @synchronized */
175 RT_AT_THROW, /* @throw */
176
177 RT_SELECT, /* selection-statement */
178 RT_ITERATION, /* iteration-statement */
179 RT_JUMP, /* jump-statement */
180 RT_CLASS_KEY, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL /* __transaction_cancel */
185 };
186
187 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
188 reverting it on destruction. */
189
190 class type_id_in_expr_sentinel
191 {
192 cp_parser *parser;
193 bool saved;
194 public:
195 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
196 : parser (parser),
197 saved (parser->in_type_id_in_expr_p)
198 { parser->in_type_id_in_expr_p = set; }
199 ~type_id_in_expr_sentinel ()
200 { parser->in_type_id_in_expr_p = saved; }
201 };
202
203 /* Prototypes. */
204
205 static cp_lexer *cp_lexer_new_main
206 (void);
207 static cp_lexer *cp_lexer_new_from_tokens
208 (cp_token_cache *tokens);
209 static void cp_lexer_destroy
210 (cp_lexer *);
211 static int cp_lexer_saving_tokens
212 (const cp_lexer *);
213 static cp_token *cp_lexer_token_at
214 (cp_lexer *, cp_token_position);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer *, cp_token *);
217 static inline cp_token *cp_lexer_peek_token
218 (cp_lexer *);
219 static cp_token *cp_lexer_peek_nth_token
220 (cp_lexer *, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer *, enum rid);
227 static cp_token *cp_lexer_consume_token
228 (cp_lexer *);
229 static void cp_lexer_purge_token
230 (cp_lexer *);
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer *, cp_token_position);
233 static void cp_lexer_save_tokens
234 (cp_lexer *);
235 static void cp_lexer_commit_tokens
236 (cp_lexer *);
237 static void cp_lexer_rollback_tokens
238 (cp_lexer *);
239 static void cp_lexer_print_token
240 (FILE *, cp_token *);
241 static inline bool cp_lexer_debugging_p
242 (cp_lexer *);
243 static void cp_lexer_start_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static void cp_lexer_stop_debugging
246 (cp_lexer *) ATTRIBUTE_UNUSED;
247
248 static cp_token_cache *cp_token_cache_new
249 (cp_token *, cp_token *);
250
251 static void cp_parser_initial_pragma
252 (cp_token *);
253
254 static bool cp_parser_omp_declare_reduction_exprs
255 (tree, cp_parser *);
256 static void cp_finalize_oacc_routine
257 (cp_parser *, tree, bool);
258
259 /* Manifest constants. */
260 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
261 #define CP_SAVED_TOKEN_STACK 5
262
263 /* Variables. */
264
265 /* The stream to which debugging output should be written. */
266 static FILE *cp_lexer_debug_stream;
267
268 /* Nonzero if we are parsing an unevaluated operand: an operand to
269 sizeof, typeof, or alignof. */
270 int cp_unevaluated_operand;
271
272 /* Dump up to NUM tokens in BUFFER to FILE starting with token
273 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
274 first token in BUFFER. If NUM is 0, dump all the tokens. If
275 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
276 highlighted by surrounding it in [[ ]]. */
277
278 static void
279 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
280 cp_token *start_token, unsigned num,
281 cp_token *curr_token)
282 {
283 unsigned i, nprinted;
284 cp_token *token;
285 bool do_print;
286
287 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
288
289 if (buffer == NULL)
290 return;
291
292 if (num == 0)
293 num = buffer->length ();
294
295 if (start_token == NULL)
296 start_token = buffer->address ();
297
298 if (start_token > buffer->address ())
299 {
300 cp_lexer_print_token (file, &(*buffer)[0]);
301 fprintf (file, " ... ");
302 }
303
304 do_print = false;
305 nprinted = 0;
306 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
307 {
308 if (token == start_token)
309 do_print = true;
310
311 if (!do_print)
312 continue;
313
314 nprinted++;
315 if (token == curr_token)
316 fprintf (file, "[[");
317
318 cp_lexer_print_token (file, token);
319
320 if (token == curr_token)
321 fprintf (file, "]]");
322
323 switch (token->type)
324 {
325 case CPP_SEMICOLON:
326 case CPP_OPEN_BRACE:
327 case CPP_CLOSE_BRACE:
328 case CPP_EOF:
329 fputc ('\n', file);
330 break;
331
332 default:
333 fputc (' ', file);
334 }
335 }
336
337 if (i == num && i < buffer->length ())
338 {
339 fprintf (file, " ... ");
340 cp_lexer_print_token (file, &buffer->last ());
341 }
342
343 fprintf (file, "\n");
344 }
345
346
347 /* Dump all tokens in BUFFER to stderr. */
348
349 void
350 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
351 {
352 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
353 }
354
355 DEBUG_FUNCTION void
356 debug (vec<cp_token, va_gc> &ref)
357 {
358 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
359 }
360
361 DEBUG_FUNCTION void
362 debug (vec<cp_token, va_gc> *ptr)
363 {
364 if (ptr)
365 debug (*ptr);
366 else
367 fprintf (stderr, "<nil>\n");
368 }
369
370
371 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
372 description for T. */
373
374 static void
375 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
376 {
377 if (t)
378 {
379 fprintf (file, "%s: ", desc);
380 print_node_brief (file, "", t, 0);
381 }
382 }
383
384
385 /* Dump parser context C to FILE. */
386
387 static void
388 cp_debug_print_context (FILE *file, cp_parser_context *c)
389 {
390 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
391 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
392 print_node_brief (file, "", c->object_type, 0);
393 fprintf (file, "}\n");
394 }
395
396
397 /* Print the stack of parsing contexts to FILE starting with FIRST. */
398
399 static void
400 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
401 {
402 unsigned i;
403 cp_parser_context *c;
404
405 fprintf (file, "Parsing context stack:\n");
406 for (i = 0, c = first; c; c = c->next, i++)
407 {
408 fprintf (file, "\t#%u: ", i);
409 cp_debug_print_context (file, c);
410 }
411 }
412
413
414 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
415
416 static void
417 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
418 {
419 if (flag)
420 fprintf (file, "%s: true\n", desc);
421 }
422
423
424 /* Print an unparsed function entry UF to FILE. */
425
426 static void
427 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
428 {
429 unsigned i;
430 cp_default_arg_entry *default_arg_fn;
431 tree fn;
432
433 fprintf (file, "\tFunctions with default args:\n");
434 for (i = 0;
435 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
436 i++)
437 {
438 fprintf (file, "\t\tClass type: ");
439 print_node_brief (file, "", default_arg_fn->class_type, 0);
440 fprintf (file, "\t\tDeclaration: ");
441 print_node_brief (file, "", default_arg_fn->decl, 0);
442 fprintf (file, "\n");
443 }
444
445 fprintf (file, "\n\tFunctions with definitions that require "
446 "post-processing\n\t\t");
447 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
448 {
449 print_node_brief (file, "", fn, 0);
450 fprintf (file, " ");
451 }
452 fprintf (file, "\n");
453
454 fprintf (file, "\n\tNon-static data members with initializers that require "
455 "post-processing\n\t\t");
456 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
457 {
458 print_node_brief (file, "", fn, 0);
459 fprintf (file, " ");
460 }
461 fprintf (file, "\n");
462 }
463
464
465 /* Print the stack of unparsed member functions S to FILE. */
466
467 static void
468 cp_debug_print_unparsed_queues (FILE *file,
469 vec<cp_unparsed_functions_entry, va_gc> *s)
470 {
471 unsigned i;
472 cp_unparsed_functions_entry *uf;
473
474 fprintf (file, "Unparsed functions\n");
475 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
476 {
477 fprintf (file, "#%u:\n", i);
478 cp_debug_print_unparsed_function (file, uf);
479 }
480 }
481
482
483 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
484 the given PARSER. If FILE is NULL, the output is printed on stderr. */
485
486 static void
487 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
488 {
489 cp_token *next_token, *first_token, *start_token;
490
491 if (file == NULL)
492 file = stderr;
493
494 next_token = parser->lexer->next_token;
495 first_token = parser->lexer->buffer->address ();
496 start_token = (next_token > first_token + window_size / 2)
497 ? next_token - window_size / 2
498 : first_token;
499 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
500 next_token);
501 }
502
503
504 /* Dump debugging information for the given PARSER. If FILE is NULL,
505 the output is printed on stderr. */
506
507 void
508 cp_debug_parser (FILE *file, cp_parser *parser)
509 {
510 const size_t window_size = 20;
511 cp_token *token;
512 expanded_location eloc;
513
514 if (file == NULL)
515 file = stderr;
516
517 fprintf (file, "Parser state\n\n");
518 fprintf (file, "Number of tokens: %u\n",
519 vec_safe_length (parser->lexer->buffer));
520 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
521 cp_debug_print_tree_if_set (file, "Object scope",
522 parser->object_scope);
523 cp_debug_print_tree_if_set (file, "Qualifying scope",
524 parser->qualifying_scope);
525 cp_debug_print_context_stack (file, parser->context);
526 cp_debug_print_flag (file, "Allow GNU extensions",
527 parser->allow_gnu_extensions_p);
528 cp_debug_print_flag (file, "'>' token is greater-than",
529 parser->greater_than_is_operator_p);
530 cp_debug_print_flag (file, "Default args allowed in current "
531 "parameter list", parser->default_arg_ok_p);
532 cp_debug_print_flag (file, "Parsing integral constant-expression",
533 parser->integral_constant_expression_p);
534 cp_debug_print_flag (file, "Allow non-constant expression in current "
535 "constant-expression",
536 parser->allow_non_integral_constant_expression_p);
537 cp_debug_print_flag (file, "Seen non-constant expression",
538 parser->non_integral_constant_expression_p);
539 cp_debug_print_flag (file, "Local names forbidden in current context",
540 (parser->local_variables_forbidden_p
541 & LOCAL_VARS_FORBIDDEN));
542 cp_debug_print_flag (file, "'this' forbidden in current context",
543 (parser->local_variables_forbidden_p
544 & THIS_FORBIDDEN));
545 cp_debug_print_flag (file, "In unbraced linkage specification",
546 parser->in_unbraced_linkage_specification_p);
547 cp_debug_print_flag (file, "Parsing a declarator",
548 parser->in_declarator_p);
549 cp_debug_print_flag (file, "In template argument list",
550 parser->in_template_argument_list_p);
551 cp_debug_print_flag (file, "Parsing an iteration statement",
552 parser->in_statement & IN_ITERATION_STMT);
553 cp_debug_print_flag (file, "Parsing a switch statement",
554 parser->in_statement & IN_SWITCH_STMT);
555 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
556 parser->in_statement & IN_OMP_BLOCK);
557 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
558 parser->in_statement & IN_OMP_FOR);
559 cp_debug_print_flag (file, "Parsing an if statement",
560 parser->in_statement & IN_IF_STMT);
561 cp_debug_print_flag (file, "Parsing a type-id in an expression "
562 "context", parser->in_type_id_in_expr_p);
563 cp_debug_print_flag (file, "String expressions should be translated "
564 "to execution character set",
565 parser->translate_strings_p);
566 cp_debug_print_flag (file, "Parsing function body outside of a "
567 "local class", parser->in_function_body);
568 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
569 parser->colon_corrects_to_scope_p);
570 cp_debug_print_flag (file, "Colon doesn't start a class definition",
571 parser->colon_doesnt_start_class_def_p);
572 if (parser->type_definition_forbidden_message)
573 fprintf (file, "Error message for forbidden type definitions: %s\n",
574 parser->type_definition_forbidden_message);
575 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
576 fprintf (file, "Number of class definitions in progress: %u\n",
577 parser->num_classes_being_defined);
578 fprintf (file, "Number of template parameter lists for the current "
579 "declaration: %u\n", parser->num_template_parameter_lists);
580 cp_debug_parser_tokens (file, parser, window_size);
581 token = parser->lexer->next_token;
582 fprintf (file, "Next token to parse:\n");
583 fprintf (file, "\tToken: ");
584 cp_lexer_print_token (file, token);
585 eloc = expand_location (token->location);
586 fprintf (file, "\n\tFile: %s\n", eloc.file);
587 fprintf (file, "\tLine: %d\n", eloc.line);
588 fprintf (file, "\tColumn: %d\n", eloc.column);
589 }
590
591 DEBUG_FUNCTION void
592 debug (cp_parser &ref)
593 {
594 cp_debug_parser (stderr, &ref);
595 }
596
597 DEBUG_FUNCTION void
598 debug (cp_parser *ptr)
599 {
600 if (ptr)
601 debug (*ptr);
602 else
603 fprintf (stderr, "<nil>\n");
604 }
605
606 /* Allocate memory for a new lexer object and return it. */
607
608 static cp_lexer *
609 cp_lexer_alloc (void)
610 {
611 cp_lexer *lexer;
612
613 c_common_no_more_pch ();
614
615 /* Allocate the memory. */
616 lexer = ggc_cleared_alloc<cp_lexer> ();
617
618 /* Initially we are not debugging. */
619 lexer->debugging_p = false;
620
621 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
622
623 /* Create the buffer. */
624 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
625
626 return lexer;
627 }
628
629
630 /* Create a new main C++ lexer, the lexer that gets tokens from the
631 preprocessor. */
632
633 static cp_lexer *
634 cp_lexer_new_main (void)
635 {
636 cp_lexer *lexer;
637 cp_token token;
638
639 /* It's possible that parsing the first pragma will load a PCH file,
640 which is a GC collection point. So we have to do that before
641 allocating any memory. */
642 cp_parser_initial_pragma (&token);
643
644 lexer = cp_lexer_alloc ();
645
646 /* Put the first token in the buffer. */
647 lexer->buffer->quick_push (token);
648
649 /* Get the remaining tokens from the preprocessor. */
650 while (token.type != CPP_EOF)
651 {
652 cp_lexer_get_preprocessor_token (lexer, &token);
653 vec_safe_push (lexer->buffer, token);
654 }
655
656 lexer->last_token = lexer->buffer->address ()
657 + lexer->buffer->length ()
658 - 1;
659 lexer->next_token = lexer->buffer->length ()
660 ? lexer->buffer->address ()
661 : &eof_token;
662
663 /* Subsequent preprocessor diagnostics should use compiler
664 diagnostic functions to get the compiler source location. */
665 done_lexing = true;
666
667 gcc_assert (!lexer->next_token->purged_p);
668 return lexer;
669 }
670
671 /* Create a new lexer whose token stream is primed with the tokens in
672 CACHE. When these tokens are exhausted, no new tokens will be read. */
673
674 static cp_lexer *
675 cp_lexer_new_from_tokens (cp_token_cache *cache)
676 {
677 cp_token *first = cache->first;
678 cp_token *last = cache->last;
679 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
680
681 /* We do not own the buffer. */
682 lexer->buffer = NULL;
683 lexer->next_token = first == last ? &eof_token : first;
684 lexer->last_token = last;
685
686 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
687
688 /* Initially we are not debugging. */
689 lexer->debugging_p = false;
690
691 gcc_assert (!lexer->next_token->purged_p);
692 return lexer;
693 }
694
695 /* Frees all resources associated with LEXER. */
696
697 static void
698 cp_lexer_destroy (cp_lexer *lexer)
699 {
700 vec_free (lexer->buffer);
701 lexer->saved_tokens.release ();
702 ggc_free (lexer);
703 }
704
705 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
706 be used. The point of this flag is to help the compiler to fold away calls
707 to cp_lexer_debugging_p within this source file at compile time, when the
708 lexer is not being debugged. */
709
710 #define LEXER_DEBUGGING_ENABLED_P false
711
712 /* Returns nonzero if debugging information should be output. */
713
714 static inline bool
715 cp_lexer_debugging_p (cp_lexer *lexer)
716 {
717 if (!LEXER_DEBUGGING_ENABLED_P)
718 return false;
719
720 return lexer->debugging_p;
721 }
722
723
724 static inline cp_token_position
725 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
726 {
727 gcc_assert (!previous_p || lexer->next_token != &eof_token);
728
729 return lexer->next_token - previous_p;
730 }
731
732 static inline cp_token *
733 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
734 {
735 return pos;
736 }
737
738 static inline void
739 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
740 {
741 lexer->next_token = cp_lexer_token_at (lexer, pos);
742 }
743
744 static inline cp_token_position
745 cp_lexer_previous_token_position (cp_lexer *lexer)
746 {
747 if (lexer->next_token == &eof_token)
748 return lexer->last_token - 1;
749 else
750 return cp_lexer_token_position (lexer, true);
751 }
752
753 static inline cp_token *
754 cp_lexer_previous_token (cp_lexer *lexer)
755 {
756 cp_token_position tp = cp_lexer_previous_token_position (lexer);
757
758 /* Skip past purged tokens. */
759 while (tp->purged_p)
760 {
761 gcc_assert (tp != vec_safe_address (lexer->buffer));
762 tp--;
763 }
764
765 return cp_lexer_token_at (lexer, tp);
766 }
767
768 /* nonzero if we are presently saving tokens. */
769
770 static inline int
771 cp_lexer_saving_tokens (const cp_lexer* lexer)
772 {
773 return lexer->saved_tokens.length () != 0;
774 }
775
776 /* Store the next token from the preprocessor in *TOKEN. Return true
777 if we reach EOF. If LEXER is NULL, assume we are handling an
778 initial #pragma pch_preprocess, and thus want the lexer to return
779 processed strings. */
780
781 static void
782 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
783 {
784 static int is_extern_c = 0;
785
786 /* Get a new token from the preprocessor. */
787 token->type
788 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
789 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
790 token->keyword = RID_MAX;
791 token->purged_p = false;
792 token->error_reported = false;
793
794 /* On some systems, some header files are surrounded by an
795 implicit extern "C" block. Set a flag in the token if it
796 comes from such a header. */
797 is_extern_c += pending_lang_change;
798 pending_lang_change = 0;
799 token->implicit_extern_c = is_extern_c > 0;
800
801 /* Check to see if this token is a keyword. */
802 if (token->type == CPP_NAME)
803 {
804 if (IDENTIFIER_KEYWORD_P (token->u.value))
805 {
806 /* Mark this token as a keyword. */
807 token->type = CPP_KEYWORD;
808 /* Record which keyword. */
809 token->keyword = C_RID_CODE (token->u.value);
810 }
811 else
812 {
813 if (warn_cxx11_compat
814 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
815 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
816 {
817 /* Warn about the C++0x keyword (but still treat it as
818 an identifier). */
819 warning (OPT_Wc__11_compat,
820 "identifier %qE is a keyword in C++11",
821 token->u.value);
822
823 /* Clear out the C_RID_CODE so we don't warn about this
824 particular identifier-turned-keyword again. */
825 C_SET_RID_CODE (token->u.value, RID_MAX);
826 }
827
828 token->keyword = RID_MAX;
829 }
830 }
831 else if (token->type == CPP_AT_NAME)
832 {
833 /* This only happens in Objective-C++; it must be a keyword. */
834 token->type = CPP_KEYWORD;
835 switch (C_RID_CODE (token->u.value))
836 {
837 /* Replace 'class' with '@class', 'private' with '@private',
838 etc. This prevents confusion with the C++ keyword
839 'class', and makes the tokens consistent with other
840 Objective-C 'AT' keywords. For example '@class' is
841 reported as RID_AT_CLASS which is consistent with
842 '@synchronized', which is reported as
843 RID_AT_SYNCHRONIZED.
844 */
845 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
846 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
847 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
848 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
849 case RID_THROW: token->keyword = RID_AT_THROW; break;
850 case RID_TRY: token->keyword = RID_AT_TRY; break;
851 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
852 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
853 default: token->keyword = C_RID_CODE (token->u.value);
854 }
855 }
856 }
857
858 /* Update the globals input_location and the input file stack from TOKEN. */
859 static inline void
860 cp_lexer_set_source_position_from_token (cp_token *token)
861 {
862 if (token->type != CPP_EOF)
863 {
864 input_location = token->location;
865 }
866 }
867
868 /* Update the globals input_location and the input file stack from LEXER. */
869 static inline void
870 cp_lexer_set_source_position (cp_lexer *lexer)
871 {
872 cp_token *token = cp_lexer_peek_token (lexer);
873 cp_lexer_set_source_position_from_token (token);
874 }
875
876 /* Return a pointer to the next token in the token stream, but do not
877 consume it. */
878
879 static inline cp_token *
880 cp_lexer_peek_token (cp_lexer *lexer)
881 {
882 if (cp_lexer_debugging_p (lexer))
883 {
884 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
885 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
886 putc ('\n', cp_lexer_debug_stream);
887 }
888 return lexer->next_token;
889 }
890
891 /* Return true if the next token has the indicated TYPE. */
892
893 static inline bool
894 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
895 {
896 return cp_lexer_peek_token (lexer)->type == type;
897 }
898
899 /* Return true if the next token does not have the indicated TYPE. */
900
901 static inline bool
902 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
903 {
904 return !cp_lexer_next_token_is (lexer, type);
905 }
906
907 /* Return true if the next token is the indicated KEYWORD. */
908
909 static inline bool
910 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
911 {
912 return cp_lexer_peek_token (lexer)->keyword == keyword;
913 }
914
915 static inline bool
916 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
917 {
918 return cp_lexer_peek_nth_token (lexer, n)->type == type;
919 }
920
921 static inline bool
922 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
923 {
924 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
925 }
926
927 /* Return true if KEYWORD can start a decl-specifier. */
928
929 bool
930 cp_keyword_starts_decl_specifier_p (enum rid keyword)
931 {
932 switch (keyword)
933 {
934 /* auto specifier: storage-class-specifier in C++,
935 simple-type-specifier in C++0x. */
936 case RID_AUTO:
937 /* Storage classes. */
938 case RID_REGISTER:
939 case RID_STATIC:
940 case RID_EXTERN:
941 case RID_MUTABLE:
942 case RID_THREAD:
943 /* Elaborated type specifiers. */
944 case RID_ENUM:
945 case RID_CLASS:
946 case RID_STRUCT:
947 case RID_UNION:
948 case RID_TYPENAME:
949 /* Simple type specifiers. */
950 case RID_CHAR:
951 case RID_CHAR8:
952 case RID_CHAR16:
953 case RID_CHAR32:
954 case RID_WCHAR:
955 case RID_BOOL:
956 case RID_SHORT:
957 case RID_INT:
958 case RID_LONG:
959 case RID_SIGNED:
960 case RID_UNSIGNED:
961 case RID_FLOAT:
962 case RID_DOUBLE:
963 case RID_VOID:
964 /* GNU extensions. */
965 case RID_ATTRIBUTE:
966 case RID_TYPEOF:
967 /* C++0x extensions. */
968 case RID_DECLTYPE:
969 case RID_UNDERLYING_TYPE:
970 case RID_CONSTEXPR:
971 return true;
972
973 default:
974 if (keyword >= RID_FIRST_INT_N
975 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
976 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
977 return true;
978 return false;
979 }
980 }
981
982 /* Return true if the next token is a keyword for a decl-specifier. */
983
984 static bool
985 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
986 {
987 cp_token *token;
988
989 token = cp_lexer_peek_token (lexer);
990 return cp_keyword_starts_decl_specifier_p (token->keyword);
991 }
992
993 /* Returns TRUE iff the token T begins a decltype type. */
994
995 static bool
996 token_is_decltype (cp_token *t)
997 {
998 return (t->keyword == RID_DECLTYPE
999 || t->type == CPP_DECLTYPE);
1000 }
1001
1002 /* Returns TRUE iff the next token begins a decltype type. */
1003
1004 static bool
1005 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1006 {
1007 cp_token *t = cp_lexer_peek_token (lexer);
1008 return token_is_decltype (t);
1009 }
1010
1011 /* Called when processing a token with tree_check_value; perform or defer the
1012 associated checks and return the value. */
1013
1014 static tree
1015 saved_checks_value (struct tree_check *check_value)
1016 {
1017 /* Perform any access checks that were deferred. */
1018 vec<deferred_access_check, va_gc> *checks;
1019 deferred_access_check *chk;
1020 checks = check_value->checks;
1021 if (checks)
1022 {
1023 int i;
1024 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1025 perform_or_defer_access_check (chk->binfo,
1026 chk->decl,
1027 chk->diag_decl, tf_warning_or_error);
1028 }
1029 /* Return the stored value. */
1030 return check_value->value;
1031 }
1032
1033 /* Return a pointer to the Nth token in the token stream. If N is 1,
1034 then this is precisely equivalent to cp_lexer_peek_token (except
1035 that it is not inline). One would like to disallow that case, but
1036 there is one case (cp_parser_nth_token_starts_template_id) where
1037 the caller passes a variable for N and it might be 1. */
1038
1039 static cp_token *
1040 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1041 {
1042 cp_token *token;
1043
1044 /* N is 1-based, not zero-based. */
1045 gcc_assert (n > 0);
1046
1047 if (cp_lexer_debugging_p (lexer))
1048 fprintf (cp_lexer_debug_stream,
1049 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1050
1051 --n;
1052 token = lexer->next_token;
1053 gcc_assert (!n || token != &eof_token);
1054 while (n != 0)
1055 {
1056 ++token;
1057 if (token == lexer->last_token)
1058 {
1059 token = &eof_token;
1060 break;
1061 }
1062
1063 if (!token->purged_p)
1064 --n;
1065 }
1066
1067 if (cp_lexer_debugging_p (lexer))
1068 {
1069 cp_lexer_print_token (cp_lexer_debug_stream, token);
1070 putc ('\n', cp_lexer_debug_stream);
1071 }
1072
1073 return token;
1074 }
1075
1076 /* Return the next token, and advance the lexer's next_token pointer
1077 to point to the next non-purged token. */
1078
1079 static cp_token *
1080 cp_lexer_consume_token (cp_lexer* lexer)
1081 {
1082 cp_token *token = lexer->next_token;
1083
1084 gcc_assert (token != &eof_token);
1085 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1086
1087 do
1088 {
1089 lexer->next_token++;
1090 if (lexer->next_token == lexer->last_token)
1091 {
1092 lexer->next_token = &eof_token;
1093 break;
1094 }
1095
1096 }
1097 while (lexer->next_token->purged_p);
1098
1099 cp_lexer_set_source_position_from_token (token);
1100
1101 /* Provide debugging output. */
1102 if (cp_lexer_debugging_p (lexer))
1103 {
1104 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1105 cp_lexer_print_token (cp_lexer_debug_stream, token);
1106 putc ('\n', cp_lexer_debug_stream);
1107 }
1108
1109 return token;
1110 }
1111
1112 /* Permanently remove the next token from the token stream, and
1113 advance the next_token pointer to refer to the next non-purged
1114 token. */
1115
1116 static void
1117 cp_lexer_purge_token (cp_lexer *lexer)
1118 {
1119 cp_token *tok = lexer->next_token;
1120
1121 gcc_assert (tok != &eof_token);
1122 tok->purged_p = true;
1123 tok->location = UNKNOWN_LOCATION;
1124 tok->u.value = NULL_TREE;
1125 tok->keyword = RID_MAX;
1126
1127 do
1128 {
1129 tok++;
1130 if (tok == lexer->last_token)
1131 {
1132 tok = &eof_token;
1133 break;
1134 }
1135 }
1136 while (tok->purged_p);
1137 lexer->next_token = tok;
1138 }
1139
1140 /* Permanently remove all tokens after TOK, up to, but not
1141 including, the token that will be returned next by
1142 cp_lexer_peek_token. */
1143
1144 static void
1145 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1146 {
1147 cp_token *peek = lexer->next_token;
1148
1149 if (peek == &eof_token)
1150 peek = lexer->last_token;
1151
1152 gcc_assert (tok < peek);
1153
1154 for ( tok += 1; tok != peek; tok += 1)
1155 {
1156 tok->purged_p = true;
1157 tok->location = UNKNOWN_LOCATION;
1158 tok->u.value = NULL_TREE;
1159 tok->keyword = RID_MAX;
1160 }
1161 }
1162
1163 /* Begin saving tokens. All tokens consumed after this point will be
1164 preserved. */
1165
1166 static void
1167 cp_lexer_save_tokens (cp_lexer* lexer)
1168 {
1169 /* Provide debugging output. */
1170 if (cp_lexer_debugging_p (lexer))
1171 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1172
1173 lexer->saved_tokens.safe_push (lexer->next_token);
1174 }
1175
1176 /* Commit to the portion of the token stream most recently saved. */
1177
1178 static void
1179 cp_lexer_commit_tokens (cp_lexer* lexer)
1180 {
1181 /* Provide debugging output. */
1182 if (cp_lexer_debugging_p (lexer))
1183 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1184
1185 lexer->saved_tokens.pop ();
1186 }
1187
1188 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1189 to the token stream. Stop saving tokens. */
1190
1191 static void
1192 cp_lexer_rollback_tokens (cp_lexer* lexer)
1193 {
1194 /* Provide debugging output. */
1195 if (cp_lexer_debugging_p (lexer))
1196 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1197
1198 lexer->next_token = lexer->saved_tokens.pop ();
1199 }
1200
1201 /* RAII wrapper around the above functions, with sanity checking. Creating
1202 a variable saves tokens, which are committed when the variable is
1203 destroyed unless they are explicitly rolled back by calling the rollback
1204 member function. */
1205
1206 struct saved_token_sentinel
1207 {
1208 cp_lexer *lexer;
1209 unsigned len;
1210 bool commit;
1211 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1212 {
1213 len = lexer->saved_tokens.length ();
1214 cp_lexer_save_tokens (lexer);
1215 }
1216 void rollback ()
1217 {
1218 cp_lexer_rollback_tokens (lexer);
1219 commit = false;
1220 }
1221 ~saved_token_sentinel()
1222 {
1223 if (commit)
1224 cp_lexer_commit_tokens (lexer);
1225 gcc_assert (lexer->saved_tokens.length () == len);
1226 }
1227 };
1228
1229 /* Print a representation of the TOKEN on the STREAM. */
1230
1231 static void
1232 cp_lexer_print_token (FILE * stream, cp_token *token)
1233 {
1234 /* We don't use cpp_type2name here because the parser defines
1235 a few tokens of its own. */
1236 static const char *const token_names[] = {
1237 /* cpplib-defined token types */
1238 #define OP(e, s) #e,
1239 #define TK(e, s) #e,
1240 TTYPE_TABLE
1241 #undef OP
1242 #undef TK
1243 /* C++ parser token types - see "Manifest constants", above. */
1244 "KEYWORD",
1245 "TEMPLATE_ID",
1246 "NESTED_NAME_SPECIFIER",
1247 };
1248
1249 /* For some tokens, print the associated data. */
1250 switch (token->type)
1251 {
1252 case CPP_KEYWORD:
1253 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1254 For example, `struct' is mapped to an INTEGER_CST. */
1255 if (!identifier_p (token->u.value))
1256 break;
1257 /* fall through */
1258 case CPP_NAME:
1259 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1260 break;
1261
1262 case CPP_STRING:
1263 case CPP_STRING16:
1264 case CPP_STRING32:
1265 case CPP_WSTRING:
1266 case CPP_UTF8STRING:
1267 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1268 break;
1269
1270 case CPP_NUMBER:
1271 print_generic_expr (stream, token->u.value);
1272 break;
1273
1274 default:
1275 /* If we have a name for the token, print it out. Otherwise, we
1276 simply give the numeric code. */
1277 if (token->type < ARRAY_SIZE(token_names))
1278 fputs (token_names[token->type], stream);
1279 else
1280 fprintf (stream, "[%d]", token->type);
1281 break;
1282 }
1283 }
1284
1285 DEBUG_FUNCTION void
1286 debug (cp_token &ref)
1287 {
1288 cp_lexer_print_token (stderr, &ref);
1289 fprintf (stderr, "\n");
1290 }
1291
1292 DEBUG_FUNCTION void
1293 debug (cp_token *ptr)
1294 {
1295 if (ptr)
1296 debug (*ptr);
1297 else
1298 fprintf (stderr, "<nil>\n");
1299 }
1300
1301
1302 /* Start emitting debugging information. */
1303
1304 static void
1305 cp_lexer_start_debugging (cp_lexer* lexer)
1306 {
1307 if (!LEXER_DEBUGGING_ENABLED_P)
1308 fatal_error (input_location,
1309 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1310
1311 lexer->debugging_p = true;
1312 cp_lexer_debug_stream = stderr;
1313 }
1314
1315 /* Stop emitting debugging information. */
1316
1317 static void
1318 cp_lexer_stop_debugging (cp_lexer* lexer)
1319 {
1320 if (!LEXER_DEBUGGING_ENABLED_P)
1321 fatal_error (input_location,
1322 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1323
1324 lexer->debugging_p = false;
1325 cp_lexer_debug_stream = NULL;
1326 }
1327
1328 /* Create a new cp_token_cache, representing a range of tokens. */
1329
1330 static cp_token_cache *
1331 cp_token_cache_new (cp_token *first, cp_token *last)
1332 {
1333 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1334 cache->first = first;
1335 cache->last = last;
1336 return cache;
1337 }
1338
1339 /* Diagnose if #pragma omp declare simd isn't followed immediately
1340 by function declaration or definition. */
1341
1342 static inline void
1343 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1344 {
1345 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1346 {
1347 error ("%<#pragma omp declare simd%> not immediately followed by "
1348 "function declaration or definition");
1349 parser->omp_declare_simd = NULL;
1350 }
1351 }
1352
1353 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1354 and put that into "omp declare simd" attribute. */
1355
1356 static inline void
1357 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1358 {
1359 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1360 {
1361 if (fndecl == error_mark_node)
1362 {
1363 parser->omp_declare_simd = NULL;
1364 return;
1365 }
1366 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1367 {
1368 cp_ensure_no_omp_declare_simd (parser);
1369 return;
1370 }
1371 }
1372 }
1373
1374 /* Diagnose if #pragma acc routine isn't followed immediately by function
1375 declaration or definition. */
1376
1377 static inline void
1378 cp_ensure_no_oacc_routine (cp_parser *parser)
1379 {
1380 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1381 {
1382 error_at (parser->oacc_routine->loc,
1383 "%<#pragma acc routine%> not immediately followed by "
1384 "function declaration or definition");
1385 parser->oacc_routine = NULL;
1386 }
1387 }
1388 \f
1389 /* Decl-specifiers. */
1390
1391 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1392
1393 static void
1394 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1395 {
1396 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1397 }
1398
1399 /* Declarators. */
1400
1401 /* Nothing other than the parser should be creating declarators;
1402 declarators are a semi-syntactic representation of C++ entities.
1403 Other parts of the front end that need to create entities (like
1404 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1405
1406 static cp_declarator *make_call_declarator
1407 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1408 static cp_declarator *make_array_declarator
1409 (cp_declarator *, tree);
1410 static cp_declarator *make_pointer_declarator
1411 (cp_cv_quals, cp_declarator *, tree);
1412 static cp_declarator *make_reference_declarator
1413 (cp_cv_quals, cp_declarator *, bool, tree);
1414 static cp_declarator *make_ptrmem_declarator
1415 (cp_cv_quals, tree, cp_declarator *, tree);
1416
1417 /* An erroneous declarator. */
1418 static cp_declarator *cp_error_declarator;
1419
1420 /* The obstack on which declarators and related data structures are
1421 allocated. */
1422 static struct obstack declarator_obstack;
1423
1424 /* Alloc BYTES from the declarator memory pool. */
1425
1426 static inline void *
1427 alloc_declarator (size_t bytes)
1428 {
1429 return obstack_alloc (&declarator_obstack, bytes);
1430 }
1431
1432 /* Allocate a declarator of the indicated KIND. Clear fields that are
1433 common to all declarators. */
1434
1435 static cp_declarator *
1436 make_declarator (cp_declarator_kind kind)
1437 {
1438 cp_declarator *declarator;
1439
1440 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1441 declarator->kind = kind;
1442 declarator->parenthesized = UNKNOWN_LOCATION;
1443 declarator->attributes = NULL_TREE;
1444 declarator->std_attributes = NULL_TREE;
1445 declarator->declarator = NULL;
1446 declarator->parameter_pack_p = false;
1447 declarator->id_loc = UNKNOWN_LOCATION;
1448
1449 return declarator;
1450 }
1451
1452 /* Make a declarator for a generalized identifier. If
1453 QUALIFYING_SCOPE is non-NULL, the identifier is
1454 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1455 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1456 is, if any. */
1457
1458 static cp_declarator *
1459 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1460 special_function_kind sfk, location_t id_location)
1461 {
1462 cp_declarator *declarator;
1463
1464 /* It is valid to write:
1465
1466 class C { void f(); };
1467 typedef C D;
1468 void D::f();
1469
1470 The standard is not clear about whether `typedef const C D' is
1471 legal; as of 2002-09-15 the committee is considering that
1472 question. EDG 3.0 allows that syntax. Therefore, we do as
1473 well. */
1474 if (qualifying_scope && TYPE_P (qualifying_scope))
1475 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1476
1477 gcc_assert (identifier_p (unqualified_name)
1478 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1479 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1480
1481 declarator = make_declarator (cdk_id);
1482 declarator->u.id.qualifying_scope = qualifying_scope;
1483 declarator->u.id.unqualified_name = unqualified_name;
1484 declarator->u.id.sfk = sfk;
1485 declarator->id_loc = id_location;
1486
1487 return declarator;
1488 }
1489
1490 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1491 of modifiers such as const or volatile to apply to the pointer
1492 type, represented as identifiers. ATTRIBUTES represent the attributes that
1493 appertain to the pointer or reference. */
1494
1495 cp_declarator *
1496 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1497 tree attributes)
1498 {
1499 cp_declarator *declarator;
1500
1501 declarator = make_declarator (cdk_pointer);
1502 declarator->declarator = target;
1503 declarator->u.pointer.qualifiers = cv_qualifiers;
1504 declarator->u.pointer.class_type = NULL_TREE;
1505 if (target)
1506 {
1507 declarator->id_loc = target->id_loc;
1508 declarator->parameter_pack_p = target->parameter_pack_p;
1509 target->parameter_pack_p = false;
1510 }
1511 else
1512 declarator->parameter_pack_p = false;
1513
1514 declarator->std_attributes = attributes;
1515
1516 return declarator;
1517 }
1518
1519 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1520 represent the attributes that appertain to the pointer or
1521 reference. */
1522
1523 cp_declarator *
1524 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1525 bool rvalue_ref, tree attributes)
1526 {
1527 cp_declarator *declarator;
1528
1529 declarator = make_declarator (cdk_reference);
1530 declarator->declarator = target;
1531 declarator->u.reference.qualifiers = cv_qualifiers;
1532 declarator->u.reference.rvalue_ref = rvalue_ref;
1533 if (target)
1534 {
1535 declarator->id_loc = target->id_loc;
1536 declarator->parameter_pack_p = target->parameter_pack_p;
1537 target->parameter_pack_p = false;
1538 }
1539 else
1540 declarator->parameter_pack_p = false;
1541
1542 declarator->std_attributes = attributes;
1543
1544 return declarator;
1545 }
1546
1547 /* Like make_pointer_declarator -- but for a pointer to a non-static
1548 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1549 appertain to the pointer or reference. */
1550
1551 cp_declarator *
1552 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1553 cp_declarator *pointee,
1554 tree attributes)
1555 {
1556 cp_declarator *declarator;
1557
1558 declarator = make_declarator (cdk_ptrmem);
1559 declarator->declarator = pointee;
1560 declarator->u.pointer.qualifiers = cv_qualifiers;
1561 declarator->u.pointer.class_type = class_type;
1562
1563 if (pointee)
1564 {
1565 declarator->parameter_pack_p = pointee->parameter_pack_p;
1566 pointee->parameter_pack_p = false;
1567 }
1568 else
1569 declarator->parameter_pack_p = false;
1570
1571 declarator->std_attributes = attributes;
1572
1573 return declarator;
1574 }
1575
1576 /* Make a declarator for the function given by TARGET, with the
1577 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1578 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1579 indicates what exceptions can be thrown. */
1580
1581 cp_declarator *
1582 make_call_declarator (cp_declarator *target,
1583 tree parms,
1584 cp_cv_quals cv_qualifiers,
1585 cp_virt_specifiers virt_specifiers,
1586 cp_ref_qualifier ref_qualifier,
1587 tree tx_qualifier,
1588 tree exception_specification,
1589 tree late_return_type,
1590 tree requires_clause)
1591 {
1592 cp_declarator *declarator;
1593
1594 declarator = make_declarator (cdk_function);
1595 declarator->declarator = target;
1596 declarator->u.function.parameters = parms;
1597 declarator->u.function.qualifiers = cv_qualifiers;
1598 declarator->u.function.virt_specifiers = virt_specifiers;
1599 declarator->u.function.ref_qualifier = ref_qualifier;
1600 declarator->u.function.tx_qualifier = tx_qualifier;
1601 declarator->u.function.exception_specification = exception_specification;
1602 declarator->u.function.late_return_type = late_return_type;
1603 declarator->u.function.requires_clause = requires_clause;
1604 if (target)
1605 {
1606 declarator->id_loc = target->id_loc;
1607 declarator->parameter_pack_p = target->parameter_pack_p;
1608 target->parameter_pack_p = false;
1609 }
1610 else
1611 declarator->parameter_pack_p = false;
1612
1613 return declarator;
1614 }
1615
1616 /* Make a declarator for an array of BOUNDS elements, each of which is
1617 defined by ELEMENT. */
1618
1619 cp_declarator *
1620 make_array_declarator (cp_declarator *element, tree bounds)
1621 {
1622 cp_declarator *declarator;
1623
1624 declarator = make_declarator (cdk_array);
1625 declarator->declarator = element;
1626 declarator->u.array.bounds = bounds;
1627 if (element)
1628 {
1629 declarator->id_loc = element->id_loc;
1630 declarator->parameter_pack_p = element->parameter_pack_p;
1631 element->parameter_pack_p = false;
1632 }
1633 else
1634 declarator->parameter_pack_p = false;
1635
1636 return declarator;
1637 }
1638
1639 /* Determine whether the declarator we've seen so far can be a
1640 parameter pack, when followed by an ellipsis. */
1641 static bool
1642 declarator_can_be_parameter_pack (cp_declarator *declarator)
1643 {
1644 if (declarator && declarator->parameter_pack_p)
1645 /* We already saw an ellipsis. */
1646 return false;
1647
1648 /* Search for a declarator name, or any other declarator that goes
1649 after the point where the ellipsis could appear in a parameter
1650 pack. If we find any of these, then this declarator cannot be
1651 made into a parameter pack. */
1652 bool found = false;
1653 while (declarator && !found)
1654 {
1655 switch ((int)declarator->kind)
1656 {
1657 case cdk_id:
1658 case cdk_array:
1659 case cdk_decomp:
1660 found = true;
1661 break;
1662
1663 case cdk_error:
1664 return true;
1665
1666 default:
1667 declarator = declarator->declarator;
1668 break;
1669 }
1670 }
1671
1672 return !found;
1673 }
1674
1675 cp_parameter_declarator *no_parameters;
1676
1677 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1678 DECLARATOR and DEFAULT_ARGUMENT. */
1679
1680 cp_parameter_declarator *
1681 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1682 cp_declarator *declarator,
1683 tree default_argument,
1684 location_t loc,
1685 bool template_parameter_pack_p = false)
1686 {
1687 cp_parameter_declarator *parameter;
1688
1689 parameter = ((cp_parameter_declarator *)
1690 alloc_declarator (sizeof (cp_parameter_declarator)));
1691 parameter->next = NULL;
1692 if (decl_specifiers)
1693 parameter->decl_specifiers = *decl_specifiers;
1694 else
1695 clear_decl_specs (&parameter->decl_specifiers);
1696 parameter->declarator = declarator;
1697 parameter->default_argument = default_argument;
1698 parameter->template_parameter_pack_p = template_parameter_pack_p;
1699 parameter->loc = loc;
1700
1701 return parameter;
1702 }
1703
1704 /* Returns true iff DECLARATOR is a declaration for a function. */
1705
1706 static bool
1707 function_declarator_p (const cp_declarator *declarator)
1708 {
1709 while (declarator)
1710 {
1711 if (declarator->kind == cdk_function
1712 && declarator->declarator->kind == cdk_id)
1713 return true;
1714 if (declarator->kind == cdk_id
1715 || declarator->kind == cdk_decomp
1716 || declarator->kind == cdk_error)
1717 return false;
1718 declarator = declarator->declarator;
1719 }
1720 return false;
1721 }
1722
1723 /* The parser. */
1724
1725 /* Overview
1726 --------
1727
1728 A cp_parser parses the token stream as specified by the C++
1729 grammar. Its job is purely parsing, not semantic analysis. For
1730 example, the parser breaks the token stream into declarators,
1731 expressions, statements, and other similar syntactic constructs.
1732 It does not check that the types of the expressions on either side
1733 of an assignment-statement are compatible, or that a function is
1734 not declared with a parameter of type `void'.
1735
1736 The parser invokes routines elsewhere in the compiler to perform
1737 semantic analysis and to build up the abstract syntax tree for the
1738 code processed.
1739
1740 The parser (and the template instantiation code, which is, in a
1741 way, a close relative of parsing) are the only parts of the
1742 compiler that should be calling push_scope and pop_scope, or
1743 related functions. The parser (and template instantiation code)
1744 keeps track of what scope is presently active; everything else
1745 should simply honor that. (The code that generates static
1746 initializers may also need to set the scope, in order to check
1747 access control correctly when emitting the initializers.)
1748
1749 Methodology
1750 -----------
1751
1752 The parser is of the standard recursive-descent variety. Upcoming
1753 tokens in the token stream are examined in order to determine which
1754 production to use when parsing a non-terminal. Some C++ constructs
1755 require arbitrary look ahead to disambiguate. For example, it is
1756 impossible, in the general case, to tell whether a statement is an
1757 expression or declaration without scanning the entire statement.
1758 Therefore, the parser is capable of "parsing tentatively." When the
1759 parser is not sure what construct comes next, it enters this mode.
1760 Then, while we attempt to parse the construct, the parser queues up
1761 error messages, rather than issuing them immediately, and saves the
1762 tokens it consumes. If the construct is parsed successfully, the
1763 parser "commits", i.e., it issues any queued error messages and
1764 the tokens that were being preserved are permanently discarded.
1765 If, however, the construct is not parsed successfully, the parser
1766 rolls back its state completely so that it can resume parsing using
1767 a different alternative.
1768
1769 Future Improvements
1770 -------------------
1771
1772 The performance of the parser could probably be improved substantially.
1773 We could often eliminate the need to parse tentatively by looking ahead
1774 a little bit. In some places, this approach might not entirely eliminate
1775 the need to parse tentatively, but it might still speed up the average
1776 case. */
1777
1778 /* Flags that are passed to some parsing functions. These values can
1779 be bitwise-ored together. */
1780
1781 enum
1782 {
1783 /* No flags. */
1784 CP_PARSER_FLAGS_NONE = 0x0,
1785 /* The construct is optional. If it is not present, then no error
1786 should be issued. */
1787 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1788 /* When parsing a type-specifier, treat user-defined type-names
1789 as non-type identifiers. */
1790 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1791 /* When parsing a type-specifier, do not try to parse a class-specifier
1792 or enum-specifier. */
1793 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1794 /* When parsing a decl-specifier-seq, only allow type-specifier or
1795 constexpr. */
1796 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1797 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1798 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1799 /* When parsing a decl-specifier-seq, allow missing typename. */
1800 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20
1801 };
1802
1803 /* This type is used for parameters and variables which hold
1804 combinations of the above flags. */
1805 typedef int cp_parser_flags;
1806
1807 /* The different kinds of declarators we want to parse. */
1808
1809 enum cp_parser_declarator_kind
1810 {
1811 /* We want an abstract declarator. */
1812 CP_PARSER_DECLARATOR_ABSTRACT,
1813 /* We want a named declarator. */
1814 CP_PARSER_DECLARATOR_NAMED,
1815 /* We don't mind, but the name must be an unqualified-id. */
1816 CP_PARSER_DECLARATOR_EITHER
1817 };
1818
1819 /* The precedence values used to parse binary expressions. The minimum value
1820 of PREC must be 1, because zero is reserved to quickly discriminate
1821 binary operators from other tokens. */
1822
1823 enum cp_parser_prec
1824 {
1825 PREC_NOT_OPERATOR,
1826 PREC_LOGICAL_OR_EXPRESSION,
1827 PREC_LOGICAL_AND_EXPRESSION,
1828 PREC_INCLUSIVE_OR_EXPRESSION,
1829 PREC_EXCLUSIVE_OR_EXPRESSION,
1830 PREC_AND_EXPRESSION,
1831 PREC_EQUALITY_EXPRESSION,
1832 PREC_RELATIONAL_EXPRESSION,
1833 PREC_SHIFT_EXPRESSION,
1834 PREC_ADDITIVE_EXPRESSION,
1835 PREC_MULTIPLICATIVE_EXPRESSION,
1836 PREC_PM_EXPRESSION,
1837 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1838 };
1839
1840 /* A mapping from a token type to a corresponding tree node type, with a
1841 precedence value. */
1842
1843 struct cp_parser_binary_operations_map_node
1844 {
1845 /* The token type. */
1846 enum cpp_ttype token_type;
1847 /* The corresponding tree code. */
1848 enum tree_code tree_type;
1849 /* The precedence of this operator. */
1850 enum cp_parser_prec prec;
1851 };
1852
1853 struct cp_parser_expression_stack_entry
1854 {
1855 /* Left hand side of the binary operation we are currently
1856 parsing. */
1857 cp_expr lhs;
1858 /* Original tree code for left hand side, if it was a binary
1859 expression itself (used for -Wparentheses). */
1860 enum tree_code lhs_type;
1861 /* Tree code for the binary operation we are parsing. */
1862 enum tree_code tree_type;
1863 /* Precedence of the binary operation we are parsing. */
1864 enum cp_parser_prec prec;
1865 /* Location of the binary operation we are parsing. */
1866 location_t loc;
1867 };
1868
1869 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1870 entries because precedence levels on the stack are monotonically
1871 increasing. */
1872 typedef struct cp_parser_expression_stack_entry
1873 cp_parser_expression_stack[NUM_PREC_VALUES];
1874
1875 /* Prototypes. */
1876
1877 /* Constructors and destructors. */
1878
1879 static cp_parser_context *cp_parser_context_new
1880 (cp_parser_context *);
1881
1882 /* Class variables. */
1883
1884 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1885
1886 /* The operator-precedence table used by cp_parser_binary_expression.
1887 Transformed into an associative array (binops_by_token) by
1888 cp_parser_new. */
1889
1890 static const cp_parser_binary_operations_map_node binops[] = {
1891 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1892 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1893
1894 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1895 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1896 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897
1898 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1899 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1900
1901 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1902 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1903
1904 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1905 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1906 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1908
1909 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1910 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1911
1912 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1913
1914 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1915
1916 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1917
1918 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1919
1920 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1921 };
1922
1923 /* The same as binops, but initialized by cp_parser_new so that
1924 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1925 for speed. */
1926 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1927
1928 /* Constructors and destructors. */
1929
1930 /* Construct a new context. The context below this one on the stack
1931 is given by NEXT. */
1932
1933 static cp_parser_context *
1934 cp_parser_context_new (cp_parser_context* next)
1935 {
1936 cp_parser_context *context;
1937
1938 /* Allocate the storage. */
1939 if (cp_parser_context_free_list != NULL)
1940 {
1941 /* Pull the first entry from the free list. */
1942 context = cp_parser_context_free_list;
1943 cp_parser_context_free_list = context->next;
1944 memset (context, 0, sizeof (*context));
1945 }
1946 else
1947 context = ggc_cleared_alloc<cp_parser_context> ();
1948
1949 /* No errors have occurred yet in this context. */
1950 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1951 /* If this is not the bottommost context, copy information that we
1952 need from the previous context. */
1953 if (next)
1954 {
1955 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1956 expression, then we are parsing one in this context, too. */
1957 context->object_type = next->object_type;
1958 /* Thread the stack. */
1959 context->next = next;
1960 }
1961
1962 return context;
1963 }
1964
1965 /* Managing the unparsed function queues. */
1966
1967 #define unparsed_funs_with_default_args \
1968 parser->unparsed_queues->last ().funs_with_default_args
1969 #define unparsed_funs_with_definitions \
1970 parser->unparsed_queues->last ().funs_with_definitions
1971 #define unparsed_nsdmis \
1972 parser->unparsed_queues->last ().nsdmis
1973 #define unparsed_classes \
1974 parser->unparsed_queues->last ().classes
1975
1976 static void
1977 push_unparsed_function_queues (cp_parser *parser)
1978 {
1979 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1980 vec_safe_push (parser->unparsed_queues, e);
1981 }
1982
1983 static void
1984 pop_unparsed_function_queues (cp_parser *parser)
1985 {
1986 release_tree_vector (unparsed_funs_with_definitions);
1987 parser->unparsed_queues->pop ();
1988 }
1989
1990 /* Prototypes. */
1991
1992 /* Constructors and destructors. */
1993
1994 static cp_parser *cp_parser_new
1995 (void);
1996
1997 /* Routines to parse various constructs.
1998
1999 Those that return `tree' will return the error_mark_node (rather
2000 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2001 Sometimes, they will return an ordinary node if error-recovery was
2002 attempted, even though a parse error occurred. So, to check
2003 whether or not a parse error occurred, you should always use
2004 cp_parser_error_occurred. If the construct is optional (indicated
2005 either by an `_opt' in the name of the function that does the
2006 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2007 the construct is not present. */
2008
2009 /* Lexical conventions [gram.lex] */
2010
2011 static cp_expr cp_parser_identifier
2012 (cp_parser *);
2013 static cp_expr cp_parser_string_literal
2014 (cp_parser *, bool, bool, bool);
2015 static cp_expr cp_parser_userdef_char_literal
2016 (cp_parser *);
2017 static tree cp_parser_userdef_string_literal
2018 (tree);
2019 static cp_expr cp_parser_userdef_numeric_literal
2020 (cp_parser *);
2021
2022 /* Basic concepts [gram.basic] */
2023
2024 static void cp_parser_translation_unit (cp_parser *);
2025
2026 /* Expressions [gram.expr] */
2027
2028 static cp_expr cp_parser_primary_expression
2029 (cp_parser *, bool, bool, bool, cp_id_kind *);
2030 static cp_expr cp_parser_id_expression
2031 (cp_parser *, bool, bool, bool *, bool, bool);
2032 static cp_expr cp_parser_unqualified_id
2033 (cp_parser *, bool, bool, bool, bool);
2034 static tree cp_parser_nested_name_specifier_opt
2035 (cp_parser *, bool, bool, bool, bool, bool = false);
2036 static tree cp_parser_nested_name_specifier
2037 (cp_parser *, bool, bool, bool, bool);
2038 static tree cp_parser_qualifying_entity
2039 (cp_parser *, bool, bool, bool, bool, bool);
2040 static cp_expr cp_parser_postfix_expression
2041 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2042 static tree cp_parser_postfix_open_square_expression
2043 (cp_parser *, tree, bool, bool);
2044 static tree cp_parser_postfix_dot_deref_expression
2045 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2046 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2047 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2048 bool = false);
2049 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2050 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2051 static void cp_parser_pseudo_destructor_name
2052 (cp_parser *, tree, tree *, tree *);
2053 static cp_expr cp_parser_unary_expression
2054 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2055 static enum tree_code cp_parser_unary_operator
2056 (cp_token *);
2057 static tree cp_parser_has_attribute_expression
2058 (cp_parser *);
2059 static tree cp_parser_new_expression
2060 (cp_parser *);
2061 static vec<tree, va_gc> *cp_parser_new_placement
2062 (cp_parser *);
2063 static tree cp_parser_new_type_id
2064 (cp_parser *, tree *);
2065 static cp_declarator *cp_parser_new_declarator_opt
2066 (cp_parser *);
2067 static cp_declarator *cp_parser_direct_new_declarator
2068 (cp_parser *);
2069 static vec<tree, va_gc> *cp_parser_new_initializer
2070 (cp_parser *);
2071 static tree cp_parser_delete_expression
2072 (cp_parser *);
2073 static cp_expr cp_parser_cast_expression
2074 (cp_parser *, bool, bool, bool, cp_id_kind *);
2075 static cp_expr cp_parser_binary_expression
2076 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2077 static tree cp_parser_question_colon_clause
2078 (cp_parser *, cp_expr);
2079 static cp_expr cp_parser_assignment_expression
2080 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2081 static enum tree_code cp_parser_assignment_operator_opt
2082 (cp_parser *);
2083 static cp_expr cp_parser_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static cp_expr cp_parser_constant_expression
2086 (cp_parser *, bool = false, bool * = NULL, bool = false);
2087 static cp_expr cp_parser_builtin_offsetof
2088 (cp_parser *);
2089 static cp_expr cp_parser_lambda_expression
2090 (cp_parser *);
2091 static void cp_parser_lambda_introducer
2092 (cp_parser *, tree);
2093 static bool cp_parser_lambda_declarator_opt
2094 (cp_parser *, tree);
2095 static void cp_parser_lambda_body
2096 (cp_parser *, tree);
2097
2098 /* Statements [gram.stmt.stmt] */
2099
2100 static void cp_parser_statement
2101 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2102 static void cp_parser_label_for_labeled_statement
2103 (cp_parser *, tree);
2104 static tree cp_parser_expression_statement
2105 (cp_parser *, tree);
2106 static tree cp_parser_compound_statement
2107 (cp_parser *, tree, int, bool);
2108 static void cp_parser_statement_seq_opt
2109 (cp_parser *, tree);
2110 static tree cp_parser_selection_statement
2111 (cp_parser *, bool *, vec<tree> *);
2112 static tree cp_parser_condition
2113 (cp_parser *);
2114 static tree cp_parser_iteration_statement
2115 (cp_parser *, bool *, bool, unsigned short);
2116 static bool cp_parser_init_statement
2117 (cp_parser *, tree *decl);
2118 static tree cp_parser_for
2119 (cp_parser *, bool, unsigned short);
2120 static tree cp_parser_c_for
2121 (cp_parser *, tree, tree, bool, unsigned short);
2122 static tree cp_parser_range_for
2123 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2124 static void do_range_for_auto_deduction
2125 (tree, tree);
2126 static tree cp_parser_perform_range_for_lookup
2127 (tree, tree *, tree *);
2128 static tree cp_parser_range_for_member_function
2129 (tree, tree);
2130 static tree cp_parser_jump_statement
2131 (cp_parser *);
2132 static void cp_parser_declaration_statement
2133 (cp_parser *);
2134
2135 static tree cp_parser_implicitly_scoped_statement
2136 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2137 static void cp_parser_already_scoped_statement
2138 (cp_parser *, bool *, const token_indent_info &);
2139
2140 /* Declarations [gram.dcl.dcl] */
2141
2142 static void cp_parser_declaration_seq_opt
2143 (cp_parser *);
2144 static void cp_parser_declaration
2145 (cp_parser *);
2146 static void cp_parser_toplevel_declaration
2147 (cp_parser *);
2148 static void cp_parser_block_declaration
2149 (cp_parser *, bool);
2150 static void cp_parser_simple_declaration
2151 (cp_parser *, bool, tree *);
2152 static void cp_parser_decl_specifier_seq
2153 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2154 static tree cp_parser_storage_class_specifier_opt
2155 (cp_parser *);
2156 static tree cp_parser_function_specifier_opt
2157 (cp_parser *, cp_decl_specifier_seq *);
2158 static tree cp_parser_type_specifier
2159 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2160 int *, bool *);
2161 static tree cp_parser_simple_type_specifier
2162 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2163 static tree cp_parser_type_name
2164 (cp_parser *, bool);
2165 static tree cp_parser_nonclass_name
2166 (cp_parser* parser);
2167 static tree cp_parser_elaborated_type_specifier
2168 (cp_parser *, bool, bool);
2169 static tree cp_parser_enum_specifier
2170 (cp_parser *);
2171 static void cp_parser_enumerator_list
2172 (cp_parser *, tree);
2173 static void cp_parser_enumerator_definition
2174 (cp_parser *, tree);
2175 static tree cp_parser_namespace_name
2176 (cp_parser *);
2177 static void cp_parser_namespace_definition
2178 (cp_parser *);
2179 static void cp_parser_namespace_body
2180 (cp_parser *);
2181 static tree cp_parser_qualified_namespace_specifier
2182 (cp_parser *);
2183 static void cp_parser_namespace_alias_definition
2184 (cp_parser *);
2185 static bool cp_parser_using_declaration
2186 (cp_parser *, bool);
2187 static void cp_parser_using_directive
2188 (cp_parser *);
2189 static tree cp_parser_alias_declaration
2190 (cp_parser *);
2191 static void cp_parser_asm_definition
2192 (cp_parser *);
2193 static void cp_parser_linkage_specification
2194 (cp_parser *);
2195 static void cp_parser_static_assert
2196 (cp_parser *, bool);
2197 static tree cp_parser_decltype
2198 (cp_parser *);
2199 static tree cp_parser_decomposition_declaration
2200 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2201
2202 /* Declarators [gram.dcl.decl] */
2203
2204 static tree cp_parser_init_declarator
2205 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2206 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2207 location_t *, tree *);
2208 static cp_declarator *cp_parser_declarator
2209 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2210 bool, bool, bool);
2211 static cp_declarator *cp_parser_direct_declarator
2212 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2213 bool);
2214 static enum tree_code cp_parser_ptr_operator
2215 (cp_parser *, tree *, cp_cv_quals *, tree *);
2216 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2217 (cp_parser *);
2218 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2219 (cp_parser *);
2220 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2221 (cp_parser *);
2222 static tree cp_parser_tx_qualifier_opt
2223 (cp_parser *);
2224 static tree cp_parser_late_return_type_opt
2225 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2226 static tree cp_parser_declarator_id
2227 (cp_parser *, bool);
2228 static tree cp_parser_type_id
2229 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2230 static tree cp_parser_template_type_arg
2231 (cp_parser *);
2232 static tree cp_parser_trailing_type_id (cp_parser *);
2233 static tree cp_parser_type_id_1
2234 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2235 static void cp_parser_type_specifier_seq
2236 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2237 static tree cp_parser_parameter_declaration_clause
2238 (cp_parser *, cp_parser_flags);
2239 static tree cp_parser_parameter_declaration_list
2240 (cp_parser *, cp_parser_flags);
2241 static cp_parameter_declarator *cp_parser_parameter_declaration
2242 (cp_parser *, cp_parser_flags, bool, bool *);
2243 static tree cp_parser_default_argument
2244 (cp_parser *, bool);
2245 static void cp_parser_function_body
2246 (cp_parser *, bool);
2247 static tree cp_parser_initializer
2248 (cp_parser *, bool *, bool *, bool = false);
2249 static cp_expr cp_parser_initializer_clause
2250 (cp_parser *, bool *);
2251 static cp_expr cp_parser_braced_list
2252 (cp_parser*, bool*);
2253 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2254 (cp_parser *, bool *);
2255
2256 static void cp_parser_ctor_initializer_opt_and_function_body
2257 (cp_parser *, bool);
2258
2259 static tree cp_parser_late_parsing_omp_declare_simd
2260 (cp_parser *, tree);
2261
2262 static tree cp_parser_late_parsing_oacc_routine
2263 (cp_parser *, tree);
2264
2265 static tree synthesize_implicit_template_parm
2266 (cp_parser *, tree);
2267 static tree finish_fully_implicit_template
2268 (cp_parser *, tree);
2269 static void abort_fully_implicit_template
2270 (cp_parser *);
2271
2272 /* Classes [gram.class] */
2273
2274 static tree cp_parser_class_name
2275 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2276 static tree cp_parser_class_specifier
2277 (cp_parser *);
2278 static tree cp_parser_class_head
2279 (cp_parser *, bool *);
2280 static enum tag_types cp_parser_class_key
2281 (cp_parser *);
2282 static void cp_parser_type_parameter_key
2283 (cp_parser* parser);
2284 static void cp_parser_member_specification_opt
2285 (cp_parser *);
2286 static void cp_parser_member_declaration
2287 (cp_parser *);
2288 static tree cp_parser_pure_specifier
2289 (cp_parser *);
2290 static tree cp_parser_constant_initializer
2291 (cp_parser *);
2292
2293 /* Derived classes [gram.class.derived] */
2294
2295 static tree cp_parser_base_clause
2296 (cp_parser *);
2297 static tree cp_parser_base_specifier
2298 (cp_parser *);
2299
2300 /* Special member functions [gram.special] */
2301
2302 static tree cp_parser_conversion_function_id
2303 (cp_parser *);
2304 static tree cp_parser_conversion_type_id
2305 (cp_parser *);
2306 static cp_declarator *cp_parser_conversion_declarator_opt
2307 (cp_parser *);
2308 static void cp_parser_ctor_initializer_opt
2309 (cp_parser *);
2310 static void cp_parser_mem_initializer_list
2311 (cp_parser *);
2312 static tree cp_parser_mem_initializer
2313 (cp_parser *);
2314 static tree cp_parser_mem_initializer_id
2315 (cp_parser *);
2316
2317 /* Overloading [gram.over] */
2318
2319 static cp_expr cp_parser_operator_function_id
2320 (cp_parser *);
2321 static cp_expr cp_parser_operator
2322 (cp_parser *, location_t);
2323
2324 /* Templates [gram.temp] */
2325
2326 static void cp_parser_template_declaration
2327 (cp_parser *, bool);
2328 static tree cp_parser_template_parameter_list
2329 (cp_parser *);
2330 static tree cp_parser_template_parameter
2331 (cp_parser *, bool *, bool *);
2332 static tree cp_parser_type_parameter
2333 (cp_parser *, bool *);
2334 static tree cp_parser_template_id
2335 (cp_parser *, bool, bool, enum tag_types, bool);
2336 static tree cp_parser_template_name
2337 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2338 static tree cp_parser_template_argument_list
2339 (cp_parser *);
2340 static tree cp_parser_template_argument
2341 (cp_parser *);
2342 static void cp_parser_explicit_instantiation
2343 (cp_parser *);
2344 static void cp_parser_explicit_specialization
2345 (cp_parser *);
2346
2347 /* Exception handling [gram.exception] */
2348
2349 static tree cp_parser_try_block
2350 (cp_parser *);
2351 static void cp_parser_function_try_block
2352 (cp_parser *);
2353 static void cp_parser_handler_seq
2354 (cp_parser *);
2355 static void cp_parser_handler
2356 (cp_parser *);
2357 static tree cp_parser_exception_declaration
2358 (cp_parser *);
2359 static tree cp_parser_throw_expression
2360 (cp_parser *);
2361 static tree cp_parser_exception_specification_opt
2362 (cp_parser *);
2363 static tree cp_parser_type_id_list
2364 (cp_parser *);
2365
2366 /* GNU Extensions */
2367
2368 static tree cp_parser_asm_specification_opt
2369 (cp_parser *);
2370 static tree cp_parser_asm_operand_list
2371 (cp_parser *);
2372 static tree cp_parser_asm_clobber_list
2373 (cp_parser *);
2374 static tree cp_parser_asm_label_list
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_attribute_p
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_gnu_attribute_p
2379 (cp_parser *);
2380 static bool cp_next_tokens_can_be_std_attribute_p
2381 (cp_parser *);
2382 static bool cp_nth_tokens_can_be_std_attribute_p
2383 (cp_parser *, size_t);
2384 static bool cp_nth_tokens_can_be_gnu_attribute_p
2385 (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_attribute_p
2387 (cp_parser *, size_t);
2388 static tree cp_parser_attributes_opt
2389 (cp_parser *);
2390 static tree cp_parser_gnu_attributes_opt
2391 (cp_parser *);
2392 static tree cp_parser_gnu_attribute_list
2393 (cp_parser *, bool = false);
2394 static tree cp_parser_std_attribute
2395 (cp_parser *, tree);
2396 static tree cp_parser_std_attribute_spec
2397 (cp_parser *);
2398 static tree cp_parser_std_attribute_spec_seq
2399 (cp_parser *);
2400 static size_t cp_parser_skip_attributes_opt
2401 (cp_parser *, size_t);
2402 static bool cp_parser_extension_opt
2403 (cp_parser *, int *);
2404 static void cp_parser_label_declaration
2405 (cp_parser *);
2406
2407 /* Concept Extensions */
2408
2409 static tree cp_parser_requires_clause
2410 (cp_parser *);
2411 static tree cp_parser_requires_clause_opt
2412 (cp_parser *);
2413 static tree cp_parser_requires_expression
2414 (cp_parser *);
2415 static tree cp_parser_requirement_parameter_list
2416 (cp_parser *);
2417 static tree cp_parser_requirement_body
2418 (cp_parser *);
2419 static tree cp_parser_requirement_list
2420 (cp_parser *);
2421 static tree cp_parser_requirement
2422 (cp_parser *);
2423 static tree cp_parser_simple_requirement
2424 (cp_parser *);
2425 static tree cp_parser_compound_requirement
2426 (cp_parser *);
2427 static tree cp_parser_type_requirement
2428 (cp_parser *);
2429 static tree cp_parser_nested_requirement
2430 (cp_parser *);
2431
2432 /* Transactional Memory Extensions */
2433
2434 static tree cp_parser_transaction
2435 (cp_parser *, cp_token *);
2436 static tree cp_parser_transaction_expression
2437 (cp_parser *, enum rid);
2438 static void cp_parser_function_transaction
2439 (cp_parser *, enum rid);
2440 static tree cp_parser_transaction_cancel
2441 (cp_parser *);
2442
2443 enum pragma_context {
2444 pragma_external,
2445 pragma_member,
2446 pragma_objc_icode,
2447 pragma_stmt,
2448 pragma_compound
2449 };
2450 static bool cp_parser_pragma
2451 (cp_parser *, enum pragma_context, bool *);
2452
2453 /* Objective-C++ Productions */
2454
2455 static tree cp_parser_objc_message_receiver
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_args
2458 (cp_parser *);
2459 static tree cp_parser_objc_message_expression
2460 (cp_parser *);
2461 static cp_expr cp_parser_objc_encode_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_defs_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_protocol_expression
2466 (cp_parser *);
2467 static tree cp_parser_objc_selector_expression
2468 (cp_parser *);
2469 static cp_expr cp_parser_objc_expression
2470 (cp_parser *);
2471 static bool cp_parser_objc_selector_p
2472 (enum cpp_ttype);
2473 static tree cp_parser_objc_selector
2474 (cp_parser *);
2475 static tree cp_parser_objc_protocol_refs_opt
2476 (cp_parser *);
2477 static void cp_parser_objc_declaration
2478 (cp_parser *, tree);
2479 static tree cp_parser_objc_statement
2480 (cp_parser *);
2481 static bool cp_parser_objc_valid_prefix_attributes
2482 (cp_parser *, tree *);
2483 static void cp_parser_objc_at_property_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_synthesize_declaration
2486 (cp_parser *) ;
2487 static void cp_parser_objc_at_dynamic_declaration
2488 (cp_parser *) ;
2489 static tree cp_parser_objc_struct_declaration
2490 (cp_parser *) ;
2491
2492 /* Utility Routines */
2493
2494 static cp_expr cp_parser_lookup_name
2495 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2496 static tree cp_parser_lookup_name_simple
2497 (cp_parser *, tree, location_t);
2498 static tree cp_parser_maybe_treat_template_as_class
2499 (tree, bool);
2500 static bool cp_parser_check_declarator_template_parameters
2501 (cp_parser *, cp_declarator *, location_t);
2502 static bool cp_parser_check_template_parameters
2503 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2504 static cp_expr cp_parser_simple_cast_expression
2505 (cp_parser *);
2506 static tree cp_parser_global_scope_opt
2507 (cp_parser *, bool);
2508 static bool cp_parser_constructor_declarator_p
2509 (cp_parser *, bool);
2510 static tree cp_parser_function_definition_from_specifiers_and_declarator
2511 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2512 static tree cp_parser_function_definition_after_declarator
2513 (cp_parser *, bool);
2514 static bool cp_parser_template_declaration_after_export
2515 (cp_parser *, bool);
2516 static void cp_parser_perform_template_parameter_access_checks
2517 (vec<deferred_access_check, va_gc> *);
2518 static tree cp_parser_single_declaration
2519 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2520 static cp_expr cp_parser_functional_cast
2521 (cp_parser *, tree);
2522 static tree cp_parser_save_member_function_body
2523 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2524 static tree cp_parser_save_nsdmi
2525 (cp_parser *);
2526 static tree cp_parser_enclosed_template_argument_list
2527 (cp_parser *);
2528 static void cp_parser_save_default_args
2529 (cp_parser *, tree);
2530 static void cp_parser_late_parsing_for_member
2531 (cp_parser *, tree);
2532 static tree cp_parser_late_parse_one_default_arg
2533 (cp_parser *, tree, tree, tree);
2534 static void cp_parser_late_parsing_nsdmi
2535 (cp_parser *, tree);
2536 static void cp_parser_late_parsing_default_args
2537 (cp_parser *, tree);
2538 static tree cp_parser_sizeof_operand
2539 (cp_parser *, enum rid);
2540 static cp_expr cp_parser_trait_expr
2541 (cp_parser *, enum rid);
2542 static bool cp_parser_declares_only_class_p
2543 (cp_parser *);
2544 static void cp_parser_set_storage_class
2545 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2546 static void cp_parser_set_decl_spec_type
2547 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2548 static void set_and_check_decl_spec_loc
2549 (cp_decl_specifier_seq *decl_specs,
2550 cp_decl_spec ds, cp_token *);
2551 static bool cp_parser_friend_p
2552 (const cp_decl_specifier_seq *);
2553 static void cp_parser_required_error
2554 (cp_parser *, required_token, bool, location_t);
2555 static cp_token *cp_parser_require
2556 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2557 static cp_token *cp_parser_require_keyword
2558 (cp_parser *, enum rid, required_token);
2559 static bool cp_parser_token_starts_function_definition_p
2560 (cp_token *);
2561 static bool cp_parser_next_token_starts_class_definition_p
2562 (cp_parser *);
2563 static bool cp_parser_next_token_ends_template_argument_p
2564 (cp_parser *);
2565 static bool cp_parser_nth_token_starts_template_argument_list_p
2566 (cp_parser *, size_t);
2567 static enum tag_types cp_parser_token_is_class_key
2568 (cp_token *);
2569 static enum tag_types cp_parser_token_is_type_parameter_key
2570 (cp_token *);
2571 static void cp_parser_check_class_key
2572 (enum tag_types, tree type);
2573 static void cp_parser_check_access_in_redeclaration
2574 (tree type, location_t location);
2575 static bool cp_parser_optional_template_keyword
2576 (cp_parser *);
2577 static void cp_parser_pre_parsed_nested_name_specifier
2578 (cp_parser *);
2579 static bool cp_parser_cache_group
2580 (cp_parser *, enum cpp_ttype, unsigned);
2581 static tree cp_parser_cache_defarg
2582 (cp_parser *parser, bool nsdmi);
2583 static void cp_parser_parse_tentatively
2584 (cp_parser *);
2585 static void cp_parser_commit_to_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_commit_to_topmost_tentative_parse
2588 (cp_parser *);
2589 static void cp_parser_abort_tentative_parse
2590 (cp_parser *);
2591 static bool cp_parser_parse_definitely
2592 (cp_parser *);
2593 static inline bool cp_parser_parsing_tentatively
2594 (cp_parser *);
2595 static bool cp_parser_uncommitted_to_tentative_parse_p
2596 (cp_parser *);
2597 static void cp_parser_error
2598 (cp_parser *, const char *);
2599 static void cp_parser_name_lookup_error
2600 (cp_parser *, tree, tree, name_lookup_error, location_t);
2601 static bool cp_parser_simulate_error
2602 (cp_parser *);
2603 static bool cp_parser_check_type_definition
2604 (cp_parser *);
2605 static void cp_parser_check_for_definition_in_return_type
2606 (cp_declarator *, tree, location_t type_location);
2607 static void cp_parser_check_for_invalid_template_id
2608 (cp_parser *, tree, enum tag_types, location_t location);
2609 static bool cp_parser_non_integral_constant_expression
2610 (cp_parser *, non_integral_constant);
2611 static void cp_parser_diagnose_invalid_type_name
2612 (cp_parser *, tree, location_t);
2613 static bool cp_parser_parse_and_diagnose_invalid_type_name
2614 (cp_parser *);
2615 static int cp_parser_skip_to_closing_parenthesis
2616 (cp_parser *, bool, bool, bool);
2617 static void cp_parser_skip_to_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_consume_semicolon_at_end_of_statement
2620 (cp_parser *);
2621 static void cp_parser_skip_to_end_of_block_or_statement
2622 (cp_parser *);
2623 static bool cp_parser_skip_to_closing_brace
2624 (cp_parser *);
2625 static void cp_parser_skip_to_end_of_template_parameter_list
2626 (cp_parser *);
2627 static void cp_parser_skip_to_pragma_eol
2628 (cp_parser*, cp_token *);
2629 static bool cp_parser_error_occurred
2630 (cp_parser *);
2631 static bool cp_parser_allow_gnu_extensions_p
2632 (cp_parser *);
2633 static bool cp_parser_is_pure_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_string_literal
2636 (cp_token *);
2637 static bool cp_parser_is_keyword
2638 (cp_token *, enum rid);
2639 static tree cp_parser_make_typename_type
2640 (cp_parser *, tree, location_t location);
2641 static cp_declarator * cp_parser_make_indirect_declarator
2642 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2643 static bool cp_parser_compound_literal_p
2644 (cp_parser *);
2645 static bool cp_parser_array_designator_p
2646 (cp_parser *);
2647 static bool cp_parser_init_statement_p
2648 (cp_parser *);
2649 static bool cp_parser_skip_to_closing_square_bracket
2650 (cp_parser *);
2651
2652 /* Concept-related syntactic transformations */
2653
2654 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2655 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2656
2657 // -------------------------------------------------------------------------- //
2658 // Unevaluated Operand Guard
2659 //
2660 // Implementation of an RAII helper for unevaluated operand parsing.
2661 cp_unevaluated::cp_unevaluated ()
2662 {
2663 ++cp_unevaluated_operand;
2664 ++c_inhibit_evaluation_warnings;
2665 }
2666
2667 cp_unevaluated::~cp_unevaluated ()
2668 {
2669 --c_inhibit_evaluation_warnings;
2670 --cp_unevaluated_operand;
2671 }
2672
2673 // -------------------------------------------------------------------------- //
2674 // Tentative Parsing
2675
2676 /* Returns nonzero if we are parsing tentatively. */
2677
2678 static inline bool
2679 cp_parser_parsing_tentatively (cp_parser* parser)
2680 {
2681 return parser->context->next != NULL;
2682 }
2683
2684 /* Returns nonzero if TOKEN is a string literal. */
2685
2686 static bool
2687 cp_parser_is_pure_string_literal (cp_token* token)
2688 {
2689 return (token->type == CPP_STRING ||
2690 token->type == CPP_STRING16 ||
2691 token->type == CPP_STRING32 ||
2692 token->type == CPP_WSTRING ||
2693 token->type == CPP_UTF8STRING);
2694 }
2695
2696 /* Returns nonzero if TOKEN is a string literal
2697 of a user-defined string literal. */
2698
2699 static bool
2700 cp_parser_is_string_literal (cp_token* token)
2701 {
2702 return (cp_parser_is_pure_string_literal (token) ||
2703 token->type == CPP_STRING_USERDEF ||
2704 token->type == CPP_STRING16_USERDEF ||
2705 token->type == CPP_STRING32_USERDEF ||
2706 token->type == CPP_WSTRING_USERDEF ||
2707 token->type == CPP_UTF8STRING_USERDEF);
2708 }
2709
2710 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2711
2712 static bool
2713 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2714 {
2715 return token->keyword == keyword;
2716 }
2717
2718 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 PRAGMA_NONE. */
2720
2721 static enum pragma_kind
2722 cp_parser_pragma_kind (cp_token *token)
2723 {
2724 if (token->type != CPP_PRAGMA)
2725 return PRAGMA_NONE;
2726 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2727 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2728 }
2729
2730 /* Helper function for cp_parser_error.
2731 Having peeked a token of kind TOK1_KIND that might signify
2732 a conflict marker, peek successor tokens to determine
2733 if we actually do have a conflict marker.
2734 Specifically, we consider a run of 7 '<', '=' or '>' characters
2735 at the start of a line as a conflict marker.
2736 These come through the lexer as three pairs and a single,
2737 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2738 If it returns true, *OUT_LOC is written to with the location/range
2739 of the marker. */
2740
2741 static bool
2742 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2743 location_t *out_loc)
2744 {
2745 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2746 if (token2->type != tok1_kind)
2747 return false;
2748 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2749 if (token3->type != tok1_kind)
2750 return false;
2751 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2752 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2753 return false;
2754
2755 /* It must be at the start of the line. */
2756 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2757 if (LOCATION_COLUMN (start_loc) != 1)
2758 return false;
2759
2760 /* We have a conflict marker. Construct a location of the form:
2761 <<<<<<<
2762 ^~~~~~~
2763 with start == caret, finishing at the end of the marker. */
2764 location_t finish_loc = get_finish (token4->location);
2765 *out_loc = make_location (start_loc, start_loc, finish_loc);
2766
2767 return true;
2768 }
2769
2770 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2771 RT_CLOSE_PAREN. */
2772
2773 static const char *
2774 get_matching_symbol (required_token token_desc)
2775 {
2776 switch (token_desc)
2777 {
2778 default:
2779 gcc_unreachable ();
2780 return "";
2781 case RT_CLOSE_BRACE:
2782 return "{";
2783 case RT_CLOSE_PAREN:
2784 return "(";
2785 }
2786 }
2787
2788 /* Attempt to convert TOKEN_DESC from a required_token to an
2789 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2790
2791 static enum cpp_ttype
2792 get_required_cpp_ttype (required_token token_desc)
2793 {
2794 switch (token_desc)
2795 {
2796 case RT_SEMICOLON:
2797 return CPP_SEMICOLON;
2798 case RT_OPEN_PAREN:
2799 return CPP_OPEN_PAREN;
2800 case RT_CLOSE_BRACE:
2801 return CPP_CLOSE_BRACE;
2802 case RT_OPEN_BRACE:
2803 return CPP_OPEN_BRACE;
2804 case RT_CLOSE_SQUARE:
2805 return CPP_CLOSE_SQUARE;
2806 case RT_OPEN_SQUARE:
2807 return CPP_OPEN_SQUARE;
2808 case RT_COMMA:
2809 return CPP_COMMA;
2810 case RT_COLON:
2811 return CPP_COLON;
2812 case RT_CLOSE_PAREN:
2813 return CPP_CLOSE_PAREN;
2814
2815 default:
2816 /* Use CPP_EOF as a "no completions possible" code. */
2817 return CPP_EOF;
2818 }
2819 }
2820
2821
2822 /* Subroutine of cp_parser_error and cp_parser_required_error.
2823
2824 Issue a diagnostic of the form
2825 FILE:LINE: MESSAGE before TOKEN
2826 where TOKEN is the next token in the input stream. MESSAGE
2827 (specified by the caller) is usually of the form "expected
2828 OTHER-TOKEN".
2829
2830 This bypasses the check for tentative passing, and potentially
2831 adds material needed by cp_parser_required_error.
2832
2833 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2834 suggesting insertion of the missing token.
2835
2836 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2837 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2838 location. */
2839
2840 static void
2841 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2842 required_token missing_token_desc,
2843 location_t matching_location)
2844 {
2845 cp_token *token = cp_lexer_peek_token (parser->lexer);
2846 /* This diagnostic makes more sense if it is tagged to the line
2847 of the token we just peeked at. */
2848 cp_lexer_set_source_position_from_token (token);
2849
2850 if (token->type == CPP_PRAGMA)
2851 {
2852 error_at (token->location,
2853 "%<#pragma%> is not allowed here");
2854 cp_parser_skip_to_pragma_eol (parser, token);
2855 return;
2856 }
2857
2858 /* If this is actually a conflict marker, report it as such. */
2859 if (token->type == CPP_LSHIFT
2860 || token->type == CPP_RSHIFT
2861 || token->type == CPP_EQ_EQ)
2862 {
2863 location_t loc;
2864 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2865 {
2866 error_at (loc, "version control conflict marker in file");
2867 expanded_location token_exploc = expand_location (token->location);
2868 /* Consume tokens until the end of the source line. */
2869 while (1)
2870 {
2871 cp_lexer_consume_token (parser->lexer);
2872 cp_token *next = cp_lexer_peek_token (parser->lexer);
2873 if (next == NULL)
2874 break;
2875 expanded_location next_exploc = expand_location (next->location);
2876 if (next_exploc.file != token_exploc.file)
2877 break;
2878 if (next_exploc.line != token_exploc.line)
2879 break;
2880 }
2881 return;
2882 }
2883 }
2884
2885 gcc_rich_location richloc (input_location);
2886
2887 bool added_matching_location = false;
2888
2889 if (missing_token_desc != RT_NONE)
2890 {
2891 /* Potentially supply a fix-it hint, suggesting to add the
2892 missing token immediately after the *previous* token.
2893 This may move the primary location within richloc. */
2894 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2895 location_t prev_token_loc
2896 = cp_lexer_previous_token (parser->lexer)->location;
2897 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2898
2899 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2900 Attempt to consolidate diagnostics by printing it as a
2901 secondary range within the main diagnostic. */
2902 if (matching_location != UNKNOWN_LOCATION)
2903 added_matching_location
2904 = richloc.add_location_if_nearby (matching_location);
2905 }
2906
2907 /* Actually emit the error. */
2908 c_parse_error (gmsgid,
2909 /* Because c_parser_error does not understand
2910 CPP_KEYWORD, keywords are treated like
2911 identifiers. */
2912 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2913 token->u.value, token->flags, &richloc);
2914
2915 if (missing_token_desc != RT_NONE)
2916 {
2917 /* If we weren't able to consolidate matching_location, then
2918 print it as a secondary diagnostic. */
2919 if (matching_location != UNKNOWN_LOCATION
2920 && !added_matching_location)
2921 inform (matching_location, "to match this %qs",
2922 get_matching_symbol (missing_token_desc));
2923 }
2924 }
2925
2926 /* If not parsing tentatively, issue a diagnostic of the form
2927 FILE:LINE: MESSAGE before TOKEN
2928 where TOKEN is the next token in the input stream. MESSAGE
2929 (specified by the caller) is usually of the form "expected
2930 OTHER-TOKEN". */
2931
2932 static void
2933 cp_parser_error (cp_parser* parser, const char* gmsgid)
2934 {
2935 if (!cp_parser_simulate_error (parser))
2936 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2937 }
2938
2939 /* Issue an error about name-lookup failing. NAME is the
2940 IDENTIFIER_NODE DECL is the result of
2941 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2942 the thing that we hoped to find. */
2943
2944 static void
2945 cp_parser_name_lookup_error (cp_parser* parser,
2946 tree name,
2947 tree decl,
2948 name_lookup_error desired,
2949 location_t location)
2950 {
2951 /* If name lookup completely failed, tell the user that NAME was not
2952 declared. */
2953 if (decl == error_mark_node)
2954 {
2955 if (parser->scope && parser->scope != global_namespace)
2956 error_at (location, "%<%E::%E%> has not been declared",
2957 parser->scope, name);
2958 else if (parser->scope == global_namespace)
2959 error_at (location, "%<::%E%> has not been declared", name);
2960 else if (parser->object_scope
2961 && !CLASS_TYPE_P (parser->object_scope))
2962 error_at (location, "request for member %qE in non-class type %qT",
2963 name, parser->object_scope);
2964 else if (parser->object_scope)
2965 error_at (location, "%<%T::%E%> has not been declared",
2966 parser->object_scope, name);
2967 else
2968 error_at (location, "%qE has not been declared", name);
2969 }
2970 else if (parser->scope && parser->scope != global_namespace)
2971 {
2972 switch (desired)
2973 {
2974 case NLE_TYPE:
2975 error_at (location, "%<%E::%E%> is not a type",
2976 parser->scope, name);
2977 break;
2978 case NLE_CXX98:
2979 error_at (location, "%<%E::%E%> is not a class or namespace",
2980 parser->scope, name);
2981 break;
2982 case NLE_NOT_CXX98:
2983 error_at (location,
2984 "%<%E::%E%> is not a class, namespace, or enumeration",
2985 parser->scope, name);
2986 break;
2987 default:
2988 gcc_unreachable ();
2989
2990 }
2991 }
2992 else if (parser->scope == global_namespace)
2993 {
2994 switch (desired)
2995 {
2996 case NLE_TYPE:
2997 error_at (location, "%<::%E%> is not a type", name);
2998 break;
2999 case NLE_CXX98:
3000 error_at (location, "%<::%E%> is not a class or namespace", name);
3001 break;
3002 case NLE_NOT_CXX98:
3003 error_at (location,
3004 "%<::%E%> is not a class, namespace, or enumeration",
3005 name);
3006 break;
3007 default:
3008 gcc_unreachable ();
3009 }
3010 }
3011 else
3012 {
3013 switch (desired)
3014 {
3015 case NLE_TYPE:
3016 error_at (location, "%qE is not a type", name);
3017 break;
3018 case NLE_CXX98:
3019 error_at (location, "%qE is not a class or namespace", name);
3020 break;
3021 case NLE_NOT_CXX98:
3022 error_at (location,
3023 "%qE is not a class, namespace, or enumeration", name);
3024 break;
3025 default:
3026 gcc_unreachable ();
3027 }
3028 }
3029 }
3030
3031 /* If we are parsing tentatively, remember that an error has occurred
3032 during this tentative parse. Returns true if the error was
3033 simulated; false if a message should be issued by the caller. */
3034
3035 static bool
3036 cp_parser_simulate_error (cp_parser* parser)
3037 {
3038 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3039 {
3040 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3041 return true;
3042 }
3043 return false;
3044 }
3045
3046 /* This function is called when a type is defined. If type
3047 definitions are forbidden at this point, an error message is
3048 issued. */
3049
3050 static bool
3051 cp_parser_check_type_definition (cp_parser* parser)
3052 {
3053 /* If types are forbidden here, issue a message. */
3054 if (parser->type_definition_forbidden_message)
3055 {
3056 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3057 in the message need to be interpreted. */
3058 error (parser->type_definition_forbidden_message);
3059 return false;
3060 }
3061 return true;
3062 }
3063
3064 /* This function is called when the DECLARATOR is processed. The TYPE
3065 was a type defined in the decl-specifiers. If it is invalid to
3066 define a type in the decl-specifiers for DECLARATOR, an error is
3067 issued. TYPE_LOCATION is the location of TYPE and is used
3068 for error reporting. */
3069
3070 static void
3071 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3072 tree type, location_t type_location)
3073 {
3074 /* [dcl.fct] forbids type definitions in return types.
3075 Unfortunately, it's not easy to know whether or not we are
3076 processing a return type until after the fact. */
3077 while (declarator
3078 && (declarator->kind == cdk_pointer
3079 || declarator->kind == cdk_reference
3080 || declarator->kind == cdk_ptrmem))
3081 declarator = declarator->declarator;
3082 if (declarator
3083 && declarator->kind == cdk_function)
3084 {
3085 error_at (type_location,
3086 "new types may not be defined in a return type");
3087 inform (type_location,
3088 "(perhaps a semicolon is missing after the definition of %qT)",
3089 type);
3090 }
3091 }
3092
3093 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3094 "<" in any valid C++ program. If the next token is indeed "<",
3095 issue a message warning the user about what appears to be an
3096 invalid attempt to form a template-id. LOCATION is the location
3097 of the type-specifier (TYPE) */
3098
3099 static void
3100 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3101 tree type,
3102 enum tag_types tag_type,
3103 location_t location)
3104 {
3105 cp_token_position start = 0;
3106
3107 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3108 {
3109 if (TREE_CODE (type) == TYPE_DECL)
3110 type = TREE_TYPE (type);
3111 if (TYPE_P (type) && !template_placeholder_p (type))
3112 error_at (location, "%qT is not a template", type);
3113 else if (identifier_p (type))
3114 {
3115 if (tag_type != none_type)
3116 error_at (location, "%qE is not a class template", type);
3117 else
3118 error_at (location, "%qE is not a template", type);
3119 }
3120 else
3121 error_at (location, "invalid template-id");
3122 /* Remember the location of the invalid "<". */
3123 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3124 start = cp_lexer_token_position (parser->lexer, true);
3125 /* Consume the "<". */
3126 cp_lexer_consume_token (parser->lexer);
3127 /* Parse the template arguments. */
3128 cp_parser_enclosed_template_argument_list (parser);
3129 /* Permanently remove the invalid template arguments so that
3130 this error message is not issued again. */
3131 if (start)
3132 cp_lexer_purge_tokens_after (parser->lexer, start);
3133 }
3134 }
3135
3136 /* If parsing an integral constant-expression, issue an error message
3137 about the fact that THING appeared and return true. Otherwise,
3138 return false. In either case, set
3139 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3140
3141 static bool
3142 cp_parser_non_integral_constant_expression (cp_parser *parser,
3143 non_integral_constant thing)
3144 {
3145 parser->non_integral_constant_expression_p = true;
3146 if (parser->integral_constant_expression_p)
3147 {
3148 if (!parser->allow_non_integral_constant_expression_p)
3149 {
3150 const char *msg = NULL;
3151 switch (thing)
3152 {
3153 case NIC_FLOAT:
3154 pedwarn (input_location, OPT_Wpedantic,
3155 "ISO C++ forbids using a floating-point literal "
3156 "in a constant-expression");
3157 return true;
3158 case NIC_CAST:
3159 error ("a cast to a type other than an integral or "
3160 "enumeration type cannot appear in a "
3161 "constant-expression");
3162 return true;
3163 case NIC_TYPEID:
3164 error ("%<typeid%> operator "
3165 "cannot appear in a constant-expression");
3166 return true;
3167 case NIC_NCC:
3168 error ("non-constant compound literals "
3169 "cannot appear in a constant-expression");
3170 return true;
3171 case NIC_FUNC_CALL:
3172 error ("a function call "
3173 "cannot appear in a constant-expression");
3174 return true;
3175 case NIC_INC:
3176 error ("an increment "
3177 "cannot appear in a constant-expression");
3178 return true;
3179 case NIC_DEC:
3180 error ("an decrement "
3181 "cannot appear in a constant-expression");
3182 return true;
3183 case NIC_ARRAY_REF:
3184 error ("an array reference "
3185 "cannot appear in a constant-expression");
3186 return true;
3187 case NIC_ADDR_LABEL:
3188 error ("the address of a label "
3189 "cannot appear in a constant-expression");
3190 return true;
3191 case NIC_OVERLOADED:
3192 error ("calls to overloaded operators "
3193 "cannot appear in a constant-expression");
3194 return true;
3195 case NIC_ASSIGNMENT:
3196 error ("an assignment cannot appear in a constant-expression");
3197 return true;
3198 case NIC_COMMA:
3199 error ("a comma operator "
3200 "cannot appear in a constant-expression");
3201 return true;
3202 case NIC_CONSTRUCTOR:
3203 error ("a call to a constructor "
3204 "cannot appear in a constant-expression");
3205 return true;
3206 case NIC_TRANSACTION:
3207 error ("a transaction expression "
3208 "cannot appear in a constant-expression");
3209 return true;
3210 case NIC_THIS:
3211 msg = "this";
3212 break;
3213 case NIC_FUNC_NAME:
3214 msg = "__FUNCTION__";
3215 break;
3216 case NIC_PRETTY_FUNC:
3217 msg = "__PRETTY_FUNCTION__";
3218 break;
3219 case NIC_C99_FUNC:
3220 msg = "__func__";
3221 break;
3222 case NIC_VA_ARG:
3223 msg = "va_arg";
3224 break;
3225 case NIC_ARROW:
3226 msg = "->";
3227 break;
3228 case NIC_POINT:
3229 msg = ".";
3230 break;
3231 case NIC_STAR:
3232 msg = "*";
3233 break;
3234 case NIC_ADDR:
3235 msg = "&";
3236 break;
3237 case NIC_PREINCREMENT:
3238 msg = "++";
3239 break;
3240 case NIC_PREDECREMENT:
3241 msg = "--";
3242 break;
3243 case NIC_NEW:
3244 msg = "new";
3245 break;
3246 case NIC_DEL:
3247 msg = "delete";
3248 break;
3249 default:
3250 gcc_unreachable ();
3251 }
3252 if (msg)
3253 error ("%qs cannot appear in a constant-expression", msg);
3254 return true;
3255 }
3256 }
3257 return false;
3258 }
3259
3260 /* Emit a diagnostic for an invalid type name. This function commits
3261 to the current active tentative parse, if any. (Otherwise, the
3262 problematic construct might be encountered again later, resulting
3263 in duplicate error messages.) LOCATION is the location of ID. */
3264
3265 static void
3266 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3267 location_t location)
3268 {
3269 tree decl, ambiguous_decls;
3270 cp_parser_commit_to_tentative_parse (parser);
3271 /* Try to lookup the identifier. */
3272 decl = cp_parser_lookup_name (parser, id, none_type,
3273 /*is_template=*/false,
3274 /*is_namespace=*/false,
3275 /*check_dependency=*/true,
3276 &ambiguous_decls, location);
3277 if (ambiguous_decls)
3278 /* If the lookup was ambiguous, an error will already have
3279 been issued. */
3280 return;
3281 /* If the lookup found a template-name, it means that the user forgot
3282 to specify an argument list. Emit a useful error message. */
3283 if (DECL_TYPE_TEMPLATE_P (decl))
3284 {
3285 auto_diagnostic_group d;
3286 error_at (location,
3287 "invalid use of template-name %qE without an argument list",
3288 decl);
3289 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3290 inform (location, "class template argument deduction is only available "
3291 "with -std=c++17 or -std=gnu++17");
3292 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3293 }
3294 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3295 error_at (location, "invalid use of destructor %qD as a type", id);
3296 else if (TREE_CODE (decl) == TYPE_DECL)
3297 /* Something like 'unsigned A a;' */
3298 error_at (location, "invalid combination of multiple type-specifiers");
3299 else if (!parser->scope)
3300 {
3301 /* Issue an error message. */
3302 auto_diagnostic_group d;
3303 name_hint hint;
3304 if (TREE_CODE (id) == IDENTIFIER_NODE)
3305 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3306 if (const char *suggestion = hint.suggestion ())
3307 {
3308 gcc_rich_location richloc (location);
3309 richloc.add_fixit_replace (suggestion);
3310 error_at (&richloc,
3311 "%qE does not name a type; did you mean %qs?",
3312 id, suggestion);
3313 }
3314 else
3315 error_at (location, "%qE does not name a type", id);
3316 /* If we're in a template class, it's possible that the user was
3317 referring to a type from a base class. For example:
3318
3319 template <typename T> struct A { typedef T X; };
3320 template <typename T> struct B : public A<T> { X x; };
3321
3322 The user should have said "typename A<T>::X". */
3323 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3324 inform (location, "C++11 %<constexpr%> only available with "
3325 "-std=c++11 or -std=gnu++11");
3326 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3327 inform (location, "C++11 %<noexcept%> only available with "
3328 "-std=c++11 or -std=gnu++11");
3329 else if (cxx_dialect < cxx11
3330 && TREE_CODE (id) == IDENTIFIER_NODE
3331 && id_equal (id, "thread_local"))
3332 inform (location, "C++11 %<thread_local%> only available with "
3333 "-std=c++11 or -std=gnu++11");
3334 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3335 inform (location, "%<concept%> only available with -fconcepts");
3336 else if (processing_template_decl && current_class_type
3337 && TYPE_BINFO (current_class_type))
3338 {
3339 tree b;
3340
3341 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3342 b;
3343 b = TREE_CHAIN (b))
3344 {
3345 tree base_type = BINFO_TYPE (b);
3346 if (CLASS_TYPE_P (base_type)
3347 && dependent_type_p (base_type))
3348 {
3349 tree field;
3350 /* Go from a particular instantiation of the
3351 template (which will have an empty TYPE_FIELDs),
3352 to the main version. */
3353 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3354 for (field = TYPE_FIELDS (base_type);
3355 field;
3356 field = DECL_CHAIN (field))
3357 if (TREE_CODE (field) == TYPE_DECL
3358 && DECL_NAME (field) == id)
3359 {
3360 inform (location,
3361 "(perhaps %<typename %T::%E%> was intended)",
3362 BINFO_TYPE (b), id);
3363 break;
3364 }
3365 if (field)
3366 break;
3367 }
3368 }
3369 }
3370 }
3371 /* Here we diagnose qualified-ids where the scope is actually correct,
3372 but the identifier does not resolve to a valid type name. */
3373 else if (parser->scope != error_mark_node)
3374 {
3375 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3376 {
3377 auto_diagnostic_group d;
3378 name_hint hint;
3379 if (decl == error_mark_node)
3380 hint = suggest_alternative_in_explicit_scope (location, id,
3381 parser->scope);
3382 const char *suggestion = hint.suggestion ();
3383 gcc_rich_location richloc (location_of (id));
3384 if (suggestion)
3385 richloc.add_fixit_replace (suggestion);
3386 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3387 {
3388 if (suggestion)
3389 error_at (&richloc,
3390 "%qE in namespace %qE does not name a template"
3391 " type; did you mean %qs?",
3392 id, parser->scope, suggestion);
3393 else
3394 error_at (&richloc,
3395 "%qE in namespace %qE does not name a template type",
3396 id, parser->scope);
3397 }
3398 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3399 {
3400 if (suggestion)
3401 error_at (&richloc,
3402 "%qE in namespace %qE does not name a template"
3403 " type; did you mean %qs?",
3404 TREE_OPERAND (id, 0), parser->scope, suggestion);
3405 else
3406 error_at (&richloc,
3407 "%qE in namespace %qE does not name a template"
3408 " type",
3409 TREE_OPERAND (id, 0), parser->scope);
3410 }
3411 else
3412 {
3413 if (suggestion)
3414 error_at (&richloc,
3415 "%qE in namespace %qE does not name a type"
3416 "; did you mean %qs?",
3417 id, parser->scope, suggestion);
3418 else
3419 error_at (&richloc,
3420 "%qE in namespace %qE does not name a type",
3421 id, parser->scope);
3422 }
3423 if (DECL_P (decl))
3424 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3425 }
3426 else if (CLASS_TYPE_P (parser->scope)
3427 && constructor_name_p (id, parser->scope))
3428 {
3429 /* A<T>::A<T>() */
3430 auto_diagnostic_group d;
3431 error_at (location, "%<%T::%E%> names the constructor, not"
3432 " the type", parser->scope, id);
3433 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3434 error_at (location, "and %qT has no template constructors",
3435 parser->scope);
3436 }
3437 else if (TYPE_P (parser->scope)
3438 && dependent_scope_p (parser->scope))
3439 {
3440 gcc_rich_location richloc (location);
3441 richloc.add_fixit_insert_before ("typename ");
3442 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3443 error_at (&richloc,
3444 "need %<typename%> before %<%T::%D::%E%> because "
3445 "%<%T::%D%> is a dependent scope",
3446 TYPE_CONTEXT (parser->scope),
3447 TYPENAME_TYPE_FULLNAME (parser->scope),
3448 id,
3449 TYPE_CONTEXT (parser->scope),
3450 TYPENAME_TYPE_FULLNAME (parser->scope));
3451 else
3452 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3453 "%qT is a dependent scope",
3454 parser->scope, id, parser->scope);
3455 }
3456 else if (TYPE_P (parser->scope))
3457 {
3458 auto_diagnostic_group d;
3459 if (!COMPLETE_TYPE_P (parser->scope))
3460 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3461 parser->scope);
3462 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3463 error_at (location_of (id),
3464 "%qE in %q#T does not name a template type",
3465 id, parser->scope);
3466 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3467 error_at (location_of (id),
3468 "%qE in %q#T does not name a template type",
3469 TREE_OPERAND (id, 0), parser->scope);
3470 else
3471 error_at (location_of (id),
3472 "%qE in %q#T does not name a type",
3473 id, parser->scope);
3474 if (DECL_P (decl))
3475 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3476 }
3477 else
3478 gcc_unreachable ();
3479 }
3480 }
3481
3482 /* Check for a common situation where a type-name should be present,
3483 but is not, and issue a sensible error message. Returns true if an
3484 invalid type-name was detected.
3485
3486 The situation handled by this function are variable declarations of the
3487 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3488 Usually, `ID' should name a type, but if we got here it means that it
3489 does not. We try to emit the best possible error message depending on
3490 how exactly the id-expression looks like. */
3491
3492 static bool
3493 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3494 {
3495 tree id;
3496 cp_token *token = cp_lexer_peek_token (parser->lexer);
3497
3498 /* Avoid duplicate error about ambiguous lookup. */
3499 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3500 {
3501 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3502 if (next->type == CPP_NAME && next->error_reported)
3503 goto out;
3504 }
3505
3506 cp_parser_parse_tentatively (parser);
3507 id = cp_parser_id_expression (parser,
3508 /*template_keyword_p=*/false,
3509 /*check_dependency_p=*/true,
3510 /*template_p=*/NULL,
3511 /*declarator_p=*/false,
3512 /*optional_p=*/false);
3513 /* If the next token is a (, this is a function with no explicit return
3514 type, i.e. constructor, destructor or conversion op. */
3515 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3516 || TREE_CODE (id) == TYPE_DECL)
3517 {
3518 cp_parser_abort_tentative_parse (parser);
3519 return false;
3520 }
3521 if (!cp_parser_parse_definitely (parser))
3522 return false;
3523
3524 /* Emit a diagnostic for the invalid type. */
3525 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3526 out:
3527 /* If we aren't in the middle of a declarator (i.e. in a
3528 parameter-declaration-clause), skip to the end of the declaration;
3529 there's no point in trying to process it. */
3530 if (!parser->in_declarator_p)
3531 cp_parser_skip_to_end_of_block_or_statement (parser);
3532 return true;
3533 }
3534
3535 /* Consume tokens up to, and including, the next non-nested closing `)'.
3536 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3537 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3538 found an unnested token of that type. */
3539
3540 static int
3541 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3542 bool recovering,
3543 cpp_ttype or_ttype,
3544 bool consume_paren)
3545 {
3546 unsigned paren_depth = 0;
3547 unsigned brace_depth = 0;
3548 unsigned square_depth = 0;
3549 unsigned condop_depth = 0;
3550
3551 if (recovering && or_ttype == CPP_EOF
3552 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3553 return 0;
3554
3555 while (true)
3556 {
3557 cp_token * token = cp_lexer_peek_token (parser->lexer);
3558
3559 /* Have we found what we're looking for before the closing paren? */
3560 if (token->type == or_ttype && or_ttype != CPP_EOF
3561 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3562 return -1;
3563
3564 switch (token->type)
3565 {
3566 case CPP_PRAGMA_EOL:
3567 if (!parser->lexer->in_pragma)
3568 break;
3569 /* FALLTHRU */
3570 case CPP_EOF:
3571 /* If we've run out of tokens, then there is no closing `)'. */
3572 return 0;
3573
3574 /* This is good for lambda expression capture-lists. */
3575 case CPP_OPEN_SQUARE:
3576 ++square_depth;
3577 break;
3578 case CPP_CLOSE_SQUARE:
3579 if (!square_depth--)
3580 return 0;
3581 break;
3582
3583 case CPP_SEMICOLON:
3584 /* This matches the processing in skip_to_end_of_statement. */
3585 if (!brace_depth)
3586 return 0;
3587 break;
3588
3589 case CPP_OPEN_BRACE:
3590 ++brace_depth;
3591 break;
3592 case CPP_CLOSE_BRACE:
3593 if (!brace_depth--)
3594 return 0;
3595 break;
3596
3597 case CPP_OPEN_PAREN:
3598 if (!brace_depth)
3599 ++paren_depth;
3600 break;
3601
3602 case CPP_CLOSE_PAREN:
3603 if (!brace_depth && !paren_depth--)
3604 {
3605 if (consume_paren)
3606 cp_lexer_consume_token (parser->lexer);
3607 return 1;
3608 }
3609 break;
3610
3611 case CPP_QUERY:
3612 if (!brace_depth && !paren_depth && !square_depth)
3613 ++condop_depth;
3614 break;
3615
3616 case CPP_COLON:
3617 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3618 condop_depth--;
3619 break;
3620
3621 default:
3622 break;
3623 }
3624
3625 /* Consume the token. */
3626 cp_lexer_consume_token (parser->lexer);
3627 }
3628 }
3629
3630 /* Consume tokens up to, and including, the next non-nested closing `)'.
3631 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3632 are doing error recovery. Returns -1 if OR_COMMA is true and we
3633 found an unnested token of that type. */
3634
3635 static int
3636 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3637 bool recovering,
3638 bool or_comma,
3639 bool consume_paren)
3640 {
3641 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3642 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3643 ttype, consume_paren);
3644 }
3645
3646 /* Consume tokens until we reach the end of the current statement.
3647 Normally, that will be just before consuming a `;'. However, if a
3648 non-nested `}' comes first, then we stop before consuming that. */
3649
3650 static void
3651 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3652 {
3653 unsigned nesting_depth = 0;
3654
3655 /* Unwind generic function template scope if necessary. */
3656 if (parser->fully_implicit_function_template_p)
3657 abort_fully_implicit_template (parser);
3658
3659 while (true)
3660 {
3661 cp_token *token = cp_lexer_peek_token (parser->lexer);
3662
3663 switch (token->type)
3664 {
3665 case CPP_PRAGMA_EOL:
3666 if (!parser->lexer->in_pragma)
3667 break;
3668 /* FALLTHRU */
3669 case CPP_EOF:
3670 /* If we've run out of tokens, stop. */
3671 return;
3672
3673 case CPP_SEMICOLON:
3674 /* If the next token is a `;', we have reached the end of the
3675 statement. */
3676 if (!nesting_depth)
3677 return;
3678 break;
3679
3680 case CPP_CLOSE_BRACE:
3681 /* If this is a non-nested '}', stop before consuming it.
3682 That way, when confronted with something like:
3683
3684 { 3 + }
3685
3686 we stop before consuming the closing '}', even though we
3687 have not yet reached a `;'. */
3688 if (nesting_depth == 0)
3689 return;
3690
3691 /* If it is the closing '}' for a block that we have
3692 scanned, stop -- but only after consuming the token.
3693 That way given:
3694
3695 void f g () { ... }
3696 typedef int I;
3697
3698 we will stop after the body of the erroneously declared
3699 function, but before consuming the following `typedef'
3700 declaration. */
3701 if (--nesting_depth == 0)
3702 {
3703 cp_lexer_consume_token (parser->lexer);
3704 return;
3705 }
3706 break;
3707
3708 case CPP_OPEN_BRACE:
3709 ++nesting_depth;
3710 break;
3711
3712 default:
3713 break;
3714 }
3715
3716 /* Consume the token. */
3717 cp_lexer_consume_token (parser->lexer);
3718 }
3719 }
3720
3721 /* This function is called at the end of a statement or declaration.
3722 If the next token is a semicolon, it is consumed; otherwise, error
3723 recovery is attempted. */
3724
3725 static void
3726 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3727 {
3728 /* Look for the trailing `;'. */
3729 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3730 {
3731 /* If there is additional (erroneous) input, skip to the end of
3732 the statement. */
3733 cp_parser_skip_to_end_of_statement (parser);
3734 /* If the next token is now a `;', consume it. */
3735 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3736 cp_lexer_consume_token (parser->lexer);
3737 }
3738 }
3739
3740 /* Skip tokens until we have consumed an entire block, or until we
3741 have consumed a non-nested `;'. */
3742
3743 static void
3744 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3745 {
3746 int nesting_depth = 0;
3747
3748 /* Unwind generic function template scope if necessary. */
3749 if (parser->fully_implicit_function_template_p)
3750 abort_fully_implicit_template (parser);
3751
3752 while (nesting_depth >= 0)
3753 {
3754 cp_token *token = cp_lexer_peek_token (parser->lexer);
3755
3756 switch (token->type)
3757 {
3758 case CPP_PRAGMA_EOL:
3759 if (!parser->lexer->in_pragma)
3760 break;
3761 /* FALLTHRU */
3762 case CPP_EOF:
3763 /* If we've run out of tokens, stop. */
3764 return;
3765
3766 case CPP_SEMICOLON:
3767 /* Stop if this is an unnested ';'. */
3768 if (!nesting_depth)
3769 nesting_depth = -1;
3770 break;
3771
3772 case CPP_CLOSE_BRACE:
3773 /* Stop if this is an unnested '}', or closes the outermost
3774 nesting level. */
3775 nesting_depth--;
3776 if (nesting_depth < 0)
3777 return;
3778 if (!nesting_depth)
3779 nesting_depth = -1;
3780 break;
3781
3782 case CPP_OPEN_BRACE:
3783 /* Nest. */
3784 nesting_depth++;
3785 break;
3786
3787 default:
3788 break;
3789 }
3790
3791 /* Consume the token. */
3792 cp_lexer_consume_token (parser->lexer);
3793 }
3794 }
3795
3796 /* Skip tokens until a non-nested closing curly brace is the next
3797 token, or there are no more tokens. Return true in the first case,
3798 false otherwise. */
3799
3800 static bool
3801 cp_parser_skip_to_closing_brace (cp_parser *parser)
3802 {
3803 unsigned nesting_depth = 0;
3804
3805 while (true)
3806 {
3807 cp_token *token = cp_lexer_peek_token (parser->lexer);
3808
3809 switch (token->type)
3810 {
3811 case CPP_PRAGMA_EOL:
3812 if (!parser->lexer->in_pragma)
3813 break;
3814 /* FALLTHRU */
3815 case CPP_EOF:
3816 /* If we've run out of tokens, stop. */
3817 return false;
3818
3819 case CPP_CLOSE_BRACE:
3820 /* If the next token is a non-nested `}', then we have reached
3821 the end of the current block. */
3822 if (nesting_depth-- == 0)
3823 return true;
3824 break;
3825
3826 case CPP_OPEN_BRACE:
3827 /* If it the next token is a `{', then we are entering a new
3828 block. Consume the entire block. */
3829 ++nesting_depth;
3830 break;
3831
3832 default:
3833 break;
3834 }
3835
3836 /* Consume the token. */
3837 cp_lexer_consume_token (parser->lexer);
3838 }
3839 }
3840
3841 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3842 parameter is the PRAGMA token, allowing us to purge the entire pragma
3843 sequence. */
3844
3845 static void
3846 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3847 {
3848 cp_token *token;
3849
3850 parser->lexer->in_pragma = false;
3851
3852 do
3853 token = cp_lexer_consume_token (parser->lexer);
3854 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3855
3856 /* Ensure that the pragma is not parsed again. */
3857 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3858 }
3859
3860 /* Require pragma end of line, resyncing with it as necessary. The
3861 arguments are as for cp_parser_skip_to_pragma_eol. */
3862
3863 static void
3864 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3865 {
3866 parser->lexer->in_pragma = false;
3867 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3868 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3869 }
3870
3871 /* This is a simple wrapper around make_typename_type. When the id is
3872 an unresolved identifier node, we can provide a superior diagnostic
3873 using cp_parser_diagnose_invalid_type_name. */
3874
3875 static tree
3876 cp_parser_make_typename_type (cp_parser *parser, tree id,
3877 location_t id_location)
3878 {
3879 tree result;
3880 if (identifier_p (id))
3881 {
3882 result = make_typename_type (parser->scope, id, typename_type,
3883 /*complain=*/tf_none);
3884 if (result == error_mark_node)
3885 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3886 return result;
3887 }
3888 return make_typename_type (parser->scope, id, typename_type, tf_error);
3889 }
3890
3891 /* This is a wrapper around the
3892 make_{pointer,ptrmem,reference}_declarator functions that decides
3893 which one to call based on the CODE and CLASS_TYPE arguments. The
3894 CODE argument should be one of the values returned by
3895 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3896 appertain to the pointer or reference. */
3897
3898 static cp_declarator *
3899 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3900 cp_cv_quals cv_qualifiers,
3901 cp_declarator *target,
3902 tree attributes)
3903 {
3904 if (code == ERROR_MARK || target == cp_error_declarator)
3905 return cp_error_declarator;
3906
3907 if (code == INDIRECT_REF)
3908 if (class_type == NULL_TREE)
3909 return make_pointer_declarator (cv_qualifiers, target, attributes);
3910 else
3911 return make_ptrmem_declarator (cv_qualifiers, class_type,
3912 target, attributes);
3913 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3914 return make_reference_declarator (cv_qualifiers, target,
3915 false, attributes);
3916 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3917 return make_reference_declarator (cv_qualifiers, target,
3918 true, attributes);
3919 gcc_unreachable ();
3920 }
3921
3922 /* Create a new C++ parser. */
3923
3924 static cp_parser *
3925 cp_parser_new (void)
3926 {
3927 cp_parser *parser;
3928 cp_lexer *lexer;
3929 unsigned i;
3930
3931 /* cp_lexer_new_main is called before doing GC allocation because
3932 cp_lexer_new_main might load a PCH file. */
3933 lexer = cp_lexer_new_main ();
3934
3935 /* Initialize the binops_by_token so that we can get the tree
3936 directly from the token. */
3937 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3938 binops_by_token[binops[i].token_type] = binops[i];
3939
3940 parser = ggc_cleared_alloc<cp_parser> ();
3941 parser->lexer = lexer;
3942 parser->context = cp_parser_context_new (NULL);
3943
3944 /* For now, we always accept GNU extensions. */
3945 parser->allow_gnu_extensions_p = 1;
3946
3947 /* The `>' token is a greater-than operator, not the end of a
3948 template-id. */
3949 parser->greater_than_is_operator_p = true;
3950
3951 parser->default_arg_ok_p = true;
3952
3953 /* We are not parsing a constant-expression. */
3954 parser->integral_constant_expression_p = false;
3955 parser->allow_non_integral_constant_expression_p = false;
3956 parser->non_integral_constant_expression_p = false;
3957
3958 /* Local variable names are not forbidden. */
3959 parser->local_variables_forbidden_p = 0;
3960
3961 /* We are not processing an `extern "C"' declaration. */
3962 parser->in_unbraced_linkage_specification_p = false;
3963
3964 /* We are not processing a declarator. */
3965 parser->in_declarator_p = false;
3966
3967 /* We are not processing a template-argument-list. */
3968 parser->in_template_argument_list_p = false;
3969
3970 /* We are not in an iteration statement. */
3971 parser->in_statement = 0;
3972
3973 /* We are not in a switch statement. */
3974 parser->in_switch_statement_p = false;
3975
3976 /* We are not parsing a type-id inside an expression. */
3977 parser->in_type_id_in_expr_p = false;
3978
3979 /* String literals should be translated to the execution character set. */
3980 parser->translate_strings_p = true;
3981
3982 /* We are not parsing a function body. */
3983 parser->in_function_body = false;
3984
3985 /* We can correct until told otherwise. */
3986 parser->colon_corrects_to_scope_p = true;
3987
3988 /* The unparsed function queue is empty. */
3989 push_unparsed_function_queues (parser);
3990
3991 /* There are no classes being defined. */
3992 parser->num_classes_being_defined = 0;
3993
3994 /* No template parameters apply. */
3995 parser->num_template_parameter_lists = 0;
3996
3997 /* Special parsing data structures. */
3998 parser->omp_declare_simd = NULL;
3999 parser->oacc_routine = NULL;
4000
4001 /* Not declaring an implicit function template. */
4002 parser->auto_is_implicit_function_template_parm_p = false;
4003 parser->fully_implicit_function_template_p = false;
4004 parser->implicit_template_parms = 0;
4005 parser->implicit_template_scope = 0;
4006
4007 /* Allow constrained-type-specifiers. */
4008 parser->prevent_constrained_type_specifiers = 0;
4009
4010 /* We haven't yet seen an 'extern "C"'. */
4011 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4012
4013 return parser;
4014 }
4015
4016 /* Create a cp_lexer structure which will emit the tokens in CACHE
4017 and push it onto the parser's lexer stack. This is used for delayed
4018 parsing of in-class method bodies and default arguments, and should
4019 not be confused with tentative parsing. */
4020 static void
4021 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4022 {
4023 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4024 lexer->next = parser->lexer;
4025 parser->lexer = lexer;
4026
4027 /* Move the current source position to that of the first token in the
4028 new lexer. */
4029 cp_lexer_set_source_position_from_token (lexer->next_token);
4030 }
4031
4032 /* Pop the top lexer off the parser stack. This is never used for the
4033 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4034 static void
4035 cp_parser_pop_lexer (cp_parser *parser)
4036 {
4037 cp_lexer *lexer = parser->lexer;
4038 parser->lexer = lexer->next;
4039 cp_lexer_destroy (lexer);
4040
4041 /* Put the current source position back where it was before this
4042 lexer was pushed. */
4043 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4044 }
4045
4046 /* Lexical conventions [gram.lex] */
4047
4048 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4049 identifier. */
4050
4051 static cp_expr
4052 cp_parser_identifier (cp_parser* parser)
4053 {
4054 cp_token *token;
4055
4056 /* Look for the identifier. */
4057 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4058 /* Return the value. */
4059 if (token)
4060 return cp_expr (token->u.value, token->location);
4061 else
4062 return error_mark_node;
4063 }
4064
4065 /* Parse a sequence of adjacent string constants. Returns a
4066 TREE_STRING representing the combined, nul-terminated string
4067 constant. If TRANSLATE is true, translate the string to the
4068 execution character set. If WIDE_OK is true, a wide string is
4069 invalid here.
4070
4071 C++98 [lex.string] says that if a narrow string literal token is
4072 adjacent to a wide string literal token, the behavior is undefined.
4073 However, C99 6.4.5p4 says that this results in a wide string literal.
4074 We follow C99 here, for consistency with the C front end.
4075
4076 This code is largely lifted from lex_string() in c-lex.c.
4077
4078 FUTURE: ObjC++ will need to handle @-strings here. */
4079 static cp_expr
4080 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4081 bool lookup_udlit = true)
4082 {
4083 tree value;
4084 size_t count;
4085 struct obstack str_ob;
4086 struct obstack loc_ob;
4087 cpp_string str, istr, *strs;
4088 cp_token *tok;
4089 enum cpp_ttype type, curr_type;
4090 int have_suffix_p = 0;
4091 tree string_tree;
4092 tree suffix_id = NULL_TREE;
4093 bool curr_tok_is_userdef_p = false;
4094
4095 tok = cp_lexer_peek_token (parser->lexer);
4096 if (!cp_parser_is_string_literal (tok))
4097 {
4098 cp_parser_error (parser, "expected string-literal");
4099 return error_mark_node;
4100 }
4101
4102 location_t loc = tok->location;
4103
4104 if (cpp_userdef_string_p (tok->type))
4105 {
4106 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4107 curr_type = cpp_userdef_string_remove_type (tok->type);
4108 curr_tok_is_userdef_p = true;
4109 }
4110 else
4111 {
4112 string_tree = tok->u.value;
4113 curr_type = tok->type;
4114 }
4115 type = curr_type;
4116
4117 /* Try to avoid the overhead of creating and destroying an obstack
4118 for the common case of just one string. */
4119 if (!cp_parser_is_string_literal
4120 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4121 {
4122 cp_lexer_consume_token (parser->lexer);
4123
4124 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4125 str.len = TREE_STRING_LENGTH (string_tree);
4126 count = 1;
4127
4128 if (curr_tok_is_userdef_p)
4129 {
4130 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4131 have_suffix_p = 1;
4132 curr_type = cpp_userdef_string_remove_type (tok->type);
4133 }
4134 else
4135 curr_type = tok->type;
4136
4137 strs = &str;
4138 }
4139 else
4140 {
4141 location_t last_tok_loc = tok->location;
4142 gcc_obstack_init (&str_ob);
4143 gcc_obstack_init (&loc_ob);
4144 count = 0;
4145
4146 do
4147 {
4148 cp_lexer_consume_token (parser->lexer);
4149 count++;
4150 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4151 str.len = TREE_STRING_LENGTH (string_tree);
4152
4153 if (curr_tok_is_userdef_p)
4154 {
4155 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4156 if (have_suffix_p == 0)
4157 {
4158 suffix_id = curr_suffix_id;
4159 have_suffix_p = 1;
4160 }
4161 else if (have_suffix_p == 1
4162 && curr_suffix_id != suffix_id)
4163 {
4164 error ("inconsistent user-defined literal suffixes"
4165 " %qD and %qD in string literal",
4166 suffix_id, curr_suffix_id);
4167 have_suffix_p = -1;
4168 }
4169 curr_type = cpp_userdef_string_remove_type (tok->type);
4170 }
4171 else
4172 curr_type = tok->type;
4173
4174 if (type != curr_type)
4175 {
4176 if (type == CPP_STRING)
4177 type = curr_type;
4178 else if (curr_type != CPP_STRING)
4179 {
4180 rich_location rich_loc (line_table, tok->location);
4181 rich_loc.add_range (last_tok_loc);
4182 error_at (&rich_loc,
4183 "unsupported non-standard concatenation "
4184 "of string literals");
4185 }
4186 }
4187
4188 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4189 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4190
4191 last_tok_loc = tok->location;
4192
4193 tok = cp_lexer_peek_token (parser->lexer);
4194 if (cpp_userdef_string_p (tok->type))
4195 {
4196 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4197 curr_type = cpp_userdef_string_remove_type (tok->type);
4198 curr_tok_is_userdef_p = true;
4199 }
4200 else
4201 {
4202 string_tree = tok->u.value;
4203 curr_type = tok->type;
4204 curr_tok_is_userdef_p = false;
4205 }
4206 }
4207 while (cp_parser_is_string_literal (tok));
4208
4209 /* A string literal built by concatenation has its caret=start at
4210 the start of the initial string, and its finish at the finish of
4211 the final string literal. */
4212 loc = make_location (loc, loc, get_finish (last_tok_loc));
4213
4214 strs = (cpp_string *) obstack_finish (&str_ob);
4215 }
4216
4217 if (type != CPP_STRING && !wide_ok)
4218 {
4219 cp_parser_error (parser, "a wide string is invalid in this context");
4220 type = CPP_STRING;
4221 }
4222
4223 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4224 (parse_in, strs, count, &istr, type))
4225 {
4226 value = build_string (istr.len, (const char *)istr.text);
4227 free (CONST_CAST (unsigned char *, istr.text));
4228 if (count > 1)
4229 {
4230 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4231 gcc_assert (g_string_concat_db);
4232 g_string_concat_db->record_string_concatenation (count, locs);
4233 }
4234
4235 switch (type)
4236 {
4237 default:
4238 case CPP_STRING:
4239 TREE_TYPE (value) = char_array_type_node;
4240 break;
4241 case CPP_UTF8STRING:
4242 if (flag_char8_t)
4243 TREE_TYPE (value) = char8_array_type_node;
4244 else
4245 TREE_TYPE (value) = char_array_type_node;
4246 break;
4247 case CPP_STRING16:
4248 TREE_TYPE (value) = char16_array_type_node;
4249 break;
4250 case CPP_STRING32:
4251 TREE_TYPE (value) = char32_array_type_node;
4252 break;
4253 case CPP_WSTRING:
4254 TREE_TYPE (value) = wchar_array_type_node;
4255 break;
4256 }
4257
4258 value = fix_string_type (value);
4259
4260 if (have_suffix_p)
4261 {
4262 tree literal = build_userdef_literal (suffix_id, value,
4263 OT_NONE, NULL_TREE);
4264 if (lookup_udlit)
4265 value = cp_parser_userdef_string_literal (literal);
4266 else
4267 value = literal;
4268 }
4269 }
4270 else
4271 /* cpp_interpret_string has issued an error. */
4272 value = error_mark_node;
4273
4274 if (count > 1)
4275 {
4276 obstack_free (&str_ob, 0);
4277 obstack_free (&loc_ob, 0);
4278 }
4279
4280 return cp_expr (value, loc);
4281 }
4282
4283 /* Look up a literal operator with the name and the exact arguments. */
4284
4285 static tree
4286 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4287 {
4288 tree decl = lookup_name (name);
4289 if (!decl || !is_overloaded_fn (decl))
4290 return error_mark_node;
4291
4292 for (lkp_iterator iter (decl); iter; ++iter)
4293 {
4294 tree fn = *iter;
4295
4296 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4297 {
4298 unsigned int ix;
4299 bool found = true;
4300
4301 for (ix = 0;
4302 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4303 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4304 {
4305 tree tparm = TREE_VALUE (parmtypes);
4306 tree targ = TREE_TYPE ((*args)[ix]);
4307 bool ptr = TYPE_PTR_P (tparm);
4308 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4309 if ((ptr || arr || !same_type_p (tparm, targ))
4310 && (!ptr || !arr
4311 || !same_type_p (TREE_TYPE (tparm),
4312 TREE_TYPE (targ))))
4313 found = false;
4314 }
4315
4316 if (found
4317 && ix == vec_safe_length (args)
4318 /* May be this should be sufficient_parms_p instead,
4319 depending on how exactly should user-defined literals
4320 work in presence of default arguments on the literal
4321 operator parameters. */
4322 && parmtypes == void_list_node)
4323 return decl;
4324 }
4325 }
4326
4327 return error_mark_node;
4328 }
4329
4330 /* Parse a user-defined char constant. Returns a call to a user-defined
4331 literal operator taking the character as an argument. */
4332
4333 static cp_expr
4334 cp_parser_userdef_char_literal (cp_parser *parser)
4335 {
4336 cp_token *token = cp_lexer_consume_token (parser->lexer);
4337 tree literal = token->u.value;
4338 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4339 tree value = USERDEF_LITERAL_VALUE (literal);
4340 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4341 tree decl, result;
4342
4343 /* Build up a call to the user-defined operator */
4344 /* Lookup the name we got back from the id-expression. */
4345 vec<tree, va_gc> *args = make_tree_vector ();
4346 vec_safe_push (args, value);
4347 decl = lookup_literal_operator (name, args);
4348 if (!decl || decl == error_mark_node)
4349 {
4350 error ("unable to find character literal operator %qD with %qT argument",
4351 name, TREE_TYPE (value));
4352 release_tree_vector (args);
4353 return error_mark_node;
4354 }
4355 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4356 release_tree_vector (args);
4357 return result;
4358 }
4359
4360 /* A subroutine of cp_parser_userdef_numeric_literal to
4361 create a char... template parameter pack from a string node. */
4362
4363 static tree
4364 make_char_string_pack (tree value)
4365 {
4366 tree charvec;
4367 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4368 const char *str = TREE_STRING_POINTER (value);
4369 int i, len = TREE_STRING_LENGTH (value) - 1;
4370 tree argvec = make_tree_vec (1);
4371
4372 /* Fill in CHARVEC with all of the parameters. */
4373 charvec = make_tree_vec (len);
4374 for (i = 0; i < len; ++i)
4375 {
4376 unsigned char s[3] = { '\'', str[i], '\'' };
4377 cpp_string in = { 3, s };
4378 cpp_string out = { 0, 0 };
4379 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4380 return NULL_TREE;
4381 gcc_assert (out.len == 2);
4382 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4383 out.text[0]);
4384 }
4385
4386 /* Build the argument packs. */
4387 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4388
4389 TREE_VEC_ELT (argvec, 0) = argpack;
4390
4391 return argvec;
4392 }
4393
4394 /* A subroutine of cp_parser_userdef_numeric_literal to
4395 create a char... template parameter pack from a string node. */
4396
4397 static tree
4398 make_string_pack (tree value)
4399 {
4400 tree charvec;
4401 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4402 const unsigned char *str
4403 = (const unsigned char *) TREE_STRING_POINTER (value);
4404 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4405 int len = TREE_STRING_LENGTH (value) / sz - 1;
4406 tree argvec = make_tree_vec (2);
4407
4408 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4409 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4410
4411 /* First template parm is character type. */
4412 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4413
4414 /* Fill in CHARVEC with all of the parameters. */
4415 charvec = make_tree_vec (len);
4416 for (int i = 0; i < len; ++i)
4417 TREE_VEC_ELT (charvec, i)
4418 = double_int_to_tree (str_char_type_node,
4419 double_int::from_buffer (str + i * sz, sz));
4420
4421 /* Build the argument packs. */
4422 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4423
4424 TREE_VEC_ELT (argvec, 1) = argpack;
4425
4426 return argvec;
4427 }
4428
4429 /* Parse a user-defined numeric constant. returns a call to a user-defined
4430 literal operator. */
4431
4432 static cp_expr
4433 cp_parser_userdef_numeric_literal (cp_parser *parser)
4434 {
4435 cp_token *token = cp_lexer_consume_token (parser->lexer);
4436 tree literal = token->u.value;
4437 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4438 tree value = USERDEF_LITERAL_VALUE (literal);
4439 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4440 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4441 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4442 tree decl, result;
4443 vec<tree, va_gc> *args;
4444
4445 /* Look for a literal operator taking the exact type of numeric argument
4446 as the literal value. */
4447 args = make_tree_vector ();
4448 vec_safe_push (args, value);
4449 decl = lookup_literal_operator (name, args);
4450 if (decl && decl != error_mark_node)
4451 {
4452 result = finish_call_expr (decl, &args, false, true,
4453 tf_warning_or_error);
4454
4455 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4456 {
4457 warning_at (token->location, OPT_Woverflow,
4458 "integer literal exceeds range of %qT type",
4459 long_long_unsigned_type_node);
4460 }
4461 else
4462 {
4463 if (overflow > 0)
4464 warning_at (token->location, OPT_Woverflow,
4465 "floating literal exceeds range of %qT type",
4466 long_double_type_node);
4467 else if (overflow < 0)
4468 warning_at (token->location, OPT_Woverflow,
4469 "floating literal truncated to zero");
4470 }
4471
4472 release_tree_vector (args);
4473 return result;
4474 }
4475 release_tree_vector (args);
4476
4477 /* If the numeric argument didn't work, look for a raw literal
4478 operator taking a const char* argument consisting of the number
4479 in string format. */
4480 args = make_tree_vector ();
4481 vec_safe_push (args, num_string);
4482 decl = lookup_literal_operator (name, args);
4483 if (decl && decl != error_mark_node)
4484 {
4485 result = finish_call_expr (decl, &args, false, true,
4486 tf_warning_or_error);
4487 release_tree_vector (args);
4488 return result;
4489 }
4490 release_tree_vector (args);
4491
4492 /* If the raw literal didn't work, look for a non-type template
4493 function with parameter pack char.... Call the function with
4494 template parameter characters representing the number. */
4495 args = make_tree_vector ();
4496 decl = lookup_literal_operator (name, args);
4497 if (decl && decl != error_mark_node)
4498 {
4499 tree tmpl_args = make_char_string_pack (num_string);
4500 if (tmpl_args == NULL_TREE)
4501 {
4502 error ("failed to translate literal to execution character set %qT",
4503 num_string);
4504 return error_mark_node;
4505 }
4506 decl = lookup_template_function (decl, tmpl_args);
4507 result = finish_call_expr (decl, &args, false, true,
4508 tf_warning_or_error);
4509 release_tree_vector (args);
4510 return result;
4511 }
4512
4513 release_tree_vector (args);
4514
4515 /* In C++14 the standard library defines complex number suffixes that
4516 conflict with GNU extensions. Prefer them if <complex> is #included. */
4517 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4518 bool i14 = (cxx_dialect > cxx11
4519 && (id_equal (suffix_id, "i")
4520 || id_equal (suffix_id, "if")
4521 || id_equal (suffix_id, "il")));
4522 diagnostic_t kind = DK_ERROR;
4523 int opt = 0;
4524
4525 if (i14 && ext)
4526 {
4527 tree cxlit = lookup_qualified_name (std_node,
4528 get_identifier ("complex_literals"),
4529 0, false, false);
4530 if (cxlit == error_mark_node)
4531 {
4532 /* No <complex>, so pedwarn and use GNU semantics. */
4533 kind = DK_PEDWARN;
4534 opt = OPT_Wpedantic;
4535 }
4536 }
4537
4538 bool complained
4539 = emit_diagnostic (kind, input_location, opt,
4540 "unable to find numeric literal operator %qD", name);
4541
4542 if (!complained)
4543 /* Don't inform either. */;
4544 else if (i14)
4545 {
4546 inform (token->location, "add %<using namespace std::complex_literals%> "
4547 "(from <complex>) to enable the C++14 user-defined literal "
4548 "suffixes");
4549 if (ext)
4550 inform (token->location, "or use %<j%> instead of %<i%> for the "
4551 "GNU built-in suffix");
4552 }
4553 else if (!ext)
4554 inform (token->location, "use -fext-numeric-literals "
4555 "to enable more built-in suffixes");
4556
4557 if (kind == DK_ERROR)
4558 value = error_mark_node;
4559 else
4560 {
4561 /* Use the built-in semantics. */
4562 tree type;
4563 if (id_equal (suffix_id, "i"))
4564 {
4565 if (TREE_CODE (value) == INTEGER_CST)
4566 type = integer_type_node;
4567 else
4568 type = double_type_node;
4569 }
4570 else if (id_equal (suffix_id, "if"))
4571 type = float_type_node;
4572 else /* if (id_equal (suffix_id, "il")) */
4573 type = long_double_type_node;
4574
4575 value = build_complex (build_complex_type (type),
4576 fold_convert (type, integer_zero_node),
4577 fold_convert (type, value));
4578 }
4579
4580 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4581 /* Avoid repeated diagnostics. */
4582 token->u.value = value;
4583 return value;
4584 }
4585
4586 /* Parse a user-defined string constant. Returns a call to a user-defined
4587 literal operator taking a character pointer and the length of the string
4588 as arguments. */
4589
4590 static tree
4591 cp_parser_userdef_string_literal (tree literal)
4592 {
4593 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4594 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4595 tree value = USERDEF_LITERAL_VALUE (literal);
4596 int len = TREE_STRING_LENGTH (value)
4597 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4598 tree decl;
4599
4600 /* Build up a call to the user-defined operator. */
4601 /* Lookup the name we got back from the id-expression. */
4602 releasing_vec rargs;
4603 vec<tree, va_gc> *&args = rargs.get_ref();
4604 vec_safe_push (args, value);
4605 vec_safe_push (args, build_int_cst (size_type_node, len));
4606 decl = lookup_literal_operator (name, args);
4607
4608 if (decl && decl != error_mark_node)
4609 return finish_call_expr (decl, &args, false, true,
4610 tf_warning_or_error);
4611
4612 /* Look for a suitable template function, either (C++20) with a single
4613 parameter of class type, or (N3599) with typename parameter CharT and
4614 parameter pack CharT... */
4615 args->truncate (0);
4616 decl = lookup_literal_operator (name, args);
4617 if (decl && decl != error_mark_node)
4618 {
4619 /* Use resolve_nondeduced_context to try to choose one form of template
4620 or the other. */
4621 tree tmpl_args = make_tree_vec (1);
4622 TREE_VEC_ELT (tmpl_args, 0) = value;
4623 decl = lookup_template_function (decl, tmpl_args);
4624 tree res = resolve_nondeduced_context (decl, tf_none);
4625 if (DECL_P (res))
4626 decl = res;
4627 else
4628 {
4629 TREE_OPERAND (decl, 1) = make_string_pack (value);
4630 res = resolve_nondeduced_context (decl, tf_none);
4631 if (DECL_P (res))
4632 decl = res;
4633 }
4634 if (!DECL_P (decl) && cxx_dialect > cxx17)
4635 TREE_OPERAND (decl, 1) = tmpl_args;
4636 return finish_call_expr (decl, &args, false, true,
4637 tf_warning_or_error);
4638 }
4639
4640 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4641 name, TREE_TYPE (value), size_type_node);
4642 return error_mark_node;
4643 }
4644
4645
4646 /* Basic concepts [gram.basic] */
4647
4648 /* Parse a translation-unit.
4649
4650 translation-unit:
4651 declaration-seq [opt] */
4652
4653 static void
4654 cp_parser_translation_unit (cp_parser* parser)
4655 {
4656 gcc_checking_assert (!cp_error_declarator);
4657
4658 /* Create the declarator obstack. */
4659 gcc_obstack_init (&declarator_obstack);
4660 /* Create the error declarator. */
4661 cp_error_declarator = make_declarator (cdk_error);
4662 /* Create the empty parameter list. */
4663 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4664 UNKNOWN_LOCATION);
4665 /* Remember where the base of the declarator obstack lies. */
4666 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4667
4668 bool implicit_extern_c = false;
4669
4670 for (;;)
4671 {
4672 cp_token *token = cp_lexer_peek_token (parser->lexer);
4673
4674 /* If we're entering or exiting a region that's implicitly
4675 extern "C", modify the lang context appropriately. */
4676 if (implicit_extern_c
4677 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4678 {
4679 implicit_extern_c = !implicit_extern_c;
4680 if (implicit_extern_c)
4681 push_lang_context (lang_name_c);
4682 else
4683 pop_lang_context ();
4684 }
4685
4686 if (token->type == CPP_EOF)
4687 break;
4688
4689 if (token->type == CPP_CLOSE_BRACE)
4690 {
4691 cp_parser_error (parser, "expected declaration");
4692 cp_lexer_consume_token (parser->lexer);
4693 /* If the next token is now a `;', consume it. */
4694 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4695 cp_lexer_consume_token (parser->lexer);
4696 }
4697 else
4698 cp_parser_toplevel_declaration (parser);
4699 }
4700
4701 /* Get rid of the token array; we don't need it any more. */
4702 cp_lexer_destroy (parser->lexer);
4703 parser->lexer = NULL;
4704
4705 /* The EOF should have reset this. */
4706 gcc_checking_assert (!implicit_extern_c);
4707
4708 /* Make sure the declarator obstack was fully cleaned up. */
4709 gcc_assert (obstack_next_free (&declarator_obstack)
4710 == declarator_obstack_base);
4711 }
4712
4713 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4714 decltype context. */
4715
4716 static inline tsubst_flags_t
4717 complain_flags (bool decltype_p)
4718 {
4719 tsubst_flags_t complain = tf_warning_or_error;
4720 if (decltype_p)
4721 complain |= tf_decltype;
4722 return complain;
4723 }
4724
4725 /* We're about to parse a collection of statements. If we're currently
4726 parsing tentatively, set up a firewall so that any nested
4727 cp_parser_commit_to_tentative_parse won't affect the current context. */
4728
4729 static cp_token_position
4730 cp_parser_start_tentative_firewall (cp_parser *parser)
4731 {
4732 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4733 return 0;
4734
4735 cp_parser_parse_tentatively (parser);
4736 cp_parser_commit_to_topmost_tentative_parse (parser);
4737 return cp_lexer_token_position (parser->lexer, false);
4738 }
4739
4740 /* We've finished parsing the collection of statements. Wrap up the
4741 firewall and replace the relevant tokens with the parsed form. */
4742
4743 static void
4744 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4745 tree expr)
4746 {
4747 if (!start)
4748 return;
4749
4750 /* Finish the firewall level. */
4751 cp_parser_parse_definitely (parser);
4752 /* And remember the result of the parse for when we try again. */
4753 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4754 token->type = CPP_PREPARSED_EXPR;
4755 token->u.value = expr;
4756 token->keyword = RID_MAX;
4757 cp_lexer_purge_tokens_after (parser->lexer, start);
4758 }
4759
4760 /* Like the above functions, but let the user modify the tokens. Used by
4761 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4762 later parses, so it makes sense to localize the effects of
4763 cp_parser_commit_to_tentative_parse. */
4764
4765 struct tentative_firewall
4766 {
4767 cp_parser *parser;
4768 bool set;
4769
4770 tentative_firewall (cp_parser *p): parser(p)
4771 {
4772 /* If we're currently parsing tentatively, start a committed level as a
4773 firewall and then an inner tentative parse. */
4774 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4775 {
4776 cp_parser_parse_tentatively (parser);
4777 cp_parser_commit_to_topmost_tentative_parse (parser);
4778 cp_parser_parse_tentatively (parser);
4779 }
4780 }
4781
4782 ~tentative_firewall()
4783 {
4784 if (set)
4785 {
4786 /* Finish the inner tentative parse and the firewall, propagating any
4787 uncommitted error state to the outer tentative parse. */
4788 bool err = cp_parser_error_occurred (parser);
4789 cp_parser_parse_definitely (parser);
4790 cp_parser_parse_definitely (parser);
4791 if (err)
4792 cp_parser_simulate_error (parser);
4793 }
4794 }
4795 };
4796
4797 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4798 This class is for tracking such a matching pair of symbols.
4799 In particular, it tracks the location of the first token,
4800 so that if the second token is missing, we can highlight the
4801 location of the first token when notifying the user about the
4802 problem. */
4803
4804 template <typename traits_t>
4805 class token_pair
4806 {
4807 public:
4808 /* token_pair's ctor. */
4809 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4810
4811 /* If the next token is the opening symbol for this pair, consume it and
4812 return true.
4813 Otherwise, issue an error and return false.
4814 In either case, record the location of the opening token. */
4815
4816 bool require_open (cp_parser *parser)
4817 {
4818 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4819 return cp_parser_require (parser, traits_t::open_token_type,
4820 traits_t::required_token_open);
4821 }
4822
4823 /* Consume the next token from PARSER, recording its location as
4824 that of the opening token within the pair. */
4825
4826 cp_token * consume_open (cp_parser *parser)
4827 {
4828 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4829 gcc_assert (tok->type == traits_t::open_token_type);
4830 m_open_loc = tok->location;
4831 return tok;
4832 }
4833
4834 /* If the next token is the closing symbol for this pair, consume it
4835 and return it.
4836 Otherwise, issue an error, highlighting the location of the
4837 corresponding opening token, and return NULL. */
4838
4839 cp_token *require_close (cp_parser *parser) const
4840 {
4841 return cp_parser_require (parser, traits_t::close_token_type,
4842 traits_t::required_token_close,
4843 m_open_loc);
4844 }
4845
4846 private:
4847 location_t m_open_loc;
4848 };
4849
4850 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4851
4852 struct matching_paren_traits
4853 {
4854 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4855 static const enum required_token required_token_open = RT_OPEN_PAREN;
4856 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4857 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4858 };
4859
4860 /* "matching_parens" is a token_pair<T> class for tracking matching
4861 pairs of parentheses. */
4862
4863 typedef token_pair<matching_paren_traits> matching_parens;
4864
4865 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4866
4867 struct matching_brace_traits
4868 {
4869 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4870 static const enum required_token required_token_open = RT_OPEN_BRACE;
4871 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4872 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4873 };
4874
4875 /* "matching_braces" is a token_pair<T> class for tracking matching
4876 pairs of braces. */
4877
4878 typedef token_pair<matching_brace_traits> matching_braces;
4879
4880
4881 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4882 enclosing parentheses. */
4883
4884 static cp_expr
4885 cp_parser_statement_expr (cp_parser *parser)
4886 {
4887 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4888
4889 /* Consume the '('. */
4890 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4891 matching_parens parens;
4892 parens.consume_open (parser);
4893 /* Start the statement-expression. */
4894 tree expr = begin_stmt_expr ();
4895 /* Parse the compound-statement. */
4896 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4897 /* Finish up. */
4898 expr = finish_stmt_expr (expr, false);
4899 /* Consume the ')'. */
4900 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4901 if (!parens.require_close (parser))
4902 cp_parser_skip_to_end_of_statement (parser);
4903
4904 cp_parser_end_tentative_firewall (parser, start, expr);
4905 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4906 return cp_expr (expr, combined_loc);
4907 }
4908
4909 /* Expressions [gram.expr] */
4910
4911 /* Parse a fold-operator.
4912
4913 fold-operator:
4914 - * / % ^ & | = < > << >>
4915 = -= *= /= %= ^= &= |= <<= >>=
4916 == != <= >= && || , .* ->*
4917
4918 This returns the tree code corresponding to the matched operator
4919 as an int. When the current token matches a compound assignment
4920 opertor, the resulting tree code is the negative value of the
4921 non-assignment operator. */
4922
4923 static int
4924 cp_parser_fold_operator (cp_token *token)
4925 {
4926 switch (token->type)
4927 {
4928 case CPP_PLUS: return PLUS_EXPR;
4929 case CPP_MINUS: return MINUS_EXPR;
4930 case CPP_MULT: return MULT_EXPR;
4931 case CPP_DIV: return TRUNC_DIV_EXPR;
4932 case CPP_MOD: return TRUNC_MOD_EXPR;
4933 case CPP_XOR: return BIT_XOR_EXPR;
4934 case CPP_AND: return BIT_AND_EXPR;
4935 case CPP_OR: return BIT_IOR_EXPR;
4936 case CPP_LSHIFT: return LSHIFT_EXPR;
4937 case CPP_RSHIFT: return RSHIFT_EXPR;
4938
4939 case CPP_EQ: return -NOP_EXPR;
4940 case CPP_PLUS_EQ: return -PLUS_EXPR;
4941 case CPP_MINUS_EQ: return -MINUS_EXPR;
4942 case CPP_MULT_EQ: return -MULT_EXPR;
4943 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4944 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4945 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4946 case CPP_AND_EQ: return -BIT_AND_EXPR;
4947 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4948 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4949 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4950
4951 case CPP_EQ_EQ: return EQ_EXPR;
4952 case CPP_NOT_EQ: return NE_EXPR;
4953 case CPP_LESS: return LT_EXPR;
4954 case CPP_GREATER: return GT_EXPR;
4955 case CPP_LESS_EQ: return LE_EXPR;
4956 case CPP_GREATER_EQ: return GE_EXPR;
4957
4958 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4959 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4960
4961 case CPP_COMMA: return COMPOUND_EXPR;
4962
4963 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4964 case CPP_DEREF_STAR: return MEMBER_REF;
4965
4966 default: return ERROR_MARK;
4967 }
4968 }
4969
4970 /* Returns true if CODE indicates a binary expression, which is not allowed in
4971 the LHS of a fold-expression. More codes will need to be added to use this
4972 function in other contexts. */
4973
4974 static bool
4975 is_binary_op (tree_code code)
4976 {
4977 switch (code)
4978 {
4979 case PLUS_EXPR:
4980 case POINTER_PLUS_EXPR:
4981 case MINUS_EXPR:
4982 case MULT_EXPR:
4983 case TRUNC_DIV_EXPR:
4984 case TRUNC_MOD_EXPR:
4985 case BIT_XOR_EXPR:
4986 case BIT_AND_EXPR:
4987 case BIT_IOR_EXPR:
4988 case LSHIFT_EXPR:
4989 case RSHIFT_EXPR:
4990
4991 case MODOP_EXPR:
4992
4993 case EQ_EXPR:
4994 case NE_EXPR:
4995 case LE_EXPR:
4996 case GE_EXPR:
4997 case LT_EXPR:
4998 case GT_EXPR:
4999
5000 case TRUTH_ANDIF_EXPR:
5001 case TRUTH_ORIF_EXPR:
5002
5003 case COMPOUND_EXPR:
5004
5005 case DOTSTAR_EXPR:
5006 case MEMBER_REF:
5007 return true;
5008
5009 default:
5010 return false;
5011 }
5012 }
5013
5014 /* If the next token is a suitable fold operator, consume it and return as
5015 the function above. */
5016
5017 static int
5018 cp_parser_fold_operator (cp_parser *parser)
5019 {
5020 cp_token* token = cp_lexer_peek_token (parser->lexer);
5021 int code = cp_parser_fold_operator (token);
5022 if (code != ERROR_MARK)
5023 cp_lexer_consume_token (parser->lexer);
5024 return code;
5025 }
5026
5027 /* Parse a fold-expression.
5028
5029 fold-expression:
5030 ( ... folding-operator cast-expression)
5031 ( cast-expression folding-operator ... )
5032 ( cast-expression folding operator ... folding-operator cast-expression)
5033
5034 Note that the '(' and ')' are matched in primary expression. */
5035
5036 static cp_expr
5037 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5038 {
5039 cp_id_kind pidk;
5040
5041 // Left fold.
5042 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5043 {
5044 cp_lexer_consume_token (parser->lexer);
5045 int op = cp_parser_fold_operator (parser);
5046 if (op == ERROR_MARK)
5047 {
5048 cp_parser_error (parser, "expected binary operator");
5049 return error_mark_node;
5050 }
5051
5052 tree expr = cp_parser_cast_expression (parser, false, false,
5053 false, &pidk);
5054 if (expr == error_mark_node)
5055 return error_mark_node;
5056 return finish_left_unary_fold_expr (expr, op);
5057 }
5058
5059 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5060 int op = cp_parser_fold_operator (parser);
5061 if (op == ERROR_MARK)
5062 {
5063 cp_parser_error (parser, "expected binary operator");
5064 return error_mark_node;
5065 }
5066
5067 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5068 {
5069 cp_parser_error (parser, "expected ...");
5070 return error_mark_node;
5071 }
5072 cp_lexer_consume_token (parser->lexer);
5073
5074 /* The operands of a fold-expression are cast-expressions, so binary or
5075 conditional expressions are not allowed. We check this here to avoid
5076 tentative parsing. */
5077 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5078 /* OK, the expression was parenthesized. */;
5079 else if (is_binary_op (TREE_CODE (expr1)))
5080 error_at (location_of (expr1),
5081 "binary expression in operand of fold-expression");
5082 else if (TREE_CODE (expr1) == COND_EXPR
5083 || (REFERENCE_REF_P (expr1)
5084 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5085 error_at (location_of (expr1),
5086 "conditional expression in operand of fold-expression");
5087
5088 // Right fold.
5089 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5090 return finish_right_unary_fold_expr (expr1, op);
5091
5092 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5093 {
5094 cp_parser_error (parser, "mismatched operator in fold-expression");
5095 return error_mark_node;
5096 }
5097 cp_lexer_consume_token (parser->lexer);
5098
5099 // Binary left or right fold.
5100 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5101 if (expr2 == error_mark_node)
5102 return error_mark_node;
5103 return finish_binary_fold_expr (expr1, expr2, op);
5104 }
5105
5106 /* Parse a primary-expression.
5107
5108 primary-expression:
5109 literal
5110 this
5111 ( expression )
5112 id-expression
5113 lambda-expression (C++11)
5114
5115 GNU Extensions:
5116
5117 primary-expression:
5118 ( compound-statement )
5119 __builtin_va_arg ( assignment-expression , type-id )
5120 __builtin_offsetof ( type-id , offsetof-expression )
5121
5122 C++ Extensions:
5123 __has_nothrow_assign ( type-id )
5124 __has_nothrow_constructor ( type-id )
5125 __has_nothrow_copy ( type-id )
5126 __has_trivial_assign ( type-id )
5127 __has_trivial_constructor ( type-id )
5128 __has_trivial_copy ( type-id )
5129 __has_trivial_destructor ( type-id )
5130 __has_virtual_destructor ( type-id )
5131 __is_abstract ( type-id )
5132 __is_base_of ( type-id , type-id )
5133 __is_class ( type-id )
5134 __is_empty ( type-id )
5135 __is_enum ( type-id )
5136 __is_final ( type-id )
5137 __is_literal_type ( type-id )
5138 __is_pod ( type-id )
5139 __is_polymorphic ( type-id )
5140 __is_std_layout ( type-id )
5141 __is_trivial ( type-id )
5142 __is_union ( type-id )
5143
5144 Objective-C++ Extension:
5145
5146 primary-expression:
5147 objc-expression
5148
5149 literal:
5150 __null
5151
5152 ADDRESS_P is true iff this expression was immediately preceded by
5153 "&" and therefore might denote a pointer-to-member. CAST_P is true
5154 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5155 true iff this expression is a template argument.
5156
5157 Returns a representation of the expression. Upon return, *IDK
5158 indicates what kind of id-expression (if any) was present. */
5159
5160 static cp_expr
5161 cp_parser_primary_expression (cp_parser *parser,
5162 bool address_p,
5163 bool cast_p,
5164 bool template_arg_p,
5165 bool decltype_p,
5166 cp_id_kind *idk)
5167 {
5168 cp_token *token = NULL;
5169
5170 /* Assume the primary expression is not an id-expression. */
5171 *idk = CP_ID_KIND_NONE;
5172
5173 /* Peek at the next token. */
5174 token = cp_lexer_peek_token (parser->lexer);
5175 switch ((int) token->type)
5176 {
5177 /* literal:
5178 integer-literal
5179 character-literal
5180 floating-literal
5181 string-literal
5182 boolean-literal
5183 pointer-literal
5184 user-defined-literal */
5185 case CPP_CHAR:
5186 case CPP_CHAR16:
5187 case CPP_CHAR32:
5188 case CPP_WCHAR:
5189 case CPP_UTF8CHAR:
5190 case CPP_NUMBER:
5191 case CPP_PREPARSED_EXPR:
5192 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5193 return cp_parser_userdef_numeric_literal (parser);
5194 token = cp_lexer_consume_token (parser->lexer);
5195 if (TREE_CODE (token->u.value) == FIXED_CST)
5196 {
5197 error_at (token->location,
5198 "fixed-point types not supported in C++");
5199 return error_mark_node;
5200 }
5201 /* Floating-point literals are only allowed in an integral
5202 constant expression if they are cast to an integral or
5203 enumeration type. */
5204 if (TREE_CODE (token->u.value) == REAL_CST
5205 && parser->integral_constant_expression_p
5206 && pedantic)
5207 {
5208 /* CAST_P will be set even in invalid code like "int(2.7 +
5209 ...)". Therefore, we have to check that the next token
5210 is sure to end the cast. */
5211 if (cast_p)
5212 {
5213 cp_token *next_token;
5214
5215 next_token = cp_lexer_peek_token (parser->lexer);
5216 if (/* The comma at the end of an
5217 enumerator-definition. */
5218 next_token->type != CPP_COMMA
5219 /* The curly brace at the end of an enum-specifier. */
5220 && next_token->type != CPP_CLOSE_BRACE
5221 /* The end of a statement. */
5222 && next_token->type != CPP_SEMICOLON
5223 /* The end of the cast-expression. */
5224 && next_token->type != CPP_CLOSE_PAREN
5225 /* The end of an array bound. */
5226 && next_token->type != CPP_CLOSE_SQUARE
5227 /* The closing ">" in a template-argument-list. */
5228 && (next_token->type != CPP_GREATER
5229 || parser->greater_than_is_operator_p)
5230 /* C++0x only: A ">>" treated like two ">" tokens,
5231 in a template-argument-list. */
5232 && (next_token->type != CPP_RSHIFT
5233 || (cxx_dialect == cxx98)
5234 || parser->greater_than_is_operator_p))
5235 cast_p = false;
5236 }
5237
5238 /* If we are within a cast, then the constraint that the
5239 cast is to an integral or enumeration type will be
5240 checked at that point. If we are not within a cast, then
5241 this code is invalid. */
5242 if (!cast_p)
5243 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5244 }
5245 return (cp_expr (token->u.value, token->location)
5246 .maybe_add_location_wrapper ());
5247
5248 case CPP_CHAR_USERDEF:
5249 case CPP_CHAR16_USERDEF:
5250 case CPP_CHAR32_USERDEF:
5251 case CPP_WCHAR_USERDEF:
5252 case CPP_UTF8CHAR_USERDEF:
5253 return cp_parser_userdef_char_literal (parser);
5254
5255 case CPP_STRING:
5256 case CPP_STRING16:
5257 case CPP_STRING32:
5258 case CPP_WSTRING:
5259 case CPP_UTF8STRING:
5260 case CPP_STRING_USERDEF:
5261 case CPP_STRING16_USERDEF:
5262 case CPP_STRING32_USERDEF:
5263 case CPP_WSTRING_USERDEF:
5264 case CPP_UTF8STRING_USERDEF:
5265 /* ??? Should wide strings be allowed when parser->translate_strings_p
5266 is false (i.e. in attributes)? If not, we can kill the third
5267 argument to cp_parser_string_literal. */
5268 return (cp_parser_string_literal (parser,
5269 parser->translate_strings_p,
5270 true)
5271 .maybe_add_location_wrapper ());
5272
5273 case CPP_OPEN_PAREN:
5274 /* If we see `( { ' then we are looking at the beginning of
5275 a GNU statement-expression. */
5276 if (cp_parser_allow_gnu_extensions_p (parser)
5277 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5278 {
5279 /* Statement-expressions are not allowed by the standard. */
5280 pedwarn (token->location, OPT_Wpedantic,
5281 "ISO C++ forbids braced-groups within expressions");
5282
5283 /* And they're not allowed outside of a function-body; you
5284 cannot, for example, write:
5285
5286 int i = ({ int j = 3; j + 1; });
5287
5288 at class or namespace scope. */
5289 if (!parser->in_function_body
5290 || parser->in_template_argument_list_p)
5291 {
5292 error_at (token->location,
5293 "statement-expressions are not allowed outside "
5294 "functions nor in template-argument lists");
5295 cp_parser_skip_to_end_of_block_or_statement (parser);
5296 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5297 cp_lexer_consume_token (parser->lexer);
5298 return error_mark_node;
5299 }
5300 else
5301 return cp_parser_statement_expr (parser);
5302 }
5303 /* Otherwise it's a normal parenthesized expression. */
5304 {
5305 cp_expr expr;
5306 bool saved_greater_than_is_operator_p;
5307
5308 location_t open_paren_loc = token->location;
5309
5310 /* Consume the `('. */
5311 matching_parens parens;
5312 parens.consume_open (parser);
5313 /* Within a parenthesized expression, a `>' token is always
5314 the greater-than operator. */
5315 saved_greater_than_is_operator_p
5316 = parser->greater_than_is_operator_p;
5317 parser->greater_than_is_operator_p = true;
5318
5319 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5320 /* Left fold expression. */
5321 expr = NULL_TREE;
5322 else
5323 /* Parse the parenthesized expression. */
5324 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5325
5326 token = cp_lexer_peek_token (parser->lexer);
5327 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5328 {
5329 expr = cp_parser_fold_expression (parser, expr);
5330 if (expr != error_mark_node
5331 && cxx_dialect < cxx17
5332 && !in_system_header_at (input_location))
5333 pedwarn (input_location, 0, "fold-expressions only available "
5334 "with -std=c++17 or -std=gnu++17");
5335 }
5336 else
5337 /* Let the front end know that this expression was
5338 enclosed in parentheses. This matters in case, for
5339 example, the expression is of the form `A::B', since
5340 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5341 not. */
5342 expr = finish_parenthesized_expr (expr);
5343
5344 /* DR 705: Wrapping an unqualified name in parentheses
5345 suppresses arg-dependent lookup. We want to pass back
5346 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5347 (c++/37862), but none of the others. */
5348 if (*idk != CP_ID_KIND_QUALIFIED)
5349 *idk = CP_ID_KIND_NONE;
5350
5351 /* The `>' token might be the end of a template-id or
5352 template-parameter-list now. */
5353 parser->greater_than_is_operator_p
5354 = saved_greater_than_is_operator_p;
5355
5356 /* Consume the `)'. */
5357 token = cp_lexer_peek_token (parser->lexer);
5358 location_t close_paren_loc = token->location;
5359 expr.set_range (open_paren_loc, close_paren_loc);
5360 if (!parens.require_close (parser)
5361 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5362 cp_parser_skip_to_end_of_statement (parser);
5363
5364 return expr;
5365 }
5366
5367 case CPP_OPEN_SQUARE:
5368 {
5369 if (c_dialect_objc ())
5370 {
5371 /* We might have an Objective-C++ message. */
5372 cp_parser_parse_tentatively (parser);
5373 tree msg = cp_parser_objc_message_expression (parser);
5374 /* If that works out, we're done ... */
5375 if (cp_parser_parse_definitely (parser))
5376 return msg;
5377 /* ... else, fall though to see if it's a lambda. */
5378 }
5379 cp_expr lam = cp_parser_lambda_expression (parser);
5380 /* Don't warn about a failed tentative parse. */
5381 if (cp_parser_error_occurred (parser))
5382 return error_mark_node;
5383 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5384 return lam;
5385 }
5386
5387 case CPP_OBJC_STRING:
5388 if (c_dialect_objc ())
5389 /* We have an Objective-C++ string literal. */
5390 return cp_parser_objc_expression (parser);
5391 cp_parser_error (parser, "expected primary-expression");
5392 return error_mark_node;
5393
5394 case CPP_KEYWORD:
5395 switch (token->keyword)
5396 {
5397 /* These two are the boolean literals. */
5398 case RID_TRUE:
5399 cp_lexer_consume_token (parser->lexer);
5400 return cp_expr (boolean_true_node, token->location);
5401 case RID_FALSE:
5402 cp_lexer_consume_token (parser->lexer);
5403 return cp_expr (boolean_false_node, token->location);
5404
5405 /* The `__null' literal. */
5406 case RID_NULL:
5407 cp_lexer_consume_token (parser->lexer);
5408 return cp_expr (null_node, token->location);
5409
5410 /* The `nullptr' literal. */
5411 case RID_NULLPTR:
5412 cp_lexer_consume_token (parser->lexer);
5413 return cp_expr (nullptr_node, token->location);
5414
5415 /* Recognize the `this' keyword. */
5416 case RID_THIS:
5417 cp_lexer_consume_token (parser->lexer);
5418 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5419 {
5420 error_at (token->location,
5421 "%<this%> may not be used in this context");
5422 return error_mark_node;
5423 }
5424 /* Pointers cannot appear in constant-expressions. */
5425 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5426 return error_mark_node;
5427 return cp_expr (finish_this_expr (), token->location);
5428
5429 /* The `operator' keyword can be the beginning of an
5430 id-expression. */
5431 case RID_OPERATOR:
5432 goto id_expression;
5433
5434 case RID_FUNCTION_NAME:
5435 case RID_PRETTY_FUNCTION_NAME:
5436 case RID_C99_FUNCTION_NAME:
5437 {
5438 non_integral_constant name;
5439
5440 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5441 __func__ are the names of variables -- but they are
5442 treated specially. Therefore, they are handled here,
5443 rather than relying on the generic id-expression logic
5444 below. Grammatically, these names are id-expressions.
5445
5446 Consume the token. */
5447 token = cp_lexer_consume_token (parser->lexer);
5448
5449 switch (token->keyword)
5450 {
5451 case RID_FUNCTION_NAME:
5452 name = NIC_FUNC_NAME;
5453 break;
5454 case RID_PRETTY_FUNCTION_NAME:
5455 name = NIC_PRETTY_FUNC;
5456 break;
5457 case RID_C99_FUNCTION_NAME:
5458 name = NIC_C99_FUNC;
5459 break;
5460 default:
5461 gcc_unreachable ();
5462 }
5463
5464 if (cp_parser_non_integral_constant_expression (parser, name))
5465 return error_mark_node;
5466
5467 /* Look up the name. */
5468 return finish_fname (token->u.value);
5469 }
5470
5471 case RID_VA_ARG:
5472 {
5473 tree expression;
5474 tree type;
5475 location_t type_location;
5476 location_t start_loc
5477 = cp_lexer_peek_token (parser->lexer)->location;
5478 /* The `__builtin_va_arg' construct is used to handle
5479 `va_arg'. Consume the `__builtin_va_arg' token. */
5480 cp_lexer_consume_token (parser->lexer);
5481 /* Look for the opening `('. */
5482 matching_parens parens;
5483 parens.require_open (parser);
5484 /* Now, parse the assignment-expression. */
5485 expression = cp_parser_assignment_expression (parser);
5486 /* Look for the `,'. */
5487 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5488 type_location = cp_lexer_peek_token (parser->lexer)->location;
5489 /* Parse the type-id. */
5490 {
5491 type_id_in_expr_sentinel s (parser);
5492 type = cp_parser_type_id (parser);
5493 }
5494 /* Look for the closing `)'. */
5495 location_t finish_loc
5496 = cp_lexer_peek_token (parser->lexer)->location;
5497 parens.require_close (parser);
5498 /* Using `va_arg' in a constant-expression is not
5499 allowed. */
5500 if (cp_parser_non_integral_constant_expression (parser,
5501 NIC_VA_ARG))
5502 return error_mark_node;
5503 /* Construct a location of the form:
5504 __builtin_va_arg (v, int)
5505 ~~~~~~~~~~~~~~~~~~~~~^~~~
5506 with the caret at the type, ranging from the start of the
5507 "__builtin_va_arg" token to the close paren. */
5508 location_t combined_loc
5509 = make_location (type_location, start_loc, finish_loc);
5510 return build_x_va_arg (combined_loc, expression, type);
5511 }
5512
5513 case RID_OFFSETOF:
5514 return cp_parser_builtin_offsetof (parser);
5515
5516 case RID_HAS_NOTHROW_ASSIGN:
5517 case RID_HAS_NOTHROW_CONSTRUCTOR:
5518 case RID_HAS_NOTHROW_COPY:
5519 case RID_HAS_TRIVIAL_ASSIGN:
5520 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5521 case RID_HAS_TRIVIAL_COPY:
5522 case RID_HAS_TRIVIAL_DESTRUCTOR:
5523 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5524 case RID_HAS_VIRTUAL_DESTRUCTOR:
5525 case RID_IS_ABSTRACT:
5526 case RID_IS_AGGREGATE:
5527 case RID_IS_BASE_OF:
5528 case RID_IS_CLASS:
5529 case RID_IS_EMPTY:
5530 case RID_IS_ENUM:
5531 case RID_IS_FINAL:
5532 case RID_IS_LITERAL_TYPE:
5533 case RID_IS_POD:
5534 case RID_IS_POLYMORPHIC:
5535 case RID_IS_SAME_AS:
5536 case RID_IS_STD_LAYOUT:
5537 case RID_IS_TRIVIAL:
5538 case RID_IS_TRIVIALLY_ASSIGNABLE:
5539 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5540 case RID_IS_TRIVIALLY_COPYABLE:
5541 case RID_IS_UNION:
5542 case RID_IS_ASSIGNABLE:
5543 case RID_IS_CONSTRUCTIBLE:
5544 return cp_parser_trait_expr (parser, token->keyword);
5545
5546 // C++ concepts
5547 case RID_REQUIRES:
5548 return cp_parser_requires_expression (parser);
5549
5550 /* Objective-C++ expressions. */
5551 case RID_AT_ENCODE:
5552 case RID_AT_PROTOCOL:
5553 case RID_AT_SELECTOR:
5554 return cp_parser_objc_expression (parser);
5555
5556 case RID_TEMPLATE:
5557 if (parser->in_function_body
5558 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5559 == CPP_LESS))
5560 {
5561 error_at (token->location,
5562 "a template declaration cannot appear at block scope");
5563 cp_parser_skip_to_end_of_block_or_statement (parser);
5564 return error_mark_node;
5565 }
5566 /* FALLTHRU */
5567 default:
5568 cp_parser_error (parser, "expected primary-expression");
5569 return error_mark_node;
5570 }
5571
5572 /* An id-expression can start with either an identifier, a
5573 `::' as the beginning of a qualified-id, or the "operator"
5574 keyword. */
5575 case CPP_NAME:
5576 case CPP_SCOPE:
5577 case CPP_TEMPLATE_ID:
5578 case CPP_NESTED_NAME_SPECIFIER:
5579 {
5580 id_expression:
5581 cp_expr id_expression;
5582 cp_expr decl;
5583 const char *error_msg;
5584 bool template_p;
5585 bool done;
5586 cp_token *id_expr_token;
5587
5588 /* Parse the id-expression. */
5589 id_expression
5590 = cp_parser_id_expression (parser,
5591 /*template_keyword_p=*/false,
5592 /*check_dependency_p=*/true,
5593 &template_p,
5594 /*declarator_p=*/false,
5595 /*optional_p=*/false);
5596 if (id_expression == error_mark_node)
5597 return error_mark_node;
5598 id_expr_token = token;
5599 token = cp_lexer_peek_token (parser->lexer);
5600 done = (token->type != CPP_OPEN_SQUARE
5601 && token->type != CPP_OPEN_PAREN
5602 && token->type != CPP_DOT
5603 && token->type != CPP_DEREF
5604 && token->type != CPP_PLUS_PLUS
5605 && token->type != CPP_MINUS_MINUS);
5606 /* If we have a template-id, then no further lookup is
5607 required. If the template-id was for a template-class, we
5608 will sometimes have a TYPE_DECL at this point. */
5609 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5610 || TREE_CODE (id_expression) == TYPE_DECL)
5611 decl = id_expression;
5612 /* Look up the name. */
5613 else
5614 {
5615 tree ambiguous_decls;
5616
5617 /* If we already know that this lookup is ambiguous, then
5618 we've already issued an error message; there's no reason
5619 to check again. */
5620 if (id_expr_token->type == CPP_NAME
5621 && id_expr_token->error_reported)
5622 {
5623 cp_parser_simulate_error (parser);
5624 return error_mark_node;
5625 }
5626
5627 decl = cp_parser_lookup_name (parser, id_expression,
5628 none_type,
5629 template_p,
5630 /*is_namespace=*/false,
5631 /*check_dependency=*/true,
5632 &ambiguous_decls,
5633 id_expression.get_location ());
5634 /* If the lookup was ambiguous, an error will already have
5635 been issued. */
5636 if (ambiguous_decls)
5637 return error_mark_node;
5638
5639 /* In Objective-C++, we may have an Objective-C 2.0
5640 dot-syntax for classes here. */
5641 if (c_dialect_objc ()
5642 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5643 && TREE_CODE (decl) == TYPE_DECL
5644 && objc_is_class_name (decl))
5645 {
5646 tree component;
5647 cp_lexer_consume_token (parser->lexer);
5648 component = cp_parser_identifier (parser);
5649 if (component == error_mark_node)
5650 return error_mark_node;
5651
5652 tree result = objc_build_class_component_ref (id_expression,
5653 component);
5654 /* Build a location of the form:
5655 expr.component
5656 ~~~~~^~~~~~~~~
5657 with caret at the start of the component name (at
5658 input_location), ranging from the start of the id_expression
5659 to the end of the component name. */
5660 location_t combined_loc
5661 = make_location (input_location, id_expression.get_start (),
5662 get_finish (input_location));
5663 protected_set_expr_location (result, combined_loc);
5664 return result;
5665 }
5666
5667 /* In Objective-C++, an instance variable (ivar) may be preferred
5668 to whatever cp_parser_lookup_name() found.
5669 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5670 rest of c-family, we have to do a little extra work to preserve
5671 any location information in cp_expr "decl". Given that
5672 objc_lookup_ivar is implemented in "c-family" and "objc", we
5673 have a trip through the pure "tree" type, rather than cp_expr.
5674 Naively copying it back to "decl" would implicitly give the
5675 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5676 store an EXPR_LOCATION. Hence we only update "decl" (and
5677 hence its location_t) if we get back a different tree node. */
5678 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5679 id_expression);
5680 if (decl_tree != decl.get_value ())
5681 decl = cp_expr (decl_tree);
5682
5683 /* If name lookup gives us a SCOPE_REF, then the
5684 qualifying scope was dependent. */
5685 if (TREE_CODE (decl) == SCOPE_REF)
5686 {
5687 /* At this point, we do not know if DECL is a valid
5688 integral constant expression. We assume that it is
5689 in fact such an expression, so that code like:
5690
5691 template <int N> struct A {
5692 int a[B<N>::i];
5693 };
5694
5695 is accepted. At template-instantiation time, we
5696 will check that B<N>::i is actually a constant. */
5697 return decl;
5698 }
5699 /* Check to see if DECL is a local variable in a context
5700 where that is forbidden. */
5701 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
5702 && local_variable_p (decl))
5703 {
5704 error_at (id_expression.get_location (),
5705 "local variable %qD may not appear in this context",
5706 decl.get_value ());
5707 return error_mark_node;
5708 }
5709 }
5710
5711 decl = (finish_id_expression
5712 (id_expression, decl, parser->scope,
5713 idk,
5714 parser->integral_constant_expression_p,
5715 parser->allow_non_integral_constant_expression_p,
5716 &parser->non_integral_constant_expression_p,
5717 template_p, done, address_p,
5718 template_arg_p,
5719 &error_msg,
5720 id_expression.get_location ()));
5721 if (error_msg)
5722 cp_parser_error (parser, error_msg);
5723 /* Build a location for an id-expression of the form:
5724 ::ns::id
5725 ~~~~~~^~
5726 or:
5727 id
5728 ^~
5729 i.e. from the start of the first token to the end of the final
5730 token, with the caret at the start of the unqualified-id. */
5731 location_t caret_loc = get_pure_location (id_expression.get_location ());
5732 location_t start_loc = get_start (id_expr_token->location);
5733 location_t finish_loc = get_finish (id_expression.get_location ());
5734 location_t combined_loc
5735 = make_location (caret_loc, start_loc, finish_loc);
5736
5737 decl.set_location (combined_loc);
5738 return decl;
5739 }
5740
5741 /* Anything else is an error. */
5742 default:
5743 cp_parser_error (parser, "expected primary-expression");
5744 return error_mark_node;
5745 }
5746 }
5747
5748 static inline cp_expr
5749 cp_parser_primary_expression (cp_parser *parser,
5750 bool address_p,
5751 bool cast_p,
5752 bool template_arg_p,
5753 cp_id_kind *idk)
5754 {
5755 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5756 /*decltype*/false, idk);
5757 }
5758
5759 /* Parse an id-expression.
5760
5761 id-expression:
5762 unqualified-id
5763 qualified-id
5764
5765 qualified-id:
5766 :: [opt] nested-name-specifier template [opt] unqualified-id
5767 :: identifier
5768 :: operator-function-id
5769 :: template-id
5770
5771 Return a representation of the unqualified portion of the
5772 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5773 a `::' or nested-name-specifier.
5774
5775 Often, if the id-expression was a qualified-id, the caller will
5776 want to make a SCOPE_REF to represent the qualified-id. This
5777 function does not do this in order to avoid wastefully creating
5778 SCOPE_REFs when they are not required.
5779
5780 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5781 `template' keyword.
5782
5783 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5784 uninstantiated templates.
5785
5786 If *TEMPLATE_P is non-NULL, it is set to true iff the
5787 `template' keyword is used to explicitly indicate that the entity
5788 named is a template.
5789
5790 If DECLARATOR_P is true, the id-expression is appearing as part of
5791 a declarator, rather than as part of an expression. */
5792
5793 static cp_expr
5794 cp_parser_id_expression (cp_parser *parser,
5795 bool template_keyword_p,
5796 bool check_dependency_p,
5797 bool *template_p,
5798 bool declarator_p,
5799 bool optional_p)
5800 {
5801 bool global_scope_p;
5802 bool nested_name_specifier_p;
5803
5804 /* Assume the `template' keyword was not used. */
5805 if (template_p)
5806 *template_p = template_keyword_p;
5807
5808 /* Look for the optional `::' operator. */
5809 global_scope_p
5810 = (!template_keyword_p
5811 && (cp_parser_global_scope_opt (parser,
5812 /*current_scope_valid_p=*/false)
5813 != NULL_TREE));
5814
5815 /* Look for the optional nested-name-specifier. */
5816 nested_name_specifier_p
5817 = (cp_parser_nested_name_specifier_opt (parser,
5818 /*typename_keyword_p=*/false,
5819 check_dependency_p,
5820 /*type_p=*/false,
5821 declarator_p,
5822 template_keyword_p)
5823 != NULL_TREE);
5824
5825 /* If there is a nested-name-specifier, then we are looking at
5826 the first qualified-id production. */
5827 if (nested_name_specifier_p)
5828 {
5829 tree saved_scope;
5830 tree saved_object_scope;
5831 tree saved_qualifying_scope;
5832 cp_expr unqualified_id;
5833 bool is_template;
5834
5835 /* See if the next token is the `template' keyword. */
5836 if (!template_p)
5837 template_p = &is_template;
5838 *template_p = cp_parser_optional_template_keyword (parser);
5839 /* Name lookup we do during the processing of the
5840 unqualified-id might obliterate SCOPE. */
5841 saved_scope = parser->scope;
5842 saved_object_scope = parser->object_scope;
5843 saved_qualifying_scope = parser->qualifying_scope;
5844 /* Process the final unqualified-id. */
5845 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5846 check_dependency_p,
5847 declarator_p,
5848 /*optional_p=*/false);
5849 /* Restore the SAVED_SCOPE for our caller. */
5850 parser->scope = saved_scope;
5851 parser->object_scope = saved_object_scope;
5852 parser->qualifying_scope = saved_qualifying_scope;
5853
5854 return unqualified_id;
5855 }
5856 /* Otherwise, if we are in global scope, then we are looking at one
5857 of the other qualified-id productions. */
5858 else if (global_scope_p)
5859 {
5860 cp_token *token;
5861 tree id;
5862
5863 /* Peek at the next token. */
5864 token = cp_lexer_peek_token (parser->lexer);
5865
5866 /* If it's an identifier, and the next token is not a "<", then
5867 we can avoid the template-id case. This is an optimization
5868 for this common case. */
5869 if (token->type == CPP_NAME
5870 && !cp_parser_nth_token_starts_template_argument_list_p
5871 (parser, 2))
5872 return cp_parser_identifier (parser);
5873
5874 cp_parser_parse_tentatively (parser);
5875 /* Try a template-id. */
5876 id = cp_parser_template_id (parser,
5877 /*template_keyword_p=*/false,
5878 /*check_dependency_p=*/true,
5879 none_type,
5880 declarator_p);
5881 /* If that worked, we're done. */
5882 if (cp_parser_parse_definitely (parser))
5883 return id;
5884
5885 /* Peek at the next token. (Changes in the token buffer may
5886 have invalidated the pointer obtained above.) */
5887 token = cp_lexer_peek_token (parser->lexer);
5888
5889 switch (token->type)
5890 {
5891 case CPP_NAME:
5892 return cp_parser_identifier (parser);
5893
5894 case CPP_KEYWORD:
5895 if (token->keyword == RID_OPERATOR)
5896 return cp_parser_operator_function_id (parser);
5897 /* Fall through. */
5898
5899 default:
5900 cp_parser_error (parser, "expected id-expression");
5901 return error_mark_node;
5902 }
5903 }
5904 else
5905 return cp_parser_unqualified_id (parser, template_keyword_p,
5906 /*check_dependency_p=*/true,
5907 declarator_p,
5908 optional_p);
5909 }
5910
5911 /* Parse an unqualified-id.
5912
5913 unqualified-id:
5914 identifier
5915 operator-function-id
5916 conversion-function-id
5917 ~ class-name
5918 template-id
5919
5920 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5921 keyword, in a construct like `A::template ...'.
5922
5923 Returns a representation of unqualified-id. For the `identifier'
5924 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5925 production a BIT_NOT_EXPR is returned; the operand of the
5926 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5927 other productions, see the documentation accompanying the
5928 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5929 names are looked up in uninstantiated templates. If DECLARATOR_P
5930 is true, the unqualified-id is appearing as part of a declarator,
5931 rather than as part of an expression. */
5932
5933 static cp_expr
5934 cp_parser_unqualified_id (cp_parser* parser,
5935 bool template_keyword_p,
5936 bool check_dependency_p,
5937 bool declarator_p,
5938 bool optional_p)
5939 {
5940 cp_token *token;
5941
5942 /* Peek at the next token. */
5943 token = cp_lexer_peek_token (parser->lexer);
5944
5945 switch ((int) token->type)
5946 {
5947 case CPP_NAME:
5948 {
5949 tree id;
5950
5951 /* We don't know yet whether or not this will be a
5952 template-id. */
5953 cp_parser_parse_tentatively (parser);
5954 /* Try a template-id. */
5955 id = cp_parser_template_id (parser, template_keyword_p,
5956 check_dependency_p,
5957 none_type,
5958 declarator_p);
5959 /* If it worked, we're done. */
5960 if (cp_parser_parse_definitely (parser))
5961 return id;
5962 /* Otherwise, it's an ordinary identifier. */
5963 return cp_parser_identifier (parser);
5964 }
5965
5966 case CPP_TEMPLATE_ID:
5967 return cp_parser_template_id (parser, template_keyword_p,
5968 check_dependency_p,
5969 none_type,
5970 declarator_p);
5971
5972 case CPP_COMPL:
5973 {
5974 tree type_decl;
5975 tree qualifying_scope;
5976 tree object_scope;
5977 tree scope;
5978 bool done;
5979
5980 /* Consume the `~' token. */
5981 cp_lexer_consume_token (parser->lexer);
5982 /* Parse the class-name. The standard, as written, seems to
5983 say that:
5984
5985 template <typename T> struct S { ~S (); };
5986 template <typename T> S<T>::~S() {}
5987
5988 is invalid, since `~' must be followed by a class-name, but
5989 `S<T>' is dependent, and so not known to be a class.
5990 That's not right; we need to look in uninstantiated
5991 templates. A further complication arises from:
5992
5993 template <typename T> void f(T t) {
5994 t.T::~T();
5995 }
5996
5997 Here, it is not possible to look up `T' in the scope of `T'
5998 itself. We must look in both the current scope, and the
5999 scope of the containing complete expression.
6000
6001 Yet another issue is:
6002
6003 struct S {
6004 int S;
6005 ~S();
6006 };
6007
6008 S::~S() {}
6009
6010 The standard does not seem to say that the `S' in `~S'
6011 should refer to the type `S' and not the data member
6012 `S::S'. */
6013
6014 /* DR 244 says that we look up the name after the "~" in the
6015 same scope as we looked up the qualifying name. That idea
6016 isn't fully worked out; it's more complicated than that. */
6017 scope = parser->scope;
6018 object_scope = parser->object_scope;
6019 qualifying_scope = parser->qualifying_scope;
6020
6021 /* Check for invalid scopes. */
6022 if (scope == error_mark_node)
6023 {
6024 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6025 cp_lexer_consume_token (parser->lexer);
6026 return error_mark_node;
6027 }
6028 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6029 {
6030 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6031 error_at (token->location,
6032 "scope %qT before %<~%> is not a class-name",
6033 scope);
6034 cp_parser_simulate_error (parser);
6035 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6036 cp_lexer_consume_token (parser->lexer);
6037 return error_mark_node;
6038 }
6039 gcc_assert (!scope || TYPE_P (scope));
6040
6041 /* If the name is of the form "X::~X" it's OK even if X is a
6042 typedef. */
6043 token = cp_lexer_peek_token (parser->lexer);
6044 if (scope
6045 && token->type == CPP_NAME
6046 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6047 != CPP_LESS)
6048 && (token->u.value == TYPE_IDENTIFIER (scope)
6049 || (CLASS_TYPE_P (scope)
6050 && constructor_name_p (token->u.value, scope))))
6051 {
6052 cp_lexer_consume_token (parser->lexer);
6053 return build_nt (BIT_NOT_EXPR, scope);
6054 }
6055
6056 /* ~auto means the destructor of whatever the object is. */
6057 if (cp_parser_is_keyword (token, RID_AUTO))
6058 {
6059 if (cxx_dialect < cxx14)
6060 pedwarn (input_location, 0,
6061 "%<~auto%> only available with "
6062 "-std=c++14 or -std=gnu++14");
6063 cp_lexer_consume_token (parser->lexer);
6064 return build_nt (BIT_NOT_EXPR, make_auto ());
6065 }
6066
6067 /* If there was an explicit qualification (S::~T), first look
6068 in the scope given by the qualification (i.e., S).
6069
6070 Note: in the calls to cp_parser_class_name below we pass
6071 typename_type so that lookup finds the injected-class-name
6072 rather than the constructor. */
6073 done = false;
6074 type_decl = NULL_TREE;
6075 if (scope)
6076 {
6077 cp_parser_parse_tentatively (parser);
6078 type_decl = cp_parser_class_name (parser,
6079 /*typename_keyword_p=*/false,
6080 /*template_keyword_p=*/false,
6081 typename_type,
6082 /*check_dependency=*/false,
6083 /*class_head_p=*/false,
6084 declarator_p);
6085 if (cp_parser_parse_definitely (parser))
6086 done = true;
6087 }
6088 /* In "N::S::~S", look in "N" as well. */
6089 if (!done && scope && qualifying_scope)
6090 {
6091 cp_parser_parse_tentatively (parser);
6092 parser->scope = qualifying_scope;
6093 parser->object_scope = NULL_TREE;
6094 parser->qualifying_scope = NULL_TREE;
6095 type_decl
6096 = cp_parser_class_name (parser,
6097 /*typename_keyword_p=*/false,
6098 /*template_keyword_p=*/false,
6099 typename_type,
6100 /*check_dependency=*/false,
6101 /*class_head_p=*/false,
6102 declarator_p);
6103 if (cp_parser_parse_definitely (parser))
6104 done = true;
6105 }
6106 /* In "p->S::~T", look in the scope given by "*p" as well. */
6107 else if (!done && object_scope)
6108 {
6109 cp_parser_parse_tentatively (parser);
6110 parser->scope = object_scope;
6111 parser->object_scope = NULL_TREE;
6112 parser->qualifying_scope = NULL_TREE;
6113 type_decl
6114 = cp_parser_class_name (parser,
6115 /*typename_keyword_p=*/false,
6116 /*template_keyword_p=*/false,
6117 typename_type,
6118 /*check_dependency=*/false,
6119 /*class_head_p=*/false,
6120 declarator_p);
6121 if (cp_parser_parse_definitely (parser))
6122 done = true;
6123 }
6124 /* Look in the surrounding context. */
6125 if (!done)
6126 {
6127 parser->scope = NULL_TREE;
6128 parser->object_scope = NULL_TREE;
6129 parser->qualifying_scope = NULL_TREE;
6130 if (processing_template_decl)
6131 cp_parser_parse_tentatively (parser);
6132 type_decl
6133 = cp_parser_class_name (parser,
6134 /*typename_keyword_p=*/false,
6135 /*template_keyword_p=*/false,
6136 typename_type,
6137 /*check_dependency=*/false,
6138 /*class_head_p=*/false,
6139 declarator_p);
6140 if (processing_template_decl
6141 && ! cp_parser_parse_definitely (parser))
6142 {
6143 /* We couldn't find a type with this name. If we're parsing
6144 tentatively, fail and try something else. */
6145 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6146 {
6147 cp_parser_simulate_error (parser);
6148 return error_mark_node;
6149 }
6150 /* Otherwise, accept it and check for a match at instantiation
6151 time. */
6152 type_decl = cp_parser_identifier (parser);
6153 if (type_decl != error_mark_node)
6154 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6155 return type_decl;
6156 }
6157 }
6158 /* If an error occurred, assume that the name of the
6159 destructor is the same as the name of the qualifying
6160 class. That allows us to keep parsing after running
6161 into ill-formed destructor names. */
6162 if (type_decl == error_mark_node && scope)
6163 return build_nt (BIT_NOT_EXPR, scope);
6164 else if (type_decl == error_mark_node)
6165 return error_mark_node;
6166
6167 /* Check that destructor name and scope match. */
6168 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6169 {
6170 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6171 error_at (token->location,
6172 "declaration of %<~%T%> as member of %qT",
6173 type_decl, scope);
6174 cp_parser_simulate_error (parser);
6175 return error_mark_node;
6176 }
6177
6178 /* [class.dtor]
6179
6180 A typedef-name that names a class shall not be used as the
6181 identifier in the declarator for a destructor declaration. */
6182 if (declarator_p
6183 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6184 && !DECL_SELF_REFERENCE_P (type_decl)
6185 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6186 error_at (token->location,
6187 "typedef-name %qD used as destructor declarator",
6188 type_decl);
6189
6190 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6191 }
6192
6193 case CPP_KEYWORD:
6194 if (token->keyword == RID_OPERATOR)
6195 {
6196 cp_expr id;
6197
6198 /* This could be a template-id, so we try that first. */
6199 cp_parser_parse_tentatively (parser);
6200 /* Try a template-id. */
6201 id = cp_parser_template_id (parser, template_keyword_p,
6202 /*check_dependency_p=*/true,
6203 none_type,
6204 declarator_p);
6205 /* If that worked, we're done. */
6206 if (cp_parser_parse_definitely (parser))
6207 return id;
6208 /* We still don't know whether we're looking at an
6209 operator-function-id or a conversion-function-id. */
6210 cp_parser_parse_tentatively (parser);
6211 /* Try an operator-function-id. */
6212 id = cp_parser_operator_function_id (parser);
6213 /* If that didn't work, try a conversion-function-id. */
6214 if (!cp_parser_parse_definitely (parser))
6215 id = cp_parser_conversion_function_id (parser);
6216
6217 return id;
6218 }
6219 /* Fall through. */
6220
6221 default:
6222 if (optional_p)
6223 return NULL_TREE;
6224 cp_parser_error (parser, "expected unqualified-id");
6225 return error_mark_node;
6226 }
6227 }
6228
6229 /* Parse an (optional) nested-name-specifier.
6230
6231 nested-name-specifier: [C++98]
6232 class-or-namespace-name :: nested-name-specifier [opt]
6233 class-or-namespace-name :: template nested-name-specifier [opt]
6234
6235 nested-name-specifier: [C++0x]
6236 type-name ::
6237 namespace-name ::
6238 nested-name-specifier identifier ::
6239 nested-name-specifier template [opt] simple-template-id ::
6240
6241 PARSER->SCOPE should be set appropriately before this function is
6242 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6243 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6244 in name lookups.
6245
6246 Sets PARSER->SCOPE to the class (TYPE) or namespace
6247 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6248 it unchanged if there is no nested-name-specifier. Returns the new
6249 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6250
6251 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6252 part of a declaration and/or decl-specifier. */
6253
6254 static tree
6255 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6256 bool typename_keyword_p,
6257 bool check_dependency_p,
6258 bool type_p,
6259 bool is_declaration,
6260 bool template_keyword_p /* = false */)
6261 {
6262 bool success = false;
6263 cp_token_position start = 0;
6264 cp_token *token;
6265
6266 /* Remember where the nested-name-specifier starts. */
6267 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6268 {
6269 start = cp_lexer_token_position (parser->lexer, false);
6270 push_deferring_access_checks (dk_deferred);
6271 }
6272
6273 while (true)
6274 {
6275 tree new_scope;
6276 tree old_scope;
6277 tree saved_qualifying_scope;
6278
6279 /* Spot cases that cannot be the beginning of a
6280 nested-name-specifier. */
6281 token = cp_lexer_peek_token (parser->lexer);
6282
6283 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6284 the already parsed nested-name-specifier. */
6285 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6286 {
6287 /* Grab the nested-name-specifier and continue the loop. */
6288 cp_parser_pre_parsed_nested_name_specifier (parser);
6289 /* If we originally encountered this nested-name-specifier
6290 with IS_DECLARATION set to false, we will not have
6291 resolved TYPENAME_TYPEs, so we must do so here. */
6292 if (is_declaration
6293 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6294 {
6295 new_scope = resolve_typename_type (parser->scope,
6296 /*only_current_p=*/false);
6297 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6298 parser->scope = new_scope;
6299 }
6300 success = true;
6301 continue;
6302 }
6303
6304 /* Spot cases that cannot be the beginning of a
6305 nested-name-specifier. On the second and subsequent times
6306 through the loop, we look for the `template' keyword. */
6307 if (success && token->keyword == RID_TEMPLATE)
6308 ;
6309 /* A template-id can start a nested-name-specifier. */
6310 else if (token->type == CPP_TEMPLATE_ID)
6311 ;
6312 /* DR 743: decltype can be used in a nested-name-specifier. */
6313 else if (token_is_decltype (token))
6314 ;
6315 else
6316 {
6317 /* If the next token is not an identifier, then it is
6318 definitely not a type-name or namespace-name. */
6319 if (token->type != CPP_NAME)
6320 break;
6321 /* If the following token is neither a `<' (to begin a
6322 template-id), nor a `::', then we are not looking at a
6323 nested-name-specifier. */
6324 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6325
6326 if (token->type == CPP_COLON
6327 && parser->colon_corrects_to_scope_p
6328 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6329 {
6330 gcc_rich_location richloc (token->location);
6331 richloc.add_fixit_replace ("::");
6332 error_at (&richloc,
6333 "found %<:%> in nested-name-specifier, "
6334 "expected %<::%>");
6335 token->type = CPP_SCOPE;
6336 }
6337
6338 if (token->type != CPP_SCOPE
6339 && !cp_parser_nth_token_starts_template_argument_list_p
6340 (parser, 2))
6341 break;
6342 }
6343
6344 /* The nested-name-specifier is optional, so we parse
6345 tentatively. */
6346 cp_parser_parse_tentatively (parser);
6347
6348 /* Look for the optional `template' keyword, if this isn't the
6349 first time through the loop. */
6350 if (success)
6351 template_keyword_p = cp_parser_optional_template_keyword (parser);
6352
6353 /* Save the old scope since the name lookup we are about to do
6354 might destroy it. */
6355 old_scope = parser->scope;
6356 saved_qualifying_scope = parser->qualifying_scope;
6357 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6358 look up names in "X<T>::I" in order to determine that "Y" is
6359 a template. So, if we have a typename at this point, we make
6360 an effort to look through it. */
6361 if (is_declaration
6362 && !typename_keyword_p
6363 && parser->scope
6364 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6365 parser->scope = resolve_typename_type (parser->scope,
6366 /*only_current_p=*/false);
6367 /* Parse the qualifying entity. */
6368 new_scope
6369 = cp_parser_qualifying_entity (parser,
6370 typename_keyword_p,
6371 template_keyword_p,
6372 check_dependency_p,
6373 type_p,
6374 is_declaration);
6375 /* Look for the `::' token. */
6376 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6377
6378 /* If we found what we wanted, we keep going; otherwise, we're
6379 done. */
6380 if (!cp_parser_parse_definitely (parser))
6381 {
6382 bool error_p = false;
6383
6384 /* Restore the OLD_SCOPE since it was valid before the
6385 failed attempt at finding the last
6386 class-or-namespace-name. */
6387 parser->scope = old_scope;
6388 parser->qualifying_scope = saved_qualifying_scope;
6389
6390 /* If the next token is a decltype, and the one after that is a
6391 `::', then the decltype has failed to resolve to a class or
6392 enumeration type. Give this error even when parsing
6393 tentatively since it can't possibly be valid--and we're going
6394 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6395 won't get another chance.*/
6396 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6397 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6398 == CPP_SCOPE))
6399 {
6400 token = cp_lexer_consume_token (parser->lexer);
6401 error_at (token->location, "decltype evaluates to %qT, "
6402 "which is not a class or enumeration type",
6403 token->u.tree_check_value->value);
6404 parser->scope = error_mark_node;
6405 error_p = true;
6406 /* As below. */
6407 success = true;
6408 cp_lexer_consume_token (parser->lexer);
6409 }
6410
6411 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6412 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6413 {
6414 /* If we have a non-type template-id followed by ::, it can't
6415 possibly be valid. */
6416 token = cp_lexer_peek_token (parser->lexer);
6417 tree tid = token->u.tree_check_value->value;
6418 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6419 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6420 {
6421 tree tmpl = NULL_TREE;
6422 if (is_overloaded_fn (tid))
6423 {
6424 tree fns = get_fns (tid);
6425 if (OVL_SINGLE_P (fns))
6426 tmpl = OVL_FIRST (fns);
6427 error_at (token->location, "function template-id %qD "
6428 "in nested-name-specifier", tid);
6429 }
6430 else
6431 {
6432 /* Variable template. */
6433 tmpl = TREE_OPERAND (tid, 0);
6434 gcc_assert (variable_template_p (tmpl));
6435 error_at (token->location, "variable template-id %qD "
6436 "in nested-name-specifier", tid);
6437 }
6438 if (tmpl)
6439 inform (DECL_SOURCE_LOCATION (tmpl),
6440 "%qD declared here", tmpl);
6441
6442 parser->scope = error_mark_node;
6443 error_p = true;
6444 /* As below. */
6445 success = true;
6446 cp_lexer_consume_token (parser->lexer);
6447 cp_lexer_consume_token (parser->lexer);
6448 }
6449 }
6450
6451 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6452 break;
6453 /* If the next token is an identifier, and the one after
6454 that is a `::', then any valid interpretation would have
6455 found a class-or-namespace-name. */
6456 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6457 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6458 == CPP_SCOPE)
6459 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6460 != CPP_COMPL))
6461 {
6462 token = cp_lexer_consume_token (parser->lexer);
6463 if (!error_p)
6464 {
6465 if (!token->error_reported)
6466 {
6467 tree decl;
6468 tree ambiguous_decls;
6469
6470 decl = cp_parser_lookup_name (parser, token->u.value,
6471 none_type,
6472 /*is_template=*/false,
6473 /*is_namespace=*/false,
6474 /*check_dependency=*/true,
6475 &ambiguous_decls,
6476 token->location);
6477 if (TREE_CODE (decl) == TEMPLATE_DECL)
6478 error_at (token->location,
6479 "%qD used without template arguments",
6480 decl);
6481 else if (ambiguous_decls)
6482 {
6483 // cp_parser_lookup_name has the same diagnostic,
6484 // thus make sure to emit it at most once.
6485 if (cp_parser_uncommitted_to_tentative_parse_p
6486 (parser))
6487 {
6488 error_at (token->location,
6489 "reference to %qD is ambiguous",
6490 token->u.value);
6491 print_candidates (ambiguous_decls);
6492 }
6493 decl = error_mark_node;
6494 }
6495 else
6496 {
6497 if (cxx_dialect != cxx98)
6498 cp_parser_name_lookup_error
6499 (parser, token->u.value, decl, NLE_NOT_CXX98,
6500 token->location);
6501 else
6502 cp_parser_name_lookup_error
6503 (parser, token->u.value, decl, NLE_CXX98,
6504 token->location);
6505 }
6506 }
6507 parser->scope = error_mark_node;
6508 error_p = true;
6509 /* Treat this as a successful nested-name-specifier
6510 due to:
6511
6512 [basic.lookup.qual]
6513
6514 If the name found is not a class-name (clause
6515 _class_) or namespace-name (_namespace.def_), the
6516 program is ill-formed. */
6517 success = true;
6518 }
6519 cp_lexer_consume_token (parser->lexer);
6520 }
6521 break;
6522 }
6523 /* We've found one valid nested-name-specifier. */
6524 success = true;
6525 /* Name lookup always gives us a DECL. */
6526 if (TREE_CODE (new_scope) == TYPE_DECL)
6527 new_scope = TREE_TYPE (new_scope);
6528 /* Uses of "template" must be followed by actual templates. */
6529 if (template_keyword_p
6530 && !(CLASS_TYPE_P (new_scope)
6531 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6532 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6533 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6534 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6535 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6536 == TEMPLATE_ID_EXPR)))
6537 permerror (input_location, TYPE_P (new_scope)
6538 ? G_("%qT is not a template")
6539 : G_("%qD is not a template"),
6540 new_scope);
6541 /* If it is a class scope, try to complete it; we are about to
6542 be looking up names inside the class. */
6543 if (TYPE_P (new_scope)
6544 /* Since checking types for dependency can be expensive,
6545 avoid doing it if the type is already complete. */
6546 && !COMPLETE_TYPE_P (new_scope)
6547 /* Do not try to complete dependent types. */
6548 && !dependent_type_p (new_scope))
6549 {
6550 new_scope = complete_type (new_scope);
6551 /* If it is a typedef to current class, use the current
6552 class instead, as the typedef won't have any names inside
6553 it yet. */
6554 if (!COMPLETE_TYPE_P (new_scope)
6555 && currently_open_class (new_scope))
6556 new_scope = TYPE_MAIN_VARIANT (new_scope);
6557 }
6558 /* Make sure we look in the right scope the next time through
6559 the loop. */
6560 parser->scope = new_scope;
6561 }
6562
6563 /* If parsing tentatively, replace the sequence of tokens that makes
6564 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6565 token. That way, should we re-parse the token stream, we will
6566 not have to repeat the effort required to do the parse, nor will
6567 we issue duplicate error messages. */
6568 if (success && start)
6569 {
6570 cp_token *token;
6571
6572 token = cp_lexer_token_at (parser->lexer, start);
6573 /* Reset the contents of the START token. */
6574 token->type = CPP_NESTED_NAME_SPECIFIER;
6575 /* Retrieve any deferred checks. Do not pop this access checks yet
6576 so the memory will not be reclaimed during token replacing below. */
6577 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6578 token->u.tree_check_value->value = parser->scope;
6579 token->u.tree_check_value->checks = get_deferred_access_checks ();
6580 token->u.tree_check_value->qualifying_scope =
6581 parser->qualifying_scope;
6582 token->keyword = RID_MAX;
6583
6584 /* Purge all subsequent tokens. */
6585 cp_lexer_purge_tokens_after (parser->lexer, start);
6586 }
6587
6588 if (start)
6589 pop_to_parent_deferring_access_checks ();
6590
6591 return success ? parser->scope : NULL_TREE;
6592 }
6593
6594 /* Parse a nested-name-specifier. See
6595 cp_parser_nested_name_specifier_opt for details. This function
6596 behaves identically, except that it will an issue an error if no
6597 nested-name-specifier is present. */
6598
6599 static tree
6600 cp_parser_nested_name_specifier (cp_parser *parser,
6601 bool typename_keyword_p,
6602 bool check_dependency_p,
6603 bool type_p,
6604 bool is_declaration)
6605 {
6606 tree scope;
6607
6608 /* Look for the nested-name-specifier. */
6609 scope = cp_parser_nested_name_specifier_opt (parser,
6610 typename_keyword_p,
6611 check_dependency_p,
6612 type_p,
6613 is_declaration);
6614 /* If it was not present, issue an error message. */
6615 if (!scope)
6616 {
6617 cp_parser_error (parser, "expected nested-name-specifier");
6618 parser->scope = NULL_TREE;
6619 }
6620
6621 return scope;
6622 }
6623
6624 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6625 this is either a class-name or a namespace-name (which corresponds
6626 to the class-or-namespace-name production in the grammar). For
6627 C++0x, it can also be a type-name that refers to an enumeration
6628 type or a simple-template-id.
6629
6630 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6631 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6632 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6633 TYPE_P is TRUE iff the next name should be taken as a class-name,
6634 even the same name is declared to be another entity in the same
6635 scope.
6636
6637 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6638 specified by the class-or-namespace-name. If neither is found the
6639 ERROR_MARK_NODE is returned. */
6640
6641 static tree
6642 cp_parser_qualifying_entity (cp_parser *parser,
6643 bool typename_keyword_p,
6644 bool template_keyword_p,
6645 bool check_dependency_p,
6646 bool type_p,
6647 bool is_declaration)
6648 {
6649 tree saved_scope;
6650 tree saved_qualifying_scope;
6651 tree saved_object_scope;
6652 tree scope;
6653 bool only_class_p;
6654 bool successful_parse_p;
6655
6656 /* DR 743: decltype can appear in a nested-name-specifier. */
6657 if (cp_lexer_next_token_is_decltype (parser->lexer))
6658 {
6659 scope = cp_parser_decltype (parser);
6660 if (TREE_CODE (scope) != ENUMERAL_TYPE
6661 && !MAYBE_CLASS_TYPE_P (scope))
6662 {
6663 cp_parser_simulate_error (parser);
6664 return error_mark_node;
6665 }
6666 if (TYPE_NAME (scope))
6667 scope = TYPE_NAME (scope);
6668 return scope;
6669 }
6670
6671 /* Before we try to parse the class-name, we must save away the
6672 current PARSER->SCOPE since cp_parser_class_name will destroy
6673 it. */
6674 saved_scope = parser->scope;
6675 saved_qualifying_scope = parser->qualifying_scope;
6676 saved_object_scope = parser->object_scope;
6677 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6678 there is no need to look for a namespace-name. */
6679 only_class_p = template_keyword_p
6680 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6681 if (!only_class_p)
6682 cp_parser_parse_tentatively (parser);
6683 scope = cp_parser_class_name (parser,
6684 typename_keyword_p,
6685 template_keyword_p,
6686 type_p ? class_type : none_type,
6687 check_dependency_p,
6688 /*class_head_p=*/false,
6689 is_declaration,
6690 /*enum_ok=*/cxx_dialect > cxx98);
6691 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6692 /* If that didn't work, try for a namespace-name. */
6693 if (!only_class_p && !successful_parse_p)
6694 {
6695 /* Restore the saved scope. */
6696 parser->scope = saved_scope;
6697 parser->qualifying_scope = saved_qualifying_scope;
6698 parser->object_scope = saved_object_scope;
6699 /* If we are not looking at an identifier followed by the scope
6700 resolution operator, then this is not part of a
6701 nested-name-specifier. (Note that this function is only used
6702 to parse the components of a nested-name-specifier.) */
6703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6704 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6705 return error_mark_node;
6706 scope = cp_parser_namespace_name (parser);
6707 }
6708
6709 return scope;
6710 }
6711
6712 /* Return true if we are looking at a compound-literal, false otherwise. */
6713
6714 static bool
6715 cp_parser_compound_literal_p (cp_parser *parser)
6716 {
6717 cp_lexer_save_tokens (parser->lexer);
6718
6719 /* Skip tokens until the next token is a closing parenthesis.
6720 If we find the closing `)', and the next token is a `{', then
6721 we are looking at a compound-literal. */
6722 bool compound_literal_p
6723 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6724 /*consume_paren=*/true)
6725 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6726
6727 /* Roll back the tokens we skipped. */
6728 cp_lexer_rollback_tokens (parser->lexer);
6729
6730 return compound_literal_p;
6731 }
6732
6733 /* Return true if EXPR is the integer constant zero or a complex constant
6734 of zero, without any folding, but ignoring location wrappers. */
6735
6736 bool
6737 literal_integer_zerop (const_tree expr)
6738 {
6739 return (location_wrapper_p (expr)
6740 && integer_zerop (TREE_OPERAND (expr, 0)));
6741 }
6742
6743 /* Parse a postfix-expression.
6744
6745 postfix-expression:
6746 primary-expression
6747 postfix-expression [ expression ]
6748 postfix-expression ( expression-list [opt] )
6749 simple-type-specifier ( expression-list [opt] )
6750 typename :: [opt] nested-name-specifier identifier
6751 ( expression-list [opt] )
6752 typename :: [opt] nested-name-specifier template [opt] template-id
6753 ( expression-list [opt] )
6754 postfix-expression . template [opt] id-expression
6755 postfix-expression -> template [opt] id-expression
6756 postfix-expression . pseudo-destructor-name
6757 postfix-expression -> pseudo-destructor-name
6758 postfix-expression ++
6759 postfix-expression --
6760 dynamic_cast < type-id > ( expression )
6761 static_cast < type-id > ( expression )
6762 reinterpret_cast < type-id > ( expression )
6763 const_cast < type-id > ( expression )
6764 typeid ( expression )
6765 typeid ( type-id )
6766
6767 GNU Extension:
6768
6769 postfix-expression:
6770 ( type-id ) { initializer-list , [opt] }
6771
6772 This extension is a GNU version of the C99 compound-literal
6773 construct. (The C99 grammar uses `type-name' instead of `type-id',
6774 but they are essentially the same concept.)
6775
6776 If ADDRESS_P is true, the postfix expression is the operand of the
6777 `&' operator. CAST_P is true if this expression is the target of a
6778 cast.
6779
6780 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6781 class member access expressions [expr.ref].
6782
6783 Returns a representation of the expression. */
6784
6785 static cp_expr
6786 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6787 bool member_access_only_p, bool decltype_p,
6788 cp_id_kind * pidk_return)
6789 {
6790 cp_token *token;
6791 location_t loc;
6792 enum rid keyword;
6793 cp_id_kind idk = CP_ID_KIND_NONE;
6794 cp_expr postfix_expression = NULL_TREE;
6795 bool is_member_access = false;
6796
6797 /* Peek at the next token. */
6798 token = cp_lexer_peek_token (parser->lexer);
6799 loc = token->location;
6800 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6801
6802 /* Some of the productions are determined by keywords. */
6803 keyword = token->keyword;
6804 switch (keyword)
6805 {
6806 case RID_DYNCAST:
6807 case RID_STATCAST:
6808 case RID_REINTCAST:
6809 case RID_CONSTCAST:
6810 {
6811 tree type;
6812 cp_expr expression;
6813 const char *saved_message;
6814 bool saved_in_type_id_in_expr_p;
6815
6816 /* All of these can be handled in the same way from the point
6817 of view of parsing. Begin by consuming the token
6818 identifying the cast. */
6819 cp_lexer_consume_token (parser->lexer);
6820
6821 /* New types cannot be defined in the cast. */
6822 saved_message = parser->type_definition_forbidden_message;
6823 parser->type_definition_forbidden_message
6824 = G_("types may not be defined in casts");
6825
6826 /* Look for the opening `<'. */
6827 cp_parser_require (parser, CPP_LESS, RT_LESS);
6828 /* Parse the type to which we are casting. */
6829 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6830 parser->in_type_id_in_expr_p = true;
6831 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
6832 NULL);
6833 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6834 /* Look for the closing `>'. */
6835 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6836 /* Restore the old message. */
6837 parser->type_definition_forbidden_message = saved_message;
6838
6839 bool saved_greater_than_is_operator_p
6840 = parser->greater_than_is_operator_p;
6841 parser->greater_than_is_operator_p = true;
6842
6843 /* And the expression which is being cast. */
6844 matching_parens parens;
6845 parens.require_open (parser);
6846 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6847 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6848 RT_CLOSE_PAREN);
6849 location_t end_loc = close_paren ?
6850 close_paren->location : UNKNOWN_LOCATION;
6851
6852 parser->greater_than_is_operator_p
6853 = saved_greater_than_is_operator_p;
6854
6855 /* Only type conversions to integral or enumeration types
6856 can be used in constant-expressions. */
6857 if (!cast_valid_in_integral_constant_expression_p (type)
6858 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6859 {
6860 postfix_expression = error_mark_node;
6861 break;
6862 }
6863
6864 switch (keyword)
6865 {
6866 case RID_DYNCAST:
6867 postfix_expression
6868 = build_dynamic_cast (type, expression, tf_warning_or_error);
6869 break;
6870 case RID_STATCAST:
6871 postfix_expression
6872 = build_static_cast (type, expression, tf_warning_or_error);
6873 break;
6874 case RID_REINTCAST:
6875 postfix_expression
6876 = build_reinterpret_cast (type, expression,
6877 tf_warning_or_error);
6878 break;
6879 case RID_CONSTCAST:
6880 postfix_expression
6881 = build_const_cast (type, expression, tf_warning_or_error);
6882 break;
6883 default:
6884 gcc_unreachable ();
6885 }
6886
6887 /* Construct a location e.g. :
6888 reinterpret_cast <int *> (expr)
6889 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6890 ranging from the start of the "*_cast" token to the final closing
6891 paren, with the caret at the start. */
6892 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6893 postfix_expression.set_location (cp_cast_loc);
6894 }
6895 break;
6896
6897 case RID_TYPEID:
6898 {
6899 tree type;
6900 const char *saved_message;
6901 bool saved_in_type_id_in_expr_p;
6902
6903 /* Consume the `typeid' token. */
6904 cp_lexer_consume_token (parser->lexer);
6905 /* Look for the `(' token. */
6906 matching_parens parens;
6907 parens.require_open (parser);
6908 /* Types cannot be defined in a `typeid' expression. */
6909 saved_message = parser->type_definition_forbidden_message;
6910 parser->type_definition_forbidden_message
6911 = G_("types may not be defined in a %<typeid%> expression");
6912 /* We can't be sure yet whether we're looking at a type-id or an
6913 expression. */
6914 cp_parser_parse_tentatively (parser);
6915 /* Try a type-id first. */
6916 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6917 parser->in_type_id_in_expr_p = true;
6918 type = cp_parser_type_id (parser);
6919 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6920 /* Look for the `)' token. Otherwise, we can't be sure that
6921 we're not looking at an expression: consider `typeid (int
6922 (3))', for example. */
6923 cp_token *close_paren = parens.require_close (parser);
6924 /* If all went well, simply lookup the type-id. */
6925 if (cp_parser_parse_definitely (parser))
6926 postfix_expression = get_typeid (type, tf_warning_or_error);
6927 /* Otherwise, fall back to the expression variant. */
6928 else
6929 {
6930 tree expression;
6931
6932 /* Look for an expression. */
6933 expression = cp_parser_expression (parser, & idk);
6934 /* Compute its typeid. */
6935 postfix_expression = build_typeid (expression, tf_warning_or_error);
6936 /* Look for the `)' token. */
6937 close_paren = parens.require_close (parser);
6938 }
6939 /* Restore the saved message. */
6940 parser->type_definition_forbidden_message = saved_message;
6941 /* `typeid' may not appear in an integral constant expression. */
6942 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6943 postfix_expression = error_mark_node;
6944
6945 /* Construct a location e.g. :
6946 typeid (expr)
6947 ^~~~~~~~~~~~~
6948 ranging from the start of the "typeid" token to the final closing
6949 paren, with the caret at the start. */
6950 if (close_paren)
6951 {
6952 location_t typeid_loc
6953 = make_location (start_loc, start_loc, close_paren->location);
6954 postfix_expression.set_location (typeid_loc);
6955 postfix_expression.maybe_add_location_wrapper ();
6956 }
6957 }
6958 break;
6959
6960 case RID_TYPENAME:
6961 {
6962 tree type;
6963 /* The syntax permitted here is the same permitted for an
6964 elaborated-type-specifier. */
6965 ++parser->prevent_constrained_type_specifiers;
6966 type = cp_parser_elaborated_type_specifier (parser,
6967 /*is_friend=*/false,
6968 /*is_declaration=*/false);
6969 --parser->prevent_constrained_type_specifiers;
6970 postfix_expression = cp_parser_functional_cast (parser, type);
6971 }
6972 break;
6973
6974 case RID_ADDRESSOF:
6975 case RID_BUILTIN_SHUFFLE:
6976 case RID_BUILTIN_LAUNDER:
6977 {
6978 vec<tree, va_gc> *vec;
6979 unsigned int i;
6980 tree p;
6981
6982 cp_lexer_consume_token (parser->lexer);
6983 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6984 /*cast_p=*/false, /*allow_expansion_p=*/true,
6985 /*non_constant_p=*/NULL);
6986 if (vec == NULL)
6987 {
6988 postfix_expression = error_mark_node;
6989 break;
6990 }
6991
6992 FOR_EACH_VEC_ELT (*vec, i, p)
6993 mark_exp_read (p);
6994
6995 switch (keyword)
6996 {
6997 case RID_ADDRESSOF:
6998 if (vec->length () == 1)
6999 postfix_expression
7000 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7001 else
7002 {
7003 error_at (loc, "wrong number of arguments to "
7004 "%<__builtin_addressof%>");
7005 postfix_expression = error_mark_node;
7006 }
7007 break;
7008
7009 case RID_BUILTIN_LAUNDER:
7010 if (vec->length () == 1)
7011 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7012 tf_warning_or_error);
7013 else
7014 {
7015 error_at (loc, "wrong number of arguments to "
7016 "%<__builtin_launder%>");
7017 postfix_expression = error_mark_node;
7018 }
7019 break;
7020
7021 case RID_BUILTIN_SHUFFLE:
7022 if (vec->length () == 2)
7023 postfix_expression
7024 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7025 (*vec)[1], tf_warning_or_error);
7026 else if (vec->length () == 3)
7027 postfix_expression
7028 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7029 (*vec)[2], tf_warning_or_error);
7030 else
7031 {
7032 error_at (loc, "wrong number of arguments to "
7033 "%<__builtin_shuffle%>");
7034 postfix_expression = error_mark_node;
7035 }
7036 break;
7037
7038 default:
7039 gcc_unreachable ();
7040 }
7041 break;
7042 }
7043
7044 case RID_BUILTIN_CONVERTVECTOR:
7045 {
7046 tree expression;
7047 tree type;
7048 /* Consume the `__builtin_convertvector' token. */
7049 cp_lexer_consume_token (parser->lexer);
7050 /* Look for the opening `('. */
7051 matching_parens parens;
7052 parens.require_open (parser);
7053 /* Now, parse the assignment-expression. */
7054 expression = cp_parser_assignment_expression (parser);
7055 /* Look for the `,'. */
7056 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7057 location_t type_location
7058 = cp_lexer_peek_token (parser->lexer)->location;
7059 /* Parse the type-id. */
7060 {
7061 type_id_in_expr_sentinel s (parser);
7062 type = cp_parser_type_id (parser);
7063 }
7064 /* Look for the closing `)'. */
7065 parens.require_close (parser);
7066 return cp_build_vec_convert (expression, type_location, type,
7067 tf_warning_or_error);
7068 }
7069
7070 default:
7071 {
7072 tree type;
7073
7074 /* If the next thing is a simple-type-specifier, we may be
7075 looking at a functional cast. We could also be looking at
7076 an id-expression. So, we try the functional cast, and if
7077 that doesn't work we fall back to the primary-expression. */
7078 cp_parser_parse_tentatively (parser);
7079 /* Look for the simple-type-specifier. */
7080 ++parser->prevent_constrained_type_specifiers;
7081 type = cp_parser_simple_type_specifier (parser,
7082 /*decl_specs=*/NULL,
7083 CP_PARSER_FLAGS_NONE);
7084 --parser->prevent_constrained_type_specifiers;
7085 /* Parse the cast itself. */
7086 if (!cp_parser_error_occurred (parser))
7087 postfix_expression
7088 = cp_parser_functional_cast (parser, type);
7089 /* If that worked, we're done. */
7090 if (cp_parser_parse_definitely (parser))
7091 break;
7092
7093 /* If the functional-cast didn't work out, try a
7094 compound-literal. */
7095 if (cp_parser_allow_gnu_extensions_p (parser)
7096 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7097 {
7098 cp_expr initializer = NULL_TREE;
7099
7100 cp_parser_parse_tentatively (parser);
7101
7102 matching_parens parens;
7103 parens.consume_open (parser);
7104
7105 /* Avoid calling cp_parser_type_id pointlessly, see comment
7106 in cp_parser_cast_expression about c++/29234. */
7107 if (!cp_parser_compound_literal_p (parser))
7108 cp_parser_simulate_error (parser);
7109 else
7110 {
7111 /* Parse the type. */
7112 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7113 parser->in_type_id_in_expr_p = true;
7114 type = cp_parser_type_id (parser);
7115 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7116 parens.require_close (parser);
7117 }
7118
7119 /* If things aren't going well, there's no need to
7120 keep going. */
7121 if (!cp_parser_error_occurred (parser))
7122 {
7123 bool non_constant_p;
7124 /* Parse the brace-enclosed initializer list. */
7125 initializer = cp_parser_braced_list (parser,
7126 &non_constant_p);
7127 }
7128 /* If that worked, we're definitely looking at a
7129 compound-literal expression. */
7130 if (cp_parser_parse_definitely (parser))
7131 {
7132 /* Warn the user that a compound literal is not
7133 allowed in standard C++. */
7134 pedwarn (input_location, OPT_Wpedantic,
7135 "ISO C++ forbids compound-literals");
7136 /* For simplicity, we disallow compound literals in
7137 constant-expressions. We could
7138 allow compound literals of integer type, whose
7139 initializer was a constant, in constant
7140 expressions. Permitting that usage, as a further
7141 extension, would not change the meaning of any
7142 currently accepted programs. (Of course, as
7143 compound literals are not part of ISO C++, the
7144 standard has nothing to say.) */
7145 if (cp_parser_non_integral_constant_expression (parser,
7146 NIC_NCC))
7147 {
7148 postfix_expression = error_mark_node;
7149 break;
7150 }
7151 /* Form the representation of the compound-literal. */
7152 postfix_expression
7153 = finish_compound_literal (type, initializer,
7154 tf_warning_or_error, fcl_c99);
7155 postfix_expression.set_location (initializer.get_location ());
7156 break;
7157 }
7158 }
7159
7160 /* It must be a primary-expression. */
7161 postfix_expression
7162 = cp_parser_primary_expression (parser, address_p, cast_p,
7163 /*template_arg_p=*/false,
7164 decltype_p,
7165 &idk);
7166 }
7167 break;
7168 }
7169
7170 /* Note that we don't need to worry about calling build_cplus_new on a
7171 class-valued CALL_EXPR in decltype when it isn't the end of the
7172 postfix-expression; unary_complex_lvalue will take care of that for
7173 all these cases. */
7174
7175 /* Keep looping until the postfix-expression is complete. */
7176 while (true)
7177 {
7178 if (idk == CP_ID_KIND_UNQUALIFIED
7179 && identifier_p (postfix_expression)
7180 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7181 /* It is not a Koenig lookup function call. */
7182 postfix_expression
7183 = unqualified_name_lookup_error (postfix_expression);
7184
7185 /* Peek at the next token. */
7186 token = cp_lexer_peek_token (parser->lexer);
7187
7188 switch (token->type)
7189 {
7190 case CPP_OPEN_SQUARE:
7191 if (cp_next_tokens_can_be_std_attribute_p (parser))
7192 {
7193 cp_parser_error (parser,
7194 "two consecutive %<[%> shall "
7195 "only introduce an attribute");
7196 return error_mark_node;
7197 }
7198 postfix_expression
7199 = cp_parser_postfix_open_square_expression (parser,
7200 postfix_expression,
7201 false,
7202 decltype_p);
7203 postfix_expression.set_range (start_loc,
7204 postfix_expression.get_location ());
7205
7206 idk = CP_ID_KIND_NONE;
7207 is_member_access = false;
7208 break;
7209
7210 case CPP_OPEN_PAREN:
7211 /* postfix-expression ( expression-list [opt] ) */
7212 {
7213 bool koenig_p;
7214 bool is_builtin_constant_p;
7215 bool saved_integral_constant_expression_p = false;
7216 bool saved_non_integral_constant_expression_p = false;
7217 tsubst_flags_t complain = complain_flags (decltype_p);
7218 vec<tree, va_gc> *args;
7219 location_t close_paren_loc = UNKNOWN_LOCATION;
7220
7221 is_member_access = false;
7222
7223 tree stripped_expression
7224 = tree_strip_any_location_wrapper (postfix_expression);
7225 is_builtin_constant_p
7226 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7227 if (is_builtin_constant_p)
7228 {
7229 /* The whole point of __builtin_constant_p is to allow
7230 non-constant expressions to appear as arguments. */
7231 saved_integral_constant_expression_p
7232 = parser->integral_constant_expression_p;
7233 saved_non_integral_constant_expression_p
7234 = parser->non_integral_constant_expression_p;
7235 parser->integral_constant_expression_p = false;
7236 }
7237 args = (cp_parser_parenthesized_expression_list
7238 (parser, non_attr,
7239 /*cast_p=*/false, /*allow_expansion_p=*/true,
7240 /*non_constant_p=*/NULL,
7241 /*close_paren_loc=*/&close_paren_loc,
7242 /*wrap_locations_p=*/true));
7243 if (is_builtin_constant_p)
7244 {
7245 parser->integral_constant_expression_p
7246 = saved_integral_constant_expression_p;
7247 parser->non_integral_constant_expression_p
7248 = saved_non_integral_constant_expression_p;
7249 }
7250
7251 if (args == NULL)
7252 {
7253 postfix_expression = error_mark_node;
7254 break;
7255 }
7256
7257 /* Function calls are not permitted in
7258 constant-expressions. */
7259 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7260 && cp_parser_non_integral_constant_expression (parser,
7261 NIC_FUNC_CALL))
7262 {
7263 postfix_expression = error_mark_node;
7264 release_tree_vector (args);
7265 break;
7266 }
7267
7268 koenig_p = false;
7269 if (idk == CP_ID_KIND_UNQUALIFIED
7270 || idk == CP_ID_KIND_TEMPLATE_ID)
7271 {
7272 if (identifier_p (postfix_expression)
7273 /* In C++2A, we may need to perform ADL for a template
7274 name. */
7275 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7276 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7277 {
7278 if (!args->is_empty ())
7279 {
7280 koenig_p = true;
7281 if (!any_type_dependent_arguments_p (args))
7282 postfix_expression
7283 = perform_koenig_lookup (postfix_expression, args,
7284 complain);
7285 }
7286 else
7287 postfix_expression
7288 = unqualified_fn_lookup_error (postfix_expression);
7289 }
7290 /* We do not perform argument-dependent lookup if
7291 normal lookup finds a non-function, in accordance
7292 with the expected resolution of DR 218. */
7293 else if (!args->is_empty ()
7294 && is_overloaded_fn (postfix_expression))
7295 {
7296 /* We only need to look at the first function,
7297 because all the fns share the attribute we're
7298 concerned with (all member fns or all local
7299 fns). */
7300 tree fn = get_first_fn (postfix_expression);
7301 fn = STRIP_TEMPLATE (fn);
7302
7303 /* Do not do argument dependent lookup if regular
7304 lookup finds a member function or a block-scope
7305 function declaration. [basic.lookup.argdep]/3 */
7306 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7307 || DECL_FUNCTION_MEMBER_P (fn)
7308 || DECL_LOCAL_FUNCTION_P (fn)))
7309 {
7310 koenig_p = true;
7311 if (!any_type_dependent_arguments_p (args))
7312 postfix_expression
7313 = perform_koenig_lookup (postfix_expression, args,
7314 complain);
7315 }
7316 }
7317 }
7318
7319 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7320 {
7321 tree instance = TREE_OPERAND (postfix_expression, 0);
7322 tree fn = TREE_OPERAND (postfix_expression, 1);
7323
7324 if (processing_template_decl
7325 && (type_dependent_object_expression_p (instance)
7326 || (!BASELINK_P (fn)
7327 && TREE_CODE (fn) != FIELD_DECL)
7328 || type_dependent_expression_p (fn)
7329 || any_type_dependent_arguments_p (args)))
7330 {
7331 maybe_generic_this_capture (instance, fn);
7332 postfix_expression
7333 = build_min_nt_call_vec (postfix_expression, args);
7334 release_tree_vector (args);
7335 break;
7336 }
7337
7338 if (BASELINK_P (fn))
7339 {
7340 postfix_expression
7341 = (build_new_method_call
7342 (instance, fn, &args, NULL_TREE,
7343 (idk == CP_ID_KIND_QUALIFIED
7344 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7345 : LOOKUP_NORMAL),
7346 /*fn_p=*/NULL,
7347 complain));
7348 }
7349 else
7350 postfix_expression
7351 = finish_call_expr (postfix_expression, &args,
7352 /*disallow_virtual=*/false,
7353 /*koenig_p=*/false,
7354 complain);
7355 }
7356 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7357 || TREE_CODE (postfix_expression) == MEMBER_REF
7358 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7359 postfix_expression = (build_offset_ref_call_from_tree
7360 (postfix_expression, &args,
7361 complain));
7362 else if (idk == CP_ID_KIND_QUALIFIED)
7363 /* A call to a static class member, or a namespace-scope
7364 function. */
7365 postfix_expression
7366 = finish_call_expr (postfix_expression, &args,
7367 /*disallow_virtual=*/true,
7368 koenig_p,
7369 complain);
7370 else
7371 /* All other function calls. */
7372 postfix_expression
7373 = finish_call_expr (postfix_expression, &args,
7374 /*disallow_virtual=*/false,
7375 koenig_p,
7376 complain);
7377
7378 if (close_paren_loc != UNKNOWN_LOCATION)
7379 {
7380 location_t combined_loc = make_location (token->location,
7381 start_loc,
7382 close_paren_loc);
7383 postfix_expression.set_location (combined_loc);
7384 }
7385
7386 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7387 idk = CP_ID_KIND_NONE;
7388
7389 release_tree_vector (args);
7390 }
7391 break;
7392
7393 case CPP_DOT:
7394 case CPP_DEREF:
7395 /* postfix-expression . template [opt] id-expression
7396 postfix-expression . pseudo-destructor-name
7397 postfix-expression -> template [opt] id-expression
7398 postfix-expression -> pseudo-destructor-name */
7399
7400 /* Consume the `.' or `->' operator. */
7401 cp_lexer_consume_token (parser->lexer);
7402
7403 postfix_expression
7404 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7405 postfix_expression,
7406 false, &idk, loc);
7407
7408 is_member_access = true;
7409 break;
7410
7411 case CPP_PLUS_PLUS:
7412 /* postfix-expression ++ */
7413 /* Consume the `++' token. */
7414 cp_lexer_consume_token (parser->lexer);
7415 /* Generate a representation for the complete expression. */
7416 postfix_expression
7417 = finish_increment_expr (postfix_expression,
7418 POSTINCREMENT_EXPR);
7419 /* Increments may not appear in constant-expressions. */
7420 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7421 postfix_expression = error_mark_node;
7422 idk = CP_ID_KIND_NONE;
7423 is_member_access = false;
7424 break;
7425
7426 case CPP_MINUS_MINUS:
7427 /* postfix-expression -- */
7428 /* Consume the `--' token. */
7429 cp_lexer_consume_token (parser->lexer);
7430 /* Generate a representation for the complete expression. */
7431 postfix_expression
7432 = finish_increment_expr (postfix_expression,
7433 POSTDECREMENT_EXPR);
7434 /* Decrements may not appear in constant-expressions. */
7435 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7436 postfix_expression = error_mark_node;
7437 idk = CP_ID_KIND_NONE;
7438 is_member_access = false;
7439 break;
7440
7441 default:
7442 if (pidk_return != NULL)
7443 * pidk_return = idk;
7444 if (member_access_only_p)
7445 return is_member_access
7446 ? postfix_expression
7447 : cp_expr (error_mark_node);
7448 else
7449 return postfix_expression;
7450 }
7451 }
7452
7453 /* We should never get here. */
7454 gcc_unreachable ();
7455 return error_mark_node;
7456 }
7457
7458 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7459 by cp_parser_builtin_offsetof. We're looking for
7460
7461 postfix-expression [ expression ]
7462 postfix-expression [ braced-init-list ] (C++11)
7463
7464 FOR_OFFSETOF is set if we're being called in that context, which
7465 changes how we deal with integer constant expressions. */
7466
7467 static tree
7468 cp_parser_postfix_open_square_expression (cp_parser *parser,
7469 tree postfix_expression,
7470 bool for_offsetof,
7471 bool decltype_p)
7472 {
7473 tree index = NULL_TREE;
7474 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7475 bool saved_greater_than_is_operator_p;
7476
7477 /* Consume the `[' token. */
7478 cp_lexer_consume_token (parser->lexer);
7479
7480 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7481 parser->greater_than_is_operator_p = true;
7482
7483 /* Parse the index expression. */
7484 /* ??? For offsetof, there is a question of what to allow here. If
7485 offsetof is not being used in an integral constant expression context,
7486 then we *could* get the right answer by computing the value at runtime.
7487 If we are in an integral constant expression context, then we might
7488 could accept any constant expression; hard to say without analysis.
7489 Rather than open the barn door too wide right away, allow only integer
7490 constant expressions here. */
7491 if (for_offsetof)
7492 index = cp_parser_constant_expression (parser);
7493 else
7494 {
7495 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7496 {
7497 bool expr_nonconst_p;
7498 cp_lexer_set_source_position (parser->lexer);
7499 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7500 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7501 }
7502 else
7503 index = cp_parser_expression (parser);
7504 }
7505
7506 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7507
7508 /* Look for the closing `]'. */
7509 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7510
7511 /* Build the ARRAY_REF. */
7512 postfix_expression = grok_array_decl (loc, postfix_expression,
7513 index, decltype_p);
7514
7515 /* When not doing offsetof, array references are not permitted in
7516 constant-expressions. */
7517 if (!for_offsetof
7518 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7519 postfix_expression = error_mark_node;
7520
7521 return postfix_expression;
7522 }
7523
7524 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7525 dereference of incomplete type, returns true if error_mark_node should
7526 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7527 and *DEPENDENT_P. */
7528
7529 bool
7530 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7531 bool *dependent_p)
7532 {
7533 /* In a template, be permissive by treating an object expression
7534 of incomplete type as dependent (after a pedwarn). */
7535 diagnostic_t kind = (processing_template_decl
7536 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7537
7538 switch (TREE_CODE (*postfix_expression))
7539 {
7540 case CAST_EXPR:
7541 case REINTERPRET_CAST_EXPR:
7542 case CONST_CAST_EXPR:
7543 case STATIC_CAST_EXPR:
7544 case DYNAMIC_CAST_EXPR:
7545 case IMPLICIT_CONV_EXPR:
7546 case VIEW_CONVERT_EXPR:
7547 case NON_LVALUE_EXPR:
7548 kind = DK_ERROR;
7549 break;
7550 case OVERLOAD:
7551 /* Don't emit any diagnostic for OVERLOADs. */
7552 kind = DK_IGNORED;
7553 break;
7554 default:
7555 /* Avoid clobbering e.g. DECLs. */
7556 if (!EXPR_P (*postfix_expression))
7557 kind = DK_ERROR;
7558 break;
7559 }
7560
7561 if (kind == DK_IGNORED)
7562 return false;
7563
7564 location_t exploc = location_of (*postfix_expression);
7565 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7566 if (!MAYBE_CLASS_TYPE_P (*scope))
7567 return true;
7568 if (kind == DK_ERROR)
7569 *scope = *postfix_expression = error_mark_node;
7570 else if (processing_template_decl)
7571 {
7572 *dependent_p = true;
7573 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7574 }
7575 return false;
7576 }
7577
7578 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7579 by cp_parser_builtin_offsetof. We're looking for
7580
7581 postfix-expression . template [opt] id-expression
7582 postfix-expression . pseudo-destructor-name
7583 postfix-expression -> template [opt] id-expression
7584 postfix-expression -> pseudo-destructor-name
7585
7586 FOR_OFFSETOF is set if we're being called in that context. That sorta
7587 limits what of the above we'll actually accept, but nevermind.
7588 TOKEN_TYPE is the "." or "->" token, which will already have been
7589 removed from the stream. */
7590
7591 static tree
7592 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7593 enum cpp_ttype token_type,
7594 cp_expr postfix_expression,
7595 bool for_offsetof, cp_id_kind *idk,
7596 location_t location)
7597 {
7598 tree name;
7599 bool dependent_p;
7600 bool pseudo_destructor_p;
7601 tree scope = NULL_TREE;
7602 location_t start_loc = postfix_expression.get_start ();
7603
7604 /* If this is a `->' operator, dereference the pointer. */
7605 if (token_type == CPP_DEREF)
7606 postfix_expression = build_x_arrow (location, postfix_expression,
7607 tf_warning_or_error);
7608 /* Check to see whether or not the expression is type-dependent and
7609 not the current instantiation. */
7610 dependent_p = type_dependent_object_expression_p (postfix_expression);
7611 /* The identifier following the `->' or `.' is not qualified. */
7612 parser->scope = NULL_TREE;
7613 parser->qualifying_scope = NULL_TREE;
7614 parser->object_scope = NULL_TREE;
7615 *idk = CP_ID_KIND_NONE;
7616
7617 /* Enter the scope corresponding to the type of the object
7618 given by the POSTFIX_EXPRESSION. */
7619 if (!dependent_p)
7620 {
7621 scope = TREE_TYPE (postfix_expression);
7622 /* According to the standard, no expression should ever have
7623 reference type. Unfortunately, we do not currently match
7624 the standard in this respect in that our internal representation
7625 of an expression may have reference type even when the standard
7626 says it does not. Therefore, we have to manually obtain the
7627 underlying type here. */
7628 scope = non_reference (scope);
7629 /* The type of the POSTFIX_EXPRESSION must be complete. */
7630 /* Unlike the object expression in other contexts, *this is not
7631 required to be of complete type for purposes of class member
7632 access (5.2.5) outside the member function body. */
7633 if (postfix_expression != current_class_ref
7634 && scope != error_mark_node
7635 && !currently_open_class (scope))
7636 {
7637 scope = complete_type (scope);
7638 if (!COMPLETE_TYPE_P (scope)
7639 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7640 &dependent_p))
7641 return error_mark_node;
7642 }
7643
7644 if (!dependent_p)
7645 {
7646 /* Let the name lookup machinery know that we are processing a
7647 class member access expression. */
7648 parser->context->object_type = scope;
7649 /* If something went wrong, we want to be able to discern that case,
7650 as opposed to the case where there was no SCOPE due to the type
7651 of expression being dependent. */
7652 if (!scope)
7653 scope = error_mark_node;
7654 /* If the SCOPE was erroneous, make the various semantic analysis
7655 functions exit quickly -- and without issuing additional error
7656 messages. */
7657 if (scope == error_mark_node)
7658 postfix_expression = error_mark_node;
7659 }
7660 }
7661
7662 if (dependent_p)
7663 /* Tell cp_parser_lookup_name that there was an object, even though it's
7664 type-dependent. */
7665 parser->context->object_type = unknown_type_node;
7666
7667 /* Assume this expression is not a pseudo-destructor access. */
7668 pseudo_destructor_p = false;
7669
7670 /* If the SCOPE is a scalar type, then, if this is a valid program,
7671 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7672 is type dependent, it can be pseudo-destructor-name or something else.
7673 Try to parse it as pseudo-destructor-name first. */
7674 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7675 {
7676 tree s;
7677 tree type;
7678
7679 cp_parser_parse_tentatively (parser);
7680 /* Parse the pseudo-destructor-name. */
7681 s = NULL_TREE;
7682 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7683 &s, &type);
7684 if (dependent_p
7685 && (cp_parser_error_occurred (parser)
7686 || !SCALAR_TYPE_P (type)))
7687 cp_parser_abort_tentative_parse (parser);
7688 else if (cp_parser_parse_definitely (parser))
7689 {
7690 pseudo_destructor_p = true;
7691 postfix_expression
7692 = finish_pseudo_destructor_expr (postfix_expression,
7693 s, type, location);
7694 }
7695 }
7696
7697 if (!pseudo_destructor_p)
7698 {
7699 /* If the SCOPE is not a scalar type, we are looking at an
7700 ordinary class member access expression, rather than a
7701 pseudo-destructor-name. */
7702 bool template_p;
7703 cp_token *token = cp_lexer_peek_token (parser->lexer);
7704 /* Parse the id-expression. */
7705 name = (cp_parser_id_expression
7706 (parser,
7707 cp_parser_optional_template_keyword (parser),
7708 /*check_dependency_p=*/true,
7709 &template_p,
7710 /*declarator_p=*/false,
7711 /*optional_p=*/false));
7712 /* In general, build a SCOPE_REF if the member name is qualified.
7713 However, if the name was not dependent and has already been
7714 resolved; there is no need to build the SCOPE_REF. For example;
7715
7716 struct X { void f(); };
7717 template <typename T> void f(T* t) { t->X::f(); }
7718
7719 Even though "t" is dependent, "X::f" is not and has been resolved
7720 to a BASELINK; there is no need to include scope information. */
7721
7722 /* But we do need to remember that there was an explicit scope for
7723 virtual function calls. */
7724 if (parser->scope)
7725 *idk = CP_ID_KIND_QUALIFIED;
7726
7727 /* If the name is a template-id that names a type, we will get a
7728 TYPE_DECL here. That is invalid code. */
7729 if (TREE_CODE (name) == TYPE_DECL)
7730 {
7731 error_at (token->location, "invalid use of %qD", name);
7732 postfix_expression = error_mark_node;
7733 }
7734 else
7735 {
7736 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7737 {
7738 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7739 {
7740 error_at (token->location, "%<%D::%D%> is not a class member",
7741 parser->scope, name);
7742 postfix_expression = error_mark_node;
7743 }
7744 else
7745 name = build_qualified_name (/*type=*/NULL_TREE,
7746 parser->scope,
7747 name,
7748 template_p);
7749 parser->scope = NULL_TREE;
7750 parser->qualifying_scope = NULL_TREE;
7751 parser->object_scope = NULL_TREE;
7752 }
7753 if (parser->scope && name && BASELINK_P (name))
7754 adjust_result_of_qualified_name_lookup
7755 (name, parser->scope, scope);
7756 postfix_expression
7757 = finish_class_member_access_expr (postfix_expression, name,
7758 template_p,
7759 tf_warning_or_error);
7760 /* Build a location e.g.:
7761 ptr->access_expr
7762 ~~~^~~~~~~~~~~~~
7763 where the caret is at the deref token, ranging from
7764 the start of postfix_expression to the end of the access expr. */
7765 location_t end_loc
7766 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7767 location_t combined_loc
7768 = make_location (input_location, start_loc, end_loc);
7769 protected_set_expr_location (postfix_expression, combined_loc);
7770 }
7771 }
7772
7773 /* We no longer need to look up names in the scope of the object on
7774 the left-hand side of the `.' or `->' operator. */
7775 parser->context->object_type = NULL_TREE;
7776
7777 /* Outside of offsetof, these operators may not appear in
7778 constant-expressions. */
7779 if (!for_offsetof
7780 && (cp_parser_non_integral_constant_expression
7781 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7782 postfix_expression = error_mark_node;
7783
7784 return postfix_expression;
7785 }
7786
7787 /* Parse a parenthesized expression-list.
7788
7789 expression-list:
7790 assignment-expression
7791 expression-list, assignment-expression
7792
7793 attribute-list:
7794 expression-list
7795 identifier
7796 identifier, expression-list
7797
7798 CAST_P is true if this expression is the target of a cast.
7799
7800 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7801 argument pack.
7802
7803 WRAP_LOCATIONS_P is true if expressions within this list for which
7804 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7805 their source locations.
7806
7807 Returns a vector of trees. Each element is a representation of an
7808 assignment-expression. NULL is returned if the ( and or ) are
7809 missing. An empty, but allocated, vector is returned on no
7810 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7811 if we are parsing an attribute list for an attribute that wants a
7812 plain identifier argument, normal_attr for an attribute that wants
7813 an expression, or non_attr if we aren't parsing an attribute list. If
7814 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7815 not all of the expressions in the list were constant.
7816 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7817 will be written to with the location of the closing parenthesis. If
7818 an error occurs, it may or may not be written to. */
7819
7820 static vec<tree, va_gc> *
7821 cp_parser_parenthesized_expression_list (cp_parser* parser,
7822 int is_attribute_list,
7823 bool cast_p,
7824 bool allow_expansion_p,
7825 bool *non_constant_p,
7826 location_t *close_paren_loc,
7827 bool wrap_locations_p)
7828 {
7829 vec<tree, va_gc> *expression_list;
7830 bool fold_expr_p = is_attribute_list != non_attr;
7831 tree identifier = NULL_TREE;
7832 bool saved_greater_than_is_operator_p;
7833
7834 /* Assume all the expressions will be constant. */
7835 if (non_constant_p)
7836 *non_constant_p = false;
7837
7838 matching_parens parens;
7839 if (!parens.require_open (parser))
7840 return NULL;
7841
7842 expression_list = make_tree_vector ();
7843
7844 /* Within a parenthesized expression, a `>' token is always
7845 the greater-than operator. */
7846 saved_greater_than_is_operator_p
7847 = parser->greater_than_is_operator_p;
7848 parser->greater_than_is_operator_p = true;
7849
7850 cp_expr expr (NULL_TREE);
7851
7852 /* Consume expressions until there are no more. */
7853 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7854 while (true)
7855 {
7856 /* At the beginning of attribute lists, check to see if the
7857 next token is an identifier. */
7858 if (is_attribute_list == id_attr
7859 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7860 {
7861 cp_token *token;
7862
7863 /* Consume the identifier. */
7864 token = cp_lexer_consume_token (parser->lexer);
7865 /* Save the identifier. */
7866 identifier = token->u.value;
7867 }
7868 else
7869 {
7870 bool expr_non_constant_p;
7871
7872 /* Parse the next assignment-expression. */
7873 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7874 {
7875 /* A braced-init-list. */
7876 cp_lexer_set_source_position (parser->lexer);
7877 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7878 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7879 if (non_constant_p && expr_non_constant_p)
7880 *non_constant_p = true;
7881 }
7882 else if (non_constant_p)
7883 {
7884 expr = (cp_parser_constant_expression
7885 (parser, /*allow_non_constant_p=*/true,
7886 &expr_non_constant_p));
7887 if (expr_non_constant_p)
7888 *non_constant_p = true;
7889 }
7890 else
7891 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7892 cast_p);
7893
7894 if (fold_expr_p)
7895 expr = instantiate_non_dependent_expr (expr);
7896
7897 /* If we have an ellipsis, then this is an expression
7898 expansion. */
7899 if (allow_expansion_p
7900 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7901 {
7902 /* Consume the `...'. */
7903 cp_lexer_consume_token (parser->lexer);
7904
7905 /* Build the argument pack. */
7906 expr = make_pack_expansion (expr);
7907 }
7908
7909 if (wrap_locations_p)
7910 expr.maybe_add_location_wrapper ();
7911
7912 /* Add it to the list. We add error_mark_node
7913 expressions to the list, so that we can still tell if
7914 the correct form for a parenthesized expression-list
7915 is found. That gives better errors. */
7916 vec_safe_push (expression_list, expr.get_value ());
7917
7918 if (expr == error_mark_node)
7919 goto skip_comma;
7920 }
7921
7922 /* After the first item, attribute lists look the same as
7923 expression lists. */
7924 is_attribute_list = non_attr;
7925
7926 get_comma:;
7927 /* If the next token isn't a `,', then we are done. */
7928 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7929 break;
7930
7931 /* Otherwise, consume the `,' and keep going. */
7932 cp_lexer_consume_token (parser->lexer);
7933 }
7934
7935 if (close_paren_loc)
7936 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7937
7938 if (!parens.require_close (parser))
7939 {
7940 int ending;
7941
7942 skip_comma:;
7943 /* We try and resync to an unnested comma, as that will give the
7944 user better diagnostics. */
7945 ending = cp_parser_skip_to_closing_parenthesis (parser,
7946 /*recovering=*/true,
7947 /*or_comma=*/true,
7948 /*consume_paren=*/true);
7949 if (ending < 0)
7950 goto get_comma;
7951 if (!ending)
7952 {
7953 parser->greater_than_is_operator_p
7954 = saved_greater_than_is_operator_p;
7955 return NULL;
7956 }
7957 }
7958
7959 parser->greater_than_is_operator_p
7960 = saved_greater_than_is_operator_p;
7961
7962 if (identifier)
7963 vec_safe_insert (expression_list, 0, identifier);
7964
7965 return expression_list;
7966 }
7967
7968 /* Parse a pseudo-destructor-name.
7969
7970 pseudo-destructor-name:
7971 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7972 :: [opt] nested-name-specifier template template-id :: ~ type-name
7973 :: [opt] nested-name-specifier [opt] ~ type-name
7974
7975 If either of the first two productions is used, sets *SCOPE to the
7976 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7977 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7978 or ERROR_MARK_NODE if the parse fails. */
7979
7980 static void
7981 cp_parser_pseudo_destructor_name (cp_parser* parser,
7982 tree object,
7983 tree* scope,
7984 tree* type)
7985 {
7986 bool nested_name_specifier_p;
7987
7988 /* Handle ~auto. */
7989 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7990 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7991 && !type_dependent_expression_p (object))
7992 {
7993 if (cxx_dialect < cxx14)
7994 pedwarn (input_location, 0,
7995 "%<~auto%> only available with "
7996 "-std=c++14 or -std=gnu++14");
7997 cp_lexer_consume_token (parser->lexer);
7998 cp_lexer_consume_token (parser->lexer);
7999 *scope = NULL_TREE;
8000 *type = TREE_TYPE (object);
8001 return;
8002 }
8003
8004 /* Assume that things will not work out. */
8005 *type = error_mark_node;
8006
8007 /* Look for the optional `::' operator. */
8008 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8009 /* Look for the optional nested-name-specifier. */
8010 nested_name_specifier_p
8011 = (cp_parser_nested_name_specifier_opt (parser,
8012 /*typename_keyword_p=*/false,
8013 /*check_dependency_p=*/true,
8014 /*type_p=*/false,
8015 /*is_declaration=*/false)
8016 != NULL_TREE);
8017 /* Now, if we saw a nested-name-specifier, we might be doing the
8018 second production. */
8019 if (nested_name_specifier_p
8020 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8021 {
8022 /* Consume the `template' keyword. */
8023 cp_lexer_consume_token (parser->lexer);
8024 /* Parse the template-id. */
8025 cp_parser_template_id (parser,
8026 /*template_keyword_p=*/true,
8027 /*check_dependency_p=*/false,
8028 class_type,
8029 /*is_declaration=*/true);
8030 /* Look for the `::' token. */
8031 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8032 }
8033 /* If the next token is not a `~', then there might be some
8034 additional qualification. */
8035 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8036 {
8037 /* At this point, we're looking for "type-name :: ~". The type-name
8038 must not be a class-name, since this is a pseudo-destructor. So,
8039 it must be either an enum-name, or a typedef-name -- both of which
8040 are just identifiers. So, we peek ahead to check that the "::"
8041 and "~" tokens are present; if they are not, then we can avoid
8042 calling type_name. */
8043 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8044 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8045 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8046 {
8047 cp_parser_error (parser, "non-scalar type");
8048 return;
8049 }
8050
8051 /* Look for the type-name. */
8052 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8053 if (*scope == error_mark_node)
8054 return;
8055
8056 /* Look for the `::' token. */
8057 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8058 }
8059 else
8060 *scope = NULL_TREE;
8061
8062 /* Look for the `~'. */
8063 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8064
8065 /* Once we see the ~, this has to be a pseudo-destructor. */
8066 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8067 cp_parser_commit_to_topmost_tentative_parse (parser);
8068
8069 /* Look for the type-name again. We are not responsible for
8070 checking that it matches the first type-name. */
8071 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8072 }
8073
8074 /* Parse a unary-expression.
8075
8076 unary-expression:
8077 postfix-expression
8078 ++ cast-expression
8079 -- cast-expression
8080 unary-operator cast-expression
8081 sizeof unary-expression
8082 sizeof ( type-id )
8083 alignof ( type-id ) [C++0x]
8084 new-expression
8085 delete-expression
8086
8087 GNU Extensions:
8088
8089 unary-expression:
8090 __extension__ cast-expression
8091 __alignof__ unary-expression
8092 __alignof__ ( type-id )
8093 alignof unary-expression [C++0x]
8094 __real__ cast-expression
8095 __imag__ cast-expression
8096 && identifier
8097 sizeof ( type-id ) { initializer-list , [opt] }
8098 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8099 __alignof__ ( type-id ) { initializer-list , [opt] }
8100
8101 ADDRESS_P is true iff the unary-expression is appearing as the
8102 operand of the `&' operator. CAST_P is true if this expression is
8103 the target of a cast.
8104
8105 Returns a representation of the expression. */
8106
8107 static cp_expr
8108 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8109 bool address_p, bool cast_p, bool decltype_p)
8110 {
8111 cp_token *token;
8112 enum tree_code unary_operator;
8113
8114 /* Peek at the next token. */
8115 token = cp_lexer_peek_token (parser->lexer);
8116 /* Some keywords give away the kind of expression. */
8117 if (token->type == CPP_KEYWORD)
8118 {
8119 enum rid keyword = token->keyword;
8120
8121 switch (keyword)
8122 {
8123 case RID_ALIGNOF:
8124 case RID_SIZEOF:
8125 {
8126 tree operand, ret;
8127 enum tree_code op;
8128 location_t start_loc = token->location;
8129
8130 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8131 bool std_alignof = id_equal (token->u.value, "alignof");
8132
8133 /* Consume the token. */
8134 cp_lexer_consume_token (parser->lexer);
8135 /* Parse the operand. */
8136 operand = cp_parser_sizeof_operand (parser, keyword);
8137
8138 if (TYPE_P (operand))
8139 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8140 true);
8141 else
8142 {
8143 /* ISO C++ defines alignof only with types, not with
8144 expressions. So pedwarn if alignof is used with a non-
8145 type expression. However, __alignof__ is ok. */
8146 if (std_alignof)
8147 pedwarn (token->location, OPT_Wpedantic,
8148 "ISO C++ does not allow %<alignof%> "
8149 "with a non-type");
8150
8151 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8152 }
8153 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8154 SIZEOF_EXPR with the original operand. */
8155 if (op == SIZEOF_EXPR && ret != error_mark_node)
8156 {
8157 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8158 {
8159 if (!processing_template_decl && TYPE_P (operand))
8160 {
8161 ret = build_min (SIZEOF_EXPR, size_type_node,
8162 build1 (NOP_EXPR, operand,
8163 error_mark_node));
8164 SIZEOF_EXPR_TYPE_P (ret) = 1;
8165 }
8166 else
8167 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8168 TREE_SIDE_EFFECTS (ret) = 0;
8169 TREE_READONLY (ret) = 1;
8170 }
8171 }
8172
8173 /* Construct a location e.g. :
8174 alignof (expr)
8175 ^~~~~~~~~~~~~~
8176 with start == caret at the start of the "alignof"/"sizeof"
8177 token, with the endpoint at the final closing paren. */
8178 location_t finish_loc
8179 = cp_lexer_previous_token (parser->lexer)->location;
8180 location_t compound_loc
8181 = make_location (start_loc, start_loc, finish_loc);
8182
8183 cp_expr ret_expr (ret);
8184 ret_expr.set_location (compound_loc);
8185 ret_expr = ret_expr.maybe_add_location_wrapper ();
8186 return ret_expr;
8187 }
8188
8189 case RID_BUILTIN_HAS_ATTRIBUTE:
8190 return cp_parser_has_attribute_expression (parser);
8191
8192 case RID_NEW:
8193 return cp_parser_new_expression (parser);
8194
8195 case RID_DELETE:
8196 return cp_parser_delete_expression (parser);
8197
8198 case RID_EXTENSION:
8199 {
8200 /* The saved value of the PEDANTIC flag. */
8201 int saved_pedantic;
8202 tree expr;
8203
8204 /* Save away the PEDANTIC flag. */
8205 cp_parser_extension_opt (parser, &saved_pedantic);
8206 /* Parse the cast-expression. */
8207 expr = cp_parser_simple_cast_expression (parser);
8208 /* Restore the PEDANTIC flag. */
8209 pedantic = saved_pedantic;
8210
8211 return expr;
8212 }
8213
8214 case RID_REALPART:
8215 case RID_IMAGPART:
8216 {
8217 tree expression;
8218
8219 /* Consume the `__real__' or `__imag__' token. */
8220 cp_lexer_consume_token (parser->lexer);
8221 /* Parse the cast-expression. */
8222 expression = cp_parser_simple_cast_expression (parser);
8223 /* Create the complete representation. */
8224 return build_x_unary_op (token->location,
8225 (keyword == RID_REALPART
8226 ? REALPART_EXPR : IMAGPART_EXPR),
8227 expression,
8228 tf_warning_or_error);
8229 }
8230 break;
8231
8232 case RID_TRANSACTION_ATOMIC:
8233 case RID_TRANSACTION_RELAXED:
8234 return cp_parser_transaction_expression (parser, keyword);
8235
8236 case RID_NOEXCEPT:
8237 {
8238 tree expr;
8239 const char *saved_message;
8240 bool saved_integral_constant_expression_p;
8241 bool saved_non_integral_constant_expression_p;
8242 bool saved_greater_than_is_operator_p;
8243
8244 location_t start_loc = token->location;
8245
8246 cp_lexer_consume_token (parser->lexer);
8247 matching_parens parens;
8248 parens.require_open (parser);
8249
8250 saved_message = parser->type_definition_forbidden_message;
8251 parser->type_definition_forbidden_message
8252 = G_("types may not be defined in %<noexcept%> expressions");
8253
8254 saved_integral_constant_expression_p
8255 = parser->integral_constant_expression_p;
8256 saved_non_integral_constant_expression_p
8257 = parser->non_integral_constant_expression_p;
8258 parser->integral_constant_expression_p = false;
8259
8260 saved_greater_than_is_operator_p
8261 = parser->greater_than_is_operator_p;
8262 parser->greater_than_is_operator_p = true;
8263
8264 ++cp_unevaluated_operand;
8265 ++c_inhibit_evaluation_warnings;
8266 ++cp_noexcept_operand;
8267 expr = cp_parser_expression (parser);
8268 --cp_noexcept_operand;
8269 --c_inhibit_evaluation_warnings;
8270 --cp_unevaluated_operand;
8271
8272 parser->greater_than_is_operator_p
8273 = saved_greater_than_is_operator_p;
8274
8275 parser->integral_constant_expression_p
8276 = saved_integral_constant_expression_p;
8277 parser->non_integral_constant_expression_p
8278 = saved_non_integral_constant_expression_p;
8279
8280 parser->type_definition_forbidden_message = saved_message;
8281
8282 location_t finish_loc
8283 = cp_lexer_peek_token (parser->lexer)->location;
8284 parens.require_close (parser);
8285
8286 /* Construct a location of the form:
8287 noexcept (expr)
8288 ^~~~~~~~~~~~~~~
8289 with start == caret, finishing at the close-paren. */
8290 location_t noexcept_loc
8291 = make_location (start_loc, start_loc, finish_loc);
8292
8293 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8294 noexcept_loc);
8295 }
8296
8297 default:
8298 break;
8299 }
8300 }
8301
8302 /* Look for the `:: new' and `:: delete', which also signal the
8303 beginning of a new-expression, or delete-expression,
8304 respectively. If the next token is `::', then it might be one of
8305 these. */
8306 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8307 {
8308 enum rid keyword;
8309
8310 /* See if the token after the `::' is one of the keywords in
8311 which we're interested. */
8312 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8313 /* If it's `new', we have a new-expression. */
8314 if (keyword == RID_NEW)
8315 return cp_parser_new_expression (parser);
8316 /* Similarly, for `delete'. */
8317 else if (keyword == RID_DELETE)
8318 return cp_parser_delete_expression (parser);
8319 }
8320
8321 /* Look for a unary operator. */
8322 unary_operator = cp_parser_unary_operator (token);
8323 /* The `++' and `--' operators can be handled similarly, even though
8324 they are not technically unary-operators in the grammar. */
8325 if (unary_operator == ERROR_MARK)
8326 {
8327 if (token->type == CPP_PLUS_PLUS)
8328 unary_operator = PREINCREMENT_EXPR;
8329 else if (token->type == CPP_MINUS_MINUS)
8330 unary_operator = PREDECREMENT_EXPR;
8331 /* Handle the GNU address-of-label extension. */
8332 else if (cp_parser_allow_gnu_extensions_p (parser)
8333 && token->type == CPP_AND_AND)
8334 {
8335 tree identifier;
8336 tree expression;
8337 location_t start_loc = token->location;
8338
8339 /* Consume the '&&' token. */
8340 cp_lexer_consume_token (parser->lexer);
8341 /* Look for the identifier. */
8342 location_t finish_loc
8343 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8344 identifier = cp_parser_identifier (parser);
8345 /* Construct a location of the form:
8346 &&label
8347 ^~~~~~~
8348 with caret==start at the "&&", finish at the end of the label. */
8349 location_t combined_loc
8350 = make_location (start_loc, start_loc, finish_loc);
8351 /* Create an expression representing the address. */
8352 expression = finish_label_address_expr (identifier, combined_loc);
8353 if (cp_parser_non_integral_constant_expression (parser,
8354 NIC_ADDR_LABEL))
8355 expression = error_mark_node;
8356 return expression;
8357 }
8358 }
8359 if (unary_operator != ERROR_MARK)
8360 {
8361 cp_expr cast_expression;
8362 cp_expr expression = error_mark_node;
8363 non_integral_constant non_constant_p = NIC_NONE;
8364 location_t loc = token->location;
8365 tsubst_flags_t complain = complain_flags (decltype_p);
8366
8367 /* Consume the operator token. */
8368 token = cp_lexer_consume_token (parser->lexer);
8369 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8370
8371 /* Parse the cast-expression. */
8372 cast_expression
8373 = cp_parser_cast_expression (parser,
8374 unary_operator == ADDR_EXPR,
8375 /*cast_p=*/false,
8376 /*decltype*/false,
8377 pidk);
8378
8379 /* Make a location:
8380 OP_TOKEN CAST_EXPRESSION
8381 ^~~~~~~~~~~~~~~~~~~~~~~~~
8382 with start==caret at the operator token, and
8383 extending to the end of the cast_expression. */
8384 loc = make_location (loc, loc, cast_expression.get_finish ());
8385
8386 /* Now, build an appropriate representation. */
8387 switch (unary_operator)
8388 {
8389 case INDIRECT_REF:
8390 non_constant_p = NIC_STAR;
8391 expression = build_x_indirect_ref (loc, cast_expression,
8392 RO_UNARY_STAR,
8393 complain);
8394 /* TODO: build_x_indirect_ref does not always honor the
8395 location, so ensure it is set. */
8396 expression.set_location (loc);
8397 break;
8398
8399 case ADDR_EXPR:
8400 non_constant_p = NIC_ADDR;
8401 /* Fall through. */
8402 case BIT_NOT_EXPR:
8403 expression = build_x_unary_op (loc, unary_operator,
8404 cast_expression,
8405 complain);
8406 /* TODO: build_x_unary_op does not always honor the location,
8407 so ensure it is set. */
8408 expression.set_location (loc);
8409 break;
8410
8411 case PREINCREMENT_EXPR:
8412 case PREDECREMENT_EXPR:
8413 non_constant_p = unary_operator == PREINCREMENT_EXPR
8414 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8415 /* Fall through. */
8416 case NEGATE_EXPR:
8417 /* Immediately fold negation of a constant, unless the constant is 0
8418 (since -0 == 0) or it would overflow. */
8419 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8420 {
8421 tree stripped_expr
8422 = tree_strip_any_location_wrapper (cast_expression);
8423 if (CONSTANT_CLASS_P (stripped_expr)
8424 && !integer_zerop (stripped_expr)
8425 && !TREE_OVERFLOW (stripped_expr))
8426 {
8427 tree folded = fold_build1 (unary_operator,
8428 TREE_TYPE (stripped_expr),
8429 stripped_expr);
8430 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8431 {
8432 expression = maybe_wrap_with_location (folded, loc);
8433 break;
8434 }
8435 }
8436 }
8437 /* Fall through. */
8438 case UNARY_PLUS_EXPR:
8439 case TRUTH_NOT_EXPR:
8440 expression = finish_unary_op_expr (loc, unary_operator,
8441 cast_expression, complain);
8442 break;
8443
8444 default:
8445 gcc_unreachable ();
8446 }
8447
8448 if (non_constant_p != NIC_NONE
8449 && cp_parser_non_integral_constant_expression (parser,
8450 non_constant_p))
8451 expression = error_mark_node;
8452
8453 return expression;
8454 }
8455
8456 return cp_parser_postfix_expression (parser, address_p, cast_p,
8457 /*member_access_only_p=*/false,
8458 decltype_p,
8459 pidk);
8460 }
8461
8462 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8463 unary-operator, the corresponding tree code is returned. */
8464
8465 static enum tree_code
8466 cp_parser_unary_operator (cp_token* token)
8467 {
8468 switch (token->type)
8469 {
8470 case CPP_MULT:
8471 return INDIRECT_REF;
8472
8473 case CPP_AND:
8474 return ADDR_EXPR;
8475
8476 case CPP_PLUS:
8477 return UNARY_PLUS_EXPR;
8478
8479 case CPP_MINUS:
8480 return NEGATE_EXPR;
8481
8482 case CPP_NOT:
8483 return TRUTH_NOT_EXPR;
8484
8485 case CPP_COMPL:
8486 return BIT_NOT_EXPR;
8487
8488 default:
8489 return ERROR_MARK;
8490 }
8491 }
8492
8493 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8494 Returns a representation of the expression. */
8495
8496 static tree
8497 cp_parser_has_attribute_expression (cp_parser *parser)
8498 {
8499 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8500
8501 /* Consume the __builtin_has_attribute token. */
8502 cp_lexer_consume_token (parser->lexer);
8503
8504 matching_parens parens;
8505 if (!parens.require_open (parser))
8506 return error_mark_node;
8507
8508 /* Types cannot be defined in a `sizeof' expression. Save away the
8509 old message. */
8510 const char *saved_message = parser->type_definition_forbidden_message;
8511 /* And create the new one. */
8512 const int kwd = RID_BUILTIN_HAS_ATTRIBUTE;
8513 char *tmp = concat ("types may not be defined in %<",
8514 IDENTIFIER_POINTER (ridpointers[kwd]),
8515 "%> expressions", NULL);
8516 parser->type_definition_forbidden_message = tmp;
8517
8518 /* The restrictions on constant-expressions do not apply inside
8519 sizeof expressions. */
8520 bool saved_integral_constant_expression_p
8521 = parser->integral_constant_expression_p;
8522 bool saved_non_integral_constant_expression_p
8523 = parser->non_integral_constant_expression_p;
8524 parser->integral_constant_expression_p = false;
8525
8526 /* Do not actually evaluate the expression. */
8527 ++cp_unevaluated_operand;
8528 ++c_inhibit_evaluation_warnings;
8529
8530 tree oper = NULL_TREE;
8531
8532 /* We can't be sure yet whether we're looking at a type-id or an
8533 expression. */
8534 cp_parser_parse_tentatively (parser);
8535
8536 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8537 parser->in_type_id_in_expr_p = true;
8538 /* Look for the type-id. */
8539 oper = cp_parser_type_id (parser);
8540 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8541
8542 cp_parser_parse_definitely (parser);
8543
8544 /* If the type-id production did not work out, then we must be
8545 looking at the unary-expression production. */
8546 if (!oper || oper == error_mark_node)
8547 oper = cp_parser_unary_expression (parser);
8548
8549 STRIP_ANY_LOCATION_WRAPPER (oper);
8550
8551 /* Go back to evaluating expressions. */
8552 --cp_unevaluated_operand;
8553 --c_inhibit_evaluation_warnings;
8554
8555 /* Free the message we created. */
8556 free (tmp);
8557 /* And restore the old one. */
8558 parser->type_definition_forbidden_message = saved_message;
8559 parser->integral_constant_expression_p
8560 = saved_integral_constant_expression_p;
8561 parser->non_integral_constant_expression_p
8562 = saved_non_integral_constant_expression_p;
8563
8564 /* Consume the comma if it's there. */
8565 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
8566 {
8567 cp_parser_skip_to_closing_parenthesis (parser, false, false,
8568 /*consume_paren=*/true);
8569 return error_mark_node;
8570 }
8571
8572 /* Parse the attribute specification. */
8573 bool ret = false;
8574 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
8575 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
8576 {
8577 if (oper != error_mark_node)
8578 {
8579 /* Fold constant expressions used in attributes first. */
8580 cp_check_const_attributes (attr);
8581
8582 /* Finally, see if OPER has been declared with ATTR. */
8583 ret = has_attribute (atloc, oper, attr, default_conversion);
8584 }
8585
8586 parens.require_close (parser);
8587 }
8588 else
8589 {
8590 error_at (atloc, "expected identifier");
8591 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8592 }
8593
8594 /* Construct a location e.g. :
8595 __builtin_has_attribute (oper, attr)
8596 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8597 with start == caret at the start of the built-in token,
8598 and with the endpoint at the final closing paren. */
8599 location_t finish_loc
8600 = cp_lexer_previous_token (parser->lexer)->location;
8601 location_t compound_loc
8602 = make_location (start_loc, start_loc, finish_loc);
8603
8604 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
8605 ret_expr.set_location (compound_loc);
8606 ret_expr = ret_expr.maybe_add_location_wrapper ();
8607 return ret_expr;
8608 }
8609
8610 /* Parse a new-expression.
8611
8612 new-expression:
8613 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8614 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8615
8616 Returns a representation of the expression. */
8617
8618 static tree
8619 cp_parser_new_expression (cp_parser* parser)
8620 {
8621 bool global_scope_p;
8622 vec<tree, va_gc> *placement;
8623 tree type;
8624 vec<tree, va_gc> *initializer;
8625 tree nelts = NULL_TREE;
8626 tree ret;
8627
8628 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8629
8630 /* Look for the optional `::' operator. */
8631 global_scope_p
8632 = (cp_parser_global_scope_opt (parser,
8633 /*current_scope_valid_p=*/false)
8634 != NULL_TREE);
8635 /* Look for the `new' operator. */
8636 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8637 /* There's no easy way to tell a new-placement from the
8638 `( type-id )' construct. */
8639 cp_parser_parse_tentatively (parser);
8640 /* Look for a new-placement. */
8641 placement = cp_parser_new_placement (parser);
8642 /* If that didn't work out, there's no new-placement. */
8643 if (!cp_parser_parse_definitely (parser))
8644 {
8645 if (placement != NULL)
8646 release_tree_vector (placement);
8647 placement = NULL;
8648 }
8649
8650 /* If the next token is a `(', then we have a parenthesized
8651 type-id. */
8652 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8653 {
8654 cp_token *token;
8655 const char *saved_message = parser->type_definition_forbidden_message;
8656
8657 /* Consume the `('. */
8658 matching_parens parens;
8659 parens.consume_open (parser);
8660
8661 /* Parse the type-id. */
8662 parser->type_definition_forbidden_message
8663 = G_("types may not be defined in a new-expression");
8664 {
8665 type_id_in_expr_sentinel s (parser);
8666 type = cp_parser_type_id (parser);
8667 }
8668 parser->type_definition_forbidden_message = saved_message;
8669
8670 /* Look for the closing `)'. */
8671 parens.require_close (parser);
8672 token = cp_lexer_peek_token (parser->lexer);
8673 /* There should not be a direct-new-declarator in this production,
8674 but GCC used to allowed this, so we check and emit a sensible error
8675 message for this case. */
8676 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8677 {
8678 error_at (token->location,
8679 "array bound forbidden after parenthesized type-id");
8680 inform (token->location,
8681 "try removing the parentheses around the type-id");
8682 cp_parser_direct_new_declarator (parser);
8683 }
8684 }
8685 /* Otherwise, there must be a new-type-id. */
8686 else
8687 type = cp_parser_new_type_id (parser, &nelts);
8688
8689 /* If the next token is a `(' or '{', then we have a new-initializer. */
8690 cp_token *token = cp_lexer_peek_token (parser->lexer);
8691 if (token->type == CPP_OPEN_PAREN
8692 || token->type == CPP_OPEN_BRACE)
8693 initializer = cp_parser_new_initializer (parser);
8694 else
8695 initializer = NULL;
8696
8697 /* A new-expression may not appear in an integral constant
8698 expression. */
8699 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8700 ret = error_mark_node;
8701 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8702 of a new-type-id or type-id of a new-expression, the new-expression shall
8703 contain a new-initializer of the form ( assignment-expression )".
8704 Additionally, consistently with the spirit of DR 1467, we want to accept
8705 'new auto { 2 }' too. */
8706 else if ((ret = type_uses_auto (type))
8707 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8708 && (vec_safe_length (initializer) != 1
8709 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8710 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8711 {
8712 error_at (token->location,
8713 "initialization of new-expression for type %<auto%> "
8714 "requires exactly one element");
8715 ret = error_mark_node;
8716 }
8717 else
8718 {
8719 /* Construct a location e.g.:
8720 ptr = new int[100]
8721 ^~~~~~~~~~~~
8722 with caret == start at the start of the "new" token, and the end
8723 at the end of the final token we consumed. */
8724 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8725 location_t end_loc = get_finish (end_tok->location);
8726 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8727
8728 /* Create a representation of the new-expression. */
8729 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8730 tf_warning_or_error);
8731 protected_set_expr_location (ret, combined_loc);
8732 }
8733
8734 if (placement != NULL)
8735 release_tree_vector (placement);
8736 if (initializer != NULL)
8737 release_tree_vector (initializer);
8738
8739 return ret;
8740 }
8741
8742 /* Parse a new-placement.
8743
8744 new-placement:
8745 ( expression-list )
8746
8747 Returns the same representation as for an expression-list. */
8748
8749 static vec<tree, va_gc> *
8750 cp_parser_new_placement (cp_parser* parser)
8751 {
8752 vec<tree, va_gc> *expression_list;
8753
8754 /* Parse the expression-list. */
8755 expression_list = (cp_parser_parenthesized_expression_list
8756 (parser, non_attr, /*cast_p=*/false,
8757 /*allow_expansion_p=*/true,
8758 /*non_constant_p=*/NULL));
8759
8760 if (expression_list && expression_list->is_empty ())
8761 error ("expected expression-list or type-id");
8762
8763 return expression_list;
8764 }
8765
8766 /* Parse a new-type-id.
8767
8768 new-type-id:
8769 type-specifier-seq new-declarator [opt]
8770
8771 Returns the TYPE allocated. If the new-type-id indicates an array
8772 type, *NELTS is set to the number of elements in the last array
8773 bound; the TYPE will not include the last array bound. */
8774
8775 static tree
8776 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8777 {
8778 cp_decl_specifier_seq type_specifier_seq;
8779 cp_declarator *new_declarator;
8780 cp_declarator *declarator;
8781 cp_declarator *outer_declarator;
8782 const char *saved_message;
8783
8784 /* The type-specifier sequence must not contain type definitions.
8785 (It cannot contain declarations of new types either, but if they
8786 are not definitions we will catch that because they are not
8787 complete.) */
8788 saved_message = parser->type_definition_forbidden_message;
8789 parser->type_definition_forbidden_message
8790 = G_("types may not be defined in a new-type-id");
8791 /* Parse the type-specifier-seq. */
8792 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
8793 /*is_declaration=*/false,
8794 /*is_trailing_return=*/false,
8795 &type_specifier_seq);
8796 /* Restore the old message. */
8797 parser->type_definition_forbidden_message = saved_message;
8798
8799 if (type_specifier_seq.type == error_mark_node)
8800 return error_mark_node;
8801
8802 /* Parse the new-declarator. */
8803 new_declarator = cp_parser_new_declarator_opt (parser);
8804
8805 /* Determine the number of elements in the last array dimension, if
8806 any. */
8807 *nelts = NULL_TREE;
8808 /* Skip down to the last array dimension. */
8809 declarator = new_declarator;
8810 outer_declarator = NULL;
8811 while (declarator && (declarator->kind == cdk_pointer
8812 || declarator->kind == cdk_ptrmem))
8813 {
8814 outer_declarator = declarator;
8815 declarator = declarator->declarator;
8816 }
8817 while (declarator
8818 && declarator->kind == cdk_array
8819 && declarator->declarator
8820 && declarator->declarator->kind == cdk_array)
8821 {
8822 outer_declarator = declarator;
8823 declarator = declarator->declarator;
8824 }
8825
8826 if (declarator && declarator->kind == cdk_array)
8827 {
8828 *nelts = declarator->u.array.bounds;
8829 if (*nelts == error_mark_node)
8830 *nelts = integer_one_node;
8831
8832 if (outer_declarator)
8833 outer_declarator->declarator = declarator->declarator;
8834 else
8835 new_declarator = NULL;
8836 }
8837
8838 return groktypename (&type_specifier_seq, new_declarator, false);
8839 }
8840
8841 /* Parse an (optional) new-declarator.
8842
8843 new-declarator:
8844 ptr-operator new-declarator [opt]
8845 direct-new-declarator
8846
8847 Returns the declarator. */
8848
8849 static cp_declarator *
8850 cp_parser_new_declarator_opt (cp_parser* parser)
8851 {
8852 enum tree_code code;
8853 tree type, std_attributes = NULL_TREE;
8854 cp_cv_quals cv_quals;
8855
8856 /* We don't know if there's a ptr-operator next, or not. */
8857 cp_parser_parse_tentatively (parser);
8858 /* Look for a ptr-operator. */
8859 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8860 /* If that worked, look for more new-declarators. */
8861 if (cp_parser_parse_definitely (parser))
8862 {
8863 cp_declarator *declarator;
8864
8865 /* Parse another optional declarator. */
8866 declarator = cp_parser_new_declarator_opt (parser);
8867
8868 declarator = cp_parser_make_indirect_declarator
8869 (code, type, cv_quals, declarator, std_attributes);
8870
8871 return declarator;
8872 }
8873
8874 /* If the next token is a `[', there is a direct-new-declarator. */
8875 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8876 return cp_parser_direct_new_declarator (parser);
8877
8878 return NULL;
8879 }
8880
8881 /* Parse a direct-new-declarator.
8882
8883 direct-new-declarator:
8884 [ expression ]
8885 direct-new-declarator [constant-expression]
8886
8887 */
8888
8889 static cp_declarator *
8890 cp_parser_direct_new_declarator (cp_parser* parser)
8891 {
8892 cp_declarator *declarator = NULL;
8893
8894 while (true)
8895 {
8896 tree expression;
8897 cp_token *token;
8898
8899 /* Look for the opening `['. */
8900 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8901
8902 token = cp_lexer_peek_token (parser->lexer);
8903 expression = cp_parser_expression (parser);
8904 /* The standard requires that the expression have integral
8905 type. DR 74 adds enumeration types. We believe that the
8906 real intent is that these expressions be handled like the
8907 expression in a `switch' condition, which also allows
8908 classes with a single conversion to integral or
8909 enumeration type. */
8910 if (!processing_template_decl)
8911 {
8912 expression
8913 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8914 expression,
8915 /*complain=*/true);
8916 if (!expression)
8917 {
8918 error_at (token->location,
8919 "expression in new-declarator must have integral "
8920 "or enumeration type");
8921 expression = error_mark_node;
8922 }
8923 }
8924
8925 /* Look for the closing `]'. */
8926 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8927
8928 /* Add this bound to the declarator. */
8929 declarator = make_array_declarator (declarator, expression);
8930
8931 /* If the next token is not a `[', then there are no more
8932 bounds. */
8933 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8934 break;
8935 }
8936
8937 return declarator;
8938 }
8939
8940 /* Parse a new-initializer.
8941
8942 new-initializer:
8943 ( expression-list [opt] )
8944 braced-init-list
8945
8946 Returns a representation of the expression-list. */
8947
8948 static vec<tree, va_gc> *
8949 cp_parser_new_initializer (cp_parser* parser)
8950 {
8951 vec<tree, va_gc> *expression_list;
8952
8953 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8954 {
8955 tree t;
8956 bool expr_non_constant_p;
8957 cp_lexer_set_source_position (parser->lexer);
8958 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8959 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8960 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8961 expression_list = make_tree_vector_single (t);
8962 }
8963 else
8964 expression_list = (cp_parser_parenthesized_expression_list
8965 (parser, non_attr, /*cast_p=*/false,
8966 /*allow_expansion_p=*/true,
8967 /*non_constant_p=*/NULL));
8968
8969 return expression_list;
8970 }
8971
8972 /* Parse a delete-expression.
8973
8974 delete-expression:
8975 :: [opt] delete cast-expression
8976 :: [opt] delete [ ] cast-expression
8977
8978 Returns a representation of the expression. */
8979
8980 static tree
8981 cp_parser_delete_expression (cp_parser* parser)
8982 {
8983 bool global_scope_p;
8984 bool array_p;
8985 tree expression;
8986
8987 /* Look for the optional `::' operator. */
8988 global_scope_p
8989 = (cp_parser_global_scope_opt (parser,
8990 /*current_scope_valid_p=*/false)
8991 != NULL_TREE);
8992 /* Look for the `delete' keyword. */
8993 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8994 /* See if the array syntax is in use. */
8995 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8996 {
8997 /* Consume the `[' token. */
8998 cp_lexer_consume_token (parser->lexer);
8999 /* Look for the `]' token. */
9000 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9001 /* Remember that this is the `[]' construct. */
9002 array_p = true;
9003 }
9004 else
9005 array_p = false;
9006
9007 /* Parse the cast-expression. */
9008 expression = cp_parser_simple_cast_expression (parser);
9009
9010 /* A delete-expression may not appear in an integral constant
9011 expression. */
9012 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9013 return error_mark_node;
9014
9015 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
9016 tf_warning_or_error);
9017 }
9018
9019 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9020 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9021 0 otherwise. */
9022
9023 static int
9024 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9025 {
9026 cp_token *token = cp_lexer_peek_token (parser->lexer);
9027 switch (token->type)
9028 {
9029 case CPP_COMMA:
9030 case CPP_SEMICOLON:
9031 case CPP_QUERY:
9032 case CPP_COLON:
9033 case CPP_CLOSE_SQUARE:
9034 case CPP_CLOSE_PAREN:
9035 case CPP_CLOSE_BRACE:
9036 case CPP_OPEN_BRACE:
9037 case CPP_DOT:
9038 case CPP_DOT_STAR:
9039 case CPP_DEREF:
9040 case CPP_DEREF_STAR:
9041 case CPP_DIV:
9042 case CPP_MOD:
9043 case CPP_LSHIFT:
9044 case CPP_RSHIFT:
9045 case CPP_LESS:
9046 case CPP_GREATER:
9047 case CPP_LESS_EQ:
9048 case CPP_GREATER_EQ:
9049 case CPP_EQ_EQ:
9050 case CPP_NOT_EQ:
9051 case CPP_EQ:
9052 case CPP_MULT_EQ:
9053 case CPP_DIV_EQ:
9054 case CPP_MOD_EQ:
9055 case CPP_PLUS_EQ:
9056 case CPP_MINUS_EQ:
9057 case CPP_RSHIFT_EQ:
9058 case CPP_LSHIFT_EQ:
9059 case CPP_AND_EQ:
9060 case CPP_XOR_EQ:
9061 case CPP_OR_EQ:
9062 case CPP_XOR:
9063 case CPP_OR:
9064 case CPP_OR_OR:
9065 case CPP_EOF:
9066 case CPP_ELLIPSIS:
9067 return 0;
9068
9069 case CPP_OPEN_PAREN:
9070 /* In ((type ()) () the last () isn't a valid cast-expression,
9071 so the whole must be parsed as postfix-expression. */
9072 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9073 != CPP_CLOSE_PAREN;
9074
9075 case CPP_OPEN_SQUARE:
9076 /* '[' may start a primary-expression in obj-c++ and in C++11,
9077 as a lambda-expression, eg, '(void)[]{}'. */
9078 if (cxx_dialect >= cxx11)
9079 return -1;
9080 return c_dialect_objc ();
9081
9082 case CPP_PLUS_PLUS:
9083 case CPP_MINUS_MINUS:
9084 /* '++' and '--' may or may not start a cast-expression:
9085
9086 struct T { void operator++(int); };
9087 void f() { (T())++; }
9088
9089 vs
9090
9091 int a;
9092 (int)++a; */
9093 return -1;
9094
9095 default:
9096 return 1;
9097 }
9098 }
9099
9100 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9101 in the order: const_cast, static_cast, reinterpret_cast.
9102
9103 Don't suggest dynamic_cast.
9104
9105 Return the first legal cast kind found, or NULL otherwise. */
9106
9107 static const char *
9108 get_cast_suggestion (tree dst_type, tree orig_expr)
9109 {
9110 tree trial;
9111
9112 /* Reuse the parser logic by attempting to build the various kinds of
9113 cast, with "complain" disabled.
9114 Identify the first such cast that is valid. */
9115
9116 /* Don't attempt to run such logic within template processing. */
9117 if (processing_template_decl)
9118 return NULL;
9119
9120 /* First try const_cast. */
9121 trial = build_const_cast (dst_type, orig_expr, tf_none);
9122 if (trial != error_mark_node)
9123 return "const_cast";
9124
9125 /* If that fails, try static_cast. */
9126 trial = build_static_cast (dst_type, orig_expr, tf_none);
9127 if (trial != error_mark_node)
9128 return "static_cast";
9129
9130 /* Finally, try reinterpret_cast. */
9131 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
9132 if (trial != error_mark_node)
9133 return "reinterpret_cast";
9134
9135 /* No such cast possible. */
9136 return NULL;
9137 }
9138
9139 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9140 suggesting how to convert a C-style cast of the form:
9141
9142 (DST_TYPE)ORIG_EXPR
9143
9144 to a C++-style cast.
9145
9146 The primary range of RICHLOC is asssumed to be that of the original
9147 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9148 of the parens in the C-style cast. */
9149
9150 static void
9151 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9152 location_t close_paren_loc, tree orig_expr,
9153 tree dst_type)
9154 {
9155 /* This function is non-trivial, so bail out now if the warning isn't
9156 going to be emitted. */
9157 if (!warn_old_style_cast)
9158 return;
9159
9160 /* Try to find a legal C++ cast, trying them in order:
9161 const_cast, static_cast, reinterpret_cast. */
9162 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9163 if (!cast_suggestion)
9164 return;
9165
9166 /* Replace the open paren with "CAST_SUGGESTION<". */
9167 pretty_printer pp;
9168 pp_printf (&pp, "%s<", cast_suggestion);
9169 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9170
9171 /* Replace the close paren with "> (". */
9172 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9173
9174 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9175 rich_loc->add_fixit_insert_after (")");
9176 }
9177
9178
9179 /* Parse a cast-expression.
9180
9181 cast-expression:
9182 unary-expression
9183 ( type-id ) cast-expression
9184
9185 ADDRESS_P is true iff the unary-expression is appearing as the
9186 operand of the `&' operator. CAST_P is true if this expression is
9187 the target of a cast.
9188
9189 Returns a representation of the expression. */
9190
9191 static cp_expr
9192 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9193 bool decltype_p, cp_id_kind * pidk)
9194 {
9195 /* If it's a `(', then we might be looking at a cast. */
9196 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9197 {
9198 tree type = NULL_TREE;
9199 cp_expr expr (NULL_TREE);
9200 int cast_expression = 0;
9201 const char *saved_message;
9202
9203 /* There's no way to know yet whether or not this is a cast.
9204 For example, `(int (3))' is a unary-expression, while `(int)
9205 3' is a cast. So, we resort to parsing tentatively. */
9206 cp_parser_parse_tentatively (parser);
9207 /* Types may not be defined in a cast. */
9208 saved_message = parser->type_definition_forbidden_message;
9209 parser->type_definition_forbidden_message
9210 = G_("types may not be defined in casts");
9211 /* Consume the `('. */
9212 matching_parens parens;
9213 cp_token *open_paren = parens.consume_open (parser);
9214 location_t open_paren_loc = open_paren->location;
9215 location_t close_paren_loc = UNKNOWN_LOCATION;
9216
9217 /* A very tricky bit is that `(struct S) { 3 }' is a
9218 compound-literal (which we permit in C++ as an extension).
9219 But, that construct is not a cast-expression -- it is a
9220 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9221 is legal; if the compound-literal were a cast-expression,
9222 you'd need an extra set of parentheses.) But, if we parse
9223 the type-id, and it happens to be a class-specifier, then we
9224 will commit to the parse at that point, because we cannot
9225 undo the action that is done when creating a new class. So,
9226 then we cannot back up and do a postfix-expression.
9227
9228 Another tricky case is the following (c++/29234):
9229
9230 struct S { void operator () (); };
9231
9232 void foo ()
9233 {
9234 ( S()() );
9235 }
9236
9237 As a type-id we parse the parenthesized S()() as a function
9238 returning a function, groktypename complains and we cannot
9239 back up in this case either.
9240
9241 Therefore, we scan ahead to the closing `)', and check to see
9242 if the tokens after the `)' can start a cast-expression. Otherwise
9243 we are dealing with an unary-expression, a postfix-expression
9244 or something else.
9245
9246 Yet another tricky case, in C++11, is the following (c++/54891):
9247
9248 (void)[]{};
9249
9250 The issue is that usually, besides the case of lambda-expressions,
9251 the parenthesized type-id cannot be followed by '[', and, eg, we
9252 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9253 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9254 we don't commit, we try a cast-expression, then an unary-expression.
9255
9256 Save tokens so that we can put them back. */
9257 cp_lexer_save_tokens (parser->lexer);
9258
9259 /* We may be looking at a cast-expression. */
9260 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9261 /*consume_paren=*/true))
9262 cast_expression
9263 = cp_parser_tokens_start_cast_expression (parser);
9264
9265 /* Roll back the tokens we skipped. */
9266 cp_lexer_rollback_tokens (parser->lexer);
9267 /* If we aren't looking at a cast-expression, simulate an error so
9268 that the call to cp_parser_error_occurred below returns true. */
9269 if (!cast_expression)
9270 cp_parser_simulate_error (parser);
9271 else
9272 {
9273 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9274 parser->in_type_id_in_expr_p = true;
9275 /* Look for the type-id. */
9276 type = cp_parser_type_id (parser);
9277 /* Look for the closing `)'. */
9278 cp_token *close_paren = parens.require_close (parser);
9279 if (close_paren)
9280 close_paren_loc = close_paren->location;
9281 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9282 }
9283
9284 /* Restore the saved message. */
9285 parser->type_definition_forbidden_message = saved_message;
9286
9287 /* At this point this can only be either a cast or a
9288 parenthesized ctor such as `(T ())' that looks like a cast to
9289 function returning T. */
9290 if (!cp_parser_error_occurred (parser))
9291 {
9292 /* Only commit if the cast-expression doesn't start with
9293 '++', '--', or '[' in C++11. */
9294 if (cast_expression > 0)
9295 cp_parser_commit_to_topmost_tentative_parse (parser);
9296
9297 expr = cp_parser_cast_expression (parser,
9298 /*address_p=*/false,
9299 /*cast_p=*/true,
9300 /*decltype_p=*/false,
9301 pidk);
9302
9303 if (cp_parser_parse_definitely (parser))
9304 {
9305 /* Warn about old-style casts, if so requested. */
9306 if (warn_old_style_cast
9307 && !in_system_header_at (input_location)
9308 && !VOID_TYPE_P (type)
9309 && current_lang_name != lang_name_c)
9310 {
9311 gcc_rich_location rich_loc (input_location);
9312 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9313 expr, type);
9314 warning_at (&rich_loc, OPT_Wold_style_cast,
9315 "use of old-style cast to %q#T", type);
9316 }
9317
9318 /* Only type conversions to integral or enumeration types
9319 can be used in constant-expressions. */
9320 if (!cast_valid_in_integral_constant_expression_p (type)
9321 && cp_parser_non_integral_constant_expression (parser,
9322 NIC_CAST))
9323 return error_mark_node;
9324
9325 /* Perform the cast. */
9326 /* Make a location:
9327 (TYPE) EXPR
9328 ^~~~~~~~~~~
9329 with start==caret at the open paren, extending to the
9330 end of "expr". */
9331 location_t cast_loc = make_location (open_paren_loc,
9332 open_paren_loc,
9333 expr.get_finish ());
9334 expr = build_c_cast (cast_loc, type, expr);
9335 return expr;
9336 }
9337 }
9338 else
9339 cp_parser_abort_tentative_parse (parser);
9340 }
9341
9342 /* If we get here, then it's not a cast, so it must be a
9343 unary-expression. */
9344 return cp_parser_unary_expression (parser, pidk, address_p,
9345 cast_p, decltype_p);
9346 }
9347
9348 /* Parse a binary expression of the general form:
9349
9350 pm-expression:
9351 cast-expression
9352 pm-expression .* cast-expression
9353 pm-expression ->* cast-expression
9354
9355 multiplicative-expression:
9356 pm-expression
9357 multiplicative-expression * pm-expression
9358 multiplicative-expression / pm-expression
9359 multiplicative-expression % pm-expression
9360
9361 additive-expression:
9362 multiplicative-expression
9363 additive-expression + multiplicative-expression
9364 additive-expression - multiplicative-expression
9365
9366 shift-expression:
9367 additive-expression
9368 shift-expression << additive-expression
9369 shift-expression >> additive-expression
9370
9371 relational-expression:
9372 shift-expression
9373 relational-expression < shift-expression
9374 relational-expression > shift-expression
9375 relational-expression <= shift-expression
9376 relational-expression >= shift-expression
9377
9378 GNU Extension:
9379
9380 relational-expression:
9381 relational-expression <? shift-expression
9382 relational-expression >? shift-expression
9383
9384 equality-expression:
9385 relational-expression
9386 equality-expression == relational-expression
9387 equality-expression != relational-expression
9388
9389 and-expression:
9390 equality-expression
9391 and-expression & equality-expression
9392
9393 exclusive-or-expression:
9394 and-expression
9395 exclusive-or-expression ^ and-expression
9396
9397 inclusive-or-expression:
9398 exclusive-or-expression
9399 inclusive-or-expression | exclusive-or-expression
9400
9401 logical-and-expression:
9402 inclusive-or-expression
9403 logical-and-expression && inclusive-or-expression
9404
9405 logical-or-expression:
9406 logical-and-expression
9407 logical-or-expression || logical-and-expression
9408
9409 All these are implemented with a single function like:
9410
9411 binary-expression:
9412 simple-cast-expression
9413 binary-expression <token> binary-expression
9414
9415 CAST_P is true if this expression is the target of a cast.
9416
9417 The binops_by_token map is used to get the tree codes for each <token> type.
9418 binary-expressions are associated according to a precedence table. */
9419
9420 #define TOKEN_PRECEDENCE(token) \
9421 (((token->type == CPP_GREATER \
9422 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9423 && !parser->greater_than_is_operator_p) \
9424 ? PREC_NOT_OPERATOR \
9425 : binops_by_token[token->type].prec)
9426
9427 static cp_expr
9428 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9429 bool no_toplevel_fold_p,
9430 bool decltype_p,
9431 enum cp_parser_prec prec,
9432 cp_id_kind * pidk)
9433 {
9434 cp_parser_expression_stack stack;
9435 cp_parser_expression_stack_entry *sp = &stack[0];
9436 cp_parser_expression_stack_entry current;
9437 cp_expr rhs;
9438 cp_token *token;
9439 enum tree_code rhs_type;
9440 enum cp_parser_prec new_prec, lookahead_prec;
9441 tree overload;
9442
9443 /* Parse the first expression. */
9444 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9445 ? TRUTH_NOT_EXPR : ERROR_MARK);
9446 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9447 cast_p, decltype_p, pidk);
9448 current.prec = prec;
9449
9450 if (cp_parser_error_occurred (parser))
9451 return error_mark_node;
9452
9453 for (;;)
9454 {
9455 /* Get an operator token. */
9456 token = cp_lexer_peek_token (parser->lexer);
9457
9458 if (warn_cxx11_compat
9459 && token->type == CPP_RSHIFT
9460 && !parser->greater_than_is_operator_p)
9461 {
9462 if (warning_at (token->location, OPT_Wc__11_compat,
9463 "%<>>%> operator is treated"
9464 " as two right angle brackets in C++11"))
9465 inform (token->location,
9466 "suggest parentheses around %<>>%> expression");
9467 }
9468
9469 new_prec = TOKEN_PRECEDENCE (token);
9470 if (new_prec != PREC_NOT_OPERATOR
9471 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9472 /* This is a fold-expression; handle it later. */
9473 new_prec = PREC_NOT_OPERATOR;
9474
9475 /* Popping an entry off the stack means we completed a subexpression:
9476 - either we found a token which is not an operator (`>' where it is not
9477 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9478 will happen repeatedly;
9479 - or, we found an operator which has lower priority. This is the case
9480 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9481 parsing `3 * 4'. */
9482 if (new_prec <= current.prec)
9483 {
9484 if (sp == stack)
9485 break;
9486 else
9487 goto pop;
9488 }
9489
9490 get_rhs:
9491 current.tree_type = binops_by_token[token->type].tree_type;
9492 current.loc = token->location;
9493
9494 /* We used the operator token. */
9495 cp_lexer_consume_token (parser->lexer);
9496
9497 /* For "false && x" or "true || x", x will never be executed;
9498 disable warnings while evaluating it. */
9499 if (current.tree_type == TRUTH_ANDIF_EXPR)
9500 c_inhibit_evaluation_warnings +=
9501 cp_fully_fold (current.lhs) == truthvalue_false_node;
9502 else if (current.tree_type == TRUTH_ORIF_EXPR)
9503 c_inhibit_evaluation_warnings +=
9504 cp_fully_fold (current.lhs) == truthvalue_true_node;
9505
9506 /* Extract another operand. It may be the RHS of this expression
9507 or the LHS of a new, higher priority expression. */
9508 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9509 ? TRUTH_NOT_EXPR : ERROR_MARK);
9510 rhs = cp_parser_simple_cast_expression (parser);
9511
9512 /* Get another operator token. Look up its precedence to avoid
9513 building a useless (immediately popped) stack entry for common
9514 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9515 token = cp_lexer_peek_token (parser->lexer);
9516 lookahead_prec = TOKEN_PRECEDENCE (token);
9517 if (lookahead_prec != PREC_NOT_OPERATOR
9518 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9519 lookahead_prec = PREC_NOT_OPERATOR;
9520 if (lookahead_prec > new_prec)
9521 {
9522 /* ... and prepare to parse the RHS of the new, higher priority
9523 expression. Since precedence levels on the stack are
9524 monotonically increasing, we do not have to care about
9525 stack overflows. */
9526 *sp = current;
9527 ++sp;
9528 current.lhs = rhs;
9529 current.lhs_type = rhs_type;
9530 current.prec = new_prec;
9531 new_prec = lookahead_prec;
9532 goto get_rhs;
9533
9534 pop:
9535 lookahead_prec = new_prec;
9536 /* If the stack is not empty, we have parsed into LHS the right side
9537 (`4' in the example above) of an expression we had suspended.
9538 We can use the information on the stack to recover the LHS (`3')
9539 from the stack together with the tree code (`MULT_EXPR'), and
9540 the precedence of the higher level subexpression
9541 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9542 which will be used to actually build the additive expression. */
9543 rhs = current.lhs;
9544 rhs_type = current.lhs_type;
9545 --sp;
9546 current = *sp;
9547 }
9548
9549 /* Undo the disabling of warnings done above. */
9550 if (current.tree_type == TRUTH_ANDIF_EXPR)
9551 c_inhibit_evaluation_warnings -=
9552 cp_fully_fold (current.lhs) == truthvalue_false_node;
9553 else if (current.tree_type == TRUTH_ORIF_EXPR)
9554 c_inhibit_evaluation_warnings -=
9555 cp_fully_fold (current.lhs) == truthvalue_true_node;
9556
9557 if (warn_logical_not_paren
9558 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9559 && current.lhs_type == TRUTH_NOT_EXPR
9560 /* Avoid warning for !!x == y. */
9561 && (TREE_CODE (current.lhs) != NE_EXPR
9562 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9563 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9564 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9565 /* Avoid warning for !b == y where b is boolean. */
9566 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9567 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9568 != BOOLEAN_TYPE))))
9569 /* Avoid warning for !!b == y where b is boolean. */
9570 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
9571 || TREE_TYPE (current.lhs) == NULL_TREE
9572 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9573 warn_logical_not_parentheses (current.loc, current.tree_type,
9574 current.lhs, maybe_constant_value (rhs));
9575
9576 overload = NULL;
9577
9578 location_t combined_loc = make_location (current.loc,
9579 current.lhs.get_start (),
9580 rhs.get_finish ());
9581
9582 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9583 ERROR_MARK for everything that is not a binary expression.
9584 This makes warn_about_parentheses miss some warnings that
9585 involve unary operators. For unary expressions we should
9586 pass the correct tree_code unless the unary expression was
9587 surrounded by parentheses.
9588 */
9589 if (no_toplevel_fold_p
9590 && lookahead_prec <= current.prec
9591 && sp == stack)
9592 {
9593 if (current.lhs == error_mark_node || rhs == error_mark_node)
9594 current.lhs = error_mark_node;
9595 else
9596 {
9597 current.lhs
9598 = build_min (current.tree_type,
9599 TREE_CODE_CLASS (current.tree_type)
9600 == tcc_comparison
9601 ? boolean_type_node : TREE_TYPE (current.lhs),
9602 current.lhs.get_value (), rhs.get_value ());
9603 SET_EXPR_LOCATION (current.lhs, combined_loc);
9604 }
9605 }
9606 else
9607 {
9608 op_location_t op_loc (current.loc, combined_loc);
9609 current.lhs = build_x_binary_op (op_loc, current.tree_type,
9610 current.lhs, current.lhs_type,
9611 rhs, rhs_type, &overload,
9612 complain_flags (decltype_p));
9613 /* TODO: build_x_binary_op doesn't always honor the location. */
9614 current.lhs.set_location (combined_loc);
9615 }
9616 current.lhs_type = current.tree_type;
9617
9618 /* If the binary operator required the use of an overloaded operator,
9619 then this expression cannot be an integral constant-expression.
9620 An overloaded operator can be used even if both operands are
9621 otherwise permissible in an integral constant-expression if at
9622 least one of the operands is of enumeration type. */
9623
9624 if (overload
9625 && cp_parser_non_integral_constant_expression (parser,
9626 NIC_OVERLOADED))
9627 return error_mark_node;
9628 }
9629
9630 return current.lhs;
9631 }
9632
9633 static cp_expr
9634 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9635 bool no_toplevel_fold_p,
9636 enum cp_parser_prec prec,
9637 cp_id_kind * pidk)
9638 {
9639 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9640 /*decltype*/false, prec, pidk);
9641 }
9642
9643 /* Parse the `? expression : assignment-expression' part of a
9644 conditional-expression. The LOGICAL_OR_EXPR is the
9645 logical-or-expression that started the conditional-expression.
9646 Returns a representation of the entire conditional-expression.
9647
9648 This routine is used by cp_parser_assignment_expression.
9649
9650 ? expression : assignment-expression
9651
9652 GNU Extensions:
9653
9654 ? : assignment-expression */
9655
9656 static tree
9657 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9658 {
9659 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9660 cp_expr assignment_expr;
9661 struct cp_token *token;
9662 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9663
9664 /* Consume the `?' token. */
9665 cp_lexer_consume_token (parser->lexer);
9666 token = cp_lexer_peek_token (parser->lexer);
9667 if (cp_parser_allow_gnu_extensions_p (parser)
9668 && token->type == CPP_COLON)
9669 {
9670 pedwarn (token->location, OPT_Wpedantic,
9671 "ISO C++ does not allow ?: with omitted middle operand");
9672 /* Implicit true clause. */
9673 expr = NULL_TREE;
9674 c_inhibit_evaluation_warnings +=
9675 folded_logical_or_expr == truthvalue_true_node;
9676 warn_for_omitted_condop (token->location, logical_or_expr);
9677 }
9678 else
9679 {
9680 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9681 parser->colon_corrects_to_scope_p = false;
9682 /* Parse the expression. */
9683 c_inhibit_evaluation_warnings +=
9684 folded_logical_or_expr == truthvalue_false_node;
9685 expr = cp_parser_expression (parser);
9686 c_inhibit_evaluation_warnings +=
9687 ((folded_logical_or_expr == truthvalue_true_node)
9688 - (folded_logical_or_expr == truthvalue_false_node));
9689 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9690 }
9691
9692 /* The next token should be a `:'. */
9693 cp_parser_require (parser, CPP_COLON, RT_COLON);
9694 /* Parse the assignment-expression. */
9695 assignment_expr = cp_parser_assignment_expression (parser);
9696 c_inhibit_evaluation_warnings -=
9697 folded_logical_or_expr == truthvalue_true_node;
9698
9699 /* Make a location:
9700 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9701 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9702 with the caret at the "?", ranging from the start of
9703 the logical_or_expr to the end of the assignment_expr. */
9704 loc = make_location (loc,
9705 logical_or_expr.get_start (),
9706 assignment_expr.get_finish ());
9707
9708 /* Build the conditional-expression. */
9709 return build_x_conditional_expr (loc, logical_or_expr,
9710 expr,
9711 assignment_expr,
9712 tf_warning_or_error);
9713 }
9714
9715 /* Parse an assignment-expression.
9716
9717 assignment-expression:
9718 conditional-expression
9719 logical-or-expression assignment-operator assignment_expression
9720 throw-expression
9721
9722 CAST_P is true if this expression is the target of a cast.
9723 DECLTYPE_P is true if this expression is the operand of decltype.
9724
9725 Returns a representation for the expression. */
9726
9727 static cp_expr
9728 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9729 bool cast_p, bool decltype_p)
9730 {
9731 cp_expr expr;
9732
9733 /* If the next token is the `throw' keyword, then we're looking at
9734 a throw-expression. */
9735 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9736 expr = cp_parser_throw_expression (parser);
9737 /* Otherwise, it must be that we are looking at a
9738 logical-or-expression. */
9739 else
9740 {
9741 /* Parse the binary expressions (logical-or-expression). */
9742 expr = cp_parser_binary_expression (parser, cast_p, false,
9743 decltype_p,
9744 PREC_NOT_OPERATOR, pidk);
9745 /* If the next token is a `?' then we're actually looking at a
9746 conditional-expression. */
9747 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9748 return cp_parser_question_colon_clause (parser, expr);
9749 else
9750 {
9751 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9752
9753 /* If it's an assignment-operator, we're using the second
9754 production. */
9755 enum tree_code assignment_operator
9756 = cp_parser_assignment_operator_opt (parser);
9757 if (assignment_operator != ERROR_MARK)
9758 {
9759 bool non_constant_p;
9760
9761 /* Parse the right-hand side of the assignment. */
9762 cp_expr rhs = cp_parser_initializer_clause (parser,
9763 &non_constant_p);
9764
9765 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9766 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9767
9768 /* An assignment may not appear in a
9769 constant-expression. */
9770 if (cp_parser_non_integral_constant_expression (parser,
9771 NIC_ASSIGNMENT))
9772 return error_mark_node;
9773 /* Build the assignment expression. Its default
9774 location:
9775 LHS = RHS
9776 ~~~~^~~~~
9777 is the location of the '=' token as the
9778 caret, ranging from the start of the lhs to the
9779 end of the rhs. */
9780 loc = make_location (loc,
9781 expr.get_start (),
9782 rhs.get_finish ());
9783 expr = build_x_modify_expr (loc, expr,
9784 assignment_operator,
9785 rhs,
9786 complain_flags (decltype_p));
9787 /* TODO: build_x_modify_expr doesn't honor the location,
9788 so we must set it here. */
9789 expr.set_location (loc);
9790 }
9791 }
9792 }
9793
9794 return expr;
9795 }
9796
9797 /* Parse an (optional) assignment-operator.
9798
9799 assignment-operator: one of
9800 = *= /= %= += -= >>= <<= &= ^= |=
9801
9802 GNU Extension:
9803
9804 assignment-operator: one of
9805 <?= >?=
9806
9807 If the next token is an assignment operator, the corresponding tree
9808 code is returned, and the token is consumed. For example, for
9809 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9810 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9811 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9812 operator, ERROR_MARK is returned. */
9813
9814 static enum tree_code
9815 cp_parser_assignment_operator_opt (cp_parser* parser)
9816 {
9817 enum tree_code op;
9818 cp_token *token;
9819
9820 /* Peek at the next token. */
9821 token = cp_lexer_peek_token (parser->lexer);
9822
9823 switch (token->type)
9824 {
9825 case CPP_EQ:
9826 op = NOP_EXPR;
9827 break;
9828
9829 case CPP_MULT_EQ:
9830 op = MULT_EXPR;
9831 break;
9832
9833 case CPP_DIV_EQ:
9834 op = TRUNC_DIV_EXPR;
9835 break;
9836
9837 case CPP_MOD_EQ:
9838 op = TRUNC_MOD_EXPR;
9839 break;
9840
9841 case CPP_PLUS_EQ:
9842 op = PLUS_EXPR;
9843 break;
9844
9845 case CPP_MINUS_EQ:
9846 op = MINUS_EXPR;
9847 break;
9848
9849 case CPP_RSHIFT_EQ:
9850 op = RSHIFT_EXPR;
9851 break;
9852
9853 case CPP_LSHIFT_EQ:
9854 op = LSHIFT_EXPR;
9855 break;
9856
9857 case CPP_AND_EQ:
9858 op = BIT_AND_EXPR;
9859 break;
9860
9861 case CPP_XOR_EQ:
9862 op = BIT_XOR_EXPR;
9863 break;
9864
9865 case CPP_OR_EQ:
9866 op = BIT_IOR_EXPR;
9867 break;
9868
9869 default:
9870 /* Nothing else is an assignment operator. */
9871 op = ERROR_MARK;
9872 }
9873
9874 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9875 if (op != ERROR_MARK
9876 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9877 op = ERROR_MARK;
9878
9879 /* If it was an assignment operator, consume it. */
9880 if (op != ERROR_MARK)
9881 cp_lexer_consume_token (parser->lexer);
9882
9883 return op;
9884 }
9885
9886 /* Parse an expression.
9887
9888 expression:
9889 assignment-expression
9890 expression , assignment-expression
9891
9892 CAST_P is true if this expression is the target of a cast.
9893 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9894 except possibly parenthesized or on the RHS of a comma (N3276).
9895
9896 Returns a representation of the expression. */
9897
9898 static cp_expr
9899 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9900 bool cast_p, bool decltype_p)
9901 {
9902 cp_expr expression = NULL_TREE;
9903 location_t loc = UNKNOWN_LOCATION;
9904
9905 while (true)
9906 {
9907 cp_expr assignment_expression;
9908
9909 /* Parse the next assignment-expression. */
9910 assignment_expression
9911 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9912
9913 /* We don't create a temporary for a call that is the immediate operand
9914 of decltype or on the RHS of a comma. But when we see a comma, we
9915 need to create a temporary for a call on the LHS. */
9916 if (decltype_p && !processing_template_decl
9917 && TREE_CODE (assignment_expression) == CALL_EXPR
9918 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9919 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9920 assignment_expression
9921 = build_cplus_new (TREE_TYPE (assignment_expression),
9922 assignment_expression, tf_warning_or_error);
9923
9924 /* If this is the first assignment-expression, we can just
9925 save it away. */
9926 if (!expression)
9927 expression = assignment_expression;
9928 else
9929 {
9930 /* Create a location with caret at the comma, ranging
9931 from the start of the LHS to the end of the RHS. */
9932 loc = make_location (loc,
9933 expression.get_start (),
9934 assignment_expression.get_finish ());
9935 expression = build_x_compound_expr (loc, expression,
9936 assignment_expression,
9937 complain_flags (decltype_p));
9938 expression.set_location (loc);
9939 }
9940 /* If the next token is not a comma, or we're in a fold-expression, then
9941 we are done with the expression. */
9942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9943 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9944 break;
9945 /* Consume the `,'. */
9946 loc = cp_lexer_peek_token (parser->lexer)->location;
9947 cp_lexer_consume_token (parser->lexer);
9948 /* A comma operator cannot appear in a constant-expression. */
9949 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9950 expression = error_mark_node;
9951 }
9952
9953 return expression;
9954 }
9955
9956 /* Parse a constant-expression.
9957
9958 constant-expression:
9959 conditional-expression
9960
9961 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9962 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9963 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9964 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9965 only parse a conditional-expression, otherwise parse an
9966 assignment-expression. See below for rationale. */
9967
9968 static cp_expr
9969 cp_parser_constant_expression (cp_parser* parser,
9970 bool allow_non_constant_p,
9971 bool *non_constant_p,
9972 bool strict_p)
9973 {
9974 bool saved_integral_constant_expression_p;
9975 bool saved_allow_non_integral_constant_expression_p;
9976 bool saved_non_integral_constant_expression_p;
9977 cp_expr expression;
9978
9979 /* It might seem that we could simply parse the
9980 conditional-expression, and then check to see if it were
9981 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9982 one that the compiler can figure out is constant, possibly after
9983 doing some simplifications or optimizations. The standard has a
9984 precise definition of constant-expression, and we must honor
9985 that, even though it is somewhat more restrictive.
9986
9987 For example:
9988
9989 int i[(2, 3)];
9990
9991 is not a legal declaration, because `(2, 3)' is not a
9992 constant-expression. The `,' operator is forbidden in a
9993 constant-expression. However, GCC's constant-folding machinery
9994 will fold this operation to an INTEGER_CST for `3'. */
9995
9996 /* Save the old settings. */
9997 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9998 saved_allow_non_integral_constant_expression_p
9999 = parser->allow_non_integral_constant_expression_p;
10000 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10001 /* We are now parsing a constant-expression. */
10002 parser->integral_constant_expression_p = true;
10003 parser->allow_non_integral_constant_expression_p
10004 = (allow_non_constant_p || cxx_dialect >= cxx11);
10005 parser->non_integral_constant_expression_p = false;
10006 /* Although the grammar says "conditional-expression", when not STRICT_P,
10007 we parse an "assignment-expression", which also permits
10008 "throw-expression" and the use of assignment operators. In the case
10009 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10010 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10011 actually essential that we look for an assignment-expression.
10012 For example, cp_parser_initializer_clauses uses this function to
10013 determine whether a particular assignment-expression is in fact
10014 constant. */
10015 if (strict_p)
10016 {
10017 /* Parse the binary expressions (logical-or-expression). */
10018 expression = cp_parser_binary_expression (parser, false, false, false,
10019 PREC_NOT_OPERATOR, NULL);
10020 /* If the next token is a `?' then we're actually looking at
10021 a conditional-expression; otherwise we're done. */
10022 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10023 expression = cp_parser_question_colon_clause (parser, expression);
10024 }
10025 else
10026 expression = cp_parser_assignment_expression (parser);
10027 /* Restore the old settings. */
10028 parser->integral_constant_expression_p
10029 = saved_integral_constant_expression_p;
10030 parser->allow_non_integral_constant_expression_p
10031 = saved_allow_non_integral_constant_expression_p;
10032 if (cxx_dialect >= cxx11)
10033 {
10034 /* Require an rvalue constant expression here; that's what our
10035 callers expect. Reference constant expressions are handled
10036 separately in e.g. cp_parser_template_argument. */
10037 tree decay = expression;
10038 if (TREE_TYPE (expression)
10039 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10040 decay = build_address (expression);
10041 bool is_const = potential_rvalue_constant_expression (decay);
10042 parser->non_integral_constant_expression_p = !is_const;
10043 if (!is_const && !allow_non_constant_p)
10044 require_potential_rvalue_constant_expression (decay);
10045 }
10046 if (allow_non_constant_p)
10047 *non_constant_p = parser->non_integral_constant_expression_p;
10048 parser->non_integral_constant_expression_p
10049 = saved_non_integral_constant_expression_p;
10050
10051 return expression;
10052 }
10053
10054 /* Parse __builtin_offsetof.
10055
10056 offsetof-expression:
10057 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10058
10059 offsetof-member-designator:
10060 id-expression
10061 | offsetof-member-designator "." id-expression
10062 | offsetof-member-designator "[" expression "]"
10063 | offsetof-member-designator "->" id-expression */
10064
10065 static cp_expr
10066 cp_parser_builtin_offsetof (cp_parser *parser)
10067 {
10068 int save_ice_p, save_non_ice_p;
10069 tree type;
10070 cp_expr expr;
10071 cp_id_kind dummy;
10072 cp_token *token;
10073 location_t finish_loc;
10074
10075 /* We're about to accept non-integral-constant things, but will
10076 definitely yield an integral constant expression. Save and
10077 restore these values around our local parsing. */
10078 save_ice_p = parser->integral_constant_expression_p;
10079 save_non_ice_p = parser->non_integral_constant_expression_p;
10080
10081 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10082
10083 /* Consume the "__builtin_offsetof" token. */
10084 cp_lexer_consume_token (parser->lexer);
10085 /* Consume the opening `('. */
10086 matching_parens parens;
10087 parens.require_open (parser);
10088 /* Parse the type-id. */
10089 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10090 {
10091 const char *saved_message = parser->type_definition_forbidden_message;
10092 parser->type_definition_forbidden_message
10093 = G_("types may not be defined within __builtin_offsetof");
10094 type = cp_parser_type_id (parser);
10095 parser->type_definition_forbidden_message = saved_message;
10096 }
10097 /* Look for the `,'. */
10098 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10099 token = cp_lexer_peek_token (parser->lexer);
10100
10101 /* Build the (type *)null that begins the traditional offsetof macro. */
10102 tree object_ptr
10103 = build_static_cast (build_pointer_type (type), null_pointer_node,
10104 tf_warning_or_error);
10105
10106 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10107 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10108 true, &dummy, token->location);
10109 while (true)
10110 {
10111 token = cp_lexer_peek_token (parser->lexer);
10112 switch (token->type)
10113 {
10114 case CPP_OPEN_SQUARE:
10115 /* offsetof-member-designator "[" expression "]" */
10116 expr = cp_parser_postfix_open_square_expression (parser, expr,
10117 true, false);
10118 break;
10119
10120 case CPP_DEREF:
10121 /* offsetof-member-designator "->" identifier */
10122 expr = grok_array_decl (token->location, expr,
10123 integer_zero_node, false);
10124 /* FALLTHRU */
10125
10126 case CPP_DOT:
10127 /* offsetof-member-designator "." identifier */
10128 cp_lexer_consume_token (parser->lexer);
10129 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10130 expr, true, &dummy,
10131 token->location);
10132 break;
10133
10134 case CPP_CLOSE_PAREN:
10135 /* Consume the ")" token. */
10136 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10137 cp_lexer_consume_token (parser->lexer);
10138 goto success;
10139
10140 default:
10141 /* Error. We know the following require will fail, but
10142 that gives the proper error message. */
10143 parens.require_close (parser);
10144 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10145 expr = error_mark_node;
10146 goto failure;
10147 }
10148 }
10149
10150 success:
10151 /* Make a location of the form:
10152 __builtin_offsetof (struct s, f)
10153 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10154 with caret at the type-id, ranging from the start of the
10155 "_builtin_offsetof" token to the close paren. */
10156 loc = make_location (loc, start_loc, finish_loc);
10157 /* The result will be an INTEGER_CST, so we need to explicitly
10158 preserve the location. */
10159 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10160
10161 failure:
10162 parser->integral_constant_expression_p = save_ice_p;
10163 parser->non_integral_constant_expression_p = save_non_ice_p;
10164
10165 expr = expr.maybe_add_location_wrapper ();
10166 return expr;
10167 }
10168
10169 /* Parse a trait expression.
10170
10171 Returns a representation of the expression, the underlying type
10172 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10173
10174 static cp_expr
10175 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10176 {
10177 cp_trait_kind kind;
10178 tree type1, type2 = NULL_TREE;
10179 bool binary = false;
10180 bool variadic = false;
10181
10182 switch (keyword)
10183 {
10184 case RID_HAS_NOTHROW_ASSIGN:
10185 kind = CPTK_HAS_NOTHROW_ASSIGN;
10186 break;
10187 case RID_HAS_NOTHROW_CONSTRUCTOR:
10188 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10189 break;
10190 case RID_HAS_NOTHROW_COPY:
10191 kind = CPTK_HAS_NOTHROW_COPY;
10192 break;
10193 case RID_HAS_TRIVIAL_ASSIGN:
10194 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10195 break;
10196 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10197 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10198 break;
10199 case RID_HAS_TRIVIAL_COPY:
10200 kind = CPTK_HAS_TRIVIAL_COPY;
10201 break;
10202 case RID_HAS_TRIVIAL_DESTRUCTOR:
10203 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10204 break;
10205 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10206 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10207 break;
10208 case RID_HAS_VIRTUAL_DESTRUCTOR:
10209 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10210 break;
10211 case RID_IS_ABSTRACT:
10212 kind = CPTK_IS_ABSTRACT;
10213 break;
10214 case RID_IS_AGGREGATE:
10215 kind = CPTK_IS_AGGREGATE;
10216 break;
10217 case RID_IS_BASE_OF:
10218 kind = CPTK_IS_BASE_OF;
10219 binary = true;
10220 break;
10221 case RID_IS_CLASS:
10222 kind = CPTK_IS_CLASS;
10223 break;
10224 case RID_IS_EMPTY:
10225 kind = CPTK_IS_EMPTY;
10226 break;
10227 case RID_IS_ENUM:
10228 kind = CPTK_IS_ENUM;
10229 break;
10230 case RID_IS_FINAL:
10231 kind = CPTK_IS_FINAL;
10232 break;
10233 case RID_IS_LITERAL_TYPE:
10234 kind = CPTK_IS_LITERAL_TYPE;
10235 break;
10236 case RID_IS_POD:
10237 kind = CPTK_IS_POD;
10238 break;
10239 case RID_IS_POLYMORPHIC:
10240 kind = CPTK_IS_POLYMORPHIC;
10241 break;
10242 case RID_IS_SAME_AS:
10243 kind = CPTK_IS_SAME_AS;
10244 binary = true;
10245 break;
10246 case RID_IS_STD_LAYOUT:
10247 kind = CPTK_IS_STD_LAYOUT;
10248 break;
10249 case RID_IS_TRIVIAL:
10250 kind = CPTK_IS_TRIVIAL;
10251 break;
10252 case RID_IS_TRIVIALLY_ASSIGNABLE:
10253 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10254 binary = true;
10255 break;
10256 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10257 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10258 variadic = true;
10259 break;
10260 case RID_IS_TRIVIALLY_COPYABLE:
10261 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10262 break;
10263 case RID_IS_UNION:
10264 kind = CPTK_IS_UNION;
10265 break;
10266 case RID_UNDERLYING_TYPE:
10267 kind = CPTK_UNDERLYING_TYPE;
10268 break;
10269 case RID_BASES:
10270 kind = CPTK_BASES;
10271 break;
10272 case RID_DIRECT_BASES:
10273 kind = CPTK_DIRECT_BASES;
10274 break;
10275 case RID_IS_ASSIGNABLE:
10276 kind = CPTK_IS_ASSIGNABLE;
10277 binary = true;
10278 break;
10279 case RID_IS_CONSTRUCTIBLE:
10280 kind = CPTK_IS_CONSTRUCTIBLE;
10281 variadic = true;
10282 break;
10283 default:
10284 gcc_unreachable ();
10285 }
10286
10287 /* Get location of initial token. */
10288 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10289
10290 /* Consume the token. */
10291 cp_lexer_consume_token (parser->lexer);
10292
10293 matching_parens parens;
10294 parens.require_open (parser);
10295
10296 {
10297 type_id_in_expr_sentinel s (parser);
10298 type1 = cp_parser_type_id (parser);
10299 }
10300
10301 if (type1 == error_mark_node)
10302 return error_mark_node;
10303
10304 if (binary)
10305 {
10306 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10307
10308 {
10309 type_id_in_expr_sentinel s (parser);
10310 type2 = cp_parser_type_id (parser);
10311 }
10312
10313 if (type2 == error_mark_node)
10314 return error_mark_node;
10315 }
10316 else if (variadic)
10317 {
10318 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10319 {
10320 cp_lexer_consume_token (parser->lexer);
10321 tree elt = cp_parser_type_id (parser);
10322 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10323 {
10324 cp_lexer_consume_token (parser->lexer);
10325 elt = make_pack_expansion (elt);
10326 }
10327 if (elt == error_mark_node)
10328 return error_mark_node;
10329 type2 = tree_cons (NULL_TREE, elt, type2);
10330 }
10331 }
10332
10333 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10334 parens.require_close (parser);
10335
10336 /* Construct a location of the form:
10337 __is_trivially_copyable(_Tp)
10338 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10339 with start == caret, finishing at the close-paren. */
10340 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10341
10342 /* Complete the trait expression, which may mean either processing
10343 the trait expr now or saving it for template instantiation. */
10344 switch (kind)
10345 {
10346 case CPTK_UNDERLYING_TYPE:
10347 return cp_expr (finish_underlying_type (type1), trait_loc);
10348 case CPTK_BASES:
10349 return cp_expr (finish_bases (type1, false), trait_loc);
10350 case CPTK_DIRECT_BASES:
10351 return cp_expr (finish_bases (type1, true), trait_loc);
10352 default:
10353 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10354 }
10355 }
10356
10357 /* Parse a lambda expression.
10358
10359 lambda-expression:
10360 lambda-introducer lambda-declarator [opt] compound-statement
10361
10362 Returns a representation of the expression. */
10363
10364 static cp_expr
10365 cp_parser_lambda_expression (cp_parser* parser)
10366 {
10367 tree lambda_expr = build_lambda_expr ();
10368 tree type;
10369 bool ok = true;
10370 cp_token *token = cp_lexer_peek_token (parser->lexer);
10371 cp_token_position start = 0;
10372
10373 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10374
10375 if (cxx_dialect >= cxx2a)
10376 /* C++20 allows lambdas in unevaluated context. */;
10377 else if (cp_unevaluated_operand)
10378 {
10379 if (!token->error_reported)
10380 {
10381 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10382 "lambda-expression in unevaluated context"
10383 " only available with -std=c++2a or -std=gnu++2a");
10384 token->error_reported = true;
10385 }
10386 ok = false;
10387 }
10388 else if (parser->in_template_argument_list_p)
10389 {
10390 if (!token->error_reported)
10391 {
10392 error_at (token->location, "lambda-expression in template-argument"
10393 " only available with -std=c++2a or -std=gnu++2a");
10394 token->error_reported = true;
10395 }
10396 ok = false;
10397 }
10398
10399 /* We may be in the middle of deferred access check. Disable
10400 it now. */
10401 push_deferring_access_checks (dk_no_deferred);
10402
10403 cp_parser_lambda_introducer (parser, lambda_expr);
10404 if (cp_parser_error_occurred (parser))
10405 return error_mark_node;
10406
10407 type = begin_lambda_type (lambda_expr);
10408 if (type == error_mark_node)
10409 return error_mark_node;
10410
10411 record_lambda_scope (lambda_expr);
10412
10413 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10414 determine_visibility (TYPE_NAME (type));
10415
10416 /* Now that we've started the type, add the capture fields for any
10417 explicit captures. */
10418 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10419
10420 {
10421 /* Inside the class, surrounding template-parameter-lists do not apply. */
10422 unsigned int saved_num_template_parameter_lists
10423 = parser->num_template_parameter_lists;
10424 unsigned char in_statement = parser->in_statement;
10425 bool in_switch_statement_p = parser->in_switch_statement_p;
10426 bool fully_implicit_function_template_p
10427 = parser->fully_implicit_function_template_p;
10428 tree implicit_template_parms = parser->implicit_template_parms;
10429 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10430 bool auto_is_implicit_function_template_parm_p
10431 = parser->auto_is_implicit_function_template_parm_p;
10432
10433 parser->num_template_parameter_lists = 0;
10434 parser->in_statement = 0;
10435 parser->in_switch_statement_p = false;
10436 parser->fully_implicit_function_template_p = false;
10437 parser->implicit_template_parms = 0;
10438 parser->implicit_template_scope = 0;
10439 parser->auto_is_implicit_function_template_parm_p = false;
10440
10441 /* By virtue of defining a local class, a lambda expression has access to
10442 the private variables of enclosing classes. */
10443
10444 if (cp_parser_start_tentative_firewall (parser))
10445 start = token;
10446
10447 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10448
10449 if (ok && cp_parser_error_occurred (parser))
10450 ok = false;
10451
10452 if (ok)
10453 {
10454 cp_parser_lambda_body (parser, lambda_expr);
10455 }
10456 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10457 {
10458 if (cp_parser_skip_to_closing_brace (parser))
10459 cp_lexer_consume_token (parser->lexer);
10460 }
10461
10462 /* The capture list was built up in reverse order; fix that now. */
10463 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10464 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10465
10466 if (ok)
10467 maybe_add_lambda_conv_op (type);
10468
10469 type = finish_struct (type, /*attributes=*/NULL_TREE);
10470
10471 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10472 parser->in_statement = in_statement;
10473 parser->in_switch_statement_p = in_switch_statement_p;
10474 parser->fully_implicit_function_template_p
10475 = fully_implicit_function_template_p;
10476 parser->implicit_template_parms = implicit_template_parms;
10477 parser->implicit_template_scope = implicit_template_scope;
10478 parser->auto_is_implicit_function_template_parm_p
10479 = auto_is_implicit_function_template_parm_p;
10480 }
10481
10482 /* This field is only used during parsing of the lambda. */
10483 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10484
10485 /* This lambda shouldn't have any proxies left at this point. */
10486 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10487 /* And now that we're done, push proxies for an enclosing lambda. */
10488 insert_pending_capture_proxies ();
10489
10490 /* Update the lambda expression to a range. */
10491 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10492 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10493 token->location,
10494 end_tok->location);
10495
10496 if (ok)
10497 lambda_expr = build_lambda_object (lambda_expr);
10498 else
10499 lambda_expr = error_mark_node;
10500
10501 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10502
10503 pop_deferring_access_checks ();
10504
10505 return lambda_expr;
10506 }
10507
10508 /* Parse the beginning of a lambda expression.
10509
10510 lambda-introducer:
10511 [ lambda-capture [opt] ]
10512
10513 LAMBDA_EXPR is the current representation of the lambda expression. */
10514
10515 static void
10516 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10517 {
10518 /* Need commas after the first capture. */
10519 bool first = true;
10520
10521 /* Eat the leading `['. */
10522 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10523
10524 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10525 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10526 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10527 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10528 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10529 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10530
10531 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10532 {
10533 cp_lexer_consume_token (parser->lexer);
10534 first = false;
10535
10536 if (!(at_function_scope_p () || parsing_nsdmi ()))
10537 error ("non-local lambda expression cannot have a capture-default");
10538 }
10539
10540 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10541 {
10542 cp_token* capture_token;
10543 tree capture_id;
10544 tree capture_init_expr;
10545 cp_id_kind idk = CP_ID_KIND_NONE;
10546 bool explicit_init_p = false;
10547
10548 enum capture_kind_type
10549 {
10550 BY_COPY,
10551 BY_REFERENCE
10552 };
10553 enum capture_kind_type capture_kind = BY_COPY;
10554
10555 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10556 {
10557 error ("expected end of capture-list");
10558 return;
10559 }
10560
10561 if (first)
10562 first = false;
10563 else
10564 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10565
10566 /* Possibly capture `this'. */
10567 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10568 {
10569 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10570 if (cxx_dialect < cxx2a
10571 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10572 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10573 "with by-copy capture default");
10574 cp_lexer_consume_token (parser->lexer);
10575 add_capture (lambda_expr,
10576 /*id=*/this_identifier,
10577 /*initializer=*/finish_this_expr (),
10578 /*by_reference_p=*/true,
10579 explicit_init_p);
10580 continue;
10581 }
10582
10583 /* Possibly capture `*this'. */
10584 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10585 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10586 {
10587 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10588 if (cxx_dialect < cxx17)
10589 pedwarn (loc, 0, "%<*this%> capture only available with "
10590 "-std=c++17 or -std=gnu++17");
10591 cp_lexer_consume_token (parser->lexer);
10592 cp_lexer_consume_token (parser->lexer);
10593 add_capture (lambda_expr,
10594 /*id=*/this_identifier,
10595 /*initializer=*/finish_this_expr (),
10596 /*by_reference_p=*/false,
10597 explicit_init_p);
10598 continue;
10599 }
10600
10601 bool init_pack_expansion = false;
10602 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10603 {
10604 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10605 if (cxx_dialect < cxx2a)
10606 pedwarn (loc, 0, "pack init-capture only available with "
10607 "-std=c++2a or -std=gnu++2a");
10608 cp_lexer_consume_token (parser->lexer);
10609 init_pack_expansion = true;
10610 }
10611
10612 /* Remember whether we want to capture as a reference or not. */
10613 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10614 {
10615 capture_kind = BY_REFERENCE;
10616 cp_lexer_consume_token (parser->lexer);
10617 }
10618
10619 /* Get the identifier. */
10620 capture_token = cp_lexer_peek_token (parser->lexer);
10621 capture_id = cp_parser_identifier (parser);
10622
10623 if (capture_id == error_mark_node)
10624 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10625 delimiters, but I modified this to stop on unnested ']' as well. It
10626 was already changed to stop on unnested '}', so the
10627 "closing_parenthesis" name is no more misleading with my change. */
10628 {
10629 cp_parser_skip_to_closing_parenthesis (parser,
10630 /*recovering=*/true,
10631 /*or_comma=*/true,
10632 /*consume_paren=*/true);
10633 break;
10634 }
10635
10636 /* Find the initializer for this capture. */
10637 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10638 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10639 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10640 {
10641 bool direct, non_constant;
10642 /* An explicit initializer exists. */
10643 if (cxx_dialect < cxx14)
10644 pedwarn (input_location, 0,
10645 "lambda capture initializers "
10646 "only available with -std=c++14 or -std=gnu++14");
10647 capture_init_expr = cp_parser_initializer (parser, &direct,
10648 &non_constant, true);
10649 explicit_init_p = true;
10650 if (capture_init_expr == NULL_TREE)
10651 {
10652 error ("empty initializer for lambda init-capture");
10653 capture_init_expr = error_mark_node;
10654 }
10655 if (init_pack_expansion)
10656 capture_init_expr = make_pack_expansion (capture_init_expr);
10657 }
10658 else
10659 {
10660 const char* error_msg;
10661
10662 /* Turn the identifier into an id-expression. */
10663 capture_init_expr
10664 = cp_parser_lookup_name_simple (parser, capture_id,
10665 capture_token->location);
10666
10667 if (capture_init_expr == error_mark_node)
10668 {
10669 unqualified_name_lookup_error (capture_id);
10670 continue;
10671 }
10672 else if (!VAR_P (capture_init_expr)
10673 && TREE_CODE (capture_init_expr) != PARM_DECL)
10674 {
10675 error_at (capture_token->location,
10676 "capture of non-variable %qE",
10677 capture_init_expr);
10678 if (DECL_P (capture_init_expr))
10679 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10680 "%q#D declared here", capture_init_expr);
10681 continue;
10682 }
10683 if (VAR_P (capture_init_expr)
10684 && decl_storage_duration (capture_init_expr) != dk_auto)
10685 {
10686 if (pedwarn (capture_token->location, 0, "capture of variable "
10687 "%qD with non-automatic storage duration",
10688 capture_init_expr))
10689 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10690 "%q#D declared here", capture_init_expr);
10691 continue;
10692 }
10693
10694 capture_init_expr
10695 = finish_id_expression
10696 (capture_id,
10697 capture_init_expr,
10698 parser->scope,
10699 &idk,
10700 /*integral_constant_expression_p=*/false,
10701 /*allow_non_integral_constant_expression_p=*/false,
10702 /*non_integral_constant_expression_p=*/NULL,
10703 /*template_p=*/false,
10704 /*done=*/true,
10705 /*address_p=*/false,
10706 /*template_arg_p=*/false,
10707 &error_msg,
10708 capture_token->location);
10709
10710 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10711 {
10712 cp_lexer_consume_token (parser->lexer);
10713 capture_init_expr = make_pack_expansion (capture_init_expr);
10714 }
10715 }
10716
10717 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10718 && !explicit_init_p)
10719 {
10720 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10721 && capture_kind == BY_COPY)
10722 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10723 "of %qD redundant with by-copy capture default",
10724 capture_id);
10725 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10726 && capture_kind == BY_REFERENCE)
10727 pedwarn (capture_token->location, 0, "explicit by-reference "
10728 "capture of %qD redundant with by-reference capture "
10729 "default", capture_id);
10730 }
10731
10732 add_capture (lambda_expr,
10733 capture_id,
10734 capture_init_expr,
10735 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10736 explicit_init_p);
10737
10738 /* If there is any qualification still in effect, clear it
10739 now; we will be starting fresh with the next capture. */
10740 parser->scope = NULL_TREE;
10741 parser->qualifying_scope = NULL_TREE;
10742 parser->object_scope = NULL_TREE;
10743 }
10744
10745 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10746 }
10747
10748 /* Parse the (optional) middle of a lambda expression.
10749
10750 lambda-declarator:
10751 < template-parameter-list [opt] >
10752 ( parameter-declaration-clause [opt] )
10753 attribute-specifier [opt]
10754 decl-specifier-seq [opt]
10755 exception-specification [opt]
10756 lambda-return-type-clause [opt]
10757
10758 LAMBDA_EXPR is the current representation of the lambda expression. */
10759
10760 static bool
10761 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10762 {
10763 /* 5.1.1.4 of the standard says:
10764 If a lambda-expression does not include a lambda-declarator, it is as if
10765 the lambda-declarator were ().
10766 This means an empty parameter list, no attributes, and no exception
10767 specification. */
10768 tree param_list = void_list_node;
10769 tree attributes = NULL_TREE;
10770 tree exception_spec = NULL_TREE;
10771 tree template_param_list = NULL_TREE;
10772 tree tx_qual = NULL_TREE;
10773 tree return_type = NULL_TREE;
10774 cp_decl_specifier_seq lambda_specs;
10775 clear_decl_specs (&lambda_specs);
10776
10777 /* The template-parameter-list is optional, but must begin with
10778 an opening angle if present. */
10779 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10780 {
10781 if (cxx_dialect < cxx14)
10782 pedwarn (parser->lexer->next_token->location, 0,
10783 "lambda templates are only available with "
10784 "-std=c++14 or -std=gnu++14");
10785 else if (cxx_dialect < cxx2a)
10786 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10787 "lambda templates are only available with "
10788 "-std=c++2a or -std=gnu++2a");
10789
10790 cp_lexer_consume_token (parser->lexer);
10791
10792 template_param_list = cp_parser_template_parameter_list (parser);
10793
10794 cp_parser_skip_to_end_of_template_parameter_list (parser);
10795
10796 /* We just processed one more parameter list. */
10797 ++parser->num_template_parameter_lists;
10798 }
10799
10800 /* The parameter-declaration-clause is optional (unless
10801 template-parameter-list was given), but must begin with an
10802 opening parenthesis if present. */
10803 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10804 {
10805 matching_parens parens;
10806 parens.consume_open (parser);
10807
10808 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10809
10810 /* Parse parameters. */
10811 param_list
10812 = cp_parser_parameter_declaration_clause
10813 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
10814
10815 /* Default arguments shall not be specified in the
10816 parameter-declaration-clause of a lambda-declarator. */
10817 if (cxx_dialect < cxx14)
10818 for (tree t = param_list; t; t = TREE_CHAIN (t))
10819 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10820 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10821 "default argument specified for lambda parameter");
10822
10823 parens.require_close (parser);
10824
10825 /* In the decl-specifier-seq of the lambda-declarator, each
10826 decl-specifier shall either be mutable or constexpr. */
10827 int declares_class_or_enum;
10828 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10829 cp_parser_decl_specifier_seq (parser,
10830 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10831 &lambda_specs, &declares_class_or_enum);
10832 if (lambda_specs.storage_class == sc_mutable)
10833 {
10834 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10835 if (lambda_specs.conflicting_specifiers_p)
10836 error_at (lambda_specs.locations[ds_storage_class],
10837 "duplicate %<mutable%>");
10838 }
10839
10840 tx_qual = cp_parser_tx_qualifier_opt (parser);
10841
10842 /* Parse optional exception specification. */
10843 exception_spec = cp_parser_exception_specification_opt (parser);
10844
10845 attributes = cp_parser_std_attribute_spec_seq (parser);
10846
10847 /* Parse optional trailing return type. */
10848 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10849 {
10850 cp_lexer_consume_token (parser->lexer);
10851 return_type = cp_parser_trailing_type_id (parser);
10852 }
10853
10854 /* The function parameters must be in scope all the way until after the
10855 trailing-return-type in case of decltype. */
10856 pop_bindings_and_leave_scope ();
10857 }
10858 else if (template_param_list != NULL_TREE) // generate diagnostic
10859 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10860
10861 /* Create the function call operator.
10862
10863 Messing with declarators like this is no uglier than building up the
10864 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10865 other code. */
10866 {
10867 cp_decl_specifier_seq return_type_specs;
10868 cp_declarator* declarator;
10869 tree fco;
10870 int quals;
10871 void *p;
10872
10873 clear_decl_specs (&return_type_specs);
10874 return_type_specs.type = make_auto ();
10875
10876 if (lambda_specs.locations[ds_constexpr])
10877 {
10878 if (cxx_dialect >= cxx17)
10879 return_type_specs.locations[ds_constexpr]
10880 = lambda_specs.locations[ds_constexpr];
10881 else
10882 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10883 "lambda only available with -std=c++17 or -std=gnu++17");
10884 }
10885
10886 p = obstack_alloc (&declarator_obstack, 0);
10887
10888 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
10889 LAMBDA_EXPR_LOCATION (lambda_expr));
10890
10891 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10892 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10893 declarator = make_call_declarator (declarator, param_list, quals,
10894 VIRT_SPEC_UNSPECIFIED,
10895 REF_QUAL_NONE,
10896 tx_qual,
10897 exception_spec,
10898 return_type,
10899 /*requires_clause*/NULL_TREE);
10900 declarator->std_attributes = attributes;
10901
10902 fco = grokmethod (&return_type_specs,
10903 declarator,
10904 NULL_TREE);
10905 if (fco != error_mark_node)
10906 {
10907 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10908 DECL_ARTIFICIAL (fco) = 1;
10909 /* Give the object parameter a different name. */
10910 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10911 DECL_LAMBDA_FUNCTION (fco) = 1;
10912 }
10913 if (template_param_list)
10914 {
10915 fco = finish_member_template_decl (fco);
10916 finish_template_decl (template_param_list);
10917 --parser->num_template_parameter_lists;
10918 }
10919 else if (parser->fully_implicit_function_template_p)
10920 fco = finish_fully_implicit_template (parser, fco);
10921
10922 finish_member_declaration (fco);
10923
10924 obstack_free (&declarator_obstack, p);
10925
10926 return (fco != error_mark_node);
10927 }
10928 }
10929
10930 /* Parse the body of a lambda expression, which is simply
10931
10932 compound-statement
10933
10934 but which requires special handling.
10935 LAMBDA_EXPR is the current representation of the lambda expression. */
10936
10937 static void
10938 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10939 {
10940 bool nested = (current_function_decl != NULL_TREE);
10941 unsigned char local_variables_forbidden_p
10942 = parser->local_variables_forbidden_p;
10943 bool in_function_body = parser->in_function_body;
10944
10945 /* The body of a lambda-expression is not a subexpression of the enclosing
10946 expression. */
10947 cp_evaluated ev;
10948
10949 if (nested)
10950 push_function_context ();
10951 else
10952 /* Still increment function_depth so that we don't GC in the
10953 middle of an expression. */
10954 ++function_depth;
10955
10956 vec<tree> omp_privatization_save;
10957 save_omp_privatization_clauses (omp_privatization_save);
10958 /* Clear this in case we're in the middle of a default argument. */
10959 parser->local_variables_forbidden_p = 0;
10960 parser->in_function_body = true;
10961
10962 {
10963 local_specialization_stack s (lss_copy);
10964 tree fco = lambda_function (lambda_expr);
10965 tree body = start_lambda_function (fco, lambda_expr);
10966 matching_braces braces;
10967
10968 if (braces.require_open (parser))
10969 {
10970 tree compound_stmt = begin_compound_stmt (0);
10971
10972 /* Originally C++11 required us to peek for 'return expr'; and
10973 process it specially here to deduce the return type. N3638
10974 removed the need for that. */
10975
10976 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10977 cp_parser_label_declaration (parser);
10978 cp_parser_statement_seq_opt (parser, NULL_TREE);
10979 braces.require_close (parser);
10980
10981 finish_compound_stmt (compound_stmt);
10982 }
10983
10984 finish_lambda_function (body);
10985 }
10986
10987 restore_omp_privatization_clauses (omp_privatization_save);
10988 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10989 parser->in_function_body = in_function_body;
10990 if (nested)
10991 pop_function_context();
10992 else
10993 --function_depth;
10994 }
10995
10996 /* Statements [gram.stmt.stmt] */
10997
10998 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10999
11000 static void
11001 add_debug_begin_stmt (location_t loc)
11002 {
11003 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11004 return;
11005 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11006 /* A concept is never expanded normally. */
11007 return;
11008
11009 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11010 SET_EXPR_LOCATION (stmt, loc);
11011 add_stmt (stmt);
11012 }
11013
11014 /* Parse a statement.
11015
11016 statement:
11017 labeled-statement
11018 expression-statement
11019 compound-statement
11020 selection-statement
11021 iteration-statement
11022 jump-statement
11023 declaration-statement
11024 try-block
11025
11026 C++11:
11027
11028 statement:
11029 labeled-statement
11030 attribute-specifier-seq (opt) expression-statement
11031 attribute-specifier-seq (opt) compound-statement
11032 attribute-specifier-seq (opt) selection-statement
11033 attribute-specifier-seq (opt) iteration-statement
11034 attribute-specifier-seq (opt) jump-statement
11035 declaration-statement
11036 attribute-specifier-seq (opt) try-block
11037
11038 init-statement:
11039 expression-statement
11040 simple-declaration
11041
11042 TM Extension:
11043
11044 statement:
11045 atomic-statement
11046
11047 IN_COMPOUND is true when the statement is nested inside a
11048 cp_parser_compound_statement; this matters for certain pragmas.
11049
11050 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11051 is a (possibly labeled) if statement which is not enclosed in braces
11052 and has an else clause. This is used to implement -Wparentheses.
11053
11054 CHAIN is a vector of if-else-if conditions. */
11055
11056 static void
11057 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
11058 bool in_compound, bool *if_p, vec<tree> *chain,
11059 location_t *loc_after_labels)
11060 {
11061 tree statement, std_attrs = NULL_TREE;
11062 cp_token *token;
11063 location_t statement_location, attrs_location;
11064
11065 restart:
11066 if (if_p != NULL)
11067 *if_p = false;
11068 /* There is no statement yet. */
11069 statement = NULL_TREE;
11070
11071 saved_token_sentinel saved_tokens (parser->lexer);
11072 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
11073 if (c_dialect_objc ())
11074 /* In obj-c++, seeing '[[' might be the either the beginning of
11075 c++11 attributes, or a nested objc-message-expression. So
11076 let's parse the c++11 attributes tentatively. */
11077 cp_parser_parse_tentatively (parser);
11078 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11079 if (c_dialect_objc ())
11080 {
11081 if (!cp_parser_parse_definitely (parser))
11082 std_attrs = NULL_TREE;
11083 }
11084
11085 /* Peek at the next token. */
11086 token = cp_lexer_peek_token (parser->lexer);
11087 /* Remember the location of the first token in the statement. */
11088 cp_token *statement_token = token;
11089 statement_location = token->location;
11090 add_debug_begin_stmt (statement_location);
11091 /* If this is a keyword, then that will often determine what kind of
11092 statement we have. */
11093 if (token->type == CPP_KEYWORD)
11094 {
11095 enum rid keyword = token->keyword;
11096
11097 switch (keyword)
11098 {
11099 case RID_CASE:
11100 case RID_DEFAULT:
11101 /* Looks like a labeled-statement with a case label.
11102 Parse the label, and then use tail recursion to parse
11103 the statement. */
11104 cp_parser_label_for_labeled_statement (parser, std_attrs);
11105 in_compound = false;
11106 goto restart;
11107
11108 case RID_IF:
11109 case RID_SWITCH:
11110 std_attrs = process_stmt_hotness_attribute (std_attrs);
11111 statement = cp_parser_selection_statement (parser, if_p, chain);
11112 break;
11113
11114 case RID_WHILE:
11115 case RID_DO:
11116 case RID_FOR:
11117 std_attrs = process_stmt_hotness_attribute (std_attrs);
11118 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11119 break;
11120
11121 case RID_BREAK:
11122 case RID_CONTINUE:
11123 case RID_RETURN:
11124 case RID_GOTO:
11125 std_attrs = process_stmt_hotness_attribute (std_attrs);
11126 statement = cp_parser_jump_statement (parser);
11127 break;
11128
11129 /* Objective-C++ exception-handling constructs. */
11130 case RID_AT_TRY:
11131 case RID_AT_CATCH:
11132 case RID_AT_FINALLY:
11133 case RID_AT_SYNCHRONIZED:
11134 case RID_AT_THROW:
11135 std_attrs = process_stmt_hotness_attribute (std_attrs);
11136 statement = cp_parser_objc_statement (parser);
11137 break;
11138
11139 case RID_TRY:
11140 std_attrs = process_stmt_hotness_attribute (std_attrs);
11141 statement = cp_parser_try_block (parser);
11142 break;
11143
11144 case RID_NAMESPACE:
11145 /* This must be a namespace alias definition. */
11146 if (std_attrs != NULL_TREE)
11147 {
11148 /* Attributes should be parsed as part of the the
11149 declaration, so let's un-parse them. */
11150 saved_tokens.rollback();
11151 std_attrs = NULL_TREE;
11152 }
11153 cp_parser_declaration_statement (parser);
11154 return;
11155
11156 case RID_TRANSACTION_ATOMIC:
11157 case RID_TRANSACTION_RELAXED:
11158 case RID_SYNCHRONIZED:
11159 case RID_ATOMIC_NOEXCEPT:
11160 case RID_ATOMIC_CANCEL:
11161 std_attrs = process_stmt_hotness_attribute (std_attrs);
11162 statement = cp_parser_transaction (parser, token);
11163 break;
11164 case RID_TRANSACTION_CANCEL:
11165 std_attrs = process_stmt_hotness_attribute (std_attrs);
11166 statement = cp_parser_transaction_cancel (parser);
11167 break;
11168
11169 default:
11170 /* It might be a keyword like `int' that can start a
11171 declaration-statement. */
11172 break;
11173 }
11174 }
11175 else if (token->type == CPP_NAME)
11176 {
11177 /* If the next token is a `:', then we are looking at a
11178 labeled-statement. */
11179 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11180 if (token->type == CPP_COLON)
11181 {
11182 /* Looks like a labeled-statement with an ordinary label.
11183 Parse the label, and then use tail recursion to parse
11184 the statement. */
11185
11186 cp_parser_label_for_labeled_statement (parser, std_attrs);
11187 in_compound = false;
11188 goto restart;
11189 }
11190 }
11191 /* Anything that starts with a `{' must be a compound-statement. */
11192 else if (token->type == CPP_OPEN_BRACE)
11193 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11194 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11195 a statement all its own. */
11196 else if (token->type == CPP_PRAGMA)
11197 {
11198 /* Only certain OpenMP pragmas are attached to statements, and thus
11199 are considered statements themselves. All others are not. In
11200 the context of a compound, accept the pragma as a "statement" and
11201 return so that we can check for a close brace. Otherwise we
11202 require a real statement and must go back and read one. */
11203 if (in_compound)
11204 cp_parser_pragma (parser, pragma_compound, if_p);
11205 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11206 goto restart;
11207 return;
11208 }
11209 else if (token->type == CPP_EOF)
11210 {
11211 cp_parser_error (parser, "expected statement");
11212 return;
11213 }
11214
11215 /* Everything else must be a declaration-statement or an
11216 expression-statement. Try for the declaration-statement
11217 first, unless we are looking at a `;', in which case we know that
11218 we have an expression-statement. */
11219 if (!statement)
11220 {
11221 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11222 {
11223 if (std_attrs != NULL_TREE)
11224 /* Attributes should be parsed as part of the declaration,
11225 so let's un-parse them. */
11226 saved_tokens.rollback();
11227
11228 cp_parser_parse_tentatively (parser);
11229 /* Try to parse the declaration-statement. */
11230 cp_parser_declaration_statement (parser);
11231 /* If that worked, we're done. */
11232 if (cp_parser_parse_definitely (parser))
11233 return;
11234 /* It didn't work, restore the post-attribute position. */
11235 if (std_attrs)
11236 cp_lexer_set_token_position (parser->lexer, statement_token);
11237 }
11238 /* All preceding labels have been parsed at this point. */
11239 if (loc_after_labels != NULL)
11240 *loc_after_labels = statement_location;
11241
11242 std_attrs = process_stmt_hotness_attribute (std_attrs);
11243
11244 /* Look for an expression-statement instead. */
11245 statement = cp_parser_expression_statement (parser, in_statement_expr);
11246
11247 /* Handle [[fallthrough]];. */
11248 if (attribute_fallthrough_p (std_attrs))
11249 {
11250 /* The next token after the fallthrough attribute is ';'. */
11251 if (statement == NULL_TREE)
11252 {
11253 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11254 statement = build_call_expr_internal_loc (statement_location,
11255 IFN_FALLTHROUGH,
11256 void_type_node, 0);
11257 finish_expr_stmt (statement);
11258 }
11259 else
11260 warning_at (statement_location, OPT_Wattributes,
11261 "%<fallthrough%> attribute not followed by %<;%>");
11262 std_attrs = NULL_TREE;
11263 }
11264 }
11265
11266 /* Set the line number for the statement. */
11267 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11268 SET_EXPR_LOCATION (statement, statement_location);
11269
11270 /* Allow "[[fallthrough]];", but warn otherwise. */
11271 if (std_attrs != NULL_TREE)
11272 warning_at (attrs_location,
11273 OPT_Wattributes,
11274 "attributes at the beginning of statement are ignored");
11275 }
11276
11277 /* Append ATTR to attribute list ATTRS. */
11278
11279 static tree
11280 attr_chainon (tree attrs, tree attr)
11281 {
11282 if (attrs == error_mark_node)
11283 return error_mark_node;
11284 if (attr == error_mark_node)
11285 return error_mark_node;
11286 return chainon (attrs, attr);
11287 }
11288
11289 /* Parse the label for a labeled-statement, i.e.
11290
11291 identifier :
11292 case constant-expression :
11293 default :
11294
11295 GNU Extension:
11296 case constant-expression ... constant-expression : statement
11297
11298 When a label is parsed without errors, the label is added to the
11299 parse tree by the finish_* functions, so this function doesn't
11300 have to return the label. */
11301
11302 static void
11303 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11304 {
11305 cp_token *token;
11306 tree label = NULL_TREE;
11307 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11308
11309 /* The next token should be an identifier. */
11310 token = cp_lexer_peek_token (parser->lexer);
11311 if (token->type != CPP_NAME
11312 && token->type != CPP_KEYWORD)
11313 {
11314 cp_parser_error (parser, "expected labeled-statement");
11315 return;
11316 }
11317
11318 /* Remember whether this case or a user-defined label is allowed to fall
11319 through to. */
11320 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11321
11322 parser->colon_corrects_to_scope_p = false;
11323 switch (token->keyword)
11324 {
11325 case RID_CASE:
11326 {
11327 tree expr, expr_hi;
11328 cp_token *ellipsis;
11329
11330 /* Consume the `case' token. */
11331 cp_lexer_consume_token (parser->lexer);
11332 /* Parse the constant-expression. */
11333 expr = cp_parser_constant_expression (parser);
11334 if (check_for_bare_parameter_packs (expr))
11335 expr = error_mark_node;
11336
11337 ellipsis = cp_lexer_peek_token (parser->lexer);
11338 if (ellipsis->type == CPP_ELLIPSIS)
11339 {
11340 /* Consume the `...' token. */
11341 cp_lexer_consume_token (parser->lexer);
11342 expr_hi = cp_parser_constant_expression (parser);
11343 if (check_for_bare_parameter_packs (expr_hi))
11344 expr_hi = error_mark_node;
11345
11346 /* We don't need to emit warnings here, as the common code
11347 will do this for us. */
11348 }
11349 else
11350 expr_hi = NULL_TREE;
11351
11352 if (parser->in_switch_statement_p)
11353 {
11354 tree l = finish_case_label (token->location, expr, expr_hi);
11355 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11356 {
11357 label = CASE_LABEL (l);
11358 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11359 }
11360 }
11361 else
11362 error_at (token->location,
11363 "case label %qE not within a switch statement",
11364 expr);
11365 }
11366 break;
11367
11368 case RID_DEFAULT:
11369 /* Consume the `default' token. */
11370 cp_lexer_consume_token (parser->lexer);
11371
11372 if (parser->in_switch_statement_p)
11373 {
11374 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11375 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11376 {
11377 label = CASE_LABEL (l);
11378 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11379 }
11380 }
11381 else
11382 error_at (token->location, "case label not within a switch statement");
11383 break;
11384
11385 default:
11386 /* Anything else must be an ordinary label. */
11387 label = finish_label_stmt (cp_parser_identifier (parser));
11388 if (label && TREE_CODE (label) == LABEL_DECL)
11389 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11390 break;
11391 }
11392
11393 /* Require the `:' token. */
11394 cp_parser_require (parser, CPP_COLON, RT_COLON);
11395
11396 /* An ordinary label may optionally be followed by attributes.
11397 However, this is only permitted if the attributes are then
11398 followed by a semicolon. This is because, for backward
11399 compatibility, when parsing
11400 lab: __attribute__ ((unused)) int i;
11401 we want the attribute to attach to "i", not "lab". */
11402 if (label != NULL_TREE
11403 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11404 {
11405 tree attrs;
11406 cp_parser_parse_tentatively (parser);
11407 attrs = cp_parser_gnu_attributes_opt (parser);
11408 if (attrs == NULL_TREE
11409 /* And fallthrough always binds to the expression-statement. */
11410 || attribute_fallthrough_p (attrs)
11411 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11412 cp_parser_abort_tentative_parse (parser);
11413 else if (!cp_parser_parse_definitely (parser))
11414 ;
11415 else
11416 attributes = attr_chainon (attributes, attrs);
11417 }
11418
11419 if (attributes != NULL_TREE)
11420 cplus_decl_attributes (&label, attributes, 0);
11421
11422 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11423 }
11424
11425 /* Parse an expression-statement.
11426
11427 expression-statement:
11428 expression [opt] ;
11429
11430 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11431 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11432 indicates whether this expression-statement is part of an
11433 expression statement. */
11434
11435 static tree
11436 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11437 {
11438 tree statement = NULL_TREE;
11439 cp_token *token = cp_lexer_peek_token (parser->lexer);
11440 location_t loc = token->location;
11441
11442 /* There might be attribute fallthrough. */
11443 tree attr = cp_parser_gnu_attributes_opt (parser);
11444
11445 /* If the next token is a ';', then there is no expression
11446 statement. */
11447 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11448 {
11449 statement = cp_parser_expression (parser);
11450 if (statement == error_mark_node
11451 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11452 {
11453 cp_parser_skip_to_end_of_block_or_statement (parser);
11454 return error_mark_node;
11455 }
11456 }
11457
11458 /* Handle [[fallthrough]];. */
11459 if (attribute_fallthrough_p (attr))
11460 {
11461 /* The next token after the fallthrough attribute is ';'. */
11462 if (statement == NULL_TREE)
11463 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11464 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11465 void_type_node, 0);
11466 else
11467 warning_at (loc, OPT_Wattributes,
11468 "%<fallthrough%> attribute not followed by %<;%>");
11469 attr = NULL_TREE;
11470 }
11471
11472 /* Allow "[[fallthrough]];", but warn otherwise. */
11473 if (attr != NULL_TREE)
11474 warning_at (loc, OPT_Wattributes,
11475 "attributes at the beginning of statement are ignored");
11476
11477 /* Give a helpful message for "A<T>::type t;" and the like. */
11478 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11479 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11480 {
11481 if (TREE_CODE (statement) == SCOPE_REF)
11482 error_at (token->location, "need %<typename%> before %qE because "
11483 "%qT is a dependent scope",
11484 statement, TREE_OPERAND (statement, 0));
11485 else if (is_overloaded_fn (statement)
11486 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11487 {
11488 /* A::A a; */
11489 tree fn = get_first_fn (statement);
11490 error_at (token->location,
11491 "%<%T::%D%> names the constructor, not the type",
11492 DECL_CONTEXT (fn), DECL_NAME (fn));
11493 }
11494 }
11495
11496 /* Consume the final `;'. */
11497 cp_parser_consume_semicolon_at_end_of_statement (parser);
11498
11499 if (in_statement_expr
11500 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11501 /* This is the final expression statement of a statement
11502 expression. */
11503 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11504 else if (statement)
11505 statement = finish_expr_stmt (statement);
11506
11507 return statement;
11508 }
11509
11510 /* Parse a compound-statement.
11511
11512 compound-statement:
11513 { statement-seq [opt] }
11514
11515 GNU extension:
11516
11517 compound-statement:
11518 { label-declaration-seq [opt] statement-seq [opt] }
11519
11520 label-declaration-seq:
11521 label-declaration
11522 label-declaration-seq label-declaration
11523
11524 Returns a tree representing the statement. */
11525
11526 static tree
11527 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11528 int bcs_flags, bool function_body)
11529 {
11530 tree compound_stmt;
11531 matching_braces braces;
11532
11533 /* Consume the `{'. */
11534 if (!braces.require_open (parser))
11535 return error_mark_node;
11536 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11537 && !function_body && cxx_dialect < cxx14)
11538 pedwarn (input_location, OPT_Wpedantic,
11539 "compound-statement in %<constexpr%> function");
11540 /* Begin the compound-statement. */
11541 compound_stmt = begin_compound_stmt (bcs_flags);
11542 /* If the next keyword is `__label__' we have a label declaration. */
11543 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11544 cp_parser_label_declaration (parser);
11545 /* Parse an (optional) statement-seq. */
11546 cp_parser_statement_seq_opt (parser, in_statement_expr);
11547 /* Finish the compound-statement. */
11548 finish_compound_stmt (compound_stmt);
11549 /* Consume the `}'. */
11550 braces.require_close (parser);
11551
11552 return compound_stmt;
11553 }
11554
11555 /* Parse an (optional) statement-seq.
11556
11557 statement-seq:
11558 statement
11559 statement-seq [opt] statement */
11560
11561 static void
11562 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11563 {
11564 /* Scan statements until there aren't any more. */
11565 while (true)
11566 {
11567 cp_token *token = cp_lexer_peek_token (parser->lexer);
11568
11569 /* If we are looking at a `}', then we have run out of
11570 statements; the same is true if we have reached the end
11571 of file, or have stumbled upon a stray '@end'. */
11572 if (token->type == CPP_CLOSE_BRACE
11573 || token->type == CPP_EOF
11574 || token->type == CPP_PRAGMA_EOL
11575 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11576 break;
11577
11578 /* If we are in a compound statement and find 'else' then
11579 something went wrong. */
11580 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11581 {
11582 if (parser->in_statement & IN_IF_STMT)
11583 break;
11584 else
11585 {
11586 token = cp_lexer_consume_token (parser->lexer);
11587 error_at (token->location, "%<else%> without a previous %<if%>");
11588 }
11589 }
11590
11591 /* Parse the statement. */
11592 cp_parser_statement (parser, in_statement_expr, true, NULL);
11593 }
11594 }
11595
11596 /* Return true if this is the C++20 version of range-based-for with
11597 init-statement. */
11598
11599 static bool
11600 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11601 {
11602 bool r = false;
11603
11604 /* Save tokens so that we can put them back. */
11605 cp_lexer_save_tokens (parser->lexer);
11606
11607 /* There has to be an unnested ; followed by an unnested :. */
11608 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11609 /*recovering=*/false,
11610 CPP_SEMICOLON,
11611 /*consume_paren=*/false) != -1)
11612 goto out;
11613
11614 /* We found the semicolon, eat it now. */
11615 cp_lexer_consume_token (parser->lexer);
11616
11617 /* Now look for ':' that is not nested in () or {}. */
11618 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11619 /*recovering=*/false,
11620 CPP_COLON,
11621 /*consume_paren=*/false) == -1);
11622
11623 out:
11624 /* Roll back the tokens we skipped. */
11625 cp_lexer_rollback_tokens (parser->lexer);
11626
11627 return r;
11628 }
11629
11630 /* Return true if we're looking at (init; cond), false otherwise. */
11631
11632 static bool
11633 cp_parser_init_statement_p (cp_parser *parser)
11634 {
11635 /* Save tokens so that we can put them back. */
11636 cp_lexer_save_tokens (parser->lexer);
11637
11638 /* Look for ';' that is not nested in () or {}. */
11639 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11640 /*recovering=*/false,
11641 CPP_SEMICOLON,
11642 /*consume_paren=*/false);
11643
11644 /* Roll back the tokens we skipped. */
11645 cp_lexer_rollback_tokens (parser->lexer);
11646
11647 return ret == -1;
11648 }
11649
11650 /* Parse a selection-statement.
11651
11652 selection-statement:
11653 if ( init-statement [opt] condition ) statement
11654 if ( init-statement [opt] condition ) statement else statement
11655 switch ( init-statement [opt] condition ) statement
11656
11657 Returns the new IF_STMT or SWITCH_STMT.
11658
11659 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11660 is a (possibly labeled) if statement which is not enclosed in
11661 braces and has an else clause. This is used to implement
11662 -Wparentheses.
11663
11664 CHAIN is a vector of if-else-if conditions. This is used to implement
11665 -Wduplicated-cond. */
11666
11667 static tree
11668 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11669 vec<tree> *chain)
11670 {
11671 cp_token *token;
11672 enum rid keyword;
11673 token_indent_info guard_tinfo;
11674
11675 if (if_p != NULL)
11676 *if_p = false;
11677
11678 /* Peek at the next token. */
11679 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11680 guard_tinfo = get_token_indent_info (token);
11681
11682 /* See what kind of keyword it is. */
11683 keyword = token->keyword;
11684 switch (keyword)
11685 {
11686 case RID_IF:
11687 case RID_SWITCH:
11688 {
11689 tree statement;
11690 tree condition;
11691
11692 bool cx = false;
11693 if (keyword == RID_IF
11694 && cp_lexer_next_token_is_keyword (parser->lexer,
11695 RID_CONSTEXPR))
11696 {
11697 cx = true;
11698 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11699 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11700 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11701 "with -std=c++17 or -std=gnu++17");
11702 }
11703
11704 /* Look for the `('. */
11705 matching_parens parens;
11706 if (!parens.require_open (parser))
11707 {
11708 cp_parser_skip_to_end_of_statement (parser);
11709 return error_mark_node;
11710 }
11711
11712 /* Begin the selection-statement. */
11713 if (keyword == RID_IF)
11714 {
11715 statement = begin_if_stmt ();
11716 IF_STMT_CONSTEXPR_P (statement) = cx;
11717 }
11718 else
11719 statement = begin_switch_stmt ();
11720
11721 /* Parse the optional init-statement. */
11722 if (cp_parser_init_statement_p (parser))
11723 {
11724 tree decl;
11725 if (cxx_dialect < cxx17)
11726 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11727 "init-statement in selection statements only available "
11728 "with -std=c++17 or -std=gnu++17");
11729 cp_parser_init_statement (parser, &decl);
11730 }
11731
11732 /* Parse the condition. */
11733 condition = cp_parser_condition (parser);
11734 /* Look for the `)'. */
11735 if (!parens.require_close (parser))
11736 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11737 /*consume_paren=*/true);
11738
11739 if (keyword == RID_IF)
11740 {
11741 bool nested_if;
11742 unsigned char in_statement;
11743
11744 /* Add the condition. */
11745 condition = finish_if_stmt_cond (condition, statement);
11746
11747 if (warn_duplicated_cond)
11748 warn_duplicated_cond_add_or_warn (token->location, condition,
11749 &chain);
11750
11751 /* Parse the then-clause. */
11752 in_statement = parser->in_statement;
11753 parser->in_statement |= IN_IF_STMT;
11754
11755 /* Outside a template, the non-selected branch of a constexpr
11756 if is a 'discarded statement', i.e. unevaluated. */
11757 bool was_discarded = in_discarded_stmt;
11758 bool discard_then = (cx && !processing_template_decl
11759 && integer_zerop (condition));
11760 if (discard_then)
11761 {
11762 in_discarded_stmt = true;
11763 ++c_inhibit_evaluation_warnings;
11764 }
11765
11766 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11767 guard_tinfo);
11768
11769 parser->in_statement = in_statement;
11770
11771 finish_then_clause (statement);
11772
11773 if (discard_then)
11774 {
11775 THEN_CLAUSE (statement) = NULL_TREE;
11776 in_discarded_stmt = was_discarded;
11777 --c_inhibit_evaluation_warnings;
11778 }
11779
11780 /* If the next token is `else', parse the else-clause. */
11781 if (cp_lexer_next_token_is_keyword (parser->lexer,
11782 RID_ELSE))
11783 {
11784 bool discard_else = (cx && !processing_template_decl
11785 && integer_nonzerop (condition));
11786 if (discard_else)
11787 {
11788 in_discarded_stmt = true;
11789 ++c_inhibit_evaluation_warnings;
11790 }
11791
11792 guard_tinfo
11793 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11794 /* Consume the `else' keyword. */
11795 cp_lexer_consume_token (parser->lexer);
11796 if (warn_duplicated_cond)
11797 {
11798 if (cp_lexer_next_token_is_keyword (parser->lexer,
11799 RID_IF)
11800 && chain == NULL)
11801 {
11802 /* We've got "if (COND) else if (COND2)". Start
11803 the condition chain and add COND as the first
11804 element. */
11805 chain = new vec<tree> ();
11806 if (!CONSTANT_CLASS_P (condition)
11807 && !TREE_SIDE_EFFECTS (condition))
11808 {
11809 /* Wrap it in a NOP_EXPR so that we can set the
11810 location of the condition. */
11811 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11812 condition);
11813 SET_EXPR_LOCATION (e, token->location);
11814 chain->safe_push (e);
11815 }
11816 }
11817 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11818 RID_IF))
11819 {
11820 /* This is if-else without subsequent if. Zap the
11821 condition chain; we would have already warned at
11822 this point. */
11823 delete chain;
11824 chain = NULL;
11825 }
11826 }
11827 begin_else_clause (statement);
11828 /* Parse the else-clause. */
11829 cp_parser_implicitly_scoped_statement (parser, NULL,
11830 guard_tinfo, chain);
11831
11832 finish_else_clause (statement);
11833
11834 /* If we are currently parsing a then-clause, then
11835 IF_P will not be NULL. We set it to true to
11836 indicate that this if statement has an else clause.
11837 This may trigger the Wparentheses warning below
11838 when we get back up to the parent if statement. */
11839 if (if_p != NULL)
11840 *if_p = true;
11841
11842 if (discard_else)
11843 {
11844 ELSE_CLAUSE (statement) = NULL_TREE;
11845 in_discarded_stmt = was_discarded;
11846 --c_inhibit_evaluation_warnings;
11847 }
11848 }
11849 else
11850 {
11851 /* This if statement does not have an else clause. If
11852 NESTED_IF is true, then the then-clause has an if
11853 statement which does have an else clause. We warn
11854 about the potential ambiguity. */
11855 if (nested_if)
11856 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11857 "suggest explicit braces to avoid ambiguous"
11858 " %<else%>");
11859 if (warn_duplicated_cond)
11860 {
11861 /* We don't need the condition chain anymore. */
11862 delete chain;
11863 chain = NULL;
11864 }
11865 }
11866
11867 /* Now we're all done with the if-statement. */
11868 finish_if_stmt (statement);
11869 }
11870 else
11871 {
11872 bool in_switch_statement_p;
11873 unsigned char in_statement;
11874
11875 /* Add the condition. */
11876 finish_switch_cond (condition, statement);
11877
11878 /* Parse the body of the switch-statement. */
11879 in_switch_statement_p = parser->in_switch_statement_p;
11880 in_statement = parser->in_statement;
11881 parser->in_switch_statement_p = true;
11882 parser->in_statement |= IN_SWITCH_STMT;
11883 cp_parser_implicitly_scoped_statement (parser, if_p,
11884 guard_tinfo);
11885 parser->in_switch_statement_p = in_switch_statement_p;
11886 parser->in_statement = in_statement;
11887
11888 /* Now we're all done with the switch-statement. */
11889 finish_switch_stmt (statement);
11890 }
11891
11892 return statement;
11893 }
11894 break;
11895
11896 default:
11897 cp_parser_error (parser, "expected selection-statement");
11898 return error_mark_node;
11899 }
11900 }
11901
11902 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11903 If we have seen at least one decl-specifier, and the next token
11904 is not a parenthesis, then we must be looking at a declaration.
11905 (After "int (" we might be looking at a functional cast.) */
11906
11907 static void
11908 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11909 bool any_specifiers_p)
11910 {
11911 if (any_specifiers_p
11912 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11913 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11914 && !cp_parser_error_occurred (parser))
11915 cp_parser_commit_to_tentative_parse (parser);
11916 }
11917
11918 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11919 The declarator shall not specify a function or an array. Returns
11920 TRUE if the declarator is valid, FALSE otherwise. */
11921
11922 static bool
11923 cp_parser_check_condition_declarator (cp_parser* parser,
11924 cp_declarator *declarator,
11925 location_t loc)
11926 {
11927 if (declarator == cp_error_declarator
11928 || function_declarator_p (declarator)
11929 || declarator->kind == cdk_array)
11930 {
11931 if (declarator == cp_error_declarator)
11932 /* Already complained. */;
11933 else if (declarator->kind == cdk_array)
11934 error_at (loc, "condition declares an array");
11935 else
11936 error_at (loc, "condition declares a function");
11937 if (parser->fully_implicit_function_template_p)
11938 abort_fully_implicit_template (parser);
11939 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11940 /*or_comma=*/false,
11941 /*consume_paren=*/false);
11942 return false;
11943 }
11944 else
11945 return true;
11946 }
11947
11948 /* Parse a condition.
11949
11950 condition:
11951 expression
11952 type-specifier-seq declarator = initializer-clause
11953 type-specifier-seq declarator braced-init-list
11954
11955 GNU Extension:
11956
11957 condition:
11958 type-specifier-seq declarator asm-specification [opt]
11959 attributes [opt] = assignment-expression
11960
11961 Returns the expression that should be tested. */
11962
11963 static tree
11964 cp_parser_condition (cp_parser* parser)
11965 {
11966 cp_decl_specifier_seq type_specifiers;
11967 const char *saved_message;
11968 int declares_class_or_enum;
11969
11970 /* Try the declaration first. */
11971 cp_parser_parse_tentatively (parser);
11972 /* New types are not allowed in the type-specifier-seq for a
11973 condition. */
11974 saved_message = parser->type_definition_forbidden_message;
11975 parser->type_definition_forbidden_message
11976 = G_("types may not be defined in conditions");
11977 /* Parse the type-specifier-seq. */
11978 cp_parser_decl_specifier_seq (parser,
11979 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11980 &type_specifiers,
11981 &declares_class_or_enum);
11982 /* Restore the saved message. */
11983 parser->type_definition_forbidden_message = saved_message;
11984
11985 cp_parser_maybe_commit_to_declaration (parser,
11986 type_specifiers.any_specifiers_p);
11987
11988 /* If all is well, we might be looking at a declaration. */
11989 if (!cp_parser_error_occurred (parser))
11990 {
11991 tree decl;
11992 tree asm_specification;
11993 tree attributes;
11994 cp_declarator *declarator;
11995 tree initializer = NULL_TREE;
11996 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11997
11998 /* Parse the declarator. */
11999 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12000 CP_PARSER_FLAGS_NONE,
12001 /*ctor_dtor_or_conv_p=*/NULL,
12002 /*parenthesized_p=*/NULL,
12003 /*member_p=*/false,
12004 /*friend_p=*/false,
12005 /*static_p=*/false);
12006 /* Parse the attributes. */
12007 attributes = cp_parser_attributes_opt (parser);
12008 /* Parse the asm-specification. */
12009 asm_specification = cp_parser_asm_specification_opt (parser);
12010 /* If the next token is not an `=' or '{', then we might still be
12011 looking at an expression. For example:
12012
12013 if (A(a).x)
12014
12015 looks like a decl-specifier-seq and a declarator -- but then
12016 there is no `=', so this is an expression. */
12017 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12018 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12019 cp_parser_simulate_error (parser);
12020
12021 /* If we did see an `=' or '{', then we are looking at a declaration
12022 for sure. */
12023 if (cp_parser_parse_definitely (parser))
12024 {
12025 tree pushed_scope;
12026 bool non_constant_p = false;
12027 int flags = LOOKUP_ONLYCONVERTING;
12028
12029 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
12030 return error_mark_node;
12031
12032 /* Create the declaration. */
12033 decl = start_decl (declarator, &type_specifiers,
12034 /*initialized_p=*/true,
12035 attributes, /*prefix_attributes=*/NULL_TREE,
12036 &pushed_scope);
12037
12038 /* Parse the initializer. */
12039 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12040 {
12041 initializer = cp_parser_braced_list (parser, &non_constant_p);
12042 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
12043 flags = 0;
12044 }
12045 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12046 {
12047 /* Consume the `='. */
12048 cp_lexer_consume_token (parser->lexer);
12049 initializer = cp_parser_initializer_clause (parser,
12050 &non_constant_p);
12051 }
12052 else
12053 {
12054 cp_parser_error (parser, "expected initializer");
12055 initializer = error_mark_node;
12056 }
12057 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
12058 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12059
12060 /* Process the initializer. */
12061 cp_finish_decl (decl,
12062 initializer, !non_constant_p,
12063 asm_specification,
12064 flags);
12065
12066 if (pushed_scope)
12067 pop_scope (pushed_scope);
12068
12069 return convert_from_reference (decl);
12070 }
12071 }
12072 /* If we didn't even get past the declarator successfully, we are
12073 definitely not looking at a declaration. */
12074 else
12075 cp_parser_abort_tentative_parse (parser);
12076
12077 /* Otherwise, we are looking at an expression. */
12078 return cp_parser_expression (parser);
12079 }
12080
12081 /* Parses a for-statement or range-for-statement until the closing ')',
12082 not included. */
12083
12084 static tree
12085 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12086 {
12087 tree init, scope, decl;
12088 bool is_range_for;
12089
12090 /* Begin the for-statement. */
12091 scope = begin_for_scope (&init);
12092
12093 /* Parse the initialization. */
12094 is_range_for = cp_parser_init_statement (parser, &decl);
12095
12096 if (is_range_for)
12097 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12098 false);
12099 else
12100 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12101 }
12102
12103 static tree
12104 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12105 unsigned short unroll)
12106 {
12107 /* Normal for loop */
12108 tree condition = NULL_TREE;
12109 tree expression = NULL_TREE;
12110 tree stmt;
12111
12112 stmt = begin_for_stmt (scope, init);
12113 /* The init-statement has already been parsed in
12114 cp_parser_init_statement, so no work is needed here. */
12115 finish_init_stmt (stmt);
12116
12117 /* If there's a condition, process it. */
12118 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12119 condition = cp_parser_condition (parser);
12120 else if (ivdep)
12121 {
12122 cp_parser_error (parser, "missing loop condition in loop with "
12123 "%<GCC ivdep%> pragma");
12124 condition = error_mark_node;
12125 }
12126 else if (unroll)
12127 {
12128 cp_parser_error (parser, "missing loop condition in loop with "
12129 "%<GCC unroll%> pragma");
12130 condition = error_mark_node;
12131 }
12132 finish_for_cond (condition, stmt, ivdep, unroll);
12133 /* Look for the `;'. */
12134 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12135
12136 /* If there's an expression, process it. */
12137 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12138 expression = cp_parser_expression (parser);
12139 finish_for_expr (expression, stmt);
12140
12141 return stmt;
12142 }
12143
12144 /* Tries to parse a range-based for-statement:
12145
12146 range-based-for:
12147 decl-specifier-seq declarator : expression
12148
12149 The decl-specifier-seq declarator and the `:' are already parsed by
12150 cp_parser_init_statement. If processing_template_decl it returns a
12151 newly created RANGE_FOR_STMT; if not, it is converted to a
12152 regular FOR_STMT. */
12153
12154 static tree
12155 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12156 bool ivdep, unsigned short unroll, bool is_omp)
12157 {
12158 tree stmt, range_expr;
12159 auto_vec <cxx_binding *, 16> bindings;
12160 auto_vec <tree, 16> names;
12161 tree decomp_first_name = NULL_TREE;
12162 unsigned int decomp_cnt = 0;
12163
12164 /* Get the range declaration momentarily out of the way so that
12165 the range expression doesn't clash with it. */
12166 if (range_decl != error_mark_node)
12167 {
12168 if (DECL_HAS_VALUE_EXPR_P (range_decl))
12169 {
12170 tree v = DECL_VALUE_EXPR (range_decl);
12171 /* For decomposition declaration get all of the corresponding
12172 declarations out of the way. */
12173 if (TREE_CODE (v) == ARRAY_REF
12174 && VAR_P (TREE_OPERAND (v, 0))
12175 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12176 {
12177 tree d = range_decl;
12178 range_decl = TREE_OPERAND (v, 0);
12179 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12180 decomp_first_name = d;
12181 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12182 {
12183 tree name = DECL_NAME (d);
12184 names.safe_push (name);
12185 bindings.safe_push (IDENTIFIER_BINDING (name));
12186 IDENTIFIER_BINDING (name)
12187 = IDENTIFIER_BINDING (name)->previous;
12188 }
12189 }
12190 }
12191 if (names.is_empty ())
12192 {
12193 tree name = DECL_NAME (range_decl);
12194 names.safe_push (name);
12195 bindings.safe_push (IDENTIFIER_BINDING (name));
12196 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12197 }
12198 }
12199
12200 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12201 {
12202 bool expr_non_constant_p;
12203 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12204 }
12205 else
12206 range_expr = cp_parser_expression (parser);
12207
12208 /* Put the range declaration(s) back into scope. */
12209 for (unsigned int i = 0; i < names.length (); i++)
12210 {
12211 cxx_binding *binding = bindings[i];
12212 binding->previous = IDENTIFIER_BINDING (names[i]);
12213 IDENTIFIER_BINDING (names[i]) = binding;
12214 }
12215
12216 /* finish_omp_for has its own code for the following, so just
12217 return the range_expr instead. */
12218 if (is_omp)
12219 return range_expr;
12220
12221 /* If in template, STMT is converted to a normal for-statement
12222 at instantiation. If not, it is done just ahead. */
12223 if (processing_template_decl)
12224 {
12225 if (check_for_bare_parameter_packs (range_expr))
12226 range_expr = error_mark_node;
12227 stmt = begin_range_for_stmt (scope, init);
12228 if (ivdep)
12229 RANGE_FOR_IVDEP (stmt) = 1;
12230 if (unroll)
12231 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12232 finish_range_for_decl (stmt, range_decl, range_expr);
12233 if (!type_dependent_expression_p (range_expr)
12234 /* do_auto_deduction doesn't mess with template init-lists. */
12235 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12236 do_range_for_auto_deduction (range_decl, range_expr);
12237 }
12238 else
12239 {
12240 stmt = begin_for_stmt (scope, init);
12241 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12242 decomp_first_name, decomp_cnt, ivdep,
12243 unroll);
12244 }
12245 return stmt;
12246 }
12247
12248 /* Subroutine of cp_convert_range_for: given the initializer expression,
12249 builds up the range temporary. */
12250
12251 static tree
12252 build_range_temp (tree range_expr)
12253 {
12254 tree range_type, range_temp;
12255
12256 /* Find out the type deduced by the declaration
12257 `auto &&__range = range_expr'. */
12258 range_type = cp_build_reference_type (make_auto (), true);
12259 range_type = do_auto_deduction (range_type, range_expr,
12260 type_uses_auto (range_type));
12261
12262 /* Create the __range variable. */
12263 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12264 range_type);
12265 TREE_USED (range_temp) = 1;
12266 DECL_ARTIFICIAL (range_temp) = 1;
12267
12268 return range_temp;
12269 }
12270
12271 /* Used by cp_parser_range_for in template context: we aren't going to
12272 do a full conversion yet, but we still need to resolve auto in the
12273 type of the for-range-declaration if present. This is basically
12274 a shortcut version of cp_convert_range_for. */
12275
12276 static void
12277 do_range_for_auto_deduction (tree decl, tree range_expr)
12278 {
12279 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12280 if (auto_node)
12281 {
12282 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12283 range_temp = convert_from_reference (build_range_temp (range_expr));
12284 iter_type = (cp_parser_perform_range_for_lookup
12285 (range_temp, &begin_dummy, &end_dummy));
12286 if (iter_type)
12287 {
12288 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12289 iter_type);
12290 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12291 RO_UNARY_STAR,
12292 tf_warning_or_error);
12293 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12294 iter_decl, auto_node);
12295 }
12296 }
12297 }
12298
12299 /* Converts a range-based for-statement into a normal
12300 for-statement, as per the definition.
12301
12302 for (RANGE_DECL : RANGE_EXPR)
12303 BLOCK
12304
12305 should be equivalent to:
12306
12307 {
12308 auto &&__range = RANGE_EXPR;
12309 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12310 __begin != __end;
12311 ++__begin)
12312 {
12313 RANGE_DECL = *__begin;
12314 BLOCK
12315 }
12316 }
12317
12318 If RANGE_EXPR is an array:
12319 BEGIN_EXPR = __range
12320 END_EXPR = __range + ARRAY_SIZE(__range)
12321 Else if RANGE_EXPR has a member 'begin' or 'end':
12322 BEGIN_EXPR = __range.begin()
12323 END_EXPR = __range.end()
12324 Else:
12325 BEGIN_EXPR = begin(__range)
12326 END_EXPR = end(__range);
12327
12328 If __range has a member 'begin' but not 'end', or vice versa, we must
12329 still use the second alternative (it will surely fail, however).
12330 When calling begin()/end() in the third alternative we must use
12331 argument dependent lookup, but always considering 'std' as an associated
12332 namespace. */
12333
12334 tree
12335 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12336 tree decomp_first_name, unsigned int decomp_cnt,
12337 bool ivdep, unsigned short unroll)
12338 {
12339 tree begin, end;
12340 tree iter_type, begin_expr, end_expr;
12341 tree condition, expression;
12342
12343 range_expr = mark_lvalue_use (range_expr);
12344
12345 if (range_decl == error_mark_node || range_expr == error_mark_node)
12346 /* If an error happened previously do nothing or else a lot of
12347 unhelpful errors would be issued. */
12348 begin_expr = end_expr = iter_type = error_mark_node;
12349 else
12350 {
12351 tree range_temp;
12352
12353 if (VAR_P (range_expr)
12354 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12355 /* Can't bind a reference to an array of runtime bound. */
12356 range_temp = range_expr;
12357 else
12358 {
12359 range_temp = build_range_temp (range_expr);
12360 pushdecl (range_temp);
12361 cp_finish_decl (range_temp, range_expr,
12362 /*is_constant_init*/false, NULL_TREE,
12363 LOOKUP_ONLYCONVERTING);
12364 range_temp = convert_from_reference (range_temp);
12365 }
12366 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12367 &begin_expr, &end_expr);
12368 }
12369
12370 /* The new for initialization statement. */
12371 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12372 iter_type);
12373 TREE_USED (begin) = 1;
12374 DECL_ARTIFICIAL (begin) = 1;
12375 pushdecl (begin);
12376 cp_finish_decl (begin, begin_expr,
12377 /*is_constant_init*/false, NULL_TREE,
12378 LOOKUP_ONLYCONVERTING);
12379
12380 if (cxx_dialect >= cxx17)
12381 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12382 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12383 TREE_USED (end) = 1;
12384 DECL_ARTIFICIAL (end) = 1;
12385 pushdecl (end);
12386 cp_finish_decl (end, end_expr,
12387 /*is_constant_init*/false, NULL_TREE,
12388 LOOKUP_ONLYCONVERTING);
12389
12390 finish_init_stmt (statement);
12391
12392 /* The new for condition. */
12393 condition = build_x_binary_op (input_location, NE_EXPR,
12394 begin, ERROR_MARK,
12395 end, ERROR_MARK,
12396 NULL, tf_warning_or_error);
12397 finish_for_cond (condition, statement, ivdep, unroll);
12398
12399 /* The new increment expression. */
12400 expression = finish_unary_op_expr (input_location,
12401 PREINCREMENT_EXPR, begin,
12402 tf_warning_or_error);
12403 finish_for_expr (expression, statement);
12404
12405 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12406 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12407
12408 /* The declaration is initialized with *__begin inside the loop body. */
12409 cp_finish_decl (range_decl,
12410 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12411 tf_warning_or_error),
12412 /*is_constant_init*/false, NULL_TREE,
12413 LOOKUP_ONLYCONVERTING);
12414 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12415 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12416
12417 return statement;
12418 }
12419
12420 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12421 We need to solve both at the same time because the method used
12422 depends on the existence of members begin or end.
12423 Returns the type deduced for the iterator expression. */
12424
12425 static tree
12426 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12427 {
12428 if (error_operand_p (range))
12429 {
12430 *begin = *end = error_mark_node;
12431 return error_mark_node;
12432 }
12433
12434 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12435 {
12436 error ("range-based %<for%> expression of type %qT "
12437 "has incomplete type", TREE_TYPE (range));
12438 *begin = *end = error_mark_node;
12439 return error_mark_node;
12440 }
12441 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12442 {
12443 /* If RANGE is an array, we will use pointer arithmetic. */
12444 *begin = decay_conversion (range, tf_warning_or_error);
12445 *end = build_binary_op (input_location, PLUS_EXPR,
12446 range,
12447 array_type_nelts_top (TREE_TYPE (range)),
12448 false);
12449 return TREE_TYPE (*begin);
12450 }
12451 else
12452 {
12453 /* If it is not an array, we must do a bit of magic. */
12454 tree id_begin, id_end;
12455 tree member_begin, member_end;
12456
12457 *begin = *end = error_mark_node;
12458
12459 id_begin = get_identifier ("begin");
12460 id_end = get_identifier ("end");
12461 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12462 /*protect=*/2, /*want_type=*/false,
12463 tf_warning_or_error);
12464 member_end = lookup_member (TREE_TYPE (range), id_end,
12465 /*protect=*/2, /*want_type=*/false,
12466 tf_warning_or_error);
12467
12468 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12469 {
12470 /* Use the member functions. */
12471 *begin = cp_parser_range_for_member_function (range, id_begin);
12472 *end = cp_parser_range_for_member_function (range, id_end);
12473 }
12474 else
12475 {
12476 /* Use global functions with ADL. */
12477 vec<tree, va_gc> *vec;
12478 vec = make_tree_vector ();
12479
12480 vec_safe_push (vec, range);
12481
12482 member_begin = perform_koenig_lookup (id_begin, vec,
12483 tf_warning_or_error);
12484 *begin = finish_call_expr (member_begin, &vec, false, true,
12485 tf_warning_or_error);
12486 member_end = perform_koenig_lookup (id_end, vec,
12487 tf_warning_or_error);
12488 *end = finish_call_expr (member_end, &vec, false, true,
12489 tf_warning_or_error);
12490
12491 release_tree_vector (vec);
12492 }
12493
12494 /* Last common checks. */
12495 if (*begin == error_mark_node || *end == error_mark_node)
12496 {
12497 /* If one of the expressions is an error do no more checks. */
12498 *begin = *end = error_mark_node;
12499 return error_mark_node;
12500 }
12501 else if (type_dependent_expression_p (*begin)
12502 || type_dependent_expression_p (*end))
12503 /* Can happen, when, eg, in a template context, Koenig lookup
12504 can't resolve begin/end (c++/58503). */
12505 return NULL_TREE;
12506 else
12507 {
12508 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12509 /* The unqualified type of the __begin and __end temporaries should
12510 be the same, as required by the multiple auto declaration. */
12511 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12512 {
12513 if (cxx_dialect >= cxx17
12514 && (build_x_binary_op (input_location, NE_EXPR,
12515 *begin, ERROR_MARK,
12516 *end, ERROR_MARK,
12517 NULL, tf_none)
12518 != error_mark_node))
12519 /* P0184R0 allows __begin and __end to have different types,
12520 but make sure they are comparable so we can give a better
12521 diagnostic. */;
12522 else
12523 error ("inconsistent begin/end types in range-based %<for%> "
12524 "statement: %qT and %qT",
12525 TREE_TYPE (*begin), TREE_TYPE (*end));
12526 }
12527 return iter_type;
12528 }
12529 }
12530 }
12531
12532 /* Helper function for cp_parser_perform_range_for_lookup.
12533 Builds a tree for RANGE.IDENTIFIER(). */
12534
12535 static tree
12536 cp_parser_range_for_member_function (tree range, tree identifier)
12537 {
12538 tree member, res;
12539 vec<tree, va_gc> *vec;
12540
12541 member = finish_class_member_access_expr (range, identifier,
12542 false, tf_warning_or_error);
12543 if (member == error_mark_node)
12544 return error_mark_node;
12545
12546 vec = make_tree_vector ();
12547 res = finish_call_expr (member, &vec,
12548 /*disallow_virtual=*/false,
12549 /*koenig_p=*/false,
12550 tf_warning_or_error);
12551 release_tree_vector (vec);
12552 return res;
12553 }
12554
12555 /* Parse an iteration-statement.
12556
12557 iteration-statement:
12558 while ( condition ) statement
12559 do statement while ( expression ) ;
12560 for ( init-statement condition [opt] ; expression [opt] )
12561 statement
12562
12563 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12564
12565 static tree
12566 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12567 unsigned short unroll)
12568 {
12569 cp_token *token;
12570 enum rid keyword;
12571 tree statement;
12572 unsigned char in_statement;
12573 token_indent_info guard_tinfo;
12574
12575 /* Peek at the next token. */
12576 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12577 if (!token)
12578 return error_mark_node;
12579
12580 guard_tinfo = get_token_indent_info (token);
12581
12582 /* Remember whether or not we are already within an iteration
12583 statement. */
12584 in_statement = parser->in_statement;
12585
12586 /* See what kind of keyword it is. */
12587 keyword = token->keyword;
12588 switch (keyword)
12589 {
12590 case RID_WHILE:
12591 {
12592 tree condition;
12593
12594 /* Begin the while-statement. */
12595 statement = begin_while_stmt ();
12596 /* Look for the `('. */
12597 matching_parens parens;
12598 parens.require_open (parser);
12599 /* Parse the condition. */
12600 condition = cp_parser_condition (parser);
12601 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12602 /* Look for the `)'. */
12603 parens.require_close (parser);
12604 /* Parse the dependent statement. */
12605 parser->in_statement = IN_ITERATION_STMT;
12606 bool prev = note_iteration_stmt_body_start ();
12607 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12608 note_iteration_stmt_body_end (prev);
12609 parser->in_statement = in_statement;
12610 /* We're done with the while-statement. */
12611 finish_while_stmt (statement);
12612 }
12613 break;
12614
12615 case RID_DO:
12616 {
12617 tree expression;
12618
12619 /* Begin the do-statement. */
12620 statement = begin_do_stmt ();
12621 /* Parse the body of the do-statement. */
12622 parser->in_statement = IN_ITERATION_STMT;
12623 bool prev = note_iteration_stmt_body_start ();
12624 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12625 note_iteration_stmt_body_end (prev);
12626 parser->in_statement = in_statement;
12627 finish_do_body (statement);
12628 /* Look for the `while' keyword. */
12629 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12630 /* Look for the `('. */
12631 matching_parens parens;
12632 parens.require_open (parser);
12633 /* Parse the expression. */
12634 expression = cp_parser_expression (parser);
12635 /* We're done with the do-statement. */
12636 finish_do_stmt (expression, statement, ivdep, unroll);
12637 /* Look for the `)'. */
12638 parens.require_close (parser);
12639 /* Look for the `;'. */
12640 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12641 }
12642 break;
12643
12644 case RID_FOR:
12645 {
12646 /* Look for the `('. */
12647 matching_parens parens;
12648 parens.require_open (parser);
12649
12650 statement = cp_parser_for (parser, ivdep, unroll);
12651
12652 /* Look for the `)'. */
12653 parens.require_close (parser);
12654
12655 /* Parse the body of the for-statement. */
12656 parser->in_statement = IN_ITERATION_STMT;
12657 bool prev = note_iteration_stmt_body_start ();
12658 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12659 note_iteration_stmt_body_end (prev);
12660 parser->in_statement = in_statement;
12661
12662 /* We're done with the for-statement. */
12663 finish_for_stmt (statement);
12664 }
12665 break;
12666
12667 default:
12668 cp_parser_error (parser, "expected iteration-statement");
12669 statement = error_mark_node;
12670 break;
12671 }
12672
12673 return statement;
12674 }
12675
12676 /* Parse a init-statement or the declarator of a range-based-for.
12677 Returns true if a range-based-for declaration is seen.
12678
12679 init-statement:
12680 expression-statement
12681 simple-declaration */
12682
12683 static bool
12684 cp_parser_init_statement (cp_parser *parser, tree *decl)
12685 {
12686 /* If the next token is a `;', then we have an empty
12687 expression-statement. Grammatically, this is also a
12688 simple-declaration, but an invalid one, because it does not
12689 declare anything. Therefore, if we did not handle this case
12690 specially, we would issue an error message about an invalid
12691 declaration. */
12692 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12693 {
12694 bool is_range_for = false;
12695 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12696
12697 /* Try to parse the init-statement. */
12698 if (cp_parser_range_based_for_with_init_p (parser))
12699 {
12700 tree dummy;
12701 cp_parser_parse_tentatively (parser);
12702 /* Parse the declaration. */
12703 cp_parser_simple_declaration (parser,
12704 /*function_definition_allowed_p=*/false,
12705 &dummy);
12706 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12707 if (!cp_parser_parse_definitely (parser))
12708 /* That didn't work, try to parse it as an expression-statement. */
12709 cp_parser_expression_statement (parser, NULL_TREE);
12710
12711 if (cxx_dialect < cxx2a)
12712 {
12713 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12714 "range-based %<for%> loops with initializer only "
12715 "available with -std=c++2a or -std=gnu++2a");
12716 *decl = error_mark_node;
12717 }
12718 }
12719
12720 /* A colon is used in range-based for. */
12721 parser->colon_corrects_to_scope_p = false;
12722
12723 /* We're going to speculatively look for a declaration, falling back
12724 to an expression, if necessary. */
12725 cp_parser_parse_tentatively (parser);
12726 /* Parse the declaration. */
12727 cp_parser_simple_declaration (parser,
12728 /*function_definition_allowed_p=*/false,
12729 decl);
12730 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12731 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12732 {
12733 /* It is a range-for, consume the ':'. */
12734 cp_lexer_consume_token (parser->lexer);
12735 is_range_for = true;
12736 if (cxx_dialect < cxx11)
12737 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12738 "range-based %<for%> loops only available with "
12739 "-std=c++11 or -std=gnu++11");
12740 }
12741 else
12742 /* The ';' is not consumed yet because we told
12743 cp_parser_simple_declaration not to. */
12744 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12745
12746 if (cp_parser_parse_definitely (parser))
12747 return is_range_for;
12748 /* If the tentative parse failed, then we shall need to look for an
12749 expression-statement. */
12750 }
12751 /* If we are here, it is an expression-statement. */
12752 cp_parser_expression_statement (parser, NULL_TREE);
12753 return false;
12754 }
12755
12756 /* Parse a jump-statement.
12757
12758 jump-statement:
12759 break ;
12760 continue ;
12761 return expression [opt] ;
12762 return braced-init-list ;
12763 goto identifier ;
12764
12765 GNU extension:
12766
12767 jump-statement:
12768 goto * expression ;
12769
12770 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12771
12772 static tree
12773 cp_parser_jump_statement (cp_parser* parser)
12774 {
12775 tree statement = error_mark_node;
12776 cp_token *token;
12777 enum rid keyword;
12778 unsigned char in_statement;
12779
12780 /* Peek at the next token. */
12781 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12782 if (!token)
12783 return error_mark_node;
12784
12785 /* See what kind of keyword it is. */
12786 keyword = token->keyword;
12787 switch (keyword)
12788 {
12789 case RID_BREAK:
12790 in_statement = parser->in_statement & ~IN_IF_STMT;
12791 switch (in_statement)
12792 {
12793 case 0:
12794 error_at (token->location, "break statement not within loop or switch");
12795 break;
12796 default:
12797 gcc_assert ((in_statement & IN_SWITCH_STMT)
12798 || in_statement == IN_ITERATION_STMT);
12799 statement = finish_break_stmt ();
12800 if (in_statement == IN_ITERATION_STMT)
12801 break_maybe_infinite_loop ();
12802 break;
12803 case IN_OMP_BLOCK:
12804 error_at (token->location, "invalid exit from OpenMP structured block");
12805 break;
12806 case IN_OMP_FOR:
12807 error_at (token->location, "break statement used with OpenMP for loop");
12808 break;
12809 }
12810 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12811 break;
12812
12813 case RID_CONTINUE:
12814 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12815 {
12816 case 0:
12817 error_at (token->location, "continue statement not within a loop");
12818 break;
12819 /* Fall through. */
12820 case IN_ITERATION_STMT:
12821 case IN_OMP_FOR:
12822 statement = finish_continue_stmt ();
12823 break;
12824 case IN_OMP_BLOCK:
12825 error_at (token->location, "invalid exit from OpenMP structured block");
12826 break;
12827 default:
12828 gcc_unreachable ();
12829 }
12830 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12831 break;
12832
12833 case RID_RETURN:
12834 {
12835 tree expr;
12836 bool expr_non_constant_p;
12837
12838 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12839 {
12840 cp_lexer_set_source_position (parser->lexer);
12841 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12842 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12843 }
12844 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12845 expr = cp_parser_expression (parser);
12846 else
12847 /* If the next token is a `;', then there is no
12848 expression. */
12849 expr = NULL_TREE;
12850 /* Build the return-statement. */
12851 if (current_function_auto_return_pattern && in_discarded_stmt)
12852 /* Don't deduce from a discarded return statement. */;
12853 else
12854 statement = finish_return_stmt (expr);
12855 /* Look for the final `;'. */
12856 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12857 }
12858 break;
12859
12860 case RID_GOTO:
12861 if (parser->in_function_body
12862 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12863 {
12864 error ("%<goto%> in %<constexpr%> function");
12865 cp_function_chain->invalid_constexpr = true;
12866 }
12867
12868 /* Create the goto-statement. */
12869 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12870 {
12871 /* Issue a warning about this use of a GNU extension. */
12872 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12873 /* Consume the '*' token. */
12874 cp_lexer_consume_token (parser->lexer);
12875 /* Parse the dependent expression. */
12876 finish_goto_stmt (cp_parser_expression (parser));
12877 }
12878 else
12879 finish_goto_stmt (cp_parser_identifier (parser));
12880 /* Look for the final `;'. */
12881 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12882 break;
12883
12884 default:
12885 cp_parser_error (parser, "expected jump-statement");
12886 break;
12887 }
12888
12889 return statement;
12890 }
12891
12892 /* Parse a declaration-statement.
12893
12894 declaration-statement:
12895 block-declaration */
12896
12897 static void
12898 cp_parser_declaration_statement (cp_parser* parser)
12899 {
12900 void *p;
12901
12902 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12903 p = obstack_alloc (&declarator_obstack, 0);
12904
12905 /* Parse the block-declaration. */
12906 cp_parser_block_declaration (parser, /*statement_p=*/true);
12907
12908 /* Free any declarators allocated. */
12909 obstack_free (&declarator_obstack, p);
12910 }
12911
12912 /* Some dependent statements (like `if (cond) statement'), are
12913 implicitly in their own scope. In other words, if the statement is
12914 a single statement (as opposed to a compound-statement), it is
12915 none-the-less treated as if it were enclosed in braces. Any
12916 declarations appearing in the dependent statement are out of scope
12917 after control passes that point. This function parses a statement,
12918 but ensures that is in its own scope, even if it is not a
12919 compound-statement.
12920
12921 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12922 is a (possibly labeled) if statement which is not enclosed in
12923 braces and has an else clause. This is used to implement
12924 -Wparentheses.
12925
12926 CHAIN is a vector of if-else-if conditions. This is used to implement
12927 -Wduplicated-cond.
12928
12929 Returns the new statement. */
12930
12931 static tree
12932 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12933 const token_indent_info &guard_tinfo,
12934 vec<tree> *chain)
12935 {
12936 tree statement;
12937 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12938 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12939 token_indent_info body_tinfo
12940 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12941
12942 if (if_p != NULL)
12943 *if_p = false;
12944
12945 /* Mark if () ; with a special NOP_EXPR. */
12946 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12947 {
12948 cp_lexer_consume_token (parser->lexer);
12949 statement = add_stmt (build_empty_stmt (body_loc));
12950
12951 if (guard_tinfo.keyword == RID_IF
12952 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12953 warning_at (body_loc, OPT_Wempty_body,
12954 "suggest braces around empty body in an %<if%> statement");
12955 else if (guard_tinfo.keyword == RID_ELSE)
12956 warning_at (body_loc, OPT_Wempty_body,
12957 "suggest braces around empty body in an %<else%> statement");
12958 }
12959 /* if a compound is opened, we simply parse the statement directly. */
12960 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12961 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12962 /* If the token is not a `{', then we must take special action. */
12963 else
12964 {
12965 /* Create a compound-statement. */
12966 statement = begin_compound_stmt (0);
12967 /* Parse the dependent-statement. */
12968 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12969 &body_loc_after_labels);
12970 /* Finish the dummy compound-statement. */
12971 finish_compound_stmt (statement);
12972 }
12973
12974 token_indent_info next_tinfo
12975 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12976 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12977
12978 if (body_loc_after_labels != UNKNOWN_LOCATION
12979 && next_tinfo.type != CPP_SEMICOLON)
12980 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12981 guard_tinfo.location, guard_tinfo.keyword);
12982
12983 /* Return the statement. */
12984 return statement;
12985 }
12986
12987 /* For some dependent statements (like `while (cond) statement'), we
12988 have already created a scope. Therefore, even if the dependent
12989 statement is a compound-statement, we do not want to create another
12990 scope. */
12991
12992 static void
12993 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12994 const token_indent_info &guard_tinfo)
12995 {
12996 /* If the token is a `{', then we must take special action. */
12997 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12998 {
12999 token_indent_info body_tinfo
13000 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13001 location_t loc_after_labels = UNKNOWN_LOCATION;
13002
13003 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
13004 &loc_after_labels);
13005 token_indent_info next_tinfo
13006 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13007 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13008
13009 if (loc_after_labels != UNKNOWN_LOCATION
13010 && next_tinfo.type != CPP_SEMICOLON)
13011 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
13012 guard_tinfo.location,
13013 guard_tinfo.keyword);
13014 }
13015 else
13016 {
13017 /* Avoid calling cp_parser_compound_statement, so that we
13018 don't create a new scope. Do everything else by hand. */
13019 matching_braces braces;
13020 braces.require_open (parser);
13021 /* If the next keyword is `__label__' we have a label declaration. */
13022 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13023 cp_parser_label_declaration (parser);
13024 /* Parse an (optional) statement-seq. */
13025 cp_parser_statement_seq_opt (parser, NULL_TREE);
13026 braces.require_close (parser);
13027 }
13028 }
13029
13030 /* Declarations [gram.dcl.dcl] */
13031
13032 /* Parse an optional declaration-sequence.
13033
13034 declaration-seq:
13035 declaration
13036 declaration-seq declaration */
13037
13038 static void
13039 cp_parser_declaration_seq_opt (cp_parser* parser)
13040 {
13041 while (true)
13042 {
13043 cp_token *token = cp_lexer_peek_token (parser->lexer);
13044
13045 if (token->type == CPP_CLOSE_BRACE
13046 || token->type == CPP_EOF)
13047 break;
13048 else
13049 cp_parser_toplevel_declaration (parser);
13050 }
13051 }
13052
13053 /* Parse a declaration.
13054
13055 declaration:
13056 block-declaration
13057 function-definition
13058 template-declaration
13059 explicit-instantiation
13060 explicit-specialization
13061 linkage-specification
13062 namespace-definition
13063
13064 C++17:
13065 deduction-guide
13066
13067 GNU extension:
13068
13069 declaration:
13070 __extension__ declaration */
13071
13072 static void
13073 cp_parser_declaration (cp_parser* parser)
13074 {
13075 cp_token token1;
13076 cp_token token2;
13077 int saved_pedantic;
13078 void *p;
13079 tree attributes = NULL_TREE;
13080
13081 /* Check for the `__extension__' keyword. */
13082 if (cp_parser_extension_opt (parser, &saved_pedantic))
13083 {
13084 /* Parse the qualified declaration. */
13085 cp_parser_declaration (parser);
13086 /* Restore the PEDANTIC flag. */
13087 pedantic = saved_pedantic;
13088
13089 return;
13090 }
13091
13092 /* Try to figure out what kind of declaration is present. */
13093 token1 = *cp_lexer_peek_token (parser->lexer);
13094
13095 if (token1.type != CPP_EOF)
13096 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
13097 else
13098 {
13099 token2.type = CPP_EOF;
13100 token2.keyword = RID_MAX;
13101 }
13102
13103 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13104 p = obstack_alloc (&declarator_obstack, 0);
13105
13106 /* If the next token is `extern' and the following token is a string
13107 literal, then we have a linkage specification. */
13108 if (token1.keyword == RID_EXTERN
13109 && cp_parser_is_pure_string_literal (&token2))
13110 cp_parser_linkage_specification (parser);
13111 /* If the next token is `template', then we have either a template
13112 declaration, an explicit instantiation, or an explicit
13113 specialization. */
13114 else if (token1.keyword == RID_TEMPLATE)
13115 {
13116 /* `template <>' indicates a template specialization. */
13117 if (token2.type == CPP_LESS
13118 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13119 cp_parser_explicit_specialization (parser);
13120 /* `template <' indicates a template declaration. */
13121 else if (token2.type == CPP_LESS)
13122 cp_parser_template_declaration (parser, /*member_p=*/false);
13123 /* Anything else must be an explicit instantiation. */
13124 else
13125 cp_parser_explicit_instantiation (parser);
13126 }
13127 /* If the next token is `export', then we have a template
13128 declaration. */
13129 else if (token1.keyword == RID_EXPORT)
13130 cp_parser_template_declaration (parser, /*member_p=*/false);
13131 /* If the next token is `extern', 'static' or 'inline' and the one
13132 after that is `template', we have a GNU extended explicit
13133 instantiation directive. */
13134 else if (cp_parser_allow_gnu_extensions_p (parser)
13135 && (token1.keyword == RID_EXTERN
13136 || token1.keyword == RID_STATIC
13137 || token1.keyword == RID_INLINE)
13138 && token2.keyword == RID_TEMPLATE)
13139 cp_parser_explicit_instantiation (parser);
13140 /* If the next token is `namespace', check for a named or unnamed
13141 namespace definition. */
13142 else if (token1.keyword == RID_NAMESPACE
13143 && (/* A named namespace definition. */
13144 (token2.type == CPP_NAME
13145 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
13146 != CPP_EQ))
13147 || (token2.type == CPP_OPEN_SQUARE
13148 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
13149 == CPP_OPEN_SQUARE)
13150 /* An unnamed namespace definition. */
13151 || token2.type == CPP_OPEN_BRACE
13152 || token2.keyword == RID_ATTRIBUTE))
13153 cp_parser_namespace_definition (parser);
13154 /* An inline (associated) namespace definition. */
13155 else if (token1.keyword == RID_INLINE
13156 && token2.keyword == RID_NAMESPACE)
13157 cp_parser_namespace_definition (parser);
13158 /* Objective-C++ declaration/definition. */
13159 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
13160 cp_parser_objc_declaration (parser, NULL_TREE);
13161 else if (c_dialect_objc ()
13162 && token1.keyword == RID_ATTRIBUTE
13163 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
13164 cp_parser_objc_declaration (parser, attributes);
13165 /* At this point we may have a template declared by a concept
13166 introduction. */
13167 else if (flag_concepts
13168 && cp_parser_template_declaration_after_export (parser,
13169 /*member_p=*/false))
13170 /* We did. */;
13171 else
13172 /* Try to parse a block-declaration, or a function-definition. */
13173 cp_parser_block_declaration (parser, /*statement_p=*/false);
13174
13175 /* Free any declarators allocated. */
13176 obstack_free (&declarator_obstack, p);
13177 }
13178
13179 /* Parse a namespace-scope declaration. */
13180
13181 static void
13182 cp_parser_toplevel_declaration (cp_parser* parser)
13183 {
13184 cp_token *token = cp_lexer_peek_token (parser->lexer);
13185
13186 if (token->type == CPP_PRAGMA)
13187 /* A top-level declaration can consist solely of a #pragma. A
13188 nested declaration cannot, so this is done here and not in
13189 cp_parser_declaration. (A #pragma at block scope is
13190 handled in cp_parser_statement.) */
13191 cp_parser_pragma (parser, pragma_external, NULL);
13192 else if (token->type == CPP_SEMICOLON)
13193 {
13194 /* A declaration consisting of a single semicolon is
13195 invalid. Allow it unless we're being pedantic. */
13196 cp_lexer_consume_token (parser->lexer);
13197 if (!in_system_header_at (input_location))
13198 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
13199 }
13200 else
13201 /* Parse the declaration itself. */
13202 cp_parser_declaration (parser);
13203 }
13204
13205 /* Parse a block-declaration.
13206
13207 block-declaration:
13208 simple-declaration
13209 asm-definition
13210 namespace-alias-definition
13211 using-declaration
13212 using-directive
13213
13214 GNU Extension:
13215
13216 block-declaration:
13217 __extension__ block-declaration
13218
13219 C++0x Extension:
13220
13221 block-declaration:
13222 static_assert-declaration
13223
13224 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13225 part of a declaration-statement. */
13226
13227 static void
13228 cp_parser_block_declaration (cp_parser *parser,
13229 bool statement_p)
13230 {
13231 cp_token *token1;
13232 int saved_pedantic;
13233
13234 /* Check for the `__extension__' keyword. */
13235 if (cp_parser_extension_opt (parser, &saved_pedantic))
13236 {
13237 /* Parse the qualified declaration. */
13238 cp_parser_block_declaration (parser, statement_p);
13239 /* Restore the PEDANTIC flag. */
13240 pedantic = saved_pedantic;
13241
13242 return;
13243 }
13244
13245 /* Peek at the next token to figure out which kind of declaration is
13246 present. */
13247 token1 = cp_lexer_peek_token (parser->lexer);
13248
13249 /* If the next keyword is `asm', we have an asm-definition. */
13250 if (token1->keyword == RID_ASM)
13251 {
13252 if (statement_p)
13253 cp_parser_commit_to_tentative_parse (parser);
13254 cp_parser_asm_definition (parser);
13255 }
13256 /* If the next keyword is `namespace', we have a
13257 namespace-alias-definition. */
13258 else if (token1->keyword == RID_NAMESPACE)
13259 cp_parser_namespace_alias_definition (parser);
13260 /* If the next keyword is `using', we have a
13261 using-declaration, a using-directive, or an alias-declaration. */
13262 else if (token1->keyword == RID_USING)
13263 {
13264 cp_token *token2;
13265
13266 if (statement_p)
13267 cp_parser_commit_to_tentative_parse (parser);
13268 /* If the token after `using' is `namespace', then we have a
13269 using-directive. */
13270 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13271 if (token2->keyword == RID_NAMESPACE)
13272 cp_parser_using_directive (parser);
13273 /* If the second token after 'using' is '=', then we have an
13274 alias-declaration. */
13275 else if (cxx_dialect >= cxx11
13276 && token2->type == CPP_NAME
13277 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13278 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13279 cp_parser_alias_declaration (parser);
13280 /* Otherwise, it's a using-declaration. */
13281 else
13282 cp_parser_using_declaration (parser,
13283 /*access_declaration_p=*/false);
13284 }
13285 /* If the next keyword is `__label__' we have a misplaced label
13286 declaration. */
13287 else if (token1->keyword == RID_LABEL)
13288 {
13289 cp_lexer_consume_token (parser->lexer);
13290 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13291 cp_parser_skip_to_end_of_statement (parser);
13292 /* If the next token is now a `;', consume it. */
13293 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13294 cp_lexer_consume_token (parser->lexer);
13295 }
13296 /* If the next token is `static_assert' we have a static assertion. */
13297 else if (token1->keyword == RID_STATIC_ASSERT)
13298 cp_parser_static_assert (parser, /*member_p=*/false);
13299 /* Anything else must be a simple-declaration. */
13300 else
13301 cp_parser_simple_declaration (parser, !statement_p,
13302 /*maybe_range_for_decl*/NULL);
13303 }
13304
13305 /* Parse a simple-declaration.
13306
13307 simple-declaration:
13308 decl-specifier-seq [opt] init-declarator-list [opt] ;
13309 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13310 brace-or-equal-initializer ;
13311
13312 init-declarator-list:
13313 init-declarator
13314 init-declarator-list , init-declarator
13315
13316 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13317 function-definition as a simple-declaration.
13318
13319 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13320 parsed declaration if it is an uninitialized single declarator not followed
13321 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13322 if present, will not be consumed. */
13323
13324 static void
13325 cp_parser_simple_declaration (cp_parser* parser,
13326 bool function_definition_allowed_p,
13327 tree *maybe_range_for_decl)
13328 {
13329 cp_decl_specifier_seq decl_specifiers;
13330 int declares_class_or_enum;
13331 bool saw_declarator;
13332 location_t comma_loc = UNKNOWN_LOCATION;
13333 location_t init_loc = UNKNOWN_LOCATION;
13334
13335 if (maybe_range_for_decl)
13336 *maybe_range_for_decl = NULL_TREE;
13337
13338 /* Defer access checks until we know what is being declared; the
13339 checks for names appearing in the decl-specifier-seq should be
13340 done as if we were in the scope of the thing being declared. */
13341 push_deferring_access_checks (dk_deferred);
13342
13343 /* Parse the decl-specifier-seq. We have to keep track of whether
13344 or not the decl-specifier-seq declares a named class or
13345 enumeration type, since that is the only case in which the
13346 init-declarator-list is allowed to be empty.
13347
13348 [dcl.dcl]
13349
13350 In a simple-declaration, the optional init-declarator-list can be
13351 omitted only when declaring a class or enumeration, that is when
13352 the decl-specifier-seq contains either a class-specifier, an
13353 elaborated-type-specifier, or an enum-specifier. */
13354 cp_parser_decl_specifier_seq (parser,
13355 CP_PARSER_FLAGS_OPTIONAL,
13356 &decl_specifiers,
13357 &declares_class_or_enum);
13358 /* We no longer need to defer access checks. */
13359 stop_deferring_access_checks ();
13360
13361 /* In a block scope, a valid declaration must always have a
13362 decl-specifier-seq. By not trying to parse declarators, we can
13363 resolve the declaration/expression ambiguity more quickly. */
13364 if (!function_definition_allowed_p
13365 && !decl_specifiers.any_specifiers_p)
13366 {
13367 cp_parser_error (parser, "expected declaration");
13368 goto done;
13369 }
13370
13371 /* If the next two tokens are both identifiers, the code is
13372 erroneous. The usual cause of this situation is code like:
13373
13374 T t;
13375
13376 where "T" should name a type -- but does not. */
13377 if (!decl_specifiers.any_type_specifiers_p
13378 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13379 {
13380 /* If parsing tentatively, we should commit; we really are
13381 looking at a declaration. */
13382 cp_parser_commit_to_tentative_parse (parser);
13383 /* Give up. */
13384 goto done;
13385 }
13386
13387 cp_parser_maybe_commit_to_declaration (parser,
13388 decl_specifiers.any_specifiers_p);
13389
13390 /* Look for C++17 decomposition declaration. */
13391 for (size_t n = 1; ; n++)
13392 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13393 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13394 continue;
13395 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13396 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13397 && decl_specifiers.any_specifiers_p)
13398 {
13399 tree decl
13400 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13401 maybe_range_for_decl,
13402 &init_loc);
13403
13404 /* The next token should be either a `,' or a `;'. */
13405 cp_token *token = cp_lexer_peek_token (parser->lexer);
13406 /* If it's a `;', we are done. */
13407 if (token->type == CPP_SEMICOLON)
13408 goto finish;
13409 else if (maybe_range_for_decl)
13410 {
13411 if (*maybe_range_for_decl == NULL_TREE)
13412 *maybe_range_for_decl = error_mark_node;
13413 goto finish;
13414 }
13415 /* Anything else is an error. */
13416 else
13417 {
13418 /* If we have already issued an error message we don't need
13419 to issue another one. */
13420 if ((decl != error_mark_node
13421 && DECL_INITIAL (decl) != error_mark_node)
13422 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13423 cp_parser_error (parser, "expected %<,%> or %<;%>");
13424 /* Skip tokens until we reach the end of the statement. */
13425 cp_parser_skip_to_end_of_statement (parser);
13426 /* If the next token is now a `;', consume it. */
13427 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13428 cp_lexer_consume_token (parser->lexer);
13429 goto done;
13430 }
13431 }
13432 else
13433 break;
13434
13435 tree last_type;
13436 bool auto_specifier_p;
13437 /* NULL_TREE if both variable and function declaration are allowed,
13438 error_mark_node if function declaration are not allowed and
13439 a FUNCTION_DECL that should be diagnosed if it is followed by
13440 variable declarations. */
13441 tree auto_function_declaration;
13442
13443 last_type = NULL_TREE;
13444 auto_specifier_p
13445 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13446 auto_function_declaration = NULL_TREE;
13447
13448 /* Keep going until we hit the `;' at the end of the simple
13449 declaration. */
13450 saw_declarator = false;
13451 while (cp_lexer_next_token_is_not (parser->lexer,
13452 CPP_SEMICOLON))
13453 {
13454 cp_token *token;
13455 bool function_definition_p;
13456 tree decl;
13457 tree auto_result = NULL_TREE;
13458
13459 if (saw_declarator)
13460 {
13461 /* If we are processing next declarator, comma is expected */
13462 token = cp_lexer_peek_token (parser->lexer);
13463 gcc_assert (token->type == CPP_COMMA);
13464 cp_lexer_consume_token (parser->lexer);
13465 if (maybe_range_for_decl)
13466 {
13467 *maybe_range_for_decl = error_mark_node;
13468 if (comma_loc == UNKNOWN_LOCATION)
13469 comma_loc = token->location;
13470 }
13471 }
13472 else
13473 saw_declarator = true;
13474
13475 /* Parse the init-declarator. */
13476 decl = cp_parser_init_declarator (parser,
13477 CP_PARSER_FLAGS_NONE,
13478 &decl_specifiers,
13479 /*checks=*/NULL,
13480 function_definition_allowed_p,
13481 /*member_p=*/false,
13482 declares_class_or_enum,
13483 &function_definition_p,
13484 maybe_range_for_decl,
13485 &init_loc,
13486 &auto_result);
13487 /* If an error occurred while parsing tentatively, exit quickly.
13488 (That usually happens when in the body of a function; each
13489 statement is treated as a declaration-statement until proven
13490 otherwise.) */
13491 if (cp_parser_error_occurred (parser))
13492 goto done;
13493
13494 if (auto_specifier_p && cxx_dialect >= cxx14)
13495 {
13496 /* If the init-declarator-list contains more than one
13497 init-declarator, they shall all form declarations of
13498 variables. */
13499 if (auto_function_declaration == NULL_TREE)
13500 auto_function_declaration
13501 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13502 else if (TREE_CODE (decl) == FUNCTION_DECL
13503 || auto_function_declaration != error_mark_node)
13504 {
13505 error_at (decl_specifiers.locations[ds_type_spec],
13506 "non-variable %qD in declaration with more than one "
13507 "declarator with placeholder type",
13508 TREE_CODE (decl) == FUNCTION_DECL
13509 ? decl : auto_function_declaration);
13510 auto_function_declaration = error_mark_node;
13511 }
13512 }
13513
13514 if (auto_result
13515 && (!processing_template_decl || !type_uses_auto (auto_result)))
13516 {
13517 if (last_type
13518 && last_type != error_mark_node
13519 && !same_type_p (auto_result, last_type))
13520 {
13521 /* If the list of declarators contains more than one declarator,
13522 the type of each declared variable is determined as described
13523 above. If the type deduced for the template parameter U is not
13524 the same in each deduction, the program is ill-formed. */
13525 error_at (decl_specifiers.locations[ds_type_spec],
13526 "inconsistent deduction for %qT: %qT and then %qT",
13527 decl_specifiers.type, last_type, auto_result);
13528 last_type = error_mark_node;
13529 }
13530 else
13531 last_type = auto_result;
13532 }
13533
13534 /* Handle function definitions specially. */
13535 if (function_definition_p)
13536 {
13537 /* If the next token is a `,', then we are probably
13538 processing something like:
13539
13540 void f() {}, *p;
13541
13542 which is erroneous. */
13543 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13544 {
13545 cp_token *token = cp_lexer_peek_token (parser->lexer);
13546 error_at (token->location,
13547 "mixing"
13548 " declarations and function-definitions is forbidden");
13549 }
13550 /* Otherwise, we're done with the list of declarators. */
13551 else
13552 {
13553 pop_deferring_access_checks ();
13554 return;
13555 }
13556 }
13557 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13558 *maybe_range_for_decl = decl;
13559 /* The next token should be either a `,' or a `;'. */
13560 token = cp_lexer_peek_token (parser->lexer);
13561 /* If it's a `,', there are more declarators to come. */
13562 if (token->type == CPP_COMMA)
13563 /* will be consumed next time around */;
13564 /* If it's a `;', we are done. */
13565 else if (token->type == CPP_SEMICOLON)
13566 break;
13567 else if (maybe_range_for_decl)
13568 {
13569 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13570 permerror (decl_specifiers.locations[ds_type_spec],
13571 "types may not be defined in a for-range-declaration");
13572 break;
13573 }
13574 /* Anything else is an error. */
13575 else
13576 {
13577 /* If we have already issued an error message we don't need
13578 to issue another one. */
13579 if ((decl != error_mark_node
13580 && DECL_INITIAL (decl) != error_mark_node)
13581 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13582 cp_parser_error (parser, "expected %<,%> or %<;%>");
13583 /* Skip tokens until we reach the end of the statement. */
13584 cp_parser_skip_to_end_of_statement (parser);
13585 /* If the next token is now a `;', consume it. */
13586 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13587 cp_lexer_consume_token (parser->lexer);
13588 goto done;
13589 }
13590 /* After the first time around, a function-definition is not
13591 allowed -- even if it was OK at first. For example:
13592
13593 int i, f() {}
13594
13595 is not valid. */
13596 function_definition_allowed_p = false;
13597 }
13598
13599 /* Issue an error message if no declarators are present, and the
13600 decl-specifier-seq does not itself declare a class or
13601 enumeration: [dcl.dcl]/3. */
13602 if (!saw_declarator)
13603 {
13604 if (cp_parser_declares_only_class_p (parser))
13605 {
13606 if (!declares_class_or_enum
13607 && decl_specifiers.type
13608 && OVERLOAD_TYPE_P (decl_specifiers.type))
13609 /* Ensure an error is issued anyway when finish_decltype_type,
13610 called via cp_parser_decl_specifier_seq, returns a class or
13611 an enumeration (c++/51786). */
13612 decl_specifiers.type = NULL_TREE;
13613 shadow_tag (&decl_specifiers);
13614 }
13615 /* Perform any deferred access checks. */
13616 perform_deferred_access_checks (tf_warning_or_error);
13617 }
13618
13619 /* Consume the `;'. */
13620 finish:
13621 if (!maybe_range_for_decl)
13622 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13623 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13624 {
13625 if (init_loc != UNKNOWN_LOCATION)
13626 error_at (init_loc, "initializer in range-based %<for%> loop");
13627 if (comma_loc != UNKNOWN_LOCATION)
13628 error_at (comma_loc,
13629 "multiple declarations in range-based %<for%> loop");
13630 }
13631
13632 done:
13633 pop_deferring_access_checks ();
13634 }
13635
13636 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13637 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13638 initializer ; */
13639
13640 static tree
13641 cp_parser_decomposition_declaration (cp_parser *parser,
13642 cp_decl_specifier_seq *decl_specifiers,
13643 tree *maybe_range_for_decl,
13644 location_t *init_loc)
13645 {
13646 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13647 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13648 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13649
13650 /* Parse the identifier-list. */
13651 auto_vec<cp_expr, 10> v;
13652 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13653 while (true)
13654 {
13655 cp_expr e = cp_parser_identifier (parser);
13656 if (e.get_value () == error_mark_node)
13657 break;
13658 v.safe_push (e);
13659 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13660 break;
13661 cp_lexer_consume_token (parser->lexer);
13662 }
13663
13664 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13665 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13666 {
13667 end_loc = UNKNOWN_LOCATION;
13668 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13669 false);
13670 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13671 cp_lexer_consume_token (parser->lexer);
13672 else
13673 {
13674 cp_parser_skip_to_end_of_statement (parser);
13675 return error_mark_node;
13676 }
13677 }
13678
13679 if (cxx_dialect < cxx17)
13680 pedwarn (loc, 0, "structured bindings only available with "
13681 "-std=c++17 or -std=gnu++17");
13682
13683 tree pushed_scope;
13684 cp_declarator *declarator = make_declarator (cdk_decomp);
13685 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13686 declarator->id_loc = loc;
13687 if (ref_qual != REF_QUAL_NONE)
13688 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13689 ref_qual == REF_QUAL_RVALUE,
13690 NULL_TREE);
13691 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13692 NULL_TREE, decl_specifiers->attributes,
13693 &pushed_scope);
13694 tree orig_decl = decl;
13695
13696 unsigned int i;
13697 cp_expr e;
13698 cp_decl_specifier_seq decl_specs;
13699 clear_decl_specs (&decl_specs);
13700 decl_specs.type = make_auto ();
13701 tree prev = decl;
13702 FOR_EACH_VEC_ELT (v, i, e)
13703 {
13704 if (i == 0)
13705 declarator = make_id_declarator (NULL_TREE, e.get_value (),
13706 sfk_none, e.get_location ());
13707 else
13708 {
13709 declarator->u.id.unqualified_name = e.get_value ();
13710 declarator->id_loc = e.get_location ();
13711 }
13712 tree elt_pushed_scope;
13713 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13714 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13715 if (decl2 == error_mark_node)
13716 decl = error_mark_node;
13717 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13718 {
13719 /* Ensure we've diagnosed redeclaration if we aren't creating
13720 a new VAR_DECL. */
13721 gcc_assert (errorcount);
13722 decl = error_mark_node;
13723 }
13724 else
13725 prev = decl2;
13726 if (elt_pushed_scope)
13727 pop_scope (elt_pushed_scope);
13728 }
13729
13730 if (v.is_empty ())
13731 {
13732 error_at (loc, "empty structured binding declaration");
13733 decl = error_mark_node;
13734 }
13735
13736 if (maybe_range_for_decl == NULL
13737 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13738 {
13739 bool non_constant_p = false, is_direct_init = false;
13740 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13741 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13742 &non_constant_p);
13743 if (initializer == NULL_TREE
13744 || (TREE_CODE (initializer) == TREE_LIST
13745 && TREE_CHAIN (initializer))
13746 || (is_direct_init
13747 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13748 && CONSTRUCTOR_NELTS (initializer) != 1))
13749 {
13750 error_at (loc, "invalid initializer for structured binding "
13751 "declaration");
13752 initializer = error_mark_node;
13753 }
13754
13755 if (decl != error_mark_node)
13756 {
13757 cp_maybe_mangle_decomp (decl, prev, v.length ());
13758 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13759 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13760 cp_finish_decomp (decl, prev, v.length ());
13761 }
13762 }
13763 else if (decl != error_mark_node)
13764 {
13765 *maybe_range_for_decl = prev;
13766 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13767 the underlying DECL. */
13768 cp_finish_decomp (decl, prev, v.length ());
13769 }
13770
13771 if (pushed_scope)
13772 pop_scope (pushed_scope);
13773
13774 if (decl == error_mark_node && DECL_P (orig_decl))
13775 {
13776 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13777 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13778 }
13779
13780 return decl;
13781 }
13782
13783 /* Parse a decl-specifier-seq.
13784
13785 decl-specifier-seq:
13786 decl-specifier-seq [opt] decl-specifier
13787 decl-specifier attribute-specifier-seq [opt] (C++11)
13788
13789 decl-specifier:
13790 storage-class-specifier
13791 type-specifier
13792 function-specifier
13793 friend
13794 typedef
13795
13796 GNU Extension:
13797
13798 decl-specifier:
13799 attributes
13800
13801 Concepts Extension:
13802
13803 decl-specifier:
13804 concept
13805
13806 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13807
13808 The parser flags FLAGS is used to control type-specifier parsing.
13809
13810 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13811 flags:
13812
13813 1: one of the decl-specifiers is an elaborated-type-specifier
13814 (i.e., a type declaration)
13815 2: one of the decl-specifiers is an enum-specifier or a
13816 class-specifier (i.e., a type definition)
13817
13818 */
13819
13820 static void
13821 cp_parser_decl_specifier_seq (cp_parser* parser,
13822 cp_parser_flags flags,
13823 cp_decl_specifier_seq *decl_specs,
13824 int* declares_class_or_enum)
13825 {
13826 bool constructor_possible_p = !parser->in_declarator_p;
13827 bool found_decl_spec = false;
13828 cp_token *start_token = NULL;
13829 cp_decl_spec ds;
13830
13831 /* Clear DECL_SPECS. */
13832 clear_decl_specs (decl_specs);
13833
13834 /* Assume no class or enumeration type is declared. */
13835 *declares_class_or_enum = 0;
13836
13837 /* Keep reading specifiers until there are no more to read. */
13838 while (true)
13839 {
13840 bool constructor_p;
13841 cp_token *token;
13842 ds = ds_last;
13843
13844 /* Peek at the next token. */
13845 token = cp_lexer_peek_token (parser->lexer);
13846
13847 /* Save the first token of the decl spec list for error
13848 reporting. */
13849 if (!start_token)
13850 start_token = token;
13851 /* Handle attributes. */
13852 if (cp_next_tokens_can_be_attribute_p (parser))
13853 {
13854 /* Parse the attributes. */
13855 tree attrs = cp_parser_attributes_opt (parser);
13856
13857 /* In a sequence of declaration specifiers, c++11 attributes
13858 appertain to the type that precede them. In that case
13859 [dcl.spec]/1 says:
13860
13861 The attribute-specifier-seq affects the type only for
13862 the declaration it appears in, not other declarations
13863 involving the same type.
13864
13865 But for now let's force the user to position the
13866 attribute either at the beginning of the declaration or
13867 after the declarator-id, which would clearly mean that it
13868 applies to the declarator. */
13869 if (cxx11_attribute_p (attrs))
13870 {
13871 if (!found_decl_spec)
13872 /* The c++11 attribute is at the beginning of the
13873 declaration. It appertains to the entity being
13874 declared. */;
13875 else
13876 {
13877 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13878 {
13879 /* This is an attribute following a
13880 class-specifier. */
13881 if (decl_specs->type_definition_p)
13882 warn_misplaced_attr_for_class_type (token->location,
13883 decl_specs->type);
13884 attrs = NULL_TREE;
13885 }
13886 else
13887 {
13888 decl_specs->std_attributes
13889 = attr_chainon (decl_specs->std_attributes, attrs);
13890 if (decl_specs->locations[ds_std_attribute] == 0)
13891 decl_specs->locations[ds_std_attribute] = token->location;
13892 }
13893 continue;
13894 }
13895 }
13896
13897 decl_specs->attributes
13898 = attr_chainon (decl_specs->attributes, attrs);
13899 if (decl_specs->locations[ds_attribute] == 0)
13900 decl_specs->locations[ds_attribute] = token->location;
13901 continue;
13902 }
13903 /* Assume we will find a decl-specifier keyword. */
13904 found_decl_spec = true;
13905 /* If the next token is an appropriate keyword, we can simply
13906 add it to the list. */
13907 switch (token->keyword)
13908 {
13909 /* decl-specifier:
13910 friend
13911 constexpr */
13912 case RID_FRIEND:
13913 if (!at_class_scope_p ())
13914 {
13915 gcc_rich_location richloc (token->location);
13916 richloc.add_fixit_remove ();
13917 error_at (&richloc, "%<friend%> used outside of class");
13918 cp_lexer_purge_token (parser->lexer);
13919 }
13920 else
13921 {
13922 ds = ds_friend;
13923 /* Consume the token. */
13924 cp_lexer_consume_token (parser->lexer);
13925 }
13926 break;
13927
13928 case RID_CONSTEXPR:
13929 ds = ds_constexpr;
13930 cp_lexer_consume_token (parser->lexer);
13931 break;
13932
13933 case RID_CONCEPT:
13934 ds = ds_concept;
13935 cp_lexer_consume_token (parser->lexer);
13936 break;
13937
13938 /* function-specifier:
13939 inline
13940 virtual
13941 explicit */
13942 case RID_INLINE:
13943 case RID_VIRTUAL:
13944 case RID_EXPLICIT:
13945 cp_parser_function_specifier_opt (parser, decl_specs);
13946 break;
13947
13948 /* decl-specifier:
13949 typedef */
13950 case RID_TYPEDEF:
13951 ds = ds_typedef;
13952 /* Consume the token. */
13953 cp_lexer_consume_token (parser->lexer);
13954 /* A constructor declarator cannot appear in a typedef. */
13955 constructor_possible_p = false;
13956 /* The "typedef" keyword can only occur in a declaration; we
13957 may as well commit at this point. */
13958 cp_parser_commit_to_tentative_parse (parser);
13959
13960 if (decl_specs->storage_class != sc_none)
13961 decl_specs->conflicting_specifiers_p = true;
13962 break;
13963
13964 /* storage-class-specifier:
13965 auto
13966 register
13967 static
13968 extern
13969 mutable
13970
13971 GNU Extension:
13972 thread */
13973 case RID_AUTO:
13974 if (cxx_dialect == cxx98)
13975 {
13976 /* Consume the token. */
13977 cp_lexer_consume_token (parser->lexer);
13978
13979 /* Complain about `auto' as a storage specifier, if
13980 we're complaining about C++0x compatibility. */
13981 gcc_rich_location richloc (token->location);
13982 richloc.add_fixit_remove ();
13983 warning_at (&richloc, OPT_Wc__11_compat,
13984 "%<auto%> changes meaning in C++11; "
13985 "please remove it");
13986
13987 /* Set the storage class anyway. */
13988 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13989 token);
13990 }
13991 else
13992 /* C++0x auto type-specifier. */
13993 found_decl_spec = false;
13994 break;
13995
13996 case RID_REGISTER:
13997 case RID_STATIC:
13998 case RID_EXTERN:
13999 case RID_MUTABLE:
14000 /* Consume the token. */
14001 cp_lexer_consume_token (parser->lexer);
14002 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
14003 token);
14004 break;
14005 case RID_THREAD:
14006 /* Consume the token. */
14007 ds = ds_thread;
14008 cp_lexer_consume_token (parser->lexer);
14009 break;
14010
14011 default:
14012 /* We did not yet find a decl-specifier yet. */
14013 found_decl_spec = false;
14014 break;
14015 }
14016
14017 if (found_decl_spec
14018 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
14019 && token->keyword != RID_CONSTEXPR)
14020 error ("decl-specifier invalid in condition");
14021
14022 if (found_decl_spec
14023 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14024 && token->keyword != RID_MUTABLE
14025 && token->keyword != RID_CONSTEXPR)
14026 error_at (token->location, "%qD invalid in lambda",
14027 ridpointers[token->keyword]);
14028
14029 if (ds != ds_last)
14030 set_and_check_decl_spec_loc (decl_specs, ds, token);
14031
14032 /* Constructors are a special case. The `S' in `S()' is not a
14033 decl-specifier; it is the beginning of the declarator. */
14034 constructor_p
14035 = (!found_decl_spec
14036 && constructor_possible_p
14037 && (cp_parser_constructor_declarator_p
14038 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
14039
14040 /* If we don't have a DECL_SPEC yet, then we must be looking at
14041 a type-specifier. */
14042 if (!found_decl_spec && !constructor_p)
14043 {
14044 int decl_spec_declares_class_or_enum;
14045 bool is_cv_qualifier;
14046 tree type_spec;
14047
14048 type_spec
14049 = cp_parser_type_specifier (parser, flags,
14050 decl_specs,
14051 /*is_declaration=*/true,
14052 &decl_spec_declares_class_or_enum,
14053 &is_cv_qualifier);
14054 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
14055
14056 /* If this type-specifier referenced a user-defined type
14057 (a typedef, class-name, etc.), then we can't allow any
14058 more such type-specifiers henceforth.
14059
14060 [dcl.spec]
14061
14062 The longest sequence of decl-specifiers that could
14063 possibly be a type name is taken as the
14064 decl-specifier-seq of a declaration. The sequence shall
14065 be self-consistent as described below.
14066
14067 [dcl.type]
14068
14069 As a general rule, at most one type-specifier is allowed
14070 in the complete decl-specifier-seq of a declaration. The
14071 only exceptions are the following:
14072
14073 -- const or volatile can be combined with any other
14074 type-specifier.
14075
14076 -- signed or unsigned can be combined with char, long,
14077 short, or int.
14078
14079 -- ..
14080
14081 Example:
14082
14083 typedef char* Pc;
14084 void g (const int Pc);
14085
14086 Here, Pc is *not* part of the decl-specifier seq; it's
14087 the declarator. Therefore, once we see a type-specifier
14088 (other than a cv-qualifier), we forbid any additional
14089 user-defined types. We *do* still allow things like `int
14090 int' to be considered a decl-specifier-seq, and issue the
14091 error message later. */
14092 if (type_spec && !is_cv_qualifier)
14093 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14094 /* A constructor declarator cannot follow a type-specifier. */
14095 if (type_spec)
14096 {
14097 constructor_possible_p = false;
14098 found_decl_spec = true;
14099 if (!is_cv_qualifier)
14100 decl_specs->any_type_specifiers_p = true;
14101
14102 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
14103 error_at (token->location, "type-specifier invalid in lambda");
14104 }
14105 }
14106
14107 /* If we still do not have a DECL_SPEC, then there are no more
14108 decl-specifiers. */
14109 if (!found_decl_spec)
14110 break;
14111
14112 decl_specs->any_specifiers_p = true;
14113 /* After we see one decl-specifier, further decl-specifiers are
14114 always optional. */
14115 flags |= CP_PARSER_FLAGS_OPTIONAL;
14116 }
14117
14118 /* Don't allow a friend specifier with a class definition. */
14119 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
14120 && (*declares_class_or_enum & 2))
14121 error_at (decl_specs->locations[ds_friend],
14122 "class definition may not be declared a friend");
14123 }
14124
14125 /* Parse an (optional) storage-class-specifier.
14126
14127 storage-class-specifier:
14128 auto
14129 register
14130 static
14131 extern
14132 mutable
14133
14134 GNU Extension:
14135
14136 storage-class-specifier:
14137 thread
14138
14139 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14140
14141 static tree
14142 cp_parser_storage_class_specifier_opt (cp_parser* parser)
14143 {
14144 switch (cp_lexer_peek_token (parser->lexer)->keyword)
14145 {
14146 case RID_AUTO:
14147 if (cxx_dialect != cxx98)
14148 return NULL_TREE;
14149 /* Fall through for C++98. */
14150 gcc_fallthrough ();
14151
14152 case RID_REGISTER:
14153 case RID_STATIC:
14154 case RID_EXTERN:
14155 case RID_MUTABLE:
14156 case RID_THREAD:
14157 /* Consume the token. */
14158 return cp_lexer_consume_token (parser->lexer)->u.value;
14159
14160 default:
14161 return NULL_TREE;
14162 }
14163 }
14164
14165 /* Parse an (optional) function-specifier.
14166
14167 function-specifier:
14168 inline
14169 virtual
14170 explicit
14171
14172 C++2A Extension:
14173 explicit(constant-expression)
14174
14175 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14176 Updates DECL_SPECS, if it is non-NULL. */
14177
14178 static tree
14179 cp_parser_function_specifier_opt (cp_parser* parser,
14180 cp_decl_specifier_seq *decl_specs)
14181 {
14182 cp_token *token = cp_lexer_peek_token (parser->lexer);
14183 switch (token->keyword)
14184 {
14185 case RID_INLINE:
14186 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
14187 break;
14188
14189 case RID_VIRTUAL:
14190 /* 14.5.2.3 [temp.mem]
14191
14192 A member function template shall not be virtual. */
14193 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14194 && current_class_type)
14195 error_at (token->location, "templates may not be %<virtual%>");
14196 else
14197 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
14198 break;
14199
14200 case RID_EXPLICIT:
14201 {
14202 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
14203 /* If we see '(', it's C++20 explicit(bool). */
14204 tree expr;
14205 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14206 {
14207 matching_parens parens;
14208 parens.consume_open (parser);
14209
14210 /* New types are not allowed in an explicit-specifier. */
14211 const char *saved_message
14212 = parser->type_definition_forbidden_message;
14213 parser->type_definition_forbidden_message
14214 = G_("types may not be defined in explicit-specifier");
14215
14216 if (cxx_dialect < cxx2a)
14217 pedwarn (token->location, 0,
14218 "%<explicit(bool)%> only available with -std=c++2a "
14219 "or -std=gnu++2a");
14220
14221 /* Parse the constant-expression. */
14222 expr = cp_parser_constant_expression (parser);
14223
14224 /* Restore the saved message. */
14225 parser->type_definition_forbidden_message = saved_message;
14226 parens.require_close (parser);
14227 }
14228 else
14229 /* The explicit-specifier explicit without a constant-expression is
14230 equivalent to the explicit-specifier explicit(true). */
14231 expr = boolean_true_node;
14232
14233 /* [dcl.fct.spec]
14234 "the constant-expression, if supplied, shall be a contextually
14235 converted constant expression of type bool." */
14236 expr = build_explicit_specifier (expr, tf_warning_or_error);
14237 /* We could evaluate it -- mark the decl as appropriate. */
14238 if (expr == boolean_true_node)
14239 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14240 else if (expr == boolean_false_node)
14241 /* Don't mark the decl as explicit. */;
14242 else if (decl_specs)
14243 /* The expression was value-dependent. Remember it so that we can
14244 substitute it later. */
14245 decl_specs->explicit_specifier = expr;
14246 return id;
14247 }
14248
14249 default:
14250 return NULL_TREE;
14251 }
14252
14253 /* Consume the token. */
14254 return cp_lexer_consume_token (parser->lexer)->u.value;
14255 }
14256
14257 /* Parse a linkage-specification.
14258
14259 linkage-specification:
14260 extern string-literal { declaration-seq [opt] }
14261 extern string-literal declaration */
14262
14263 static void
14264 cp_parser_linkage_specification (cp_parser* parser)
14265 {
14266 tree linkage;
14267
14268 /* Look for the `extern' keyword. */
14269 cp_token *extern_token
14270 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14271
14272 /* Look for the string-literal. */
14273 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14274 linkage = cp_parser_string_literal (parser, false, false);
14275
14276 /* Transform the literal into an identifier. If the literal is a
14277 wide-character string, or contains embedded NULs, then we can't
14278 handle it as the user wants. */
14279 if (strlen (TREE_STRING_POINTER (linkage))
14280 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14281 {
14282 cp_parser_error (parser, "invalid linkage-specification");
14283 /* Assume C++ linkage. */
14284 linkage = lang_name_cplusplus;
14285 }
14286 else
14287 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14288
14289 /* We're now using the new linkage. */
14290 push_lang_context (linkage);
14291
14292 /* Preserve the location of the the innermost linkage specification,
14293 tracking the locations of nested specifications via a local. */
14294 location_t saved_location
14295 = parser->innermost_linkage_specification_location;
14296 /* Construct a location ranging from the start of the "extern" to
14297 the end of the string-literal, with the caret at the start, e.g.:
14298 extern "C" {
14299 ^~~~~~~~~~
14300 */
14301 parser->innermost_linkage_specification_location
14302 = make_location (extern_token->location,
14303 extern_token->location,
14304 get_finish (string_token->location));
14305
14306 /* If the next token is a `{', then we're using the first
14307 production. */
14308 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14309 {
14310 cp_ensure_no_omp_declare_simd (parser);
14311 cp_ensure_no_oacc_routine (parser);
14312
14313 /* Consume the `{' token. */
14314 matching_braces braces;
14315 braces.consume_open (parser);
14316 /* Parse the declarations. */
14317 cp_parser_declaration_seq_opt (parser);
14318 /* Look for the closing `}'. */
14319 braces.require_close (parser);
14320 }
14321 /* Otherwise, there's just one declaration. */
14322 else
14323 {
14324 bool saved_in_unbraced_linkage_specification_p;
14325
14326 saved_in_unbraced_linkage_specification_p
14327 = parser->in_unbraced_linkage_specification_p;
14328 parser->in_unbraced_linkage_specification_p = true;
14329 cp_parser_declaration (parser);
14330 parser->in_unbraced_linkage_specification_p
14331 = saved_in_unbraced_linkage_specification_p;
14332 }
14333
14334 /* We're done with the linkage-specification. */
14335 pop_lang_context ();
14336
14337 /* Restore location of parent linkage specification, if any. */
14338 parser->innermost_linkage_specification_location = saved_location;
14339 }
14340
14341 /* Parse a static_assert-declaration.
14342
14343 static_assert-declaration:
14344 static_assert ( constant-expression , string-literal ) ;
14345 static_assert ( constant-expression ) ; (C++17)
14346
14347 If MEMBER_P, this static_assert is a class member. */
14348
14349 static void
14350 cp_parser_static_assert(cp_parser *parser, bool member_p)
14351 {
14352 cp_expr condition;
14353 location_t token_loc;
14354 tree message;
14355 bool dummy;
14356
14357 /* Peek at the `static_assert' token so we can keep track of exactly
14358 where the static assertion started. */
14359 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14360
14361 /* Look for the `static_assert' keyword. */
14362 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14363 RT_STATIC_ASSERT))
14364 return;
14365
14366 /* We know we are in a static assertion; commit to any tentative
14367 parse. */
14368 if (cp_parser_parsing_tentatively (parser))
14369 cp_parser_commit_to_tentative_parse (parser);
14370
14371 /* Parse the `(' starting the static assertion condition. */
14372 matching_parens parens;
14373 parens.require_open (parser);
14374
14375 /* Parse the constant-expression. Allow a non-constant expression
14376 here in order to give better diagnostics in finish_static_assert. */
14377 condition =
14378 cp_parser_constant_expression (parser,
14379 /*allow_non_constant_p=*/true,
14380 /*non_constant_p=*/&dummy);
14381
14382 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14383 {
14384 if (cxx_dialect < cxx17)
14385 pedwarn (input_location, OPT_Wpedantic,
14386 "static_assert without a message "
14387 "only available with -std=c++17 or -std=gnu++17");
14388 /* Eat the ')' */
14389 cp_lexer_consume_token (parser->lexer);
14390 message = build_string (1, "");
14391 TREE_TYPE (message) = char_array_type_node;
14392 fix_string_type (message);
14393 }
14394 else
14395 {
14396 /* Parse the separating `,'. */
14397 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14398
14399 /* Parse the string-literal message. */
14400 message = cp_parser_string_literal (parser,
14401 /*translate=*/false,
14402 /*wide_ok=*/true);
14403
14404 /* A `)' completes the static assertion. */
14405 if (!parens.require_close (parser))
14406 cp_parser_skip_to_closing_parenthesis (parser,
14407 /*recovering=*/true,
14408 /*or_comma=*/false,
14409 /*consume_paren=*/true);
14410 }
14411
14412 /* A semicolon terminates the declaration. */
14413 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14414
14415 /* Get the location for the static assertion. Use that of the
14416 condition if available, otherwise, use that of the "static_assert"
14417 token. */
14418 location_t assert_loc = condition.get_location ();
14419 if (assert_loc == UNKNOWN_LOCATION)
14420 assert_loc = token_loc;
14421
14422 /* Complete the static assertion, which may mean either processing
14423 the static assert now or saving it for template instantiation. */
14424 finish_static_assert (condition, message, assert_loc, member_p);
14425 }
14426
14427 /* Parse the expression in decltype ( expression ). */
14428
14429 static tree
14430 cp_parser_decltype_expr (cp_parser *parser,
14431 bool &id_expression_or_member_access_p)
14432 {
14433 cp_token *id_expr_start_token;
14434 tree expr;
14435
14436 /* Since we're going to preserve any side-effects from this parse, set up a
14437 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14438 in the expression. */
14439 tentative_firewall firewall (parser);
14440
14441 /* First, try parsing an id-expression. */
14442 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14443 cp_parser_parse_tentatively (parser);
14444 expr = cp_parser_id_expression (parser,
14445 /*template_keyword_p=*/false,
14446 /*check_dependency_p=*/true,
14447 /*template_p=*/NULL,
14448 /*declarator_p=*/false,
14449 /*optional_p=*/false);
14450
14451 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14452 {
14453 bool non_integral_constant_expression_p = false;
14454 tree id_expression = expr;
14455 cp_id_kind idk;
14456 const char *error_msg;
14457
14458 if (identifier_p (expr))
14459 /* Lookup the name we got back from the id-expression. */
14460 expr = cp_parser_lookup_name_simple (parser, expr,
14461 id_expr_start_token->location);
14462
14463 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14464 /* A template without args is not a complete id-expression. */
14465 expr = error_mark_node;
14466
14467 if (expr
14468 && expr != error_mark_node
14469 && TREE_CODE (expr) != TYPE_DECL
14470 && (TREE_CODE (expr) != BIT_NOT_EXPR
14471 || !TYPE_P (TREE_OPERAND (expr, 0)))
14472 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14473 {
14474 /* Complete lookup of the id-expression. */
14475 expr = (finish_id_expression
14476 (id_expression, expr, parser->scope, &idk,
14477 /*integral_constant_expression_p=*/false,
14478 /*allow_non_integral_constant_expression_p=*/true,
14479 &non_integral_constant_expression_p,
14480 /*template_p=*/false,
14481 /*done=*/true,
14482 /*address_p=*/false,
14483 /*template_arg_p=*/false,
14484 &error_msg,
14485 id_expr_start_token->location));
14486
14487 if (expr == error_mark_node)
14488 /* We found an id-expression, but it was something that we
14489 should not have found. This is an error, not something
14490 we can recover from, so note that we found an
14491 id-expression and we'll recover as gracefully as
14492 possible. */
14493 id_expression_or_member_access_p = true;
14494 }
14495
14496 if (expr
14497 && expr != error_mark_node
14498 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14499 /* We have an id-expression. */
14500 id_expression_or_member_access_p = true;
14501 }
14502
14503 if (!id_expression_or_member_access_p)
14504 {
14505 /* Abort the id-expression parse. */
14506 cp_parser_abort_tentative_parse (parser);
14507
14508 /* Parsing tentatively, again. */
14509 cp_parser_parse_tentatively (parser);
14510
14511 /* Parse a class member access. */
14512 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14513 /*cast_p=*/false, /*decltype*/true,
14514 /*member_access_only_p=*/true, NULL);
14515
14516 if (expr
14517 && expr != error_mark_node
14518 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14519 /* We have an id-expression. */
14520 id_expression_or_member_access_p = true;
14521 }
14522
14523 if (id_expression_or_member_access_p)
14524 /* We have parsed the complete id-expression or member access. */
14525 cp_parser_parse_definitely (parser);
14526 else
14527 {
14528 /* Abort our attempt to parse an id-expression or member access
14529 expression. */
14530 cp_parser_abort_tentative_parse (parser);
14531
14532 /* Commit to the tentative_firewall so we get syntax errors. */
14533 cp_parser_commit_to_tentative_parse (parser);
14534
14535 /* Parse a full expression. */
14536 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14537 /*decltype_p=*/true);
14538 }
14539
14540 return expr;
14541 }
14542
14543 /* Parse a `decltype' type. Returns the type.
14544
14545 simple-type-specifier:
14546 decltype ( expression )
14547 C++14 proposal:
14548 decltype ( auto ) */
14549
14550 static tree
14551 cp_parser_decltype (cp_parser *parser)
14552 {
14553 bool id_expression_or_member_access_p = false;
14554 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14555
14556 if (start_token->type == CPP_DECLTYPE)
14557 {
14558 /* Already parsed. */
14559 cp_lexer_consume_token (parser->lexer);
14560 return saved_checks_value (start_token->u.tree_check_value);
14561 }
14562
14563 /* Look for the `decltype' token. */
14564 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14565 return error_mark_node;
14566
14567 /* Parse the opening `('. */
14568 matching_parens parens;
14569 if (!parens.require_open (parser))
14570 return error_mark_node;
14571
14572 push_deferring_access_checks (dk_deferred);
14573
14574 tree expr = NULL_TREE;
14575
14576 if (cxx_dialect >= cxx14
14577 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14578 /* decltype (auto) */
14579 cp_lexer_consume_token (parser->lexer);
14580 else
14581 {
14582 /* decltype (expression) */
14583
14584 /* Types cannot be defined in a `decltype' expression. Save away the
14585 old message and set the new one. */
14586 const char *saved_message = parser->type_definition_forbidden_message;
14587 parser->type_definition_forbidden_message
14588 = G_("types may not be defined in %<decltype%> expressions");
14589
14590 /* The restrictions on constant-expressions do not apply inside
14591 decltype expressions. */
14592 bool saved_integral_constant_expression_p
14593 = parser->integral_constant_expression_p;
14594 bool saved_non_integral_constant_expression_p
14595 = parser->non_integral_constant_expression_p;
14596 parser->integral_constant_expression_p = false;
14597
14598 /* Within a parenthesized expression, a `>' token is always
14599 the greater-than operator. */
14600 bool saved_greater_than_is_operator_p
14601 = parser->greater_than_is_operator_p;
14602 parser->greater_than_is_operator_p = true;
14603
14604 /* Do not actually evaluate the expression. */
14605 ++cp_unevaluated_operand;
14606
14607 /* Do not warn about problems with the expression. */
14608 ++c_inhibit_evaluation_warnings;
14609
14610 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14611 STRIP_ANY_LOCATION_WRAPPER (expr);
14612
14613 /* Go back to evaluating expressions. */
14614 --cp_unevaluated_operand;
14615 --c_inhibit_evaluation_warnings;
14616
14617 /* The `>' token might be the end of a template-id or
14618 template-parameter-list now. */
14619 parser->greater_than_is_operator_p
14620 = saved_greater_than_is_operator_p;
14621
14622 /* Restore the old message and the integral constant expression
14623 flags. */
14624 parser->type_definition_forbidden_message = saved_message;
14625 parser->integral_constant_expression_p
14626 = saved_integral_constant_expression_p;
14627 parser->non_integral_constant_expression_p
14628 = saved_non_integral_constant_expression_p;
14629 }
14630
14631 /* Parse to the closing `)'. */
14632 if (!parens.require_close (parser))
14633 {
14634 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14635 /*consume_paren=*/true);
14636 pop_deferring_access_checks ();
14637 return error_mark_node;
14638 }
14639
14640 if (!expr)
14641 {
14642 /* Build auto. */
14643 expr = make_decltype_auto ();
14644 AUTO_IS_DECLTYPE (expr) = true;
14645 }
14646 else
14647 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14648 tf_warning_or_error);
14649
14650 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14651 it again. */
14652 start_token->type = CPP_DECLTYPE;
14653 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14654 start_token->u.tree_check_value->value = expr;
14655 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14656 start_token->keyword = RID_MAX;
14657 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14658
14659 pop_to_parent_deferring_access_checks ();
14660
14661 return expr;
14662 }
14663
14664 /* Special member functions [gram.special] */
14665
14666 /* Parse a conversion-function-id.
14667
14668 conversion-function-id:
14669 operator conversion-type-id
14670
14671 Returns an IDENTIFIER_NODE representing the operator. */
14672
14673 static tree
14674 cp_parser_conversion_function_id (cp_parser* parser)
14675 {
14676 tree type;
14677 tree saved_scope;
14678 tree saved_qualifying_scope;
14679 tree saved_object_scope;
14680 tree pushed_scope = NULL_TREE;
14681
14682 /* Look for the `operator' token. */
14683 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14684 return error_mark_node;
14685 /* When we parse the conversion-type-id, the current scope will be
14686 reset. However, we need that information in able to look up the
14687 conversion function later, so we save it here. */
14688 saved_scope = parser->scope;
14689 saved_qualifying_scope = parser->qualifying_scope;
14690 saved_object_scope = parser->object_scope;
14691 /* We must enter the scope of the class so that the names of
14692 entities declared within the class are available in the
14693 conversion-type-id. For example, consider:
14694
14695 struct S {
14696 typedef int I;
14697 operator I();
14698 };
14699
14700 S::operator I() { ... }
14701
14702 In order to see that `I' is a type-name in the definition, we
14703 must be in the scope of `S'. */
14704 if (saved_scope)
14705 pushed_scope = push_scope (saved_scope);
14706 /* Parse the conversion-type-id. */
14707 type = cp_parser_conversion_type_id (parser);
14708 /* Leave the scope of the class, if any. */
14709 if (pushed_scope)
14710 pop_scope (pushed_scope);
14711 /* Restore the saved scope. */
14712 parser->scope = saved_scope;
14713 parser->qualifying_scope = saved_qualifying_scope;
14714 parser->object_scope = saved_object_scope;
14715 /* If the TYPE is invalid, indicate failure. */
14716 if (type == error_mark_node)
14717 return error_mark_node;
14718 return make_conv_op_name (type);
14719 }
14720
14721 /* Parse a conversion-type-id:
14722
14723 conversion-type-id:
14724 type-specifier-seq conversion-declarator [opt]
14725
14726 Returns the TYPE specified. */
14727
14728 static tree
14729 cp_parser_conversion_type_id (cp_parser* parser)
14730 {
14731 tree attributes;
14732 cp_decl_specifier_seq type_specifiers;
14733 cp_declarator *declarator;
14734 tree type_specified;
14735 const char *saved_message;
14736
14737 /* Parse the attributes. */
14738 attributes = cp_parser_attributes_opt (parser);
14739
14740 saved_message = parser->type_definition_forbidden_message;
14741 parser->type_definition_forbidden_message
14742 = G_("types may not be defined in a conversion-type-id");
14743
14744 /* Parse the type-specifiers. */
14745 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
14746 /*is_declaration=*/false,
14747 /*is_trailing_return=*/false,
14748 &type_specifiers);
14749
14750 parser->type_definition_forbidden_message = saved_message;
14751
14752 /* If that didn't work, stop. */
14753 if (type_specifiers.type == error_mark_node)
14754 return error_mark_node;
14755 /* Parse the conversion-declarator. */
14756 declarator = cp_parser_conversion_declarator_opt (parser);
14757
14758 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14759 /*initialized=*/0, &attributes);
14760 if (attributes)
14761 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14762
14763 /* Don't give this error when parsing tentatively. This happens to
14764 work because we always parse this definitively once. */
14765 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14766 && type_uses_auto (type_specified))
14767 {
14768 if (cxx_dialect < cxx14)
14769 {
14770 error ("invalid use of %<auto%> in conversion operator");
14771 return error_mark_node;
14772 }
14773 else if (template_parm_scope_p ())
14774 warning (0, "use of %<auto%> in member template "
14775 "conversion operator can never be deduced");
14776 }
14777
14778 return type_specified;
14779 }
14780
14781 /* Parse an (optional) conversion-declarator.
14782
14783 conversion-declarator:
14784 ptr-operator conversion-declarator [opt]
14785
14786 */
14787
14788 static cp_declarator *
14789 cp_parser_conversion_declarator_opt (cp_parser* parser)
14790 {
14791 enum tree_code code;
14792 tree class_type, std_attributes = NULL_TREE;
14793 cp_cv_quals cv_quals;
14794
14795 /* We don't know if there's a ptr-operator next, or not. */
14796 cp_parser_parse_tentatively (parser);
14797 /* Try the ptr-operator. */
14798 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14799 &std_attributes);
14800 /* If it worked, look for more conversion-declarators. */
14801 if (cp_parser_parse_definitely (parser))
14802 {
14803 cp_declarator *declarator;
14804
14805 /* Parse another optional declarator. */
14806 declarator = cp_parser_conversion_declarator_opt (parser);
14807
14808 declarator = cp_parser_make_indirect_declarator
14809 (code, class_type, cv_quals, declarator, std_attributes);
14810
14811 return declarator;
14812 }
14813
14814 return NULL;
14815 }
14816
14817 /* Parse an (optional) ctor-initializer.
14818
14819 ctor-initializer:
14820 : mem-initializer-list */
14821
14822 static void
14823 cp_parser_ctor_initializer_opt (cp_parser* parser)
14824 {
14825 /* If the next token is not a `:', then there is no
14826 ctor-initializer. */
14827 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14828 {
14829 /* Do default initialization of any bases and members. */
14830 if (DECL_CONSTRUCTOR_P (current_function_decl))
14831 finish_mem_initializers (NULL_TREE);
14832 return;
14833 }
14834
14835 /* Consume the `:' token. */
14836 cp_lexer_consume_token (parser->lexer);
14837 /* And the mem-initializer-list. */
14838 cp_parser_mem_initializer_list (parser);
14839 }
14840
14841 /* Parse a mem-initializer-list.
14842
14843 mem-initializer-list:
14844 mem-initializer ... [opt]
14845 mem-initializer ... [opt] , mem-initializer-list */
14846
14847 static void
14848 cp_parser_mem_initializer_list (cp_parser* parser)
14849 {
14850 tree mem_initializer_list = NULL_TREE;
14851 tree target_ctor = error_mark_node;
14852 cp_token *token = cp_lexer_peek_token (parser->lexer);
14853
14854 /* Let the semantic analysis code know that we are starting the
14855 mem-initializer-list. */
14856 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14857 error_at (token->location,
14858 "only constructors take member initializers");
14859
14860 /* Loop through the list. */
14861 while (true)
14862 {
14863 tree mem_initializer;
14864
14865 token = cp_lexer_peek_token (parser->lexer);
14866 /* Parse the mem-initializer. */
14867 mem_initializer = cp_parser_mem_initializer (parser);
14868 /* If the next token is a `...', we're expanding member initializers. */
14869 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14870 if (ellipsis
14871 || (mem_initializer != error_mark_node
14872 && check_for_bare_parameter_packs (TREE_PURPOSE
14873 (mem_initializer))))
14874 {
14875 /* Consume the `...'. */
14876 if (ellipsis)
14877 cp_lexer_consume_token (parser->lexer);
14878
14879 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14880 can be expanded but members cannot. */
14881 if (mem_initializer != error_mark_node
14882 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14883 {
14884 error_at (token->location,
14885 "cannot expand initializer for member %qD",
14886 TREE_PURPOSE (mem_initializer));
14887 mem_initializer = error_mark_node;
14888 }
14889
14890 /* Construct the pack expansion type. */
14891 if (mem_initializer != error_mark_node)
14892 mem_initializer = make_pack_expansion (mem_initializer);
14893 }
14894 if (target_ctor != error_mark_node
14895 && mem_initializer != error_mark_node)
14896 {
14897 error ("mem-initializer for %qD follows constructor delegation",
14898 TREE_PURPOSE (mem_initializer));
14899 mem_initializer = error_mark_node;
14900 }
14901 /* Look for a target constructor. */
14902 if (mem_initializer != error_mark_node
14903 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14904 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14905 {
14906 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14907 if (mem_initializer_list)
14908 {
14909 error ("constructor delegation follows mem-initializer for %qD",
14910 TREE_PURPOSE (mem_initializer_list));
14911 mem_initializer = error_mark_node;
14912 }
14913 target_ctor = mem_initializer;
14914 }
14915 /* Add it to the list, unless it was erroneous. */
14916 if (mem_initializer != error_mark_node)
14917 {
14918 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14919 mem_initializer_list = mem_initializer;
14920 }
14921 /* If the next token is not a `,', we're done. */
14922 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14923 break;
14924 /* Consume the `,' token. */
14925 cp_lexer_consume_token (parser->lexer);
14926 }
14927
14928 /* Perform semantic analysis. */
14929 if (DECL_CONSTRUCTOR_P (current_function_decl))
14930 finish_mem_initializers (mem_initializer_list);
14931 }
14932
14933 /* Parse a mem-initializer.
14934
14935 mem-initializer:
14936 mem-initializer-id ( expression-list [opt] )
14937 mem-initializer-id braced-init-list
14938
14939 GNU extension:
14940
14941 mem-initializer:
14942 ( expression-list [opt] )
14943
14944 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14945 class) or FIELD_DECL (for a non-static data member) to initialize;
14946 the TREE_VALUE is the expression-list. An empty initialization
14947 list is represented by void_list_node. */
14948
14949 static tree
14950 cp_parser_mem_initializer (cp_parser* parser)
14951 {
14952 tree mem_initializer_id;
14953 tree expression_list;
14954 tree member;
14955 cp_token *token = cp_lexer_peek_token (parser->lexer);
14956
14957 /* Find out what is being initialized. */
14958 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14959 {
14960 permerror (token->location,
14961 "anachronistic old-style base class initializer");
14962 mem_initializer_id = NULL_TREE;
14963 }
14964 else
14965 {
14966 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14967 if (mem_initializer_id == error_mark_node)
14968 return mem_initializer_id;
14969 }
14970 member = expand_member_init (mem_initializer_id);
14971 if (member && !DECL_P (member))
14972 in_base_initializer = 1;
14973
14974 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14975 {
14976 bool expr_non_constant_p;
14977 cp_lexer_set_source_position (parser->lexer);
14978 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14979 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14980 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14981 expression_list = build_tree_list (NULL_TREE, expression_list);
14982 }
14983 else
14984 {
14985 vec<tree, va_gc> *vec;
14986 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14987 /*cast_p=*/false,
14988 /*allow_expansion_p=*/true,
14989 /*non_constant_p=*/NULL,
14990 /*close_paren_loc=*/NULL,
14991 /*wrap_locations_p=*/true);
14992 if (vec == NULL)
14993 return error_mark_node;
14994 expression_list = build_tree_list_vec (vec);
14995 release_tree_vector (vec);
14996 }
14997
14998 if (expression_list == error_mark_node)
14999 return error_mark_node;
15000 if (!expression_list)
15001 expression_list = void_type_node;
15002
15003 in_base_initializer = 0;
15004
15005 return member ? build_tree_list (member, expression_list) : error_mark_node;
15006 }
15007
15008 /* Parse a mem-initializer-id.
15009
15010 mem-initializer-id:
15011 :: [opt] nested-name-specifier [opt] class-name
15012 decltype-specifier (C++11)
15013 identifier
15014
15015 Returns a TYPE indicating the class to be initialized for the first
15016 production (and the second in C++11). Returns an IDENTIFIER_NODE
15017 indicating the data member to be initialized for the last production. */
15018
15019 static tree
15020 cp_parser_mem_initializer_id (cp_parser* parser)
15021 {
15022 bool global_scope_p;
15023 bool nested_name_specifier_p;
15024 bool template_p = false;
15025 tree id;
15026
15027 cp_token *token = cp_lexer_peek_token (parser->lexer);
15028
15029 /* `typename' is not allowed in this context ([temp.res]). */
15030 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15031 {
15032 error_at (token->location,
15033 "keyword %<typename%> not allowed in this context (a qualified "
15034 "member initializer is implicitly a type)");
15035 cp_lexer_consume_token (parser->lexer);
15036 }
15037 /* Look for the optional `::' operator. */
15038 global_scope_p
15039 = (cp_parser_global_scope_opt (parser,
15040 /*current_scope_valid_p=*/false)
15041 != NULL_TREE);
15042 /* Look for the optional nested-name-specifier. The simplest way to
15043 implement:
15044
15045 [temp.res]
15046
15047 The keyword `typename' is not permitted in a base-specifier or
15048 mem-initializer; in these contexts a qualified name that
15049 depends on a template-parameter is implicitly assumed to be a
15050 type name.
15051
15052 is to assume that we have seen the `typename' keyword at this
15053 point. */
15054 nested_name_specifier_p
15055 = (cp_parser_nested_name_specifier_opt (parser,
15056 /*typename_keyword_p=*/true,
15057 /*check_dependency_p=*/true,
15058 /*type_p=*/true,
15059 /*is_declaration=*/true)
15060 != NULL_TREE);
15061 if (nested_name_specifier_p)
15062 template_p = cp_parser_optional_template_keyword (parser);
15063 /* If there is a `::' operator or a nested-name-specifier, then we
15064 are definitely looking for a class-name. */
15065 if (global_scope_p || nested_name_specifier_p)
15066 return cp_parser_class_name (parser,
15067 /*typename_keyword_p=*/true,
15068 /*template_keyword_p=*/template_p,
15069 typename_type,
15070 /*check_dependency_p=*/true,
15071 /*class_head_p=*/false,
15072 /*is_declaration=*/true);
15073 /* Otherwise, we could also be looking for an ordinary identifier. */
15074 cp_parser_parse_tentatively (parser);
15075 if (cp_lexer_next_token_is_decltype (parser->lexer))
15076 /* Try a decltype-specifier. */
15077 id = cp_parser_decltype (parser);
15078 else
15079 /* Otherwise, try a class-name. */
15080 id = cp_parser_class_name (parser,
15081 /*typename_keyword_p=*/true,
15082 /*template_keyword_p=*/false,
15083 none_type,
15084 /*check_dependency_p=*/true,
15085 /*class_head_p=*/false,
15086 /*is_declaration=*/true);
15087 /* If we found one, we're done. */
15088 if (cp_parser_parse_definitely (parser))
15089 return id;
15090 /* Otherwise, look for an ordinary identifier. */
15091 return cp_parser_identifier (parser);
15092 }
15093
15094 /* Overloading [gram.over] */
15095
15096 /* Parse an operator-function-id.
15097
15098 operator-function-id:
15099 operator operator
15100
15101 Returns an IDENTIFIER_NODE for the operator which is a
15102 human-readable spelling of the identifier, e.g., `operator +'. */
15103
15104 static cp_expr
15105 cp_parser_operator_function_id (cp_parser* parser)
15106 {
15107 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
15108 /* Look for the `operator' keyword. */
15109 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15110 return error_mark_node;
15111 /* And then the name of the operator itself. */
15112 return cp_parser_operator (parser, start_loc);
15113 }
15114
15115 /* Return an identifier node for a user-defined literal operator.
15116 The suffix identifier is chained to the operator name identifier. */
15117
15118 tree
15119 cp_literal_operator_id (const char* name)
15120 {
15121 tree identifier;
15122 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
15123 + strlen (name) + 10);
15124 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
15125 identifier = get_identifier (buffer);
15126
15127 return identifier;
15128 }
15129
15130 /* Parse an operator.
15131
15132 operator:
15133 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15134 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15135 || ++ -- , ->* -> () []
15136
15137 GNU Extensions:
15138
15139 operator:
15140 <? >? <?= >?=
15141
15142 Returns an IDENTIFIER_NODE for the operator which is a
15143 human-readable spelling of the identifier, e.g., `operator +'. */
15144
15145 static cp_expr
15146 cp_parser_operator (cp_parser* parser, location_t start_loc)
15147 {
15148 tree id = NULL_TREE;
15149 cp_token *token;
15150 bool utf8 = false;
15151
15152 /* Peek at the next token. */
15153 token = cp_lexer_peek_token (parser->lexer);
15154
15155 location_t end_loc = token->location;
15156
15157 /* Figure out which operator we have. */
15158 enum tree_code op = ERROR_MARK;
15159 bool assop = false;
15160 bool consumed = false;
15161 switch (token->type)
15162 {
15163 case CPP_KEYWORD:
15164 {
15165 /* The keyword should be either `new' or `delete'. */
15166 if (token->keyword == RID_NEW)
15167 op = NEW_EXPR;
15168 else if (token->keyword == RID_DELETE)
15169 op = DELETE_EXPR;
15170 else
15171 break;
15172
15173 /* Consume the `new' or `delete' token. */
15174 end_loc = cp_lexer_consume_token (parser->lexer)->location;
15175
15176 /* Peek at the next token. */
15177 token = cp_lexer_peek_token (parser->lexer);
15178 /* If it's a `[' token then this is the array variant of the
15179 operator. */
15180 if (token->type == CPP_OPEN_SQUARE)
15181 {
15182 /* Consume the `[' token. */
15183 cp_lexer_consume_token (parser->lexer);
15184 /* Look for the `]' token. */
15185 if (cp_token *close_token
15186 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15187 end_loc = close_token->location;
15188 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
15189 }
15190 consumed = true;
15191 break;
15192 }
15193
15194 case CPP_PLUS:
15195 op = PLUS_EXPR;
15196 break;
15197
15198 case CPP_MINUS:
15199 op = MINUS_EXPR;
15200 break;
15201
15202 case CPP_MULT:
15203 op = MULT_EXPR;
15204 break;
15205
15206 case CPP_DIV:
15207 op = TRUNC_DIV_EXPR;
15208 break;
15209
15210 case CPP_MOD:
15211 op = TRUNC_MOD_EXPR;
15212 break;
15213
15214 case CPP_XOR:
15215 op = BIT_XOR_EXPR;
15216 break;
15217
15218 case CPP_AND:
15219 op = BIT_AND_EXPR;
15220 break;
15221
15222 case CPP_OR:
15223 op = BIT_IOR_EXPR;
15224 break;
15225
15226 case CPP_COMPL:
15227 op = BIT_NOT_EXPR;
15228 break;
15229
15230 case CPP_NOT:
15231 op = TRUTH_NOT_EXPR;
15232 break;
15233
15234 case CPP_EQ:
15235 assop = true;
15236 op = NOP_EXPR;
15237 break;
15238
15239 case CPP_LESS:
15240 op = LT_EXPR;
15241 break;
15242
15243 case CPP_GREATER:
15244 op = GT_EXPR;
15245 break;
15246
15247 case CPP_PLUS_EQ:
15248 assop = true;
15249 op = PLUS_EXPR;
15250 break;
15251
15252 case CPP_MINUS_EQ:
15253 assop = true;
15254 op = MINUS_EXPR;
15255 break;
15256
15257 case CPP_MULT_EQ:
15258 assop = true;
15259 op = MULT_EXPR;
15260 break;
15261
15262 case CPP_DIV_EQ:
15263 assop = true;
15264 op = TRUNC_DIV_EXPR;
15265 break;
15266
15267 case CPP_MOD_EQ:
15268 assop = true;
15269 op = TRUNC_MOD_EXPR;
15270 break;
15271
15272 case CPP_XOR_EQ:
15273 assop = true;
15274 op = BIT_XOR_EXPR;
15275 break;
15276
15277 case CPP_AND_EQ:
15278 assop = true;
15279 op = BIT_AND_EXPR;
15280 break;
15281
15282 case CPP_OR_EQ:
15283 assop = true;
15284 op = BIT_IOR_EXPR;
15285 break;
15286
15287 case CPP_LSHIFT:
15288 op = LSHIFT_EXPR;
15289 break;
15290
15291 case CPP_RSHIFT:
15292 op = RSHIFT_EXPR;
15293 break;
15294
15295 case CPP_LSHIFT_EQ:
15296 assop = true;
15297 op = LSHIFT_EXPR;
15298 break;
15299
15300 case CPP_RSHIFT_EQ:
15301 assop = true;
15302 op = RSHIFT_EXPR;
15303 break;
15304
15305 case CPP_EQ_EQ:
15306 op = EQ_EXPR;
15307 break;
15308
15309 case CPP_NOT_EQ:
15310 op = NE_EXPR;
15311 break;
15312
15313 case CPP_LESS_EQ:
15314 op = LE_EXPR;
15315 break;
15316
15317 case CPP_GREATER_EQ:
15318 op = GE_EXPR;
15319 break;
15320
15321 case CPP_AND_AND:
15322 op = TRUTH_ANDIF_EXPR;
15323 break;
15324
15325 case CPP_OR_OR:
15326 op = TRUTH_ORIF_EXPR;
15327 break;
15328
15329 case CPP_PLUS_PLUS:
15330 op = POSTINCREMENT_EXPR;
15331 break;
15332
15333 case CPP_MINUS_MINUS:
15334 op = PREDECREMENT_EXPR;
15335 break;
15336
15337 case CPP_COMMA:
15338 op = COMPOUND_EXPR;
15339 break;
15340
15341 case CPP_DEREF_STAR:
15342 op = MEMBER_REF;
15343 break;
15344
15345 case CPP_DEREF:
15346 op = COMPONENT_REF;
15347 break;
15348
15349 case CPP_OPEN_PAREN:
15350 {
15351 /* Consume the `('. */
15352 matching_parens parens;
15353 parens.consume_open (parser);
15354 /* Look for the matching `)'. */
15355 token = parens.require_close (parser);
15356 if (token)
15357 end_loc = token->location;
15358 op = CALL_EXPR;
15359 consumed = true;
15360 break;
15361 }
15362
15363 case CPP_OPEN_SQUARE:
15364 /* Consume the `['. */
15365 cp_lexer_consume_token (parser->lexer);
15366 /* Look for the matching `]'. */
15367 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15368 if (token)
15369 end_loc = token->location;
15370 op = ARRAY_REF;
15371 consumed = true;
15372 break;
15373
15374 case CPP_UTF8STRING:
15375 case CPP_UTF8STRING_USERDEF:
15376 utf8 = true;
15377 /* FALLTHRU */
15378 case CPP_STRING:
15379 case CPP_WSTRING:
15380 case CPP_STRING16:
15381 case CPP_STRING32:
15382 case CPP_STRING_USERDEF:
15383 case CPP_WSTRING_USERDEF:
15384 case CPP_STRING16_USERDEF:
15385 case CPP_STRING32_USERDEF:
15386 {
15387 cp_expr str;
15388 tree string_tree;
15389 int sz, len;
15390
15391 if (cxx_dialect == cxx98)
15392 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15393
15394 /* Consume the string. */
15395 str = cp_parser_string_literal (parser, /*translate=*/true,
15396 /*wide_ok=*/true, /*lookup_udlit=*/false);
15397 if (str == error_mark_node)
15398 return error_mark_node;
15399 else if (TREE_CODE (str) == USERDEF_LITERAL)
15400 {
15401 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
15402 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
15403 end_loc = str.get_location ();
15404 }
15405 else
15406 {
15407 string_tree = str;
15408 /* Look for the suffix identifier. */
15409 token = cp_lexer_peek_token (parser->lexer);
15410 if (token->type == CPP_NAME)
15411 {
15412 id = cp_parser_identifier (parser);
15413 end_loc = token->location;
15414 }
15415 else if (token->type == CPP_KEYWORD)
15416 {
15417 error ("unexpected keyword;"
15418 " remove space between quotes and suffix identifier");
15419 return error_mark_node;
15420 }
15421 else
15422 {
15423 error ("expected suffix identifier");
15424 return error_mark_node;
15425 }
15426 }
15427 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15428 (TREE_TYPE (TREE_TYPE (string_tree))));
15429 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15430 if (len != 0)
15431 {
15432 error ("expected empty string after %<operator%> keyword");
15433 return error_mark_node;
15434 }
15435 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15436 != char_type_node)
15437 {
15438 error ("invalid encoding prefix in literal operator");
15439 return error_mark_node;
15440 }
15441 if (id != error_mark_node)
15442 {
15443 const char *name = IDENTIFIER_POINTER (id);
15444 id = cp_literal_operator_id (name);
15445 }
15446 /* Generate a location of the form:
15447 "" _suffix_identifier
15448 ^~~~~~~~~~~~~~~~~~~~~
15449 with caret == start at the start token, finish at the end of the
15450 suffix identifier. */
15451 location_t finish_loc
15452 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15453 location_t combined_loc
15454 = make_location (start_loc, start_loc, finish_loc);
15455 return cp_expr (id, combined_loc);
15456 }
15457
15458 default:
15459 /* Anything else is an error. */
15460 break;
15461 }
15462
15463 /* If we have selected an identifier, we need to consume the
15464 operator token. */
15465 if (op != ERROR_MARK)
15466 {
15467 id = ovl_op_identifier (assop, op);
15468 if (!consumed)
15469 cp_lexer_consume_token (parser->lexer);
15470 }
15471 /* Otherwise, no valid operator name was present. */
15472 else
15473 {
15474 cp_parser_error (parser, "expected operator");
15475 id = error_mark_node;
15476 }
15477
15478 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15479 return cp_expr (id, start_loc);
15480 }
15481
15482 /* Parse a template-declaration.
15483
15484 template-declaration:
15485 export [opt] template < template-parameter-list > declaration
15486
15487 If MEMBER_P is TRUE, this template-declaration occurs within a
15488 class-specifier.
15489
15490 The grammar rule given by the standard isn't correct. What
15491 is really meant is:
15492
15493 template-declaration:
15494 export [opt] template-parameter-list-seq
15495 decl-specifier-seq [opt] init-declarator [opt] ;
15496 export [opt] template-parameter-list-seq
15497 function-definition
15498
15499 template-parameter-list-seq:
15500 template-parameter-list-seq [opt]
15501 template < template-parameter-list >
15502
15503 Concept Extensions:
15504
15505 template-parameter-list-seq:
15506 template < template-parameter-list > requires-clause [opt]
15507
15508 requires-clause:
15509 requires logical-or-expression */
15510
15511 static void
15512 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15513 {
15514 /* Check for `export'. */
15515 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15516 {
15517 /* Consume the `export' token. */
15518 cp_lexer_consume_token (parser->lexer);
15519 /* Warn that we do not support `export'. */
15520 warning (0, "keyword %<export%> not implemented, and will be ignored");
15521 }
15522
15523 cp_parser_template_declaration_after_export (parser, member_p);
15524 }
15525
15526 /* Parse a template-parameter-list.
15527
15528 template-parameter-list:
15529 template-parameter
15530 template-parameter-list , template-parameter
15531
15532 Returns a TREE_LIST. Each node represents a template parameter.
15533 The nodes are connected via their TREE_CHAINs. */
15534
15535 static tree
15536 cp_parser_template_parameter_list (cp_parser* parser)
15537 {
15538 tree parameter_list = NULL_TREE;
15539
15540 /* Don't create wrapper nodes within a template-parameter-list,
15541 since we don't want to have different types based on the
15542 spelling location of constants and decls within them. */
15543 auto_suppress_location_wrappers sentinel;
15544
15545 begin_template_parm_list ();
15546
15547 /* The loop below parses the template parms. We first need to know
15548 the total number of template parms to be able to compute proper
15549 canonical types of each dependent type. So after the loop, when
15550 we know the total number of template parms,
15551 end_template_parm_list computes the proper canonical types and
15552 fixes up the dependent types accordingly. */
15553 while (true)
15554 {
15555 tree parameter;
15556 bool is_non_type;
15557 bool is_parameter_pack;
15558 location_t parm_loc;
15559
15560 /* Parse the template-parameter. */
15561 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15562 parameter = cp_parser_template_parameter (parser,
15563 &is_non_type,
15564 &is_parameter_pack);
15565 /* Add it to the list. */
15566 if (parameter != error_mark_node)
15567 parameter_list = process_template_parm (parameter_list,
15568 parm_loc,
15569 parameter,
15570 is_non_type,
15571 is_parameter_pack);
15572 else
15573 {
15574 tree err_parm = build_tree_list (parameter, parameter);
15575 parameter_list = chainon (parameter_list, err_parm);
15576 }
15577
15578 /* If the next token is not a `,', we're done. */
15579 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15580 break;
15581 /* Otherwise, consume the `,' token. */
15582 cp_lexer_consume_token (parser->lexer);
15583 }
15584
15585 return end_template_parm_list (parameter_list);
15586 }
15587
15588 /* Parse a introduction-list.
15589
15590 introduction-list:
15591 introduced-parameter
15592 introduction-list , introduced-parameter
15593
15594 introduced-parameter:
15595 ...[opt] identifier
15596
15597 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15598 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15599 WILDCARD_DECL will also have DECL_NAME set and token location in
15600 DECL_SOURCE_LOCATION. */
15601
15602 static tree
15603 cp_parser_introduction_list (cp_parser *parser)
15604 {
15605 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15606
15607 while (true)
15608 {
15609 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15610 if (is_pack)
15611 cp_lexer_consume_token (parser->lexer);
15612
15613 tree identifier = cp_parser_identifier (parser);
15614 if (identifier == error_mark_node)
15615 break;
15616
15617 /* Build placeholder. */
15618 tree parm = build_nt (WILDCARD_DECL);
15619 DECL_SOURCE_LOCATION (parm)
15620 = cp_lexer_peek_token (parser->lexer)->location;
15621 DECL_NAME (parm) = identifier;
15622 WILDCARD_PACK_P (parm) = is_pack;
15623 vec_safe_push (introduction_vec, parm);
15624
15625 /* If the next token is not a `,', we're done. */
15626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15627 break;
15628 /* Otherwise, consume the `,' token. */
15629 cp_lexer_consume_token (parser->lexer);
15630 }
15631
15632 /* Convert the vec into a TREE_VEC. */
15633 tree introduction_list = make_tree_vec (introduction_vec->length ());
15634 unsigned int n;
15635 tree parm;
15636 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15637 TREE_VEC_ELT (introduction_list, n) = parm;
15638
15639 release_tree_vector (introduction_vec);
15640 return introduction_list;
15641 }
15642
15643 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15644 is an abstract declarator. */
15645
15646 static inline cp_declarator*
15647 get_id_declarator (cp_declarator *declarator)
15648 {
15649 cp_declarator *d = declarator;
15650 while (d && d->kind != cdk_id)
15651 d = d->declarator;
15652 return d;
15653 }
15654
15655 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15656 is an abstract declarator. */
15657
15658 static inline tree
15659 get_unqualified_id (cp_declarator *declarator)
15660 {
15661 declarator = get_id_declarator (declarator);
15662 if (declarator)
15663 return declarator->u.id.unqualified_name;
15664 else
15665 return NULL_TREE;
15666 }
15667
15668 /* Returns true if DECL represents a constrained-parameter. */
15669
15670 static inline bool
15671 is_constrained_parameter (tree decl)
15672 {
15673 return (decl
15674 && TREE_CODE (decl) == TYPE_DECL
15675 && CONSTRAINED_PARM_CONCEPT (decl)
15676 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15677 }
15678
15679 /* Returns true if PARM declares a constrained-parameter. */
15680
15681 static inline bool
15682 is_constrained_parameter (cp_parameter_declarator *parm)
15683 {
15684 return is_constrained_parameter (parm->decl_specifiers.type);
15685 }
15686
15687 /* Check that the type parameter is only a declarator-id, and that its
15688 type is not cv-qualified. */
15689
15690 bool
15691 cp_parser_check_constrained_type_parm (cp_parser *parser,
15692 cp_parameter_declarator *parm)
15693 {
15694 if (!parm->declarator)
15695 return true;
15696
15697 if (parm->declarator->kind != cdk_id)
15698 {
15699 cp_parser_error (parser, "invalid constrained type parameter");
15700 return false;
15701 }
15702
15703 /* Don't allow cv-qualified type parameters. */
15704 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15705 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15706 {
15707 cp_parser_error (parser, "cv-qualified type parameter");
15708 return false;
15709 }
15710
15711 return true;
15712 }
15713
15714 /* Finish parsing/processing a template type parameter and checking
15715 various restrictions. */
15716
15717 static inline tree
15718 cp_parser_constrained_type_template_parm (cp_parser *parser,
15719 tree id,
15720 cp_parameter_declarator* parmdecl)
15721 {
15722 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15723 return finish_template_type_parm (class_type_node, id);
15724 else
15725 return error_mark_node;
15726 }
15727
15728 static tree
15729 finish_constrained_template_template_parm (tree proto, tree id)
15730 {
15731 /* FIXME: This should probably be copied, and we may need to adjust
15732 the template parameter depths. */
15733 tree saved_parms = current_template_parms;
15734 begin_template_parm_list ();
15735 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15736 end_template_parm_list ();
15737
15738 tree parm = finish_template_template_parm (class_type_node, id);
15739 current_template_parms = saved_parms;
15740
15741 return parm;
15742 }
15743
15744 /* Finish parsing/processing a template template parameter by borrowing
15745 the template parameter list from the prototype parameter. */
15746
15747 static tree
15748 cp_parser_constrained_template_template_parm (cp_parser *parser,
15749 tree proto,
15750 tree id,
15751 cp_parameter_declarator *parmdecl)
15752 {
15753 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15754 return error_mark_node;
15755 return finish_constrained_template_template_parm (proto, id);
15756 }
15757
15758 /* Create a new non-type template parameter from the given PARM
15759 declarator. */
15760
15761 static tree
15762 constrained_non_type_template_parm (bool *is_non_type,
15763 cp_parameter_declarator *parm)
15764 {
15765 *is_non_type = true;
15766 cp_declarator *decl = parm->declarator;
15767 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15768 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15769 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15770 }
15771
15772 /* Build a constrained template parameter based on the PARMDECL
15773 declarator. The type of PARMDECL is the constrained type, which
15774 refers to the prototype template parameter that ultimately
15775 specifies the type of the declared parameter. */
15776
15777 static tree
15778 finish_constrained_parameter (cp_parser *parser,
15779 cp_parameter_declarator *parmdecl,
15780 bool *is_non_type,
15781 bool *is_parameter_pack)
15782 {
15783 tree decl = parmdecl->decl_specifiers.type;
15784 tree id = get_unqualified_id (parmdecl->declarator);
15785 tree def = parmdecl->default_argument;
15786 tree proto = DECL_INITIAL (decl);
15787
15788 /* A template parameter constrained by a variadic concept shall also
15789 be declared as a template parameter pack. */
15790 bool is_variadic = template_parameter_pack_p (proto);
15791 if (is_variadic && !*is_parameter_pack)
15792 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15793
15794 /* Build the parameter. Return an error if the declarator was invalid. */
15795 tree parm;
15796 if (TREE_CODE (proto) == TYPE_DECL)
15797 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15798 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15799 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15800 parmdecl);
15801 else
15802 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15803 if (parm == error_mark_node)
15804 return error_mark_node;
15805
15806 /* Finish the parameter decl and create a node attaching the
15807 default argument and constraint. */
15808 parm = build_tree_list (def, parm);
15809 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15810
15811 return parm;
15812 }
15813
15814 /* Returns true if the parsed type actually represents the declaration
15815 of a type template-parameter. */
15816
15817 static inline bool
15818 declares_constrained_type_template_parameter (tree type)
15819 {
15820 return (is_constrained_parameter (type)
15821 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15822 }
15823
15824
15825 /* Returns true if the parsed type actually represents the declaration of
15826 a template template-parameter. */
15827
15828 static bool
15829 declares_constrained_template_template_parameter (tree type)
15830 {
15831 return (is_constrained_parameter (type)
15832 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15833 }
15834
15835 /* Parse a default argument for a type template-parameter.
15836 Note that diagnostics are handled in cp_parser_template_parameter. */
15837
15838 static tree
15839 cp_parser_default_type_template_argument (cp_parser *parser)
15840 {
15841 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15842
15843 /* Consume the `=' token. */
15844 cp_lexer_consume_token (parser->lexer);
15845
15846 cp_token *token = cp_lexer_peek_token (parser->lexer);
15847
15848 /* Parse the default-argument. */
15849 push_deferring_access_checks (dk_no_deferred);
15850 tree default_argument = cp_parser_type_id (parser,
15851 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15852 NULL);
15853 pop_deferring_access_checks ();
15854
15855 if (flag_concepts && type_uses_auto (default_argument))
15856 {
15857 error_at (token->location,
15858 "invalid use of %<auto%> in default template argument");
15859 return error_mark_node;
15860 }
15861
15862 return default_argument;
15863 }
15864
15865 /* Parse a default argument for a template template-parameter. */
15866
15867 static tree
15868 cp_parser_default_template_template_argument (cp_parser *parser)
15869 {
15870 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15871
15872 bool is_template;
15873
15874 /* Consume the `='. */
15875 cp_lexer_consume_token (parser->lexer);
15876 /* Parse the id-expression. */
15877 push_deferring_access_checks (dk_no_deferred);
15878 /* save token before parsing the id-expression, for error
15879 reporting */
15880 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15881 tree default_argument
15882 = cp_parser_id_expression (parser,
15883 /*template_keyword_p=*/false,
15884 /*check_dependency_p=*/true,
15885 /*template_p=*/&is_template,
15886 /*declarator_p=*/false,
15887 /*optional_p=*/false);
15888 if (TREE_CODE (default_argument) == TYPE_DECL)
15889 /* If the id-expression was a template-id that refers to
15890 a template-class, we already have the declaration here,
15891 so no further lookup is needed. */
15892 ;
15893 else
15894 /* Look up the name. */
15895 default_argument
15896 = cp_parser_lookup_name (parser, default_argument,
15897 none_type,
15898 /*is_template=*/is_template,
15899 /*is_namespace=*/false,
15900 /*check_dependency=*/true,
15901 /*ambiguous_decls=*/NULL,
15902 token->location);
15903 /* See if the default argument is valid. */
15904 default_argument = check_template_template_default_arg (default_argument);
15905 pop_deferring_access_checks ();
15906 return default_argument;
15907 }
15908
15909 /* Parse a template-parameter.
15910
15911 template-parameter:
15912 type-parameter
15913 parameter-declaration
15914
15915 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15916 the parameter. The TREE_PURPOSE is the default value, if any.
15917 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15918 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15919 set to true iff this parameter is a parameter pack. */
15920
15921 static tree
15922 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15923 bool *is_parameter_pack)
15924 {
15925 cp_token *token;
15926 cp_parameter_declarator *parameter_declarator;
15927 tree parm;
15928
15929 /* Assume it is a type parameter or a template parameter. */
15930 *is_non_type = false;
15931 /* Assume it not a parameter pack. */
15932 *is_parameter_pack = false;
15933 /* Peek at the next token. */
15934 token = cp_lexer_peek_token (parser->lexer);
15935 /* If it is `template', we have a type-parameter. */
15936 if (token->keyword == RID_TEMPLATE)
15937 return cp_parser_type_parameter (parser, is_parameter_pack);
15938 /* If it is `class' or `typename' we do not know yet whether it is a
15939 type parameter or a non-type parameter. Consider:
15940
15941 template <typename T, typename T::X X> ...
15942
15943 or:
15944
15945 template <class C, class D*> ...
15946
15947 Here, the first parameter is a type parameter, and the second is
15948 a non-type parameter. We can tell by looking at the token after
15949 the identifier -- if it is a `,', `=', or `>' then we have a type
15950 parameter. */
15951 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15952 {
15953 /* Peek at the token after `class' or `typename'. */
15954 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15955 /* If it's an ellipsis, we have a template type parameter
15956 pack. */
15957 if (token->type == CPP_ELLIPSIS)
15958 return cp_parser_type_parameter (parser, is_parameter_pack);
15959 /* If it's an identifier, skip it. */
15960 if (token->type == CPP_NAME)
15961 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15962 /* Now, see if the token looks like the end of a template
15963 parameter. */
15964 if (token->type == CPP_COMMA
15965 || token->type == CPP_EQ
15966 || token->type == CPP_GREATER)
15967 return cp_parser_type_parameter (parser, is_parameter_pack);
15968 }
15969
15970 /* Otherwise, it is a non-type parameter or a constrained parameter.
15971
15972 [temp.param]
15973
15974 When parsing a default template-argument for a non-type
15975 template-parameter, the first non-nested `>' is taken as the end
15976 of the template parameter-list rather than a greater-than
15977 operator. */
15978 parameter_declarator
15979 = cp_parser_parameter_declaration (parser,
15980 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15981 /*template_parm_p=*/true,
15982 /*parenthesized_p=*/NULL);
15983
15984 if (!parameter_declarator)
15985 return error_mark_node;
15986
15987 /* If the parameter declaration is marked as a parameter pack, set
15988 *IS_PARAMETER_PACK to notify the caller. */
15989 if (parameter_declarator->template_parameter_pack_p)
15990 *is_parameter_pack = true;
15991
15992 if (parameter_declarator->default_argument)
15993 {
15994 /* Can happen in some cases of erroneous input (c++/34892). */
15995 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15996 /* Consume the `...' for better error recovery. */
15997 cp_lexer_consume_token (parser->lexer);
15998 }
15999
16000 // The parameter may have been constrained.
16001 if (is_constrained_parameter (parameter_declarator))
16002 return finish_constrained_parameter (parser,
16003 parameter_declarator,
16004 is_non_type,
16005 is_parameter_pack);
16006
16007 // Now we're sure that the parameter is a non-type parameter.
16008 *is_non_type = true;
16009
16010 parm = grokdeclarator (parameter_declarator->declarator,
16011 &parameter_declarator->decl_specifiers,
16012 TPARM, /*initialized=*/0,
16013 /*attrlist=*/NULL);
16014 if (parm == error_mark_node)
16015 return error_mark_node;
16016
16017 return build_tree_list (parameter_declarator->default_argument, parm);
16018 }
16019
16020 /* Parse a type-parameter.
16021
16022 type-parameter:
16023 class identifier [opt]
16024 class identifier [opt] = type-id
16025 typename identifier [opt]
16026 typename identifier [opt] = type-id
16027 template < template-parameter-list > class identifier [opt]
16028 template < template-parameter-list > class identifier [opt]
16029 = id-expression
16030
16031 GNU Extension (variadic templates):
16032
16033 type-parameter:
16034 class ... identifier [opt]
16035 typename ... identifier [opt]
16036
16037 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16038 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16039 the declaration of the parameter.
16040
16041 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16042
16043 static tree
16044 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
16045 {
16046 cp_token *token;
16047 tree parameter;
16048
16049 /* Look for a keyword to tell us what kind of parameter this is. */
16050 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
16051 if (!token)
16052 return error_mark_node;
16053
16054 switch (token->keyword)
16055 {
16056 case RID_CLASS:
16057 case RID_TYPENAME:
16058 {
16059 tree identifier;
16060 tree default_argument;
16061
16062 /* If the next token is an ellipsis, we have a template
16063 argument pack. */
16064 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16065 {
16066 /* Consume the `...' token. */
16067 cp_lexer_consume_token (parser->lexer);
16068 maybe_warn_variadic_templates ();
16069
16070 *is_parameter_pack = true;
16071 }
16072
16073 /* If the next token is an identifier, then it names the
16074 parameter. */
16075 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16076 identifier = cp_parser_identifier (parser);
16077 else
16078 identifier = NULL_TREE;
16079
16080 /* Create the parameter. */
16081 parameter = finish_template_type_parm (class_type_node, identifier);
16082
16083 /* If the next token is an `=', we have a default argument. */
16084 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16085 {
16086 default_argument
16087 = cp_parser_default_type_template_argument (parser);
16088
16089 /* Template parameter packs cannot have default
16090 arguments. */
16091 if (*is_parameter_pack)
16092 {
16093 if (identifier)
16094 error_at (token->location,
16095 "template parameter pack %qD cannot have a "
16096 "default argument", identifier);
16097 else
16098 error_at (token->location,
16099 "template parameter packs cannot have "
16100 "default arguments");
16101 default_argument = NULL_TREE;
16102 }
16103 else if (check_for_bare_parameter_packs (default_argument))
16104 default_argument = error_mark_node;
16105 }
16106 else
16107 default_argument = NULL_TREE;
16108
16109 /* Create the combined representation of the parameter and the
16110 default argument. */
16111 parameter = build_tree_list (default_argument, parameter);
16112 }
16113 break;
16114
16115 case RID_TEMPLATE:
16116 {
16117 tree identifier;
16118 tree default_argument;
16119
16120 /* Look for the `<'. */
16121 cp_parser_require (parser, CPP_LESS, RT_LESS);
16122 /* Parse the template-parameter-list. */
16123 cp_parser_template_parameter_list (parser);
16124 /* Look for the `>'. */
16125 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16126
16127 // If template requirements are present, parse them.
16128 if (flag_concepts)
16129 {
16130 tree reqs = get_shorthand_constraints (current_template_parms);
16131 if (tree r = cp_parser_requires_clause_opt (parser))
16132 reqs = conjoin_constraints (reqs, normalize_expression (r));
16133 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
16134 }
16135
16136 /* Look for the `class' or 'typename' keywords. */
16137 cp_parser_type_parameter_key (parser);
16138 /* If the next token is an ellipsis, we have a template
16139 argument pack. */
16140 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16141 {
16142 /* Consume the `...' token. */
16143 cp_lexer_consume_token (parser->lexer);
16144 maybe_warn_variadic_templates ();
16145
16146 *is_parameter_pack = true;
16147 }
16148 /* If the next token is an `=', then there is a
16149 default-argument. If the next token is a `>', we are at
16150 the end of the parameter-list. If the next token is a `,',
16151 then we are at the end of this parameter. */
16152 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16153 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
16154 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16155 {
16156 identifier = cp_parser_identifier (parser);
16157 /* Treat invalid names as if the parameter were nameless. */
16158 if (identifier == error_mark_node)
16159 identifier = NULL_TREE;
16160 }
16161 else
16162 identifier = NULL_TREE;
16163
16164 /* Create the template parameter. */
16165 parameter = finish_template_template_parm (class_type_node,
16166 identifier);
16167
16168 /* If the next token is an `=', then there is a
16169 default-argument. */
16170 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16171 {
16172 default_argument
16173 = cp_parser_default_template_template_argument (parser);
16174
16175 /* Template parameter packs cannot have default
16176 arguments. */
16177 if (*is_parameter_pack)
16178 {
16179 if (identifier)
16180 error_at (token->location,
16181 "template parameter pack %qD cannot "
16182 "have a default argument",
16183 identifier);
16184 else
16185 error_at (token->location, "template parameter packs cannot "
16186 "have default arguments");
16187 default_argument = NULL_TREE;
16188 }
16189 }
16190 else
16191 default_argument = NULL_TREE;
16192
16193 /* Create the combined representation of the parameter and the
16194 default argument. */
16195 parameter = build_tree_list (default_argument, parameter);
16196 }
16197 break;
16198
16199 default:
16200 gcc_unreachable ();
16201 break;
16202 }
16203
16204 return parameter;
16205 }
16206
16207 /* Parse a template-id.
16208
16209 template-id:
16210 template-name < template-argument-list [opt] >
16211
16212 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16213 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16214 returned. Otherwise, if the template-name names a function, or set
16215 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16216 names a class, returns a TYPE_DECL for the specialization.
16217
16218 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16219 uninstantiated templates. */
16220
16221 static tree
16222 cp_parser_template_id (cp_parser *parser,
16223 bool template_keyword_p,
16224 bool check_dependency_p,
16225 enum tag_types tag_type,
16226 bool is_declaration)
16227 {
16228 tree templ;
16229 tree arguments;
16230 tree template_id;
16231 cp_token_position start_of_id = 0;
16232 cp_token *next_token = NULL, *next_token_2 = NULL;
16233 bool is_identifier;
16234
16235 /* If the next token corresponds to a template-id, there is no need
16236 to reparse it. */
16237 cp_token *token = cp_lexer_peek_token (parser->lexer);
16238 if (token->type == CPP_TEMPLATE_ID)
16239 {
16240 cp_lexer_consume_token (parser->lexer);
16241 return saved_checks_value (token->u.tree_check_value);
16242 }
16243
16244 /* Avoid performing name lookup if there is no possibility of
16245 finding a template-id. */
16246 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16247 || (token->type == CPP_NAME
16248 && !cp_parser_nth_token_starts_template_argument_list_p
16249 (parser, 2)))
16250 {
16251 cp_parser_error (parser, "expected template-id");
16252 return error_mark_node;
16253 }
16254
16255 /* Remember where the template-id starts. */
16256 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16257 start_of_id = cp_lexer_token_position (parser->lexer, false);
16258
16259 push_deferring_access_checks (dk_deferred);
16260
16261 /* Parse the template-name. */
16262 is_identifier = false;
16263 templ = cp_parser_template_name (parser, template_keyword_p,
16264 check_dependency_p,
16265 is_declaration,
16266 tag_type,
16267 &is_identifier);
16268
16269 /* Push any access checks inside the firewall we're about to create. */
16270 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
16271 pop_deferring_access_checks ();
16272 if (templ == error_mark_node || is_identifier)
16273 return templ;
16274
16275 /* Since we're going to preserve any side-effects from this parse, set up a
16276 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16277 in the template arguments. */
16278 tentative_firewall firewall (parser);
16279 reopen_deferring_access_checks (checks);
16280
16281 /* If we find the sequence `[:' after a template-name, it's probably
16282 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16283 parse correctly the argument list. */
16284 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16285 == CPP_OPEN_SQUARE)
16286 && next_token->flags & DIGRAPH
16287 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16288 == CPP_COLON)
16289 && !(next_token_2->flags & PREV_WHITE))
16290 {
16291 cp_parser_parse_tentatively (parser);
16292 /* Change `:' into `::'. */
16293 next_token_2->type = CPP_SCOPE;
16294 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16295 CPP_LESS. */
16296 cp_lexer_consume_token (parser->lexer);
16297
16298 /* Parse the arguments. */
16299 arguments = cp_parser_enclosed_template_argument_list (parser);
16300 if (!cp_parser_parse_definitely (parser))
16301 {
16302 /* If we couldn't parse an argument list, then we revert our changes
16303 and return simply an error. Maybe this is not a template-id
16304 after all. */
16305 next_token_2->type = CPP_COLON;
16306 cp_parser_error (parser, "expected %<<%>");
16307 pop_deferring_access_checks ();
16308 return error_mark_node;
16309 }
16310 /* Otherwise, emit an error about the invalid digraph, but continue
16311 parsing because we got our argument list. */
16312 if (permerror (next_token->location,
16313 "%<<::%> cannot begin a template-argument list"))
16314 {
16315 static bool hint = false;
16316 inform (next_token->location,
16317 "%<<:%> is an alternate spelling for %<[%>."
16318 " Insert whitespace between %<<%> and %<::%>");
16319 if (!hint && !flag_permissive)
16320 {
16321 inform (next_token->location, "(if you use %<-fpermissive%> "
16322 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16323 "accept your code)");
16324 hint = true;
16325 }
16326 }
16327 }
16328 else
16329 {
16330 /* Look for the `<' that starts the template-argument-list. */
16331 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16332 {
16333 pop_deferring_access_checks ();
16334 return error_mark_node;
16335 }
16336 /* Parse the arguments. */
16337 arguments = cp_parser_enclosed_template_argument_list (parser);
16338
16339 if ((cxx_dialect > cxx17)
16340 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16341 && !template_keyword_p
16342 && (cp_parser_error_occurred (parser)
16343 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16344 {
16345 /* This didn't go well. */
16346 if (TREE_CODE (templ) == FUNCTION_DECL)
16347 {
16348 /* C++2A says that "function-name < a;" is now ill-formed. */
16349 if (cp_parser_error_occurred (parser))
16350 {
16351 error_at (token->location, "invalid template-argument-list");
16352 inform (token->location, "function name as the left hand "
16353 "operand of %<<%> is ill-formed in C++2a; wrap the "
16354 "function name in %<()%>");
16355 }
16356 else
16357 /* We expect "f<targs>" to be followed by "(args)". */
16358 error_at (cp_lexer_peek_token (parser->lexer)->location,
16359 "expected %<(%> after template-argument-list");
16360 if (start_of_id)
16361 /* Purge all subsequent tokens. */
16362 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16363 }
16364 else
16365 cp_parser_simulate_error (parser);
16366 pop_deferring_access_checks ();
16367 return error_mark_node;
16368 }
16369 }
16370
16371 /* Set the location to be of the form:
16372 template-name < template-argument-list [opt] >
16373 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16374 with caret == start at the start of the template-name,
16375 ranging until the closing '>'. */
16376 location_t finish_loc
16377 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16378 location_t combined_loc
16379 = make_location (token->location, token->location, finish_loc);
16380
16381 /* Check for concepts autos where they don't belong. We could
16382 identify types in some cases of idnetifier TEMPL, looking ahead
16383 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16384 types. We reject them in functions, but if what we have is an
16385 identifier, even with none_type we can't conclude it's NOT a
16386 type, we have to wait for template substitution. */
16387 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16388 template_id = error_mark_node;
16389 /* Build a representation of the specialization. */
16390 else if (identifier_p (templ))
16391 template_id = build_min_nt_loc (combined_loc,
16392 TEMPLATE_ID_EXPR,
16393 templ, arguments);
16394 else if (DECL_TYPE_TEMPLATE_P (templ)
16395 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16396 {
16397 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16398 template (rather than some instantiation thereof) only if
16399 is not nested within some other construct. For example, in
16400 "template <typename T> void f(T) { A<T>::", A<T> is just an
16401 instantiation of A. */
16402 bool entering_scope
16403 = (template_parm_scope_p ()
16404 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16405 template_id
16406 = finish_template_type (templ, arguments, entering_scope);
16407 }
16408 /* A template-like identifier may be a partial concept id. */
16409 else if (flag_concepts
16410 && (template_id = (cp_parser_maybe_partial_concept_id
16411 (parser, templ, arguments))))
16412 return template_id;
16413 else if (variable_template_p (templ))
16414 {
16415 template_id = lookup_template_variable (templ, arguments);
16416 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16417 SET_EXPR_LOCATION (template_id, combined_loc);
16418 }
16419 else
16420 {
16421 /* If it's not a class-template or a template-template, it should be
16422 a function-template. */
16423 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16424 || TREE_CODE (templ) == OVERLOAD
16425 || TREE_CODE (templ) == FUNCTION_DECL
16426 || BASELINK_P (templ)));
16427
16428 template_id = lookup_template_function (templ, arguments);
16429 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16430 SET_EXPR_LOCATION (template_id, combined_loc);
16431 }
16432
16433 /* If parsing tentatively, replace the sequence of tokens that makes
16434 up the template-id with a CPP_TEMPLATE_ID token. That way,
16435 should we re-parse the token stream, we will not have to repeat
16436 the effort required to do the parse, nor will we issue duplicate
16437 error messages about problems during instantiation of the
16438 template. */
16439 if (start_of_id
16440 /* Don't do this if we had a parse error in a declarator; re-parsing
16441 might succeed if a name changes meaning (60361). */
16442 && !(cp_parser_error_occurred (parser)
16443 && cp_parser_parsing_tentatively (parser)
16444 && parser->in_declarator_p))
16445 {
16446 /* Reset the contents of the START_OF_ID token. */
16447 token->type = CPP_TEMPLATE_ID;
16448 token->location = combined_loc;
16449
16450 /* Retrieve any deferred checks. Do not pop this access checks yet
16451 so the memory will not be reclaimed during token replacing below. */
16452 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16453 token->u.tree_check_value->value = template_id;
16454 token->u.tree_check_value->checks = get_deferred_access_checks ();
16455 token->keyword = RID_MAX;
16456
16457 /* Purge all subsequent tokens. */
16458 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16459
16460 /* ??? Can we actually assume that, if template_id ==
16461 error_mark_node, we will have issued a diagnostic to the
16462 user, as opposed to simply marking the tentative parse as
16463 failed? */
16464 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16465 error_at (token->location, "parse error in template argument list");
16466 }
16467
16468 pop_to_parent_deferring_access_checks ();
16469 return template_id;
16470 }
16471
16472 /* Parse a template-name.
16473
16474 template-name:
16475 identifier
16476
16477 The standard should actually say:
16478
16479 template-name:
16480 identifier
16481 operator-function-id
16482
16483 A defect report has been filed about this issue.
16484
16485 A conversion-function-id cannot be a template name because they cannot
16486 be part of a template-id. In fact, looking at this code:
16487
16488 a.operator K<int>()
16489
16490 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16491 It is impossible to call a templated conversion-function-id with an
16492 explicit argument list, since the only allowed template parameter is
16493 the type to which it is converting.
16494
16495 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16496 `template' keyword, in a construction like:
16497
16498 T::template f<3>()
16499
16500 In that case `f' is taken to be a template-name, even though there
16501 is no way of knowing for sure.
16502
16503 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16504 name refers to a set of overloaded functions, at least one of which
16505 is a template, or an IDENTIFIER_NODE with the name of the template,
16506 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16507 names are looked up inside uninstantiated templates. */
16508
16509 static tree
16510 cp_parser_template_name (cp_parser* parser,
16511 bool template_keyword_p,
16512 bool check_dependency_p,
16513 bool is_declaration,
16514 enum tag_types tag_type,
16515 bool *is_identifier)
16516 {
16517 tree identifier;
16518 tree decl;
16519 cp_token *token = cp_lexer_peek_token (parser->lexer);
16520
16521 /* If the next token is `operator', then we have either an
16522 operator-function-id or a conversion-function-id. */
16523 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16524 {
16525 /* We don't know whether we're looking at an
16526 operator-function-id or a conversion-function-id. */
16527 cp_parser_parse_tentatively (parser);
16528 /* Try an operator-function-id. */
16529 identifier = cp_parser_operator_function_id (parser);
16530 /* If that didn't work, try a conversion-function-id. */
16531 if (!cp_parser_parse_definitely (parser))
16532 {
16533 cp_parser_error (parser, "expected template-name");
16534 return error_mark_node;
16535 }
16536 }
16537 /* Look for the identifier. */
16538 else
16539 identifier = cp_parser_identifier (parser);
16540
16541 /* If we didn't find an identifier, we don't have a template-id. */
16542 if (identifier == error_mark_node)
16543 return error_mark_node;
16544
16545 /* If the name immediately followed the `template' keyword, then it
16546 is a template-name. However, if the next token is not `<', then
16547 we do not treat it as a template-name, since it is not being used
16548 as part of a template-id. This enables us to handle constructs
16549 like:
16550
16551 template <typename T> struct S { S(); };
16552 template <typename T> S<T>::S();
16553
16554 correctly. We would treat `S' as a template -- if it were `S<T>'
16555 -- but we do not if there is no `<'. */
16556
16557 if (processing_template_decl
16558 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16559 {
16560 /* In a declaration, in a dependent context, we pretend that the
16561 "template" keyword was present in order to improve error
16562 recovery. For example, given:
16563
16564 template <typename T> void f(T::X<int>);
16565
16566 we want to treat "X<int>" as a template-id. */
16567 if (is_declaration
16568 && !template_keyword_p
16569 && parser->scope && TYPE_P (parser->scope)
16570 && check_dependency_p
16571 && dependent_scope_p (parser->scope)
16572 /* Do not do this for dtors (or ctors), since they never
16573 need the template keyword before their name. */
16574 && !constructor_name_p (identifier, parser->scope))
16575 {
16576 cp_token_position start = 0;
16577
16578 /* Explain what went wrong. */
16579 error_at (token->location, "non-template %qD used as template",
16580 identifier);
16581 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16582 parser->scope, identifier);
16583 /* If parsing tentatively, find the location of the "<" token. */
16584 if (cp_parser_simulate_error (parser))
16585 start = cp_lexer_token_position (parser->lexer, true);
16586 /* Parse the template arguments so that we can issue error
16587 messages about them. */
16588 cp_lexer_consume_token (parser->lexer);
16589 cp_parser_enclosed_template_argument_list (parser);
16590 /* Skip tokens until we find a good place from which to
16591 continue parsing. */
16592 cp_parser_skip_to_closing_parenthesis (parser,
16593 /*recovering=*/true,
16594 /*or_comma=*/true,
16595 /*consume_paren=*/false);
16596 /* If parsing tentatively, permanently remove the
16597 template argument list. That will prevent duplicate
16598 error messages from being issued about the missing
16599 "template" keyword. */
16600 if (start)
16601 cp_lexer_purge_tokens_after (parser->lexer, start);
16602 if (is_identifier)
16603 *is_identifier = true;
16604 parser->context->object_type = NULL_TREE;
16605 return identifier;
16606 }
16607
16608 /* If the "template" keyword is present, then there is generally
16609 no point in doing name-lookup, so we just return IDENTIFIER.
16610 But, if the qualifying scope is non-dependent then we can
16611 (and must) do name-lookup normally. */
16612 if (template_keyword_p)
16613 {
16614 tree scope = (parser->scope ? parser->scope
16615 : parser->context->object_type);
16616 if (scope && TYPE_P (scope)
16617 && (!CLASS_TYPE_P (scope)
16618 || (check_dependency_p && dependent_type_p (scope))))
16619 {
16620 /* We're optimizing away the call to cp_parser_lookup_name, but
16621 we still need to do this. */
16622 parser->context->object_type = NULL_TREE;
16623 return identifier;
16624 }
16625 }
16626 }
16627
16628 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16629 const bool scoped_p = ((parser->scope ? parser->scope
16630 : parser->context->object_type) != NULL_TREE);
16631
16632 /* Look up the name. */
16633 decl = cp_parser_lookup_name (parser, identifier,
16634 tag_type,
16635 /*is_template=*/true,
16636 /*is_namespace=*/false,
16637 check_dependency_p,
16638 /*ambiguous_decls=*/NULL,
16639 token->location);
16640
16641 decl = strip_using_decl (decl);
16642
16643 /* If DECL is a template, then the name was a template-name. */
16644 if (TREE_CODE (decl) == TEMPLATE_DECL)
16645 {
16646 if (TREE_DEPRECATED (decl)
16647 && deprecated_state != DEPRECATED_SUPPRESS)
16648 warn_deprecated_use (decl, NULL_TREE);
16649 }
16650 else
16651 {
16652 /* The standard does not explicitly indicate whether a name that
16653 names a set of overloaded declarations, some of which are
16654 templates, is a template-name. However, such a name should
16655 be a template-name; otherwise, there is no way to form a
16656 template-id for the overloaded templates. */
16657 bool found = false;
16658
16659 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16660 !found && iter; ++iter)
16661 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16662 found = true;
16663
16664 if (!found
16665 && (cxx_dialect > cxx17)
16666 && !scoped_p
16667 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
16668 && tag_type == none_type)
16669 {
16670 /* [temp.names] says "A name is also considered to refer to a template
16671 if it is an unqualified-id followed by a < and name lookup finds
16672 either one or more functions or finds nothing." */
16673
16674 /* The "more functions" case. Just use the OVERLOAD as normally.
16675 We don't use is_overloaded_fn here to avoid considering
16676 BASELINKs. */
16677 if (TREE_CODE (decl) == OVERLOAD
16678 /* Name lookup found one function. */
16679 || TREE_CODE (decl) == FUNCTION_DECL)
16680 found = true;
16681 /* Name lookup found nothing. */
16682 else if (decl == error_mark_node)
16683 return identifier;
16684 }
16685
16686 if (!found)
16687 {
16688 /* The name does not name a template. */
16689 cp_parser_error (parser, "expected template-name");
16690 return error_mark_node;
16691 }
16692 }
16693
16694 return decl;
16695 }
16696
16697 /* Parse a template-argument-list.
16698
16699 template-argument-list:
16700 template-argument ... [opt]
16701 template-argument-list , template-argument ... [opt]
16702
16703 Returns a TREE_VEC containing the arguments. */
16704
16705 static tree
16706 cp_parser_template_argument_list (cp_parser* parser)
16707 {
16708 tree fixed_args[10];
16709 unsigned n_args = 0;
16710 unsigned alloced = 10;
16711 tree *arg_ary = fixed_args;
16712 tree vec;
16713 bool saved_in_template_argument_list_p;
16714 bool saved_ice_p;
16715 bool saved_non_ice_p;
16716
16717 /* Don't create location wrapper nodes within a template-argument-list. */
16718 auto_suppress_location_wrappers sentinel;
16719
16720 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16721 parser->in_template_argument_list_p = true;
16722 /* Even if the template-id appears in an integral
16723 constant-expression, the contents of the argument list do
16724 not. */
16725 saved_ice_p = parser->integral_constant_expression_p;
16726 parser->integral_constant_expression_p = false;
16727 saved_non_ice_p = parser->non_integral_constant_expression_p;
16728 parser->non_integral_constant_expression_p = false;
16729
16730 /* Parse the arguments. */
16731 do
16732 {
16733 tree argument;
16734
16735 if (n_args)
16736 /* Consume the comma. */
16737 cp_lexer_consume_token (parser->lexer);
16738
16739 /* Parse the template-argument. */
16740 argument = cp_parser_template_argument (parser);
16741
16742 /* If the next token is an ellipsis, we're expanding a template
16743 argument pack. */
16744 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16745 {
16746 if (argument == error_mark_node)
16747 {
16748 cp_token *token = cp_lexer_peek_token (parser->lexer);
16749 error_at (token->location,
16750 "expected parameter pack before %<...%>");
16751 }
16752 /* Consume the `...' token. */
16753 cp_lexer_consume_token (parser->lexer);
16754
16755 /* Make the argument into a TYPE_PACK_EXPANSION or
16756 EXPR_PACK_EXPANSION. */
16757 argument = make_pack_expansion (argument);
16758 }
16759
16760 if (n_args == alloced)
16761 {
16762 alloced *= 2;
16763
16764 if (arg_ary == fixed_args)
16765 {
16766 arg_ary = XNEWVEC (tree, alloced);
16767 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16768 }
16769 else
16770 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16771 }
16772 arg_ary[n_args++] = argument;
16773 }
16774 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16775
16776 vec = make_tree_vec (n_args);
16777
16778 while (n_args--)
16779 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16780
16781 if (arg_ary != fixed_args)
16782 free (arg_ary);
16783 parser->non_integral_constant_expression_p = saved_non_ice_p;
16784 parser->integral_constant_expression_p = saved_ice_p;
16785 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16786 if (CHECKING_P)
16787 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16788 return vec;
16789 }
16790
16791 /* Parse a template-argument.
16792
16793 template-argument:
16794 assignment-expression
16795 type-id
16796 id-expression
16797
16798 The representation is that of an assignment-expression, type-id, or
16799 id-expression -- except that the qualified id-expression is
16800 evaluated, so that the value returned is either a DECL or an
16801 OVERLOAD.
16802
16803 Although the standard says "assignment-expression", it forbids
16804 throw-expressions or assignments in the template argument.
16805 Therefore, we use "conditional-expression" instead. */
16806
16807 static tree
16808 cp_parser_template_argument (cp_parser* parser)
16809 {
16810 tree argument;
16811 bool template_p;
16812 bool address_p;
16813 bool maybe_type_id = false;
16814 cp_token *token = NULL, *argument_start_token = NULL;
16815 location_t loc = 0;
16816 cp_id_kind idk;
16817
16818 /* There's really no way to know what we're looking at, so we just
16819 try each alternative in order.
16820
16821 [temp.arg]
16822
16823 In a template-argument, an ambiguity between a type-id and an
16824 expression is resolved to a type-id, regardless of the form of
16825 the corresponding template-parameter.
16826
16827 Therefore, we try a type-id first. */
16828 cp_parser_parse_tentatively (parser);
16829 argument = cp_parser_template_type_arg (parser);
16830 /* If there was no error parsing the type-id but the next token is a
16831 '>>', our behavior depends on which dialect of C++ we're
16832 parsing. In C++98, we probably found a typo for '> >'. But there
16833 are type-id which are also valid expressions. For instance:
16834
16835 struct X { int operator >> (int); };
16836 template <int V> struct Foo {};
16837 Foo<X () >> 5> r;
16838
16839 Here 'X()' is a valid type-id of a function type, but the user just
16840 wanted to write the expression "X() >> 5". Thus, we remember that we
16841 found a valid type-id, but we still try to parse the argument as an
16842 expression to see what happens.
16843
16844 In C++0x, the '>>' will be considered two separate '>'
16845 tokens. */
16846 if (!cp_parser_error_occurred (parser)
16847 && cxx_dialect == cxx98
16848 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16849 {
16850 maybe_type_id = true;
16851 cp_parser_abort_tentative_parse (parser);
16852 }
16853 else
16854 {
16855 /* If the next token isn't a `,' or a `>', then this argument wasn't
16856 really finished. This means that the argument is not a valid
16857 type-id. */
16858 if (!cp_parser_next_token_ends_template_argument_p (parser))
16859 cp_parser_error (parser, "expected template-argument");
16860 /* If that worked, we're done. */
16861 if (cp_parser_parse_definitely (parser))
16862 return argument;
16863 }
16864 /* We're still not sure what the argument will be. */
16865 cp_parser_parse_tentatively (parser);
16866 /* Try a template. */
16867 argument_start_token = cp_lexer_peek_token (parser->lexer);
16868 argument = cp_parser_id_expression (parser,
16869 /*template_keyword_p=*/false,
16870 /*check_dependency_p=*/true,
16871 &template_p,
16872 /*declarator_p=*/false,
16873 /*optional_p=*/false);
16874 /* If the next token isn't a `,' or a `>', then this argument wasn't
16875 really finished. */
16876 if (!cp_parser_next_token_ends_template_argument_p (parser))
16877 cp_parser_error (parser, "expected template-argument");
16878 if (!cp_parser_error_occurred (parser))
16879 {
16880 /* Figure out what is being referred to. If the id-expression
16881 was for a class template specialization, then we will have a
16882 TYPE_DECL at this point. There is no need to do name lookup
16883 at this point in that case. */
16884 if (TREE_CODE (argument) != TYPE_DECL)
16885 argument = cp_parser_lookup_name (parser, argument,
16886 none_type,
16887 /*is_template=*/template_p,
16888 /*is_namespace=*/false,
16889 /*check_dependency=*/true,
16890 /*ambiguous_decls=*/NULL,
16891 argument_start_token->location);
16892 /* Handle a constrained-type-specifier for a non-type template
16893 parameter. */
16894 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16895 argument = decl;
16896 else if (TREE_CODE (argument) != TEMPLATE_DECL
16897 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16898 cp_parser_error (parser, "expected template-name");
16899 }
16900 if (cp_parser_parse_definitely (parser))
16901 {
16902 if (TREE_DEPRECATED (argument))
16903 warn_deprecated_use (argument, NULL_TREE);
16904 return argument;
16905 }
16906 /* It must be a non-type argument. In C++17 any constant-expression is
16907 allowed. */
16908 if (cxx_dialect > cxx14)
16909 goto general_expr;
16910
16911 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16912
16913 -- an integral constant-expression of integral or enumeration
16914 type; or
16915
16916 -- the name of a non-type template-parameter; or
16917
16918 -- the name of an object or function with external linkage...
16919
16920 -- the address of an object or function with external linkage...
16921
16922 -- a pointer to member... */
16923 /* Look for a non-type template parameter. */
16924 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16925 {
16926 cp_parser_parse_tentatively (parser);
16927 argument = cp_parser_primary_expression (parser,
16928 /*address_p=*/false,
16929 /*cast_p=*/false,
16930 /*template_arg_p=*/true,
16931 &idk);
16932 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16933 || !cp_parser_next_token_ends_template_argument_p (parser))
16934 cp_parser_simulate_error (parser);
16935 if (cp_parser_parse_definitely (parser))
16936 return argument;
16937 }
16938
16939 /* If the next token is "&", the argument must be the address of an
16940 object or function with external linkage. */
16941 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16942 if (address_p)
16943 {
16944 loc = cp_lexer_peek_token (parser->lexer)->location;
16945 cp_lexer_consume_token (parser->lexer);
16946 }
16947 /* See if we might have an id-expression. */
16948 token = cp_lexer_peek_token (parser->lexer);
16949 if (token->type == CPP_NAME
16950 || token->keyword == RID_OPERATOR
16951 || token->type == CPP_SCOPE
16952 || token->type == CPP_TEMPLATE_ID
16953 || token->type == CPP_NESTED_NAME_SPECIFIER)
16954 {
16955 cp_parser_parse_tentatively (parser);
16956 argument = cp_parser_primary_expression (parser,
16957 address_p,
16958 /*cast_p=*/false,
16959 /*template_arg_p=*/true,
16960 &idk);
16961 if (cp_parser_error_occurred (parser)
16962 || !cp_parser_next_token_ends_template_argument_p (parser))
16963 cp_parser_abort_tentative_parse (parser);
16964 else
16965 {
16966 tree probe;
16967
16968 if (INDIRECT_REF_P (argument))
16969 {
16970 /* Strip the dereference temporarily. */
16971 gcc_assert (REFERENCE_REF_P (argument));
16972 argument = TREE_OPERAND (argument, 0);
16973 }
16974
16975 /* If we're in a template, we represent a qualified-id referring
16976 to a static data member as a SCOPE_REF even if the scope isn't
16977 dependent so that we can check access control later. */
16978 probe = argument;
16979 if (TREE_CODE (probe) == SCOPE_REF)
16980 probe = TREE_OPERAND (probe, 1);
16981 if (VAR_P (probe))
16982 {
16983 /* A variable without external linkage might still be a
16984 valid constant-expression, so no error is issued here
16985 if the external-linkage check fails. */
16986 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16987 cp_parser_simulate_error (parser);
16988 }
16989 else if (is_overloaded_fn (argument))
16990 /* All overloaded functions are allowed; if the external
16991 linkage test does not pass, an error will be issued
16992 later. */
16993 ;
16994 else if (address_p
16995 && (TREE_CODE (argument) == OFFSET_REF
16996 || TREE_CODE (argument) == SCOPE_REF))
16997 /* A pointer-to-member. */
16998 ;
16999 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
17000 ;
17001 else
17002 cp_parser_simulate_error (parser);
17003
17004 if (cp_parser_parse_definitely (parser))
17005 {
17006 if (address_p)
17007 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
17008 tf_warning_or_error);
17009 else
17010 argument = convert_from_reference (argument);
17011 return argument;
17012 }
17013 }
17014 }
17015 /* If the argument started with "&", there are no other valid
17016 alternatives at this point. */
17017 if (address_p)
17018 {
17019 cp_parser_error (parser, "invalid non-type template argument");
17020 return error_mark_node;
17021 }
17022
17023 general_expr:
17024 /* If the argument wasn't successfully parsed as a type-id followed
17025 by '>>', the argument can only be a constant expression now.
17026 Otherwise, we try parsing the constant-expression tentatively,
17027 because the argument could really be a type-id. */
17028 if (maybe_type_id)
17029 cp_parser_parse_tentatively (parser);
17030
17031 if (cxx_dialect <= cxx14)
17032 argument = cp_parser_constant_expression (parser);
17033 else
17034 {
17035 /* In C++20, we can encounter a braced-init-list. */
17036 if (cxx_dialect >= cxx2a
17037 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17038 {
17039 bool expr_non_constant_p;
17040 return cp_parser_braced_list (parser, &expr_non_constant_p);
17041 }
17042
17043 /* With C++17 generalized non-type template arguments we need to handle
17044 lvalue constant expressions, too. */
17045 argument = cp_parser_assignment_expression (parser);
17046 require_potential_constant_expression (argument);
17047 }
17048
17049 if (!maybe_type_id)
17050 return argument;
17051 if (!cp_parser_next_token_ends_template_argument_p (parser))
17052 cp_parser_error (parser, "expected template-argument");
17053 if (cp_parser_parse_definitely (parser))
17054 return argument;
17055 /* We did our best to parse the argument as a non type-id, but that
17056 was the only alternative that matched (albeit with a '>' after
17057 it). We can assume it's just a typo from the user, and a
17058 diagnostic will then be issued. */
17059 return cp_parser_template_type_arg (parser);
17060 }
17061
17062 /* Parse an explicit-instantiation.
17063
17064 explicit-instantiation:
17065 template declaration
17066
17067 Although the standard says `declaration', what it really means is:
17068
17069 explicit-instantiation:
17070 template decl-specifier-seq [opt] declarator [opt] ;
17071
17072 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17073 supposed to be allowed. A defect report has been filed about this
17074 issue.
17075
17076 GNU Extension:
17077
17078 explicit-instantiation:
17079 storage-class-specifier template
17080 decl-specifier-seq [opt] declarator [opt] ;
17081 function-specifier template
17082 decl-specifier-seq [opt] declarator [opt] ; */
17083
17084 static void
17085 cp_parser_explicit_instantiation (cp_parser* parser)
17086 {
17087 int declares_class_or_enum;
17088 cp_decl_specifier_seq decl_specifiers;
17089 tree extension_specifier = NULL_TREE;
17090
17091 timevar_push (TV_TEMPLATE_INST);
17092
17093 /* Look for an (optional) storage-class-specifier or
17094 function-specifier. */
17095 if (cp_parser_allow_gnu_extensions_p (parser))
17096 {
17097 extension_specifier
17098 = cp_parser_storage_class_specifier_opt (parser);
17099 if (!extension_specifier)
17100 extension_specifier
17101 = cp_parser_function_specifier_opt (parser,
17102 /*decl_specs=*/NULL);
17103 }
17104
17105 /* Look for the `template' keyword. */
17106 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17107 /* Let the front end know that we are processing an explicit
17108 instantiation. */
17109 begin_explicit_instantiation ();
17110 /* [temp.explicit] says that we are supposed to ignore access
17111 control while processing explicit instantiation directives. */
17112 push_deferring_access_checks (dk_no_check);
17113 /* Parse a decl-specifier-seq. */
17114 cp_parser_decl_specifier_seq (parser,
17115 CP_PARSER_FLAGS_OPTIONAL,
17116 &decl_specifiers,
17117 &declares_class_or_enum);
17118 /* If there was exactly one decl-specifier, and it declared a class,
17119 and there's no declarator, then we have an explicit type
17120 instantiation. */
17121 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
17122 {
17123 tree type;
17124
17125 type = check_tag_decl (&decl_specifiers,
17126 /*explicit_type_instantiation_p=*/true);
17127 /* Turn access control back on for names used during
17128 template instantiation. */
17129 pop_deferring_access_checks ();
17130 if (type)
17131 do_type_instantiation (type, extension_specifier,
17132 /*complain=*/tf_error);
17133 }
17134 else
17135 {
17136 cp_declarator *declarator;
17137 tree decl;
17138
17139 /* Parse the declarator. */
17140 declarator
17141 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17142 CP_PARSER_FLAGS_NONE,
17143 /*ctor_dtor_or_conv_p=*/NULL,
17144 /*parenthesized_p=*/NULL,
17145 /*member_p=*/false,
17146 /*friend_p=*/false,
17147 /*static_p=*/false);
17148 if (declares_class_or_enum & 2)
17149 cp_parser_check_for_definition_in_return_type (declarator,
17150 decl_specifiers.type,
17151 decl_specifiers.locations[ds_type_spec]);
17152 if (declarator != cp_error_declarator)
17153 {
17154 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
17155 permerror (decl_specifiers.locations[ds_inline],
17156 "explicit instantiation shall not use"
17157 " %<inline%> specifier");
17158 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
17159 permerror (decl_specifiers.locations[ds_constexpr],
17160 "explicit instantiation shall not use"
17161 " %<constexpr%> specifier");
17162
17163 decl = grokdeclarator (declarator, &decl_specifiers,
17164 NORMAL, 0, &decl_specifiers.attributes);
17165 /* Turn access control back on for names used during
17166 template instantiation. */
17167 pop_deferring_access_checks ();
17168 /* Do the explicit instantiation. */
17169 do_decl_instantiation (decl, extension_specifier);
17170 }
17171 else
17172 {
17173 pop_deferring_access_checks ();
17174 /* Skip the body of the explicit instantiation. */
17175 cp_parser_skip_to_end_of_statement (parser);
17176 }
17177 }
17178 /* We're done with the instantiation. */
17179 end_explicit_instantiation ();
17180
17181 cp_parser_consume_semicolon_at_end_of_statement (parser);
17182
17183 timevar_pop (TV_TEMPLATE_INST);
17184 }
17185
17186 /* Parse an explicit-specialization.
17187
17188 explicit-specialization:
17189 template < > declaration
17190
17191 Although the standard says `declaration', what it really means is:
17192
17193 explicit-specialization:
17194 template <> decl-specifier [opt] init-declarator [opt] ;
17195 template <> function-definition
17196 template <> explicit-specialization
17197 template <> template-declaration */
17198
17199 static void
17200 cp_parser_explicit_specialization (cp_parser* parser)
17201 {
17202 bool need_lang_pop;
17203 cp_token *token = cp_lexer_peek_token (parser->lexer);
17204
17205 /* Look for the `template' keyword. */
17206 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17207 /* Look for the `<'. */
17208 cp_parser_require (parser, CPP_LESS, RT_LESS);
17209 /* Look for the `>'. */
17210 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17211 /* We have processed another parameter list. */
17212 ++parser->num_template_parameter_lists;
17213 /* [temp]
17214
17215 A template ... explicit specialization ... shall not have C
17216 linkage. */
17217 if (current_lang_name == lang_name_c)
17218 {
17219 error_at (token->location, "template specialization with C linkage");
17220 maybe_show_extern_c_location ();
17221 /* Give it C++ linkage to avoid confusing other parts of the
17222 front end. */
17223 push_lang_context (lang_name_cplusplus);
17224 need_lang_pop = true;
17225 }
17226 else
17227 need_lang_pop = false;
17228 /* Let the front end know that we are beginning a specialization. */
17229 if (!begin_specialization ())
17230 {
17231 end_specialization ();
17232 return;
17233 }
17234
17235 /* If the next keyword is `template', we need to figure out whether
17236 or not we're looking a template-declaration. */
17237 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17238 {
17239 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17240 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
17241 cp_parser_template_declaration_after_export (parser,
17242 /*member_p=*/false);
17243 else
17244 cp_parser_explicit_specialization (parser);
17245 }
17246 else
17247 /* Parse the dependent declaration. */
17248 cp_parser_single_declaration (parser,
17249 /*checks=*/NULL,
17250 /*member_p=*/false,
17251 /*explicit_specialization_p=*/true,
17252 /*friend_p=*/NULL);
17253 /* We're done with the specialization. */
17254 end_specialization ();
17255 /* For the erroneous case of a template with C linkage, we pushed an
17256 implicit C++ linkage scope; exit that scope now. */
17257 if (need_lang_pop)
17258 pop_lang_context ();
17259 /* We're done with this parameter list. */
17260 --parser->num_template_parameter_lists;
17261 }
17262
17263 /* Parse a type-specifier.
17264
17265 type-specifier:
17266 simple-type-specifier
17267 class-specifier
17268 enum-specifier
17269 elaborated-type-specifier
17270 cv-qualifier
17271
17272 GNU Extension:
17273
17274 type-specifier:
17275 __complex__
17276
17277 Returns a representation of the type-specifier. For a
17278 class-specifier, enum-specifier, or elaborated-type-specifier, a
17279 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17280
17281 The parser flags FLAGS is used to control type-specifier parsing.
17282
17283 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17284 in a decl-specifier-seq.
17285
17286 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17287 class-specifier, enum-specifier, or elaborated-type-specifier, then
17288 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17289 if a type is declared; 2 if it is defined. Otherwise, it is set to
17290 zero.
17291
17292 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17293 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17294 is set to FALSE. */
17295
17296 static tree
17297 cp_parser_type_specifier (cp_parser* parser,
17298 cp_parser_flags flags,
17299 cp_decl_specifier_seq *decl_specs,
17300 bool is_declaration,
17301 int* declares_class_or_enum,
17302 bool* is_cv_qualifier)
17303 {
17304 tree type_spec = NULL_TREE;
17305 cp_token *token;
17306 enum rid keyword;
17307 cp_decl_spec ds = ds_last;
17308
17309 /* Assume this type-specifier does not declare a new type. */
17310 if (declares_class_or_enum)
17311 *declares_class_or_enum = 0;
17312 /* And that it does not specify a cv-qualifier. */
17313 if (is_cv_qualifier)
17314 *is_cv_qualifier = false;
17315 /* Peek at the next token. */
17316 token = cp_lexer_peek_token (parser->lexer);
17317
17318 /* If we're looking at a keyword, we can use that to guide the
17319 production we choose. */
17320 keyword = token->keyword;
17321 switch (keyword)
17322 {
17323 case RID_ENUM:
17324 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17325 goto elaborated_type_specifier;
17326
17327 /* Look for the enum-specifier. */
17328 type_spec = cp_parser_enum_specifier (parser);
17329 /* If that worked, we're done. */
17330 if (type_spec)
17331 {
17332 if (declares_class_or_enum)
17333 *declares_class_or_enum = 2;
17334 if (decl_specs)
17335 cp_parser_set_decl_spec_type (decl_specs,
17336 type_spec,
17337 token,
17338 /*type_definition_p=*/true);
17339 return type_spec;
17340 }
17341 else
17342 goto elaborated_type_specifier;
17343
17344 /* Any of these indicate either a class-specifier, or an
17345 elaborated-type-specifier. */
17346 case RID_CLASS:
17347 case RID_STRUCT:
17348 case RID_UNION:
17349 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17350 goto elaborated_type_specifier;
17351
17352 /* Parse tentatively so that we can back up if we don't find a
17353 class-specifier. */
17354 cp_parser_parse_tentatively (parser);
17355 /* Look for the class-specifier. */
17356 type_spec = cp_parser_class_specifier (parser);
17357 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17358 /* If that worked, we're done. */
17359 if (cp_parser_parse_definitely (parser))
17360 {
17361 if (declares_class_or_enum)
17362 *declares_class_or_enum = 2;
17363 if (decl_specs)
17364 cp_parser_set_decl_spec_type (decl_specs,
17365 type_spec,
17366 token,
17367 /*type_definition_p=*/true);
17368 return type_spec;
17369 }
17370
17371 /* Fall through. */
17372 elaborated_type_specifier:
17373 /* We're declaring (not defining) a class or enum. */
17374 if (declares_class_or_enum)
17375 *declares_class_or_enum = 1;
17376
17377 /* Fall through. */
17378 case RID_TYPENAME:
17379 /* Look for an elaborated-type-specifier. */
17380 type_spec
17381 = (cp_parser_elaborated_type_specifier
17382 (parser,
17383 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17384 is_declaration));
17385 if (decl_specs)
17386 cp_parser_set_decl_spec_type (decl_specs,
17387 type_spec,
17388 token,
17389 /*type_definition_p=*/false);
17390 return type_spec;
17391
17392 case RID_CONST:
17393 ds = ds_const;
17394 if (is_cv_qualifier)
17395 *is_cv_qualifier = true;
17396 break;
17397
17398 case RID_VOLATILE:
17399 ds = ds_volatile;
17400 if (is_cv_qualifier)
17401 *is_cv_qualifier = true;
17402 break;
17403
17404 case RID_RESTRICT:
17405 ds = ds_restrict;
17406 if (is_cv_qualifier)
17407 *is_cv_qualifier = true;
17408 break;
17409
17410 case RID_COMPLEX:
17411 /* The `__complex__' keyword is a GNU extension. */
17412 ds = ds_complex;
17413 break;
17414
17415 default:
17416 break;
17417 }
17418
17419 /* Handle simple keywords. */
17420 if (ds != ds_last)
17421 {
17422 if (decl_specs)
17423 {
17424 set_and_check_decl_spec_loc (decl_specs, ds, token);
17425 decl_specs->any_specifiers_p = true;
17426 }
17427 return cp_lexer_consume_token (parser->lexer)->u.value;
17428 }
17429
17430 /* If we do not already have a type-specifier, assume we are looking
17431 at a simple-type-specifier. */
17432 type_spec = cp_parser_simple_type_specifier (parser,
17433 decl_specs,
17434 flags);
17435
17436 /* If we didn't find a type-specifier, and a type-specifier was not
17437 optional in this context, issue an error message. */
17438 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17439 {
17440 cp_parser_error (parser, "expected type specifier");
17441 return error_mark_node;
17442 }
17443
17444 return type_spec;
17445 }
17446
17447 /* Parse a simple-type-specifier.
17448
17449 simple-type-specifier:
17450 :: [opt] nested-name-specifier [opt] type-name
17451 :: [opt] nested-name-specifier template template-id
17452 char
17453 wchar_t
17454 bool
17455 short
17456 int
17457 long
17458 signed
17459 unsigned
17460 float
17461 double
17462 void
17463
17464 C++11 Extension:
17465
17466 simple-type-specifier:
17467 auto
17468 decltype ( expression )
17469 char16_t
17470 char32_t
17471 __underlying_type ( type-id )
17472
17473 C++17 extension:
17474
17475 nested-name-specifier(opt) template-name
17476
17477 GNU Extension:
17478
17479 simple-type-specifier:
17480 __int128
17481 __typeof__ unary-expression
17482 __typeof__ ( type-id )
17483 __typeof__ ( type-id ) { initializer-list , [opt] }
17484
17485 Concepts Extension:
17486
17487 simple-type-specifier:
17488 constrained-type-specifier
17489
17490 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17491 appropriately updated. */
17492
17493 static tree
17494 cp_parser_simple_type_specifier (cp_parser* parser,
17495 cp_decl_specifier_seq *decl_specs,
17496 cp_parser_flags flags)
17497 {
17498 tree type = NULL_TREE;
17499 cp_token *token;
17500 int idx;
17501
17502 /* Peek at the next token. */
17503 token = cp_lexer_peek_token (parser->lexer);
17504
17505 /* If we're looking at a keyword, things are easy. */
17506 switch (token->keyword)
17507 {
17508 case RID_CHAR:
17509 if (decl_specs)
17510 decl_specs->explicit_char_p = true;
17511 type = char_type_node;
17512 break;
17513 case RID_CHAR8:
17514 type = char8_type_node;
17515 break;
17516 case RID_CHAR16:
17517 type = char16_type_node;
17518 break;
17519 case RID_CHAR32:
17520 type = char32_type_node;
17521 break;
17522 case RID_WCHAR:
17523 type = wchar_type_node;
17524 break;
17525 case RID_BOOL:
17526 type = boolean_type_node;
17527 break;
17528 case RID_SHORT:
17529 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17530 type = short_integer_type_node;
17531 break;
17532 case RID_INT:
17533 if (decl_specs)
17534 decl_specs->explicit_int_p = true;
17535 type = integer_type_node;
17536 break;
17537 case RID_INT_N_0:
17538 case RID_INT_N_1:
17539 case RID_INT_N_2:
17540 case RID_INT_N_3:
17541 idx = token->keyword - RID_INT_N_0;
17542 if (! int_n_enabled_p [idx])
17543 break;
17544 if (decl_specs)
17545 {
17546 decl_specs->explicit_intN_p = true;
17547 decl_specs->int_n_idx = idx;
17548 }
17549 type = int_n_trees [idx].signed_type;
17550 break;
17551 case RID_LONG:
17552 if (decl_specs)
17553 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17554 type = long_integer_type_node;
17555 break;
17556 case RID_SIGNED:
17557 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17558 type = integer_type_node;
17559 break;
17560 case RID_UNSIGNED:
17561 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17562 type = unsigned_type_node;
17563 break;
17564 case RID_FLOAT:
17565 type = float_type_node;
17566 break;
17567 case RID_DOUBLE:
17568 type = double_type_node;
17569 break;
17570 case RID_VOID:
17571 type = void_type_node;
17572 break;
17573
17574 case RID_AUTO:
17575 maybe_warn_cpp0x (CPP0X_AUTO);
17576 if (parser->auto_is_implicit_function_template_parm_p)
17577 {
17578 /* The 'auto' might be the placeholder return type for a function decl
17579 with trailing return type. */
17580 bool have_trailing_return_fn_decl = false;
17581
17582 cp_parser_parse_tentatively (parser);
17583 cp_lexer_consume_token (parser->lexer);
17584 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17585 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17586 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17587 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17588 {
17589 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17590 {
17591 cp_lexer_consume_token (parser->lexer);
17592 cp_parser_skip_to_closing_parenthesis (parser,
17593 /*recovering*/false,
17594 /*or_comma*/false,
17595 /*consume_paren*/true);
17596 continue;
17597 }
17598
17599 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17600 {
17601 have_trailing_return_fn_decl = true;
17602 break;
17603 }
17604
17605 cp_lexer_consume_token (parser->lexer);
17606 }
17607 cp_parser_abort_tentative_parse (parser);
17608
17609 if (have_trailing_return_fn_decl)
17610 {
17611 type = make_auto ();
17612 break;
17613 }
17614
17615 if (cxx_dialect >= cxx14)
17616 {
17617 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17618 type = TREE_TYPE (type);
17619 }
17620 else
17621 type = error_mark_node;
17622
17623 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17624 {
17625 if (cxx_dialect < cxx14)
17626 error_at (token->location,
17627 "use of %<auto%> in lambda parameter declaration "
17628 "only available with "
17629 "-std=c++14 or -std=gnu++14");
17630 }
17631 else if (cxx_dialect < cxx14)
17632 error_at (token->location,
17633 "use of %<auto%> in parameter declaration "
17634 "only available with "
17635 "-std=c++14 or -std=gnu++14");
17636 else if (!flag_concepts)
17637 pedwarn (token->location, 0,
17638 "use of %<auto%> in parameter declaration "
17639 "only available with -fconcepts");
17640 }
17641 else
17642 type = make_auto ();
17643 break;
17644
17645 case RID_DECLTYPE:
17646 /* Since DR 743, decltype can either be a simple-type-specifier by
17647 itself or begin a nested-name-specifier. Parsing it will replace
17648 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17649 handling below decide what to do. */
17650 cp_parser_decltype (parser);
17651 cp_lexer_set_token_position (parser->lexer, token);
17652 break;
17653
17654 case RID_TYPEOF:
17655 /* Consume the `typeof' token. */
17656 cp_lexer_consume_token (parser->lexer);
17657 /* Parse the operand to `typeof'. */
17658 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17659 /* If it is not already a TYPE, take its type. */
17660 if (!TYPE_P (type))
17661 type = finish_typeof (type);
17662
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_UNDERLYING_TYPE:
17671 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17672 if (decl_specs)
17673 cp_parser_set_decl_spec_type (decl_specs, type,
17674 token,
17675 /*type_definition_p=*/false);
17676
17677 return type;
17678
17679 case RID_BASES:
17680 case RID_DIRECT_BASES:
17681 type = cp_parser_trait_expr (parser, token->keyword);
17682 if (decl_specs)
17683 cp_parser_set_decl_spec_type (decl_specs, type,
17684 token,
17685 /*type_definition_p=*/false);
17686 return type;
17687 default:
17688 break;
17689 }
17690
17691 /* If token is an already-parsed decltype not followed by ::,
17692 it's a simple-type-specifier. */
17693 if (token->type == CPP_DECLTYPE
17694 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17695 {
17696 type = saved_checks_value (token->u.tree_check_value);
17697 if (decl_specs)
17698 {
17699 cp_parser_set_decl_spec_type (decl_specs, type,
17700 token,
17701 /*type_definition_p=*/false);
17702 /* Remember that we are handling a decltype in order to
17703 implement the resolution of DR 1510 when the argument
17704 isn't instantiation dependent. */
17705 decl_specs->decltype_p = true;
17706 }
17707 cp_lexer_consume_token (parser->lexer);
17708 return type;
17709 }
17710
17711 /* If the type-specifier was for a built-in type, we're done. */
17712 if (type)
17713 {
17714 /* Record the type. */
17715 if (decl_specs
17716 && (token->keyword != RID_SIGNED
17717 && token->keyword != RID_UNSIGNED
17718 && token->keyword != RID_SHORT
17719 && token->keyword != RID_LONG))
17720 cp_parser_set_decl_spec_type (decl_specs,
17721 type,
17722 token,
17723 /*type_definition_p=*/false);
17724 if (decl_specs)
17725 decl_specs->any_specifiers_p = true;
17726
17727 /* Consume the token. */
17728 cp_lexer_consume_token (parser->lexer);
17729
17730 if (type == error_mark_node)
17731 return error_mark_node;
17732
17733 /* There is no valid C++ program where a non-template type is
17734 followed by a "<". That usually indicates that the user thought
17735 that the type was a template. */
17736 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17737 token->location);
17738
17739 return TYPE_NAME (type);
17740 }
17741
17742 /* The type-specifier must be a user-defined type. */
17743 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17744 {
17745 bool qualified_p;
17746 bool global_p;
17747 const bool typename_p = (cxx_dialect >= cxx2a
17748 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
17749
17750 /* Don't gobble tokens or issue error messages if this is an
17751 optional type-specifier. */
17752 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17753 cp_parser_parse_tentatively (parser);
17754
17755 token = cp_lexer_peek_token (parser->lexer);
17756
17757 /* Look for the optional `::' operator. */
17758 global_p
17759 = (cp_parser_global_scope_opt (parser,
17760 /*current_scope_valid_p=*/false)
17761 != NULL_TREE);
17762 /* Look for the nested-name specifier. */
17763 qualified_p
17764 = (cp_parser_nested_name_specifier_opt (parser,
17765 /*typename_keyword_p=*/false,
17766 /*check_dependency_p=*/true,
17767 /*type_p=*/false,
17768 /*is_declaration=*/false)
17769 != NULL_TREE);
17770 /* If we have seen a nested-name-specifier, and the next token
17771 is `template', then we are using the template-id production. */
17772 if (parser->scope
17773 && cp_parser_optional_template_keyword (parser))
17774 {
17775 /* Look for the template-id. */
17776 type = cp_parser_template_id (parser,
17777 /*template_keyword_p=*/true,
17778 /*check_dependency_p=*/true,
17779 none_type,
17780 /*is_declaration=*/false);
17781 /* If the template-id did not name a type, we are out of
17782 luck. */
17783 if (TREE_CODE (type) != TYPE_DECL)
17784 {
17785 /* ...unless we pretend we have seen 'typename'. */
17786 if (typename_p)
17787 type = cp_parser_make_typename_type (parser, type,
17788 token->location);
17789 else
17790 {
17791 cp_parser_error (parser, "expected template-id for type");
17792 type = NULL_TREE;
17793 }
17794 }
17795 }
17796 /* Otherwise, look for a type-name. */
17797 else
17798 type = cp_parser_type_name (parser, (qualified_p && typename_p));
17799
17800 /* Keep track of all name-lookups performed in class scopes. */
17801 if (type
17802 && !global_p
17803 && !qualified_p
17804 && TREE_CODE (type) == TYPE_DECL
17805 && identifier_p (DECL_NAME (type)))
17806 maybe_note_name_used_in_class (DECL_NAME (type), type);
17807 /* If it didn't work out, we don't have a TYPE. */
17808 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17809 && !cp_parser_parse_definitely (parser))
17810 type = NULL_TREE;
17811 if (!type && cxx_dialect >= cxx17)
17812 {
17813 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17814 cp_parser_parse_tentatively (parser);
17815
17816 cp_parser_global_scope_opt (parser,
17817 /*current_scope_valid_p=*/false);
17818 cp_parser_nested_name_specifier_opt (parser,
17819 /*typename_keyword_p=*/false,
17820 /*check_dependency_p=*/true,
17821 /*type_p=*/false,
17822 /*is_declaration=*/false);
17823 tree name = cp_parser_identifier (parser);
17824 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17825 && parser->scope != error_mark_node)
17826 {
17827 tree tmpl = cp_parser_lookup_name (parser, name,
17828 none_type,
17829 /*is_template=*/false,
17830 /*is_namespace=*/false,
17831 /*check_dependency=*/true,
17832 /*ambiguous_decls=*/NULL,
17833 token->location);
17834 if (tmpl && tmpl != error_mark_node
17835 && (DECL_CLASS_TEMPLATE_P (tmpl)
17836 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17837 type = make_template_placeholder (tmpl);
17838 else
17839 {
17840 type = error_mark_node;
17841 if (!cp_parser_simulate_error (parser))
17842 cp_parser_name_lookup_error (parser, name, tmpl,
17843 NLE_TYPE, token->location);
17844 }
17845 }
17846 else
17847 type = error_mark_node;
17848
17849 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17850 && !cp_parser_parse_definitely (parser))
17851 type = NULL_TREE;
17852 }
17853 if (type && decl_specs)
17854 cp_parser_set_decl_spec_type (decl_specs, type,
17855 token,
17856 /*type_definition_p=*/false);
17857 }
17858
17859 /* If we didn't get a type-name, issue an error message. */
17860 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17861 {
17862 cp_parser_error (parser, "expected type-name");
17863 return error_mark_node;
17864 }
17865
17866 if (type && type != error_mark_node)
17867 {
17868 /* See if TYPE is an Objective-C type, and if so, parse and
17869 accept any protocol references following it. Do this before
17870 the cp_parser_check_for_invalid_template_id() call, because
17871 Objective-C types can be followed by '<...>' which would
17872 enclose protocol names rather than template arguments, and so
17873 everything is fine. */
17874 if (c_dialect_objc () && !parser->scope
17875 && (objc_is_id (type) || objc_is_class_name (type)))
17876 {
17877 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17878 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17879
17880 /* Clobber the "unqualified" type previously entered into
17881 DECL_SPECS with the new, improved protocol-qualified version. */
17882 if (decl_specs)
17883 decl_specs->type = qual_type;
17884
17885 return qual_type;
17886 }
17887
17888 /* There is no valid C++ program where a non-template type is
17889 followed by a "<". That usually indicates that the user
17890 thought that the type was a template. */
17891 cp_parser_check_for_invalid_template_id (parser, type,
17892 none_type,
17893 token->location);
17894 }
17895
17896 return type;
17897 }
17898
17899 /* Parse a type-name.
17900
17901 type-name:
17902 class-name
17903 enum-name
17904 typedef-name
17905 simple-template-id [in c++0x]
17906
17907 enum-name:
17908 identifier
17909
17910 typedef-name:
17911 identifier
17912
17913 Concepts:
17914
17915 type-name:
17916 concept-name
17917 partial-concept-id
17918
17919 concept-name:
17920 identifier
17921
17922 Returns a TYPE_DECL for the type. */
17923
17924 static tree
17925 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17926 {
17927 tree type_decl;
17928
17929 /* We can't know yet whether it is a class-name or not. */
17930 cp_parser_parse_tentatively (parser);
17931 /* Try a class-name. */
17932 type_decl = cp_parser_class_name (parser,
17933 typename_keyword_p,
17934 /*template_keyword_p=*/false,
17935 none_type,
17936 /*check_dependency_p=*/true,
17937 /*class_head_p=*/false,
17938 /*is_declaration=*/false);
17939 /* If it's not a class-name, keep looking. */
17940 if (!cp_parser_parse_definitely (parser))
17941 {
17942 if (cxx_dialect < cxx11)
17943 /* It must be a typedef-name or an enum-name. */
17944 return cp_parser_nonclass_name (parser);
17945
17946 cp_parser_parse_tentatively (parser);
17947 /* It is either a simple-template-id representing an
17948 instantiation of an alias template... */
17949 type_decl = cp_parser_template_id (parser,
17950 /*template_keyword_p=*/false,
17951 /*check_dependency_p=*/true,
17952 none_type,
17953 /*is_declaration=*/false);
17954 /* Note that this must be an instantiation of an alias template
17955 because [temp.names]/6 says:
17956
17957 A template-id that names an alias template specialization
17958 is a type-name.
17959
17960 Whereas [temp.names]/7 says:
17961
17962 A simple-template-id that names a class template
17963 specialization is a class-name.
17964
17965 With concepts, this could also be a partial-concept-id that
17966 declares a non-type template parameter. */
17967 if (type_decl != NULL_TREE
17968 && TREE_CODE (type_decl) == TYPE_DECL
17969 && TYPE_DECL_ALIAS_P (type_decl))
17970 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17971 else if (is_constrained_parameter (type_decl))
17972 /* Don't do anything. */ ;
17973 else
17974 cp_parser_simulate_error (parser);
17975
17976 if (!cp_parser_parse_definitely (parser))
17977 /* ... Or a typedef-name or an enum-name. */
17978 return cp_parser_nonclass_name (parser);
17979 }
17980
17981 return type_decl;
17982 }
17983
17984 /* Check if DECL and ARGS can form a constrained-type-specifier.
17985 If ARGS is non-null, we try to form a concept check of the
17986 form DECL<?, ARGS> where ? is a wildcard that matches any
17987 kind of template argument. If ARGS is NULL, then we try to
17988 form a concept check of the form DECL<?>. */
17989
17990 static tree
17991 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17992 tree decl, tree args)
17993 {
17994 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17995
17996 /* If we a constrained-type-specifier cannot be deduced. */
17997 if (parser->prevent_constrained_type_specifiers)
17998 return NULL_TREE;
17999
18000 /* A constrained type specifier can only be found in an
18001 overload set or as a reference to a template declaration.
18002
18003 FIXME: This might be masking a bug. It's possible that
18004 that the deduction below is causing template specializations
18005 to be formed with the wildcard as an argument. */
18006 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
18007 return NULL_TREE;
18008
18009 /* Try to build a call expression that evaluates the
18010 concept. This can fail if the overload set refers
18011 only to non-templates. */
18012 tree placeholder = build_nt (WILDCARD_DECL);
18013 tree check = build_concept_check (decl, placeholder, args);
18014 if (check == error_mark_node)
18015 return NULL_TREE;
18016
18017 /* Deduce the checked constraint and the prototype parameter.
18018
18019 FIXME: In certain cases, failure to deduce should be a
18020 diagnosable error. */
18021 tree conc;
18022 tree proto;
18023 if (!deduce_constrained_parameter (check, conc, proto))
18024 return NULL_TREE;
18025
18026 /* In template parameter scope, this results in a constrained
18027 parameter. Return a descriptor of that parm. */
18028 if (processing_template_parmlist)
18029 return build_constrained_parameter (conc, proto, args);
18030
18031 /* In a parameter-declaration-clause, constrained-type
18032 specifiers result in invented template parameters. */
18033 if (parser->auto_is_implicit_function_template_parm_p)
18034 {
18035 tree x = build_constrained_parameter (conc, proto, args);
18036 return synthesize_implicit_template_parm (parser, x);
18037 }
18038 else
18039 {
18040 /* Otherwise, we're in a context where the constrained
18041 type name is deduced and the constraint applies
18042 after deduction. */
18043 return make_constrained_auto (conc, args);
18044 }
18045
18046 return NULL_TREE;
18047 }
18048
18049 /* If DECL refers to a concept, return a TYPE_DECL representing
18050 the result of using the constrained type specifier in the
18051 current context. DECL refers to a concept if
18052
18053 - it is an overload set containing a function concept taking a single
18054 type argument, or
18055
18056 - it is a variable concept taking a single type argument. */
18057
18058 static tree
18059 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
18060 {
18061 if (flag_concepts
18062 && (TREE_CODE (decl) == OVERLOAD
18063 || BASELINK_P (decl)
18064 || variable_concept_p (decl)))
18065 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
18066 else
18067 return NULL_TREE;
18068 }
18069
18070 /* Check if DECL and ARGS form a partial-concept-id. If so,
18071 assign ID to the resulting constrained placeholder.
18072
18073 Returns true if the partial-concept-id designates a placeholder
18074 and false otherwise. Note that *id is set to NULL_TREE in
18075 this case. */
18076
18077 static tree
18078 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
18079 {
18080 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
18081 }
18082
18083 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18084 or a concept-name.
18085
18086 enum-name:
18087 identifier
18088
18089 typedef-name:
18090 identifier
18091
18092 concept-name:
18093 identifier
18094
18095 Returns a TYPE_DECL for the type. */
18096
18097 static tree
18098 cp_parser_nonclass_name (cp_parser* parser)
18099 {
18100 tree type_decl;
18101 tree identifier;
18102
18103 cp_token *token = cp_lexer_peek_token (parser->lexer);
18104 identifier = cp_parser_identifier (parser);
18105 if (identifier == error_mark_node)
18106 return error_mark_node;
18107
18108 /* Look up the type-name. */
18109 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
18110
18111 type_decl = strip_using_decl (type_decl);
18112
18113 /* If we found an overload set, then it may refer to a concept-name. */
18114 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
18115 type_decl = decl;
18116
18117 if (TREE_CODE (type_decl) != TYPE_DECL
18118 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
18119 {
18120 /* See if this is an Objective-C type. */
18121 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18122 tree type = objc_get_protocol_qualified_type (identifier, protos);
18123 if (type)
18124 type_decl = TYPE_NAME (type);
18125 }
18126
18127 /* Issue an error if we did not find a type-name. */
18128 if (TREE_CODE (type_decl) != TYPE_DECL
18129 /* In Objective-C, we have the complication that class names are
18130 normally type names and start declarations (eg, the
18131 "NSObject" in "NSObject *object;"), but can be used in an
18132 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18133 is an expression. So, a classname followed by a dot is not a
18134 valid type-name. */
18135 || (objc_is_class_name (TREE_TYPE (type_decl))
18136 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
18137 {
18138 if (!cp_parser_simulate_error (parser))
18139 cp_parser_name_lookup_error (parser, identifier, type_decl,
18140 NLE_TYPE, token->location);
18141 return error_mark_node;
18142 }
18143 /* Remember that the name was used in the definition of the
18144 current class so that we can check later to see if the
18145 meaning would have been different after the class was
18146 entirely defined. */
18147 else if (type_decl != error_mark_node
18148 && !parser->scope)
18149 maybe_note_name_used_in_class (identifier, type_decl);
18150
18151 return type_decl;
18152 }
18153
18154 /* Parse an elaborated-type-specifier. Note that the grammar given
18155 here incorporates the resolution to DR68.
18156
18157 elaborated-type-specifier:
18158 class-key :: [opt] nested-name-specifier [opt] identifier
18159 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18160 enum-key :: [opt] nested-name-specifier [opt] identifier
18161 typename :: [opt] nested-name-specifier identifier
18162 typename :: [opt] nested-name-specifier template [opt]
18163 template-id
18164
18165 GNU extension:
18166
18167 elaborated-type-specifier:
18168 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18169 class-key attributes :: [opt] nested-name-specifier [opt]
18170 template [opt] template-id
18171 enum attributes :: [opt] nested-name-specifier [opt] identifier
18172
18173 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18174 declared `friend'. If IS_DECLARATION is TRUE, then this
18175 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18176 something is being declared.
18177
18178 Returns the TYPE specified. */
18179
18180 static tree
18181 cp_parser_elaborated_type_specifier (cp_parser* parser,
18182 bool is_friend,
18183 bool is_declaration)
18184 {
18185 enum tag_types tag_type;
18186 tree identifier;
18187 tree type = NULL_TREE;
18188 tree attributes = NULL_TREE;
18189 tree globalscope;
18190 cp_token *token = NULL;
18191
18192 /* See if we're looking at the `enum' keyword. */
18193 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
18194 {
18195 /* Consume the `enum' token. */
18196 cp_lexer_consume_token (parser->lexer);
18197 /* Remember that it's an enumeration type. */
18198 tag_type = enum_type;
18199 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18200 enums) is used here. */
18201 cp_token *token = cp_lexer_peek_token (parser->lexer);
18202 if (cp_parser_is_keyword (token, RID_CLASS)
18203 || cp_parser_is_keyword (token, RID_STRUCT))
18204 {
18205 gcc_rich_location richloc (token->location);
18206 richloc.add_range (input_location);
18207 richloc.add_fixit_remove ();
18208 pedwarn (&richloc, 0, "elaborated-type-specifier for "
18209 "a scoped enum must not use the %qD keyword",
18210 token->u.value);
18211 /* Consume the `struct' or `class' and parse it anyway. */
18212 cp_lexer_consume_token (parser->lexer);
18213 }
18214 /* Parse the attributes. */
18215 attributes = cp_parser_attributes_opt (parser);
18216 }
18217 /* Or, it might be `typename'. */
18218 else if (cp_lexer_next_token_is_keyword (parser->lexer,
18219 RID_TYPENAME))
18220 {
18221 /* Consume the `typename' token. */
18222 cp_lexer_consume_token (parser->lexer);
18223 /* Remember that it's a `typename' type. */
18224 tag_type = typename_type;
18225 }
18226 /* Otherwise it must be a class-key. */
18227 else
18228 {
18229 tag_type = cp_parser_class_key (parser);
18230 if (tag_type == none_type)
18231 return error_mark_node;
18232 /* Parse the attributes. */
18233 attributes = cp_parser_attributes_opt (parser);
18234 }
18235
18236 /* Look for the `::' operator. */
18237 globalscope = cp_parser_global_scope_opt (parser,
18238 /*current_scope_valid_p=*/false);
18239 /* Look for the nested-name-specifier. */
18240 tree nested_name_specifier;
18241 if (tag_type == typename_type && !globalscope)
18242 {
18243 nested_name_specifier
18244 = cp_parser_nested_name_specifier (parser,
18245 /*typename_keyword_p=*/true,
18246 /*check_dependency_p=*/true,
18247 /*type_p=*/true,
18248 is_declaration);
18249 if (!nested_name_specifier)
18250 return error_mark_node;
18251 }
18252 else
18253 /* Even though `typename' is not present, the proposed resolution
18254 to Core Issue 180 says that in `class A<T>::B', `B' should be
18255 considered a type-name, even if `A<T>' is dependent. */
18256 nested_name_specifier
18257 = cp_parser_nested_name_specifier_opt (parser,
18258 /*typename_keyword_p=*/true,
18259 /*check_dependency_p=*/true,
18260 /*type_p=*/true,
18261 is_declaration);
18262 /* For everything but enumeration types, consider a template-id.
18263 For an enumeration type, consider only a plain identifier. */
18264 if (tag_type != enum_type)
18265 {
18266 bool template_p = false;
18267 tree decl;
18268
18269 /* Allow the `template' keyword. */
18270 template_p = cp_parser_optional_template_keyword (parser);
18271 /* If we didn't see `template', we don't know if there's a
18272 template-id or not. */
18273 if (!template_p)
18274 cp_parser_parse_tentatively (parser);
18275 /* The `template' keyword must follow a nested-name-specifier. */
18276 else if (!nested_name_specifier)
18277 {
18278 cp_parser_error (parser, "%<template%> must follow a nested-"
18279 "name-specifier");
18280 return error_mark_node;
18281 }
18282
18283 /* Parse the template-id. */
18284 token = cp_lexer_peek_token (parser->lexer);
18285 decl = cp_parser_template_id (parser, template_p,
18286 /*check_dependency_p=*/true,
18287 tag_type,
18288 is_declaration);
18289 /* If we didn't find a template-id, look for an ordinary
18290 identifier. */
18291 if (!template_p && !cp_parser_parse_definitely (parser))
18292 ;
18293 /* We can get here when cp_parser_template_id, called by
18294 cp_parser_class_name with tag_type == none_type, succeeds
18295 and caches a BASELINK. Then, when called again here,
18296 instead of failing and returning an error_mark_node
18297 returns it (see template/typename17.C in C++11).
18298 ??? Could we diagnose this earlier? */
18299 else if (tag_type == typename_type && BASELINK_P (decl))
18300 {
18301 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18302 type = error_mark_node;
18303 }
18304 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18305 in effect, then we must assume that, upon instantiation, the
18306 template will correspond to a class. */
18307 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18308 && tag_type == typename_type)
18309 type = make_typename_type (parser->scope, decl,
18310 typename_type,
18311 /*complain=*/tf_error);
18312 /* If the `typename' keyword is in effect and DECL is not a type
18313 decl, then type is non existent. */
18314 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18315 ;
18316 else if (TREE_CODE (decl) == TYPE_DECL)
18317 {
18318 type = check_elaborated_type_specifier (tag_type, decl,
18319 /*allow_template_p=*/true);
18320
18321 /* If the next token is a semicolon, this must be a specialization,
18322 instantiation, or friend declaration. Check the scope while we
18323 still know whether or not we had a nested-name-specifier. */
18324 if (type != error_mark_node
18325 && !nested_name_specifier && !is_friend
18326 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18327 check_unqualified_spec_or_inst (type, token->location);
18328 }
18329 else if (decl == error_mark_node)
18330 type = error_mark_node;
18331 }
18332
18333 if (!type)
18334 {
18335 token = cp_lexer_peek_token (parser->lexer);
18336 identifier = cp_parser_identifier (parser);
18337
18338 if (identifier == error_mark_node)
18339 {
18340 parser->scope = NULL_TREE;
18341 return error_mark_node;
18342 }
18343
18344 /* For a `typename', we needn't call xref_tag. */
18345 if (tag_type == typename_type
18346 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18347 return cp_parser_make_typename_type (parser, identifier,
18348 token->location);
18349
18350 /* Template parameter lists apply only if we are not within a
18351 function parameter list. */
18352 bool template_parm_lists_apply
18353 = parser->num_template_parameter_lists;
18354 if (template_parm_lists_apply)
18355 for (cp_binding_level *s = current_binding_level;
18356 s && s->kind != sk_template_parms;
18357 s = s->level_chain)
18358 if (s->kind == sk_function_parms)
18359 template_parm_lists_apply = false;
18360
18361 /* Look up a qualified name in the usual way. */
18362 if (parser->scope)
18363 {
18364 tree decl;
18365 tree ambiguous_decls;
18366
18367 decl = cp_parser_lookup_name (parser, identifier,
18368 tag_type,
18369 /*is_template=*/false,
18370 /*is_namespace=*/false,
18371 /*check_dependency=*/true,
18372 &ambiguous_decls,
18373 token->location);
18374
18375 /* If the lookup was ambiguous, an error will already have been
18376 issued. */
18377 if (ambiguous_decls)
18378 return error_mark_node;
18379
18380 /* If we are parsing friend declaration, DECL may be a
18381 TEMPLATE_DECL tree node here. However, we need to check
18382 whether this TEMPLATE_DECL results in valid code. Consider
18383 the following example:
18384
18385 namespace N {
18386 template <class T> class C {};
18387 }
18388 class X {
18389 template <class T> friend class N::C; // #1, valid code
18390 };
18391 template <class T> class Y {
18392 friend class N::C; // #2, invalid code
18393 };
18394
18395 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18396 name lookup of `N::C'. We see that friend declaration must
18397 be template for the code to be valid. Note that
18398 processing_template_decl does not work here since it is
18399 always 1 for the above two cases. */
18400
18401 decl = (cp_parser_maybe_treat_template_as_class
18402 (decl, /*tag_name_p=*/is_friend
18403 && template_parm_lists_apply));
18404
18405 if (TREE_CODE (decl) != TYPE_DECL)
18406 {
18407 cp_parser_diagnose_invalid_type_name (parser,
18408 identifier,
18409 token->location);
18410 return error_mark_node;
18411 }
18412
18413 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18414 {
18415 bool allow_template = (template_parm_lists_apply
18416 || DECL_SELF_REFERENCE_P (decl));
18417 type = check_elaborated_type_specifier (tag_type, decl,
18418 allow_template);
18419
18420 if (type == error_mark_node)
18421 return error_mark_node;
18422 }
18423
18424 /* Forward declarations of nested types, such as
18425
18426 class C1::C2;
18427 class C1::C2::C3;
18428
18429 are invalid unless all components preceding the final '::'
18430 are complete. If all enclosing types are complete, these
18431 declarations become merely pointless.
18432
18433 Invalid forward declarations of nested types are errors
18434 caught elsewhere in parsing. Those that are pointless arrive
18435 here. */
18436
18437 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18438 && !is_friend && !processing_explicit_instantiation)
18439 warning (0, "declaration %qD does not declare anything", decl);
18440
18441 type = TREE_TYPE (decl);
18442 }
18443 else
18444 {
18445 /* An elaborated-type-specifier sometimes introduces a new type and
18446 sometimes names an existing type. Normally, the rule is that it
18447 introduces a new type only if there is not an existing type of
18448 the same name already in scope. For example, given:
18449
18450 struct S {};
18451 void f() { struct S s; }
18452
18453 the `struct S' in the body of `f' is the same `struct S' as in
18454 the global scope; the existing definition is used. However, if
18455 there were no global declaration, this would introduce a new
18456 local class named `S'.
18457
18458 An exception to this rule applies to the following code:
18459
18460 namespace N { struct S; }
18461
18462 Here, the elaborated-type-specifier names a new type
18463 unconditionally; even if there is already an `S' in the
18464 containing scope this declaration names a new type.
18465 This exception only applies if the elaborated-type-specifier
18466 forms the complete declaration:
18467
18468 [class.name]
18469
18470 A declaration consisting solely of `class-key identifier ;' is
18471 either a redeclaration of the name in the current scope or a
18472 forward declaration of the identifier as a class name. It
18473 introduces the name into the current scope.
18474
18475 We are in this situation precisely when the next token is a `;'.
18476
18477 An exception to the exception is that a `friend' declaration does
18478 *not* name a new type; i.e., given:
18479
18480 struct S { friend struct T; };
18481
18482 `T' is not a new type in the scope of `S'.
18483
18484 Also, `new struct S' or `sizeof (struct S)' never results in the
18485 definition of a new type; a new type can only be declared in a
18486 declaration context. */
18487
18488 tag_scope ts;
18489 bool template_p;
18490
18491 if (is_friend)
18492 /* Friends have special name lookup rules. */
18493 ts = ts_within_enclosing_non_class;
18494 else if (is_declaration
18495 && cp_lexer_next_token_is (parser->lexer,
18496 CPP_SEMICOLON))
18497 /* This is a `class-key identifier ;' */
18498 ts = ts_current;
18499 else
18500 ts = ts_global;
18501
18502 template_p =
18503 (template_parm_lists_apply
18504 && (cp_parser_next_token_starts_class_definition_p (parser)
18505 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18506 /* An unqualified name was used to reference this type, so
18507 there were no qualifying templates. */
18508 if (template_parm_lists_apply
18509 && !cp_parser_check_template_parameters (parser,
18510 /*num_templates=*/0,
18511 /*template_id*/false,
18512 token->location,
18513 /*declarator=*/NULL))
18514 return error_mark_node;
18515 type = xref_tag (tag_type, identifier, ts, template_p);
18516 }
18517 }
18518
18519 if (type == error_mark_node)
18520 return error_mark_node;
18521
18522 /* Allow attributes on forward declarations of classes. */
18523 if (attributes)
18524 {
18525 if (TREE_CODE (type) == TYPENAME_TYPE)
18526 warning (OPT_Wattributes,
18527 "attributes ignored on uninstantiated type");
18528 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18529 && ! processing_explicit_instantiation)
18530 warning (OPT_Wattributes,
18531 "attributes ignored on template instantiation");
18532 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18533 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18534 else
18535 warning (OPT_Wattributes,
18536 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18537 }
18538
18539 if (tag_type != enum_type)
18540 {
18541 /* Indicate whether this class was declared as a `class' or as a
18542 `struct'. */
18543 if (CLASS_TYPE_P (type))
18544 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18545 cp_parser_check_class_key (tag_type, type);
18546 }
18547
18548 /* A "<" cannot follow an elaborated type specifier. If that
18549 happens, the user was probably trying to form a template-id. */
18550 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18551 token->location);
18552
18553 return type;
18554 }
18555
18556 /* Parse an enum-specifier.
18557
18558 enum-specifier:
18559 enum-head { enumerator-list [opt] }
18560 enum-head { enumerator-list , } [C++0x]
18561
18562 enum-head:
18563 enum-key identifier [opt] enum-base [opt]
18564 enum-key nested-name-specifier identifier enum-base [opt]
18565
18566 enum-key:
18567 enum
18568 enum class [C++0x]
18569 enum struct [C++0x]
18570
18571 enum-base: [C++0x]
18572 : type-specifier-seq
18573
18574 opaque-enum-specifier:
18575 enum-key identifier enum-base [opt] ;
18576
18577 GNU Extensions:
18578 enum-key attributes[opt] identifier [opt] enum-base [opt]
18579 { enumerator-list [opt] }attributes[opt]
18580 enum-key attributes[opt] identifier [opt] enum-base [opt]
18581 { enumerator-list, }attributes[opt] [C++0x]
18582
18583 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18584 if the token stream isn't an enum-specifier after all. */
18585
18586 static tree
18587 cp_parser_enum_specifier (cp_parser* parser)
18588 {
18589 tree identifier;
18590 tree type = NULL_TREE;
18591 tree prev_scope;
18592 tree nested_name_specifier = NULL_TREE;
18593 tree attributes;
18594 bool scoped_enum_p = false;
18595 bool has_underlying_type = false;
18596 bool nested_being_defined = false;
18597 bool new_value_list = false;
18598 bool is_new_type = false;
18599 bool is_unnamed = false;
18600 tree underlying_type = NULL_TREE;
18601 cp_token *type_start_token = NULL;
18602 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18603
18604 parser->colon_corrects_to_scope_p = false;
18605
18606 /* Parse tentatively so that we can back up if we don't find a
18607 enum-specifier. */
18608 cp_parser_parse_tentatively (parser);
18609
18610 /* Caller guarantees that the current token is 'enum', an identifier
18611 possibly follows, and the token after that is an opening brace.
18612 If we don't have an identifier, fabricate an anonymous name for
18613 the enumeration being defined. */
18614 cp_lexer_consume_token (parser->lexer);
18615
18616 /* Parse the "class" or "struct", which indicates a scoped
18617 enumeration type in C++0x. */
18618 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18619 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18620 {
18621 if (cxx_dialect < cxx11)
18622 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18623
18624 /* Consume the `struct' or `class' token. */
18625 cp_lexer_consume_token (parser->lexer);
18626
18627 scoped_enum_p = true;
18628 }
18629
18630 attributes = cp_parser_attributes_opt (parser);
18631
18632 /* Clear the qualification. */
18633 parser->scope = NULL_TREE;
18634 parser->qualifying_scope = NULL_TREE;
18635 parser->object_scope = NULL_TREE;
18636
18637 /* Figure out in what scope the declaration is being placed. */
18638 prev_scope = current_scope ();
18639
18640 type_start_token = cp_lexer_peek_token (parser->lexer);
18641
18642 push_deferring_access_checks (dk_no_check);
18643 nested_name_specifier
18644 = cp_parser_nested_name_specifier_opt (parser,
18645 /*typename_keyword_p=*/true,
18646 /*check_dependency_p=*/false,
18647 /*type_p=*/false,
18648 /*is_declaration=*/false);
18649
18650 if (nested_name_specifier)
18651 {
18652 tree name;
18653
18654 identifier = cp_parser_identifier (parser);
18655 name = cp_parser_lookup_name (parser, identifier,
18656 enum_type,
18657 /*is_template=*/false,
18658 /*is_namespace=*/false,
18659 /*check_dependency=*/true,
18660 /*ambiguous_decls=*/NULL,
18661 input_location);
18662 if (name && name != error_mark_node)
18663 {
18664 type = TREE_TYPE (name);
18665 if (TREE_CODE (type) == TYPENAME_TYPE)
18666 {
18667 /* Are template enums allowed in ISO? */
18668 if (template_parm_scope_p ())
18669 pedwarn (type_start_token->location, OPT_Wpedantic,
18670 "%qD is an enumeration template", name);
18671 /* ignore a typename reference, for it will be solved by name
18672 in start_enum. */
18673 type = NULL_TREE;
18674 }
18675 }
18676 else if (nested_name_specifier == error_mark_node)
18677 /* We already issued an error. */;
18678 else
18679 {
18680 error_at (type_start_token->location,
18681 "%qD does not name an enumeration in %qT",
18682 identifier, nested_name_specifier);
18683 nested_name_specifier = error_mark_node;
18684 }
18685 }
18686 else
18687 {
18688 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18689 identifier = cp_parser_identifier (parser);
18690 else
18691 {
18692 identifier = make_anon_name ();
18693 is_unnamed = true;
18694 if (scoped_enum_p)
18695 error_at (type_start_token->location,
18696 "unnamed scoped enum is not allowed");
18697 }
18698 }
18699 pop_deferring_access_checks ();
18700
18701 /* Check for the `:' that denotes a specified underlying type in C++0x.
18702 Note that a ':' could also indicate a bitfield width, however. */
18703 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18704 {
18705 cp_decl_specifier_seq type_specifiers;
18706
18707 /* Consume the `:'. */
18708 cp_lexer_consume_token (parser->lexer);
18709
18710 /* Parse the type-specifier-seq. */
18711 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
18712 /*is_declaration=*/false,
18713 /*is_trailing_return=*/false,
18714 &type_specifiers);
18715
18716 /* At this point this is surely not elaborated type specifier. */
18717 if (!cp_parser_parse_definitely (parser))
18718 return NULL_TREE;
18719
18720 if (cxx_dialect < cxx11)
18721 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18722
18723 has_underlying_type = true;
18724
18725 /* If that didn't work, stop. */
18726 if (type_specifiers.type != error_mark_node)
18727 {
18728 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18729 /*initialized=*/0, NULL);
18730 if (underlying_type == error_mark_node
18731 || check_for_bare_parameter_packs (underlying_type))
18732 underlying_type = NULL_TREE;
18733 }
18734 }
18735
18736 /* Look for the `{' but don't consume it yet. */
18737 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18738 {
18739 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18740 {
18741 cp_parser_error (parser, "expected %<{%>");
18742 if (has_underlying_type)
18743 {
18744 type = NULL_TREE;
18745 goto out;
18746 }
18747 }
18748 /* An opaque-enum-specifier must have a ';' here. */
18749 if ((scoped_enum_p || underlying_type)
18750 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18751 {
18752 cp_parser_error (parser, "expected %<;%> or %<{%>");
18753 if (has_underlying_type)
18754 {
18755 type = NULL_TREE;
18756 goto out;
18757 }
18758 }
18759 }
18760
18761 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18762 return NULL_TREE;
18763
18764 if (nested_name_specifier)
18765 {
18766 if (CLASS_TYPE_P (nested_name_specifier))
18767 {
18768 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18769 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18770 push_scope (nested_name_specifier);
18771 }
18772 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18773 {
18774 push_nested_namespace (nested_name_specifier);
18775 }
18776 }
18777
18778 /* Issue an error message if type-definitions are forbidden here. */
18779 if (!cp_parser_check_type_definition (parser))
18780 type = error_mark_node;
18781 else
18782 /* Create the new type. We do this before consuming the opening
18783 brace so the enum will be recorded as being on the line of its
18784 tag (or the 'enum' keyword, if there is no tag). */
18785 type = start_enum (identifier, type, underlying_type,
18786 attributes, scoped_enum_p, &is_new_type);
18787
18788 /* If the next token is not '{' it is an opaque-enum-specifier or an
18789 elaborated-type-specifier. */
18790 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18791 {
18792 timevar_push (TV_PARSE_ENUM);
18793 if (nested_name_specifier
18794 && nested_name_specifier != error_mark_node)
18795 {
18796 /* The following catches invalid code such as:
18797 enum class S<int>::E { A, B, C }; */
18798 if (!processing_specialization
18799 && CLASS_TYPE_P (nested_name_specifier)
18800 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18801 error_at (type_start_token->location, "cannot add an enumerator "
18802 "list to a template instantiation");
18803
18804 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18805 {
18806 error_at (type_start_token->location,
18807 "%<%T::%E%> has not been declared",
18808 TYPE_CONTEXT (nested_name_specifier),
18809 nested_name_specifier);
18810 type = error_mark_node;
18811 }
18812 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18813 && !CLASS_TYPE_P (nested_name_specifier))
18814 {
18815 error_at (type_start_token->location, "nested name specifier "
18816 "%qT for enum declaration does not name a class "
18817 "or namespace", nested_name_specifier);
18818 type = error_mark_node;
18819 }
18820 /* If that scope does not contain the scope in which the
18821 class was originally declared, the program is invalid. */
18822 else if (prev_scope && !is_ancestor (prev_scope,
18823 nested_name_specifier))
18824 {
18825 if (at_namespace_scope_p ())
18826 error_at (type_start_token->location,
18827 "declaration of %qD in namespace %qD which does not "
18828 "enclose %qD",
18829 type, prev_scope, nested_name_specifier);
18830 else
18831 error_at (type_start_token->location,
18832 "declaration of %qD in %qD which does not "
18833 "enclose %qD",
18834 type, prev_scope, nested_name_specifier);
18835 type = error_mark_node;
18836 }
18837 /* If that scope is the scope where the declaration is being placed
18838 the program is invalid. */
18839 else if (CLASS_TYPE_P (nested_name_specifier)
18840 && CLASS_TYPE_P (prev_scope)
18841 && same_type_p (nested_name_specifier, prev_scope))
18842 {
18843 permerror (type_start_token->location,
18844 "extra qualification not allowed");
18845 nested_name_specifier = NULL_TREE;
18846 }
18847 }
18848
18849 if (scoped_enum_p)
18850 begin_scope (sk_scoped_enum, type);
18851
18852 /* Consume the opening brace. */
18853 matching_braces braces;
18854 braces.consume_open (parser);
18855
18856 if (type == error_mark_node)
18857 ; /* Nothing to add */
18858 else if (OPAQUE_ENUM_P (type)
18859 || (cxx_dialect > cxx98 && processing_specialization))
18860 {
18861 new_value_list = true;
18862 SET_OPAQUE_ENUM_P (type, false);
18863 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18864 }
18865 else
18866 {
18867 error_at (type_start_token->location,
18868 "multiple definition of %q#T", type);
18869 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18870 "previous definition here");
18871 type = error_mark_node;
18872 }
18873
18874 if (type == error_mark_node)
18875 cp_parser_skip_to_end_of_block_or_statement (parser);
18876 /* If the next token is not '}', then there are some enumerators. */
18877 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18878 {
18879 if (is_unnamed && !scoped_enum_p)
18880 pedwarn (type_start_token->location, OPT_Wpedantic,
18881 "ISO C++ forbids empty unnamed enum");
18882 }
18883 else
18884 cp_parser_enumerator_list (parser, type);
18885
18886 /* Consume the final '}'. */
18887 braces.require_close (parser);
18888
18889 if (scoped_enum_p)
18890 finish_scope ();
18891 timevar_pop (TV_PARSE_ENUM);
18892 }
18893 else
18894 {
18895 /* If a ';' follows, then it is an opaque-enum-specifier
18896 and additional restrictions apply. */
18897 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18898 {
18899 if (is_unnamed)
18900 error_at (type_start_token->location,
18901 "opaque-enum-specifier without name");
18902 else if (nested_name_specifier)
18903 error_at (type_start_token->location,
18904 "opaque-enum-specifier must use a simple identifier");
18905 }
18906 }
18907
18908 /* Look for trailing attributes to apply to this enumeration, and
18909 apply them if appropriate. */
18910 if (cp_parser_allow_gnu_extensions_p (parser))
18911 {
18912 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18913 cplus_decl_attributes (&type,
18914 trailing_attr,
18915 (int) ATTR_FLAG_TYPE_IN_PLACE);
18916 }
18917
18918 /* Finish up the enumeration. */
18919 if (type != error_mark_node)
18920 {
18921 if (new_value_list)
18922 finish_enum_value_list (type);
18923 if (is_new_type)
18924 finish_enum (type);
18925 }
18926
18927 if (nested_name_specifier)
18928 {
18929 if (CLASS_TYPE_P (nested_name_specifier))
18930 {
18931 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18932 pop_scope (nested_name_specifier);
18933 }
18934 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18935 {
18936 pop_nested_namespace (nested_name_specifier);
18937 }
18938 }
18939 out:
18940 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18941 return type;
18942 }
18943
18944 /* Parse an enumerator-list. The enumerators all have the indicated
18945 TYPE.
18946
18947 enumerator-list:
18948 enumerator-definition
18949 enumerator-list , enumerator-definition */
18950
18951 static void
18952 cp_parser_enumerator_list (cp_parser* parser, tree type)
18953 {
18954 while (true)
18955 {
18956 /* Parse an enumerator-definition. */
18957 cp_parser_enumerator_definition (parser, type);
18958
18959 /* If the next token is not a ',', we've reached the end of
18960 the list. */
18961 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18962 break;
18963 /* Otherwise, consume the `,' and keep going. */
18964 cp_lexer_consume_token (parser->lexer);
18965 /* If the next token is a `}', there is a trailing comma. */
18966 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18967 {
18968 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18969 pedwarn (input_location, OPT_Wpedantic,
18970 "comma at end of enumerator list");
18971 break;
18972 }
18973 }
18974 }
18975
18976 /* Parse an enumerator-definition. The enumerator has the indicated
18977 TYPE.
18978
18979 enumerator-definition:
18980 enumerator
18981 enumerator = constant-expression
18982
18983 enumerator:
18984 identifier
18985
18986 GNU Extensions:
18987
18988 enumerator-definition:
18989 enumerator attributes [opt]
18990 enumerator attributes [opt] = constant-expression */
18991
18992 static void
18993 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18994 {
18995 tree identifier;
18996 tree value;
18997 location_t loc;
18998
18999 /* Save the input location because we are interested in the location
19000 of the identifier and not the location of the explicit value. */
19001 loc = cp_lexer_peek_token (parser->lexer)->location;
19002
19003 /* Look for the identifier. */
19004 identifier = cp_parser_identifier (parser);
19005 if (identifier == error_mark_node)
19006 return;
19007
19008 /* Parse any specified attributes. */
19009 tree attrs = cp_parser_attributes_opt (parser);
19010
19011 /* If the next token is an '=', then there is an explicit value. */
19012 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19013 {
19014 /* Consume the `=' token. */
19015 cp_lexer_consume_token (parser->lexer);
19016 /* Parse the value. */
19017 value = cp_parser_constant_expression (parser);
19018 }
19019 else
19020 value = NULL_TREE;
19021
19022 /* If we are processing a template, make sure the initializer of the
19023 enumerator doesn't contain any bare template parameter pack. */
19024 if (check_for_bare_parameter_packs (value))
19025 value = error_mark_node;
19026
19027 /* Create the enumerator. */
19028 build_enumerator (identifier, value, type, attrs, loc);
19029 }
19030
19031 /* Parse a namespace-name.
19032
19033 namespace-name:
19034 original-namespace-name
19035 namespace-alias
19036
19037 Returns the NAMESPACE_DECL for the namespace. */
19038
19039 static tree
19040 cp_parser_namespace_name (cp_parser* parser)
19041 {
19042 tree identifier;
19043 tree namespace_decl;
19044
19045 cp_token *token = cp_lexer_peek_token (parser->lexer);
19046
19047 /* Get the name of the namespace. */
19048 identifier = cp_parser_identifier (parser);
19049 if (identifier == error_mark_node)
19050 return error_mark_node;
19051
19052 /* Look up the identifier in the currently active scope. Look only
19053 for namespaces, due to:
19054
19055 [basic.lookup.udir]
19056
19057 When looking up a namespace-name in a using-directive or alias
19058 definition, only namespace names are considered.
19059
19060 And:
19061
19062 [basic.lookup.qual]
19063
19064 During the lookup of a name preceding the :: scope resolution
19065 operator, object, function, and enumerator names are ignored.
19066
19067 (Note that cp_parser_qualifying_entity only calls this
19068 function if the token after the name is the scope resolution
19069 operator.) */
19070 namespace_decl = cp_parser_lookup_name (parser, identifier,
19071 none_type,
19072 /*is_template=*/false,
19073 /*is_namespace=*/true,
19074 /*check_dependency=*/true,
19075 /*ambiguous_decls=*/NULL,
19076 token->location);
19077 /* If it's not a namespace, issue an error. */
19078 if (namespace_decl == error_mark_node
19079 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
19080 {
19081 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19082 {
19083 auto_diagnostic_group d;
19084 name_hint hint;
19085 if (namespace_decl == error_mark_node
19086 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
19087 hint = suggest_alternative_in_explicit_scope (token->location,
19088 identifier,
19089 parser->scope);
19090 if (const char *suggestion = hint.suggestion ())
19091 {
19092 gcc_rich_location richloc (token->location);
19093 richloc.add_fixit_replace (suggestion);
19094 error_at (&richloc,
19095 "%qD is not a namespace-name; did you mean %qs?",
19096 identifier, suggestion);
19097 }
19098 else
19099 error_at (token->location, "%qD is not a namespace-name",
19100 identifier);
19101 }
19102 else
19103 cp_parser_error (parser, "expected namespace-name");
19104 namespace_decl = error_mark_node;
19105 }
19106
19107 return namespace_decl;
19108 }
19109
19110 /* Parse a namespace-definition.
19111
19112 namespace-definition:
19113 named-namespace-definition
19114 unnamed-namespace-definition
19115
19116 named-namespace-definition:
19117 original-namespace-definition
19118 extension-namespace-definition
19119
19120 original-namespace-definition:
19121 namespace identifier { namespace-body }
19122
19123 extension-namespace-definition:
19124 namespace original-namespace-name { namespace-body }
19125
19126 unnamed-namespace-definition:
19127 namespace { namespace-body } */
19128
19129 static void
19130 cp_parser_namespace_definition (cp_parser* parser)
19131 {
19132 tree identifier;
19133 int nested_definition_count = 0;
19134
19135 cp_ensure_no_omp_declare_simd (parser);
19136 cp_ensure_no_oacc_routine (parser);
19137
19138 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
19139 const bool topmost_inline_p = is_inline;
19140
19141 if (is_inline)
19142 {
19143 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
19144 cp_lexer_consume_token (parser->lexer);
19145 }
19146
19147 /* Look for the `namespace' keyword. */
19148 cp_token* token
19149 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19150
19151 /* Parse any specified attributes before the identifier. */
19152 tree attribs = cp_parser_attributes_opt (parser);
19153
19154 for (;;)
19155 {
19156 identifier = NULL_TREE;
19157
19158 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
19159 RID_INLINE);
19160 if (nested_inline_p && nested_definition_count != 0)
19161 {
19162 if (cxx_dialect < cxx2a)
19163 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
19164 OPT_Wpedantic, "nested inline namespace definitions only "
19165 "available with -std=c++2a or -std=gnu++2a");
19166 cp_lexer_consume_token (parser->lexer);
19167 }
19168
19169 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19170 {
19171 identifier = cp_parser_identifier (parser);
19172
19173 if (cp_next_tokens_can_be_std_attribute_p (parser))
19174 pedwarn (input_location, OPT_Wpedantic,
19175 "standard attributes on namespaces must precede "
19176 "the namespace name");
19177
19178 /* Parse any attributes specified after the identifier. */
19179 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
19180 }
19181
19182 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19183 {
19184 /* Don't forget that the innermost namespace might have been
19185 marked as inline. Use |= because we cannot overwrite
19186 IS_INLINE in case the outermost namespace is inline, but
19187 there are no nested inlines. */
19188 is_inline |= nested_inline_p;
19189 break;
19190 }
19191
19192 if (!nested_definition_count && cxx_dialect < cxx17)
19193 pedwarn (input_location, OPT_Wpedantic,
19194 "nested namespace definitions only available with "
19195 "-std=c++17 or -std=gnu++17");
19196
19197 /* Nested namespace names can create new namespaces (unlike
19198 other qualified-ids). */
19199 if (int count = (identifier
19200 ? push_namespace (identifier, nested_inline_p)
19201 : 0))
19202 nested_definition_count += count;
19203 else
19204 cp_parser_error (parser, "nested namespace name required");
19205 cp_lexer_consume_token (parser->lexer);
19206 }
19207
19208 if (nested_definition_count && !identifier)
19209 cp_parser_error (parser, "namespace name required");
19210
19211 if (nested_definition_count && attribs)
19212 error_at (token->location,
19213 "a nested namespace definition cannot have attributes");
19214 if (nested_definition_count && topmost_inline_p)
19215 error_at (token->location,
19216 "a nested namespace definition cannot be inline");
19217
19218 /* Start the namespace. */
19219 nested_definition_count += push_namespace (identifier, is_inline);
19220
19221 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
19222
19223 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
19224
19225 /* Look for the `{' to validate starting the namespace. */
19226 matching_braces braces;
19227 if (braces.require_open (parser))
19228 {
19229 /* Parse the body of the namespace. */
19230 cp_parser_namespace_body (parser);
19231
19232 /* Look for the final `}'. */
19233 braces.require_close (parser);
19234 }
19235
19236 if (has_visibility)
19237 pop_visibility (1);
19238
19239 /* Pop the nested namespace definitions. */
19240 while (nested_definition_count--)
19241 pop_namespace ();
19242 }
19243
19244 /* Parse a namespace-body.
19245
19246 namespace-body:
19247 declaration-seq [opt] */
19248
19249 static void
19250 cp_parser_namespace_body (cp_parser* parser)
19251 {
19252 cp_parser_declaration_seq_opt (parser);
19253 }
19254
19255 /* Parse a namespace-alias-definition.
19256
19257 namespace-alias-definition:
19258 namespace identifier = qualified-namespace-specifier ; */
19259
19260 static void
19261 cp_parser_namespace_alias_definition (cp_parser* parser)
19262 {
19263 tree identifier;
19264 tree namespace_specifier;
19265
19266 cp_token *token = cp_lexer_peek_token (parser->lexer);
19267
19268 /* Look for the `namespace' keyword. */
19269 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19270 /* Look for the identifier. */
19271 identifier = cp_parser_identifier (parser);
19272 if (identifier == error_mark_node)
19273 return;
19274 /* Look for the `=' token. */
19275 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
19276 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19277 {
19278 error_at (token->location, "%<namespace%> definition is not allowed here");
19279 /* Skip the definition. */
19280 cp_lexer_consume_token (parser->lexer);
19281 if (cp_parser_skip_to_closing_brace (parser))
19282 cp_lexer_consume_token (parser->lexer);
19283 return;
19284 }
19285 cp_parser_require (parser, CPP_EQ, RT_EQ);
19286 /* Look for the qualified-namespace-specifier. */
19287 namespace_specifier
19288 = cp_parser_qualified_namespace_specifier (parser);
19289 /* Look for the `;' token. */
19290 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19291
19292 /* Register the alias in the symbol table. */
19293 do_namespace_alias (identifier, namespace_specifier);
19294 }
19295
19296 /* Parse a qualified-namespace-specifier.
19297
19298 qualified-namespace-specifier:
19299 :: [opt] nested-name-specifier [opt] namespace-name
19300
19301 Returns a NAMESPACE_DECL corresponding to the specified
19302 namespace. */
19303
19304 static tree
19305 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19306 {
19307 /* Look for the optional `::'. */
19308 cp_parser_global_scope_opt (parser,
19309 /*current_scope_valid_p=*/false);
19310
19311 /* Look for the optional nested-name-specifier. */
19312 cp_parser_nested_name_specifier_opt (parser,
19313 /*typename_keyword_p=*/false,
19314 /*check_dependency_p=*/true,
19315 /*type_p=*/false,
19316 /*is_declaration=*/true);
19317
19318 return cp_parser_namespace_name (parser);
19319 }
19320
19321 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19322 access declaration.
19323
19324 using-declaration:
19325 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19326 using :: unqualified-id ;
19327
19328 access-declaration:
19329 qualified-id ;
19330
19331 */
19332
19333 static bool
19334 cp_parser_using_declaration (cp_parser* parser,
19335 bool access_declaration_p)
19336 {
19337 cp_token *token;
19338 bool typename_p = false;
19339 bool global_scope_p;
19340 tree decl;
19341 tree identifier;
19342 tree qscope;
19343 int oldcount = errorcount;
19344 cp_token *diag_token = NULL;
19345
19346 if (access_declaration_p)
19347 {
19348 diag_token = cp_lexer_peek_token (parser->lexer);
19349 cp_parser_parse_tentatively (parser);
19350 }
19351 else
19352 {
19353 /* Look for the `using' keyword. */
19354 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19355
19356 again:
19357 /* Peek at the next token. */
19358 token = cp_lexer_peek_token (parser->lexer);
19359 /* See if it's `typename'. */
19360 if (token->keyword == RID_TYPENAME)
19361 {
19362 /* Remember that we've seen it. */
19363 typename_p = true;
19364 /* Consume the `typename' token. */
19365 cp_lexer_consume_token (parser->lexer);
19366 }
19367 }
19368
19369 /* Look for the optional global scope qualification. */
19370 global_scope_p
19371 = (cp_parser_global_scope_opt (parser,
19372 /*current_scope_valid_p=*/false)
19373 != NULL_TREE);
19374
19375 /* If we saw `typename', or didn't see `::', then there must be a
19376 nested-name-specifier present. */
19377 if (typename_p || !global_scope_p)
19378 {
19379 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19380 /*check_dependency_p=*/true,
19381 /*type_p=*/false,
19382 /*is_declaration=*/true);
19383 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19384 {
19385 cp_parser_skip_to_end_of_block_or_statement (parser);
19386 return false;
19387 }
19388 }
19389 /* Otherwise, we could be in either of the two productions. In that
19390 case, treat the nested-name-specifier as optional. */
19391 else
19392 qscope = cp_parser_nested_name_specifier_opt (parser,
19393 /*typename_keyword_p=*/false,
19394 /*check_dependency_p=*/true,
19395 /*type_p=*/false,
19396 /*is_declaration=*/true);
19397 if (!qscope)
19398 qscope = global_namespace;
19399 else if (UNSCOPED_ENUM_P (qscope))
19400 qscope = CP_TYPE_CONTEXT (qscope);
19401
19402 if (access_declaration_p && cp_parser_error_occurred (parser))
19403 /* Something has already gone wrong; there's no need to parse
19404 further. Since an error has occurred, the return value of
19405 cp_parser_parse_definitely will be false, as required. */
19406 return cp_parser_parse_definitely (parser);
19407
19408 token = cp_lexer_peek_token (parser->lexer);
19409 /* Parse the unqualified-id. */
19410 identifier = cp_parser_unqualified_id (parser,
19411 /*template_keyword_p=*/false,
19412 /*check_dependency_p=*/true,
19413 /*declarator_p=*/true,
19414 /*optional_p=*/false);
19415
19416 if (access_declaration_p)
19417 {
19418 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19419 cp_parser_simulate_error (parser);
19420 if (!cp_parser_parse_definitely (parser))
19421 return false;
19422 }
19423 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19424 {
19425 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19426 if (cxx_dialect < cxx17
19427 && !in_system_header_at (ell->location))
19428 pedwarn (ell->location, 0,
19429 "pack expansion in using-declaration only available "
19430 "with -std=c++17 or -std=gnu++17");
19431 qscope = make_pack_expansion (qscope);
19432 }
19433
19434 /* The function we call to handle a using-declaration is different
19435 depending on what scope we are in. */
19436 if (qscope == error_mark_node || identifier == error_mark_node)
19437 ;
19438 else if (!identifier_p (identifier)
19439 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19440 /* [namespace.udecl]
19441
19442 A using declaration shall not name a template-id. */
19443 error_at (token->location,
19444 "a template-id may not appear in a using-declaration");
19445 else
19446 {
19447 if (at_class_scope_p ())
19448 {
19449 /* Create the USING_DECL. */
19450 decl = do_class_using_decl (qscope, identifier);
19451
19452 if (decl && typename_p)
19453 USING_DECL_TYPENAME_P (decl) = 1;
19454
19455 if (check_for_bare_parameter_packs (decl))
19456 {
19457 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19458 return false;
19459 }
19460 else
19461 /* Add it to the list of members in this class. */
19462 finish_member_declaration (decl);
19463 }
19464 else
19465 {
19466 decl = cp_parser_lookup_name_simple (parser,
19467 identifier,
19468 token->location);
19469 if (decl == error_mark_node)
19470 cp_parser_name_lookup_error (parser, identifier,
19471 decl, NLE_NULL,
19472 token->location);
19473 else if (check_for_bare_parameter_packs (decl))
19474 {
19475 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19476 return false;
19477 }
19478 else if (!at_namespace_scope_p ())
19479 finish_local_using_decl (decl, qscope, identifier);
19480 else
19481 finish_namespace_using_decl (decl, qscope, identifier);
19482 }
19483 }
19484
19485 if (!access_declaration_p
19486 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19487 {
19488 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19489 if (cxx_dialect < cxx17)
19490 pedwarn (comma->location, 0,
19491 "comma-separated list in using-declaration only available "
19492 "with -std=c++17 or -std=gnu++17");
19493 goto again;
19494 }
19495
19496 /* Look for the final `;'. */
19497 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19498
19499 if (access_declaration_p && errorcount == oldcount)
19500 warning_at (diag_token->location, OPT_Wdeprecated,
19501 "access declarations are deprecated "
19502 "in favour of using-declarations; "
19503 "suggestion: add the %<using%> keyword");
19504
19505 return true;
19506 }
19507
19508 /* Parse an alias-declaration.
19509
19510 alias-declaration:
19511 using identifier attribute-specifier-seq [opt] = type-id */
19512
19513 static tree
19514 cp_parser_alias_declaration (cp_parser* parser)
19515 {
19516 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19517 location_t id_location, type_location;
19518 cp_declarator *declarator;
19519 cp_decl_specifier_seq decl_specs;
19520 bool member_p;
19521 const char *saved_message = NULL;
19522
19523 /* Look for the `using' keyword. */
19524 cp_token *using_token
19525 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19526 if (using_token == NULL)
19527 return error_mark_node;
19528
19529 id_location = cp_lexer_peek_token (parser->lexer)->location;
19530 id = cp_parser_identifier (parser);
19531 if (id == error_mark_node)
19532 return error_mark_node;
19533
19534 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19535 attributes = cp_parser_attributes_opt (parser);
19536 if (attributes == error_mark_node)
19537 return error_mark_node;
19538
19539 cp_parser_require (parser, CPP_EQ, RT_EQ);
19540
19541 if (cp_parser_error_occurred (parser))
19542 return error_mark_node;
19543
19544 cp_parser_commit_to_tentative_parse (parser);
19545
19546 /* Now we are going to parse the type-id of the declaration. */
19547
19548 /*
19549 [dcl.type]/3 says:
19550
19551 "A type-specifier-seq shall not define a class or enumeration
19552 unless it appears in the type-id of an alias-declaration (7.1.3) that
19553 is not the declaration of a template-declaration."
19554
19555 In other words, if we currently are in an alias template, the
19556 type-id should not define a type.
19557
19558 So let's set parser->type_definition_forbidden_message in that
19559 case; cp_parser_check_type_definition (called by
19560 cp_parser_class_specifier) will then emit an error if a type is
19561 defined in the type-id. */
19562 if (parser->num_template_parameter_lists)
19563 {
19564 saved_message = parser->type_definition_forbidden_message;
19565 parser->type_definition_forbidden_message =
19566 G_("types may not be defined in alias template declarations");
19567 }
19568
19569 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
19570 &type_location);
19571
19572 /* Restore the error message if need be. */
19573 if (parser->num_template_parameter_lists)
19574 parser->type_definition_forbidden_message = saved_message;
19575
19576 if (type == error_mark_node
19577 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19578 {
19579 cp_parser_skip_to_end_of_block_or_statement (parser);
19580 return error_mark_node;
19581 }
19582
19583 /* A typedef-name can also be introduced by an alias-declaration. The
19584 identifier following the using keyword becomes a typedef-name. It has
19585 the same semantics as if it were introduced by the typedef
19586 specifier. In particular, it does not define a new type and it shall
19587 not appear in the type-id. */
19588
19589 clear_decl_specs (&decl_specs);
19590 decl_specs.type = type;
19591 if (attributes != NULL_TREE)
19592 {
19593 decl_specs.attributes = attributes;
19594 set_and_check_decl_spec_loc (&decl_specs,
19595 ds_attribute,
19596 attrs_token);
19597 }
19598 set_and_check_decl_spec_loc (&decl_specs,
19599 ds_typedef,
19600 using_token);
19601 set_and_check_decl_spec_loc (&decl_specs,
19602 ds_alias,
19603 using_token);
19604 decl_specs.locations[ds_type_spec] = type_location;
19605
19606 if (parser->num_template_parameter_lists
19607 && !cp_parser_check_template_parameters (parser,
19608 /*num_templates=*/0,
19609 /*template_id*/false,
19610 id_location,
19611 /*declarator=*/NULL))
19612 return error_mark_node;
19613
19614 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
19615
19616 member_p = at_class_scope_p ();
19617 if (member_p)
19618 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19619 NULL_TREE, attributes);
19620 else
19621 decl = start_decl (declarator, &decl_specs, 0,
19622 attributes, NULL_TREE, &pushed_scope);
19623 if (decl == error_mark_node)
19624 return decl;
19625
19626 // Attach constraints to the alias declaration.
19627 if (flag_concepts && current_template_parms)
19628 {
19629 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19630 tree constr = build_constraints (reqs, NULL_TREE);
19631 set_constraints (decl, constr);
19632 }
19633
19634 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19635
19636 if (pushed_scope)
19637 pop_scope (pushed_scope);
19638
19639 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19640 added into the symbol table; otherwise, return the TYPE_DECL. */
19641 if (DECL_LANG_SPECIFIC (decl)
19642 && DECL_TEMPLATE_INFO (decl)
19643 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19644 {
19645 decl = DECL_TI_TEMPLATE (decl);
19646 if (member_p)
19647 check_member_template (decl);
19648 }
19649
19650 return decl;
19651 }
19652
19653 /* Parse a using-directive.
19654
19655 using-directive:
19656 using namespace :: [opt] nested-name-specifier [opt]
19657 namespace-name ; */
19658
19659 static void
19660 cp_parser_using_directive (cp_parser* parser)
19661 {
19662 tree namespace_decl;
19663 tree attribs;
19664
19665 /* Look for the `using' keyword. */
19666 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19667 /* And the `namespace' keyword. */
19668 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19669 /* Look for the optional `::' operator. */
19670 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19671 /* And the optional nested-name-specifier. */
19672 cp_parser_nested_name_specifier_opt (parser,
19673 /*typename_keyword_p=*/false,
19674 /*check_dependency_p=*/true,
19675 /*type_p=*/false,
19676 /*is_declaration=*/true);
19677 /* Get the namespace being used. */
19678 namespace_decl = cp_parser_namespace_name (parser);
19679 /* And any specified attributes. */
19680 attribs = cp_parser_attributes_opt (parser);
19681
19682 /* Update the symbol table. */
19683 if (namespace_bindings_p ())
19684 finish_namespace_using_directive (namespace_decl, attribs);
19685 else
19686 finish_local_using_directive (namespace_decl, attribs);
19687
19688 /* Look for the final `;'. */
19689 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19690 }
19691
19692 /* Parse an asm-definition.
19693
19694 asm-qualifier:
19695 volatile
19696 inline
19697 goto
19698
19699 asm-qualifier-list:
19700 asm-qualifier
19701 asm-qualifier-list asm-qualifier
19702
19703 asm-definition:
19704 asm ( string-literal ) ;
19705
19706 GNU Extension:
19707
19708 asm-definition:
19709 asm asm-qualifier-list [opt] ( string-literal ) ;
19710 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19711 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19712 : asm-operand-list [opt] ) ;
19713 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19714 : asm-operand-list [opt]
19715 : asm-clobber-list [opt] ) ;
19716 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19717 : asm-clobber-list [opt]
19718 : asm-goto-list ) ;
19719
19720 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19721 contains goto, and is the only allowed form in that case. No duplicates are
19722 allowed in an asm-qualifier-list. */
19723
19724 static void
19725 cp_parser_asm_definition (cp_parser* parser)
19726 {
19727 tree string;
19728 tree outputs = NULL_TREE;
19729 tree inputs = NULL_TREE;
19730 tree clobbers = NULL_TREE;
19731 tree labels = NULL_TREE;
19732 tree asm_stmt;
19733 bool extended_p = false;
19734 bool invalid_inputs_p = false;
19735 bool invalid_outputs_p = false;
19736 required_token missing = RT_NONE;
19737
19738 /* Look for the `asm' keyword. */
19739 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19740
19741 if (parser->in_function_body
19742 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19743 {
19744 error ("%<asm%> in %<constexpr%> function");
19745 cp_function_chain->invalid_constexpr = true;
19746 }
19747
19748 /* Handle the asm-qualifier-list. */
19749 location_t volatile_loc = UNKNOWN_LOCATION;
19750 location_t inline_loc = UNKNOWN_LOCATION;
19751 location_t goto_loc = UNKNOWN_LOCATION;
19752
19753 if (cp_parser_allow_gnu_extensions_p (parser) && parser->in_function_body)
19754 for (;;)
19755 {
19756 cp_token *token = cp_lexer_peek_token (parser->lexer);
19757 location_t loc = token->location;
19758 switch (cp_lexer_peek_token (parser->lexer)->keyword)
19759 {
19760 case RID_VOLATILE:
19761 if (volatile_loc)
19762 {
19763 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19764 inform (volatile_loc, "first seen here");
19765 }
19766 else
19767 volatile_loc = loc;
19768 cp_lexer_consume_token (parser->lexer);
19769 continue;
19770
19771 case RID_INLINE:
19772 if (inline_loc)
19773 {
19774 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19775 inform (inline_loc, "first seen here");
19776 }
19777 else
19778 inline_loc = loc;
19779 cp_lexer_consume_token (parser->lexer);
19780 continue;
19781
19782 case RID_GOTO:
19783 if (goto_loc)
19784 {
19785 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19786 inform (goto_loc, "first seen here");
19787 }
19788 else
19789 goto_loc = loc;
19790 cp_lexer_consume_token (parser->lexer);
19791 continue;
19792
19793 case RID_CONST:
19794 case RID_RESTRICT:
19795 error_at (loc, "%qT is not an asm qualifier", token->u.value);
19796 cp_lexer_consume_token (parser->lexer);
19797 continue;
19798
19799 default:
19800 break;
19801 }
19802 break;
19803 }
19804
19805 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
19806 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
19807 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
19808
19809 /* Look for the opening `('. */
19810 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19811 return;
19812 /* Look for the string. */
19813 string = cp_parser_string_literal (parser, false, false);
19814 if (string == error_mark_node)
19815 {
19816 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19817 /*consume_paren=*/true);
19818 return;
19819 }
19820
19821 /* If we're allowing GNU extensions, check for the extended assembly
19822 syntax. Unfortunately, the `:' tokens need not be separated by
19823 a space in C, and so, for compatibility, we tolerate that here
19824 too. Doing that means that we have to treat the `::' operator as
19825 two `:' tokens. */
19826 if (cp_parser_allow_gnu_extensions_p (parser)
19827 && parser->in_function_body
19828 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19829 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19830 {
19831 bool inputs_p = false;
19832 bool clobbers_p = false;
19833 bool labels_p = false;
19834
19835 /* The extended syntax was used. */
19836 extended_p = true;
19837
19838 /* Look for outputs. */
19839 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19840 {
19841 /* Consume the `:'. */
19842 cp_lexer_consume_token (parser->lexer);
19843 /* Parse the output-operands. */
19844 if (cp_lexer_next_token_is_not (parser->lexer,
19845 CPP_COLON)
19846 && cp_lexer_next_token_is_not (parser->lexer,
19847 CPP_SCOPE)
19848 && cp_lexer_next_token_is_not (parser->lexer,
19849 CPP_CLOSE_PAREN)
19850 && !goto_p)
19851 {
19852 outputs = cp_parser_asm_operand_list (parser);
19853 if (outputs == error_mark_node)
19854 invalid_outputs_p = true;
19855 }
19856 }
19857 /* If the next token is `::', there are no outputs, and the
19858 next token is the beginning of the inputs. */
19859 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19860 /* The inputs are coming next. */
19861 inputs_p = true;
19862
19863 /* Look for inputs. */
19864 if (inputs_p
19865 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19866 {
19867 /* Consume the `:' or `::'. */
19868 cp_lexer_consume_token (parser->lexer);
19869 /* Parse the output-operands. */
19870 if (cp_lexer_next_token_is_not (parser->lexer,
19871 CPP_COLON)
19872 && cp_lexer_next_token_is_not (parser->lexer,
19873 CPP_SCOPE)
19874 && cp_lexer_next_token_is_not (parser->lexer,
19875 CPP_CLOSE_PAREN))
19876 {
19877 inputs = cp_parser_asm_operand_list (parser);
19878 if (inputs == error_mark_node)
19879 invalid_inputs_p = true;
19880 }
19881 }
19882 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19883 /* The clobbers are coming next. */
19884 clobbers_p = true;
19885
19886 /* Look for clobbers. */
19887 if (clobbers_p
19888 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19889 {
19890 clobbers_p = true;
19891 /* Consume the `:' or `::'. */
19892 cp_lexer_consume_token (parser->lexer);
19893 /* Parse the clobbers. */
19894 if (cp_lexer_next_token_is_not (parser->lexer,
19895 CPP_COLON)
19896 && cp_lexer_next_token_is_not (parser->lexer,
19897 CPP_CLOSE_PAREN))
19898 clobbers = cp_parser_asm_clobber_list (parser);
19899 }
19900 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19901 /* The labels are coming next. */
19902 labels_p = true;
19903
19904 /* Look for labels. */
19905 if (labels_p
19906 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19907 {
19908 labels_p = true;
19909 /* Consume the `:' or `::'. */
19910 cp_lexer_consume_token (parser->lexer);
19911 /* Parse the labels. */
19912 labels = cp_parser_asm_label_list (parser);
19913 }
19914
19915 if (goto_p && !labels_p)
19916 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19917 }
19918 else if (goto_p)
19919 missing = RT_COLON_SCOPE;
19920
19921 /* Look for the closing `)'. */
19922 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19923 missing ? missing : RT_CLOSE_PAREN))
19924 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19925 /*consume_paren=*/true);
19926 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19927
19928 if (!invalid_inputs_p && !invalid_outputs_p)
19929 {
19930 /* Create the ASM_EXPR. */
19931 if (parser->in_function_body)
19932 {
19933 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19934 inputs, clobbers, labels, inline_p);
19935 /* If the extended syntax was not used, mark the ASM_EXPR. */
19936 if (!extended_p)
19937 {
19938 tree temp = asm_stmt;
19939 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19940 temp = TREE_OPERAND (temp, 0);
19941
19942 ASM_INPUT_P (temp) = 1;
19943 }
19944 }
19945 else
19946 symtab->finalize_toplevel_asm (string);
19947 }
19948 }
19949
19950 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19951 type that comes from the decl-specifier-seq. */
19952
19953 static tree
19954 strip_declarator_types (tree type, cp_declarator *declarator)
19955 {
19956 for (cp_declarator *d = declarator; d;)
19957 switch (d->kind)
19958 {
19959 case cdk_id:
19960 case cdk_decomp:
19961 case cdk_error:
19962 d = NULL;
19963 break;
19964
19965 default:
19966 if (TYPE_PTRMEMFUNC_P (type))
19967 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19968 type = TREE_TYPE (type);
19969 d = d->declarator;
19970 break;
19971 }
19972
19973 return type;
19974 }
19975
19976 /* Declarators [gram.dcl.decl] */
19977
19978 /* Parse an init-declarator.
19979
19980 init-declarator:
19981 declarator initializer [opt]
19982
19983 GNU Extension:
19984
19985 init-declarator:
19986 declarator asm-specification [opt] attributes [opt] initializer [opt]
19987
19988 function-definition:
19989 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19990 function-body
19991 decl-specifier-seq [opt] declarator function-try-block
19992
19993 GNU Extension:
19994
19995 function-definition:
19996 __extension__ function-definition
19997
19998 TM Extension:
19999
20000 function-definition:
20001 decl-specifier-seq [opt] declarator function-transaction-block
20002
20003 The parser flags FLAGS is used to control type-specifier parsing.
20004
20005 The DECL_SPECIFIERS apply to this declarator. Returns a
20006 representation of the entity declared. If MEMBER_P is TRUE, then
20007 this declarator appears in a class scope. The new DECL created by
20008 this declarator is returned.
20009
20010 The CHECKS are access checks that should be performed once we know
20011 what entity is being declared (and, therefore, what classes have
20012 befriended it).
20013
20014 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20015 for a function-definition here as well. If the declarator is a
20016 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20017 be TRUE upon return. By that point, the function-definition will
20018 have been completely parsed.
20019
20020 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20021 is FALSE.
20022
20023 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20024 parsed declaration if it is an uninitialized single declarator not followed
20025 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20026 if present, will not be consumed. If returned, this declarator will be
20027 created with SD_INITIALIZED but will not call cp_finish_decl.
20028
20029 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20030 and there is an initializer, the pointed location_t is set to the
20031 location of the '=' or `(', or '{' in C++11 token introducing the
20032 initializer. */
20033
20034 static tree
20035 cp_parser_init_declarator (cp_parser* parser,
20036 cp_parser_flags flags,
20037 cp_decl_specifier_seq *decl_specifiers,
20038 vec<deferred_access_check, va_gc> *checks,
20039 bool function_definition_allowed_p,
20040 bool member_p,
20041 int declares_class_or_enum,
20042 bool* function_definition_p,
20043 tree* maybe_range_for_decl,
20044 location_t* init_loc,
20045 tree* auto_result)
20046 {
20047 cp_token *token = NULL, *asm_spec_start_token = NULL,
20048 *attributes_start_token = NULL;
20049 cp_declarator *declarator;
20050 tree prefix_attributes;
20051 tree attributes = NULL;
20052 tree asm_specification;
20053 tree initializer;
20054 tree decl = NULL_TREE;
20055 tree scope;
20056 int is_initialized;
20057 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20058 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20059 "(...)". */
20060 enum cpp_ttype initialization_kind;
20061 bool is_direct_init = false;
20062 bool is_non_constant_init;
20063 int ctor_dtor_or_conv_p;
20064 bool friend_p = cp_parser_friend_p (decl_specifiers);
20065 tree pushed_scope = NULL_TREE;
20066 bool range_for_decl_p = false;
20067 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20068 location_t tmp_init_loc = UNKNOWN_LOCATION;
20069
20070 /* Gather the attributes that were provided with the
20071 decl-specifiers. */
20072 prefix_attributes = decl_specifiers->attributes;
20073
20074 /* Assume that this is not the declarator for a function
20075 definition. */
20076 if (function_definition_p)
20077 *function_definition_p = false;
20078
20079 /* Default arguments are only permitted for function parameters. */
20080 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
20081 parser->default_arg_ok_p = false;
20082
20083 /* Defer access checks while parsing the declarator; we cannot know
20084 what names are accessible until we know what is being
20085 declared. */
20086 resume_deferring_access_checks ();
20087
20088 token = cp_lexer_peek_token (parser->lexer);
20089
20090 /* Parse the declarator. */
20091 declarator
20092 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20093 flags, &ctor_dtor_or_conv_p,
20094 /*parenthesized_p=*/NULL,
20095 member_p, friend_p, /*static_p=*/false);
20096 /* Gather up the deferred checks. */
20097 stop_deferring_access_checks ();
20098
20099 parser->default_arg_ok_p = saved_default_arg_ok_p;
20100
20101 /* If the DECLARATOR was erroneous, there's no need to go
20102 further. */
20103 if (declarator == cp_error_declarator)
20104 return error_mark_node;
20105
20106 /* Check that the number of template-parameter-lists is OK. */
20107 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
20108 token->location))
20109 return error_mark_node;
20110
20111 if (declares_class_or_enum & 2)
20112 cp_parser_check_for_definition_in_return_type (declarator,
20113 decl_specifiers->type,
20114 decl_specifiers->locations[ds_type_spec]);
20115
20116 /* Figure out what scope the entity declared by the DECLARATOR is
20117 located in. `grokdeclarator' sometimes changes the scope, so
20118 we compute it now. */
20119 scope = get_scope_of_declarator (declarator);
20120
20121 /* Perform any lookups in the declared type which were thought to be
20122 dependent, but are not in the scope of the declarator. */
20123 decl_specifiers->type
20124 = maybe_update_decl_type (decl_specifiers->type, scope);
20125
20126 /* If we're allowing GNU extensions, look for an
20127 asm-specification. */
20128 if (cp_parser_allow_gnu_extensions_p (parser))
20129 {
20130 /* Look for an asm-specification. */
20131 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
20132 asm_specification = cp_parser_asm_specification_opt (parser);
20133 }
20134 else
20135 asm_specification = NULL_TREE;
20136
20137 /* Look for attributes. */
20138 attributes_start_token = cp_lexer_peek_token (parser->lexer);
20139 attributes = cp_parser_attributes_opt (parser);
20140
20141 /* Peek at the next token. */
20142 token = cp_lexer_peek_token (parser->lexer);
20143
20144 bool bogus_implicit_tmpl = false;
20145
20146 if (function_declarator_p (declarator))
20147 {
20148 /* Handle C++17 deduction guides. */
20149 if (!decl_specifiers->type
20150 && ctor_dtor_or_conv_p <= 0
20151 && cxx_dialect >= cxx17)
20152 {
20153 cp_declarator *id = get_id_declarator (declarator);
20154 tree name = id->u.id.unqualified_name;
20155 parser->scope = id->u.id.qualifying_scope;
20156 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
20157 if (tmpl
20158 && (DECL_CLASS_TEMPLATE_P (tmpl)
20159 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
20160 {
20161 id->u.id.unqualified_name = dguide_name (tmpl);
20162 id->u.id.sfk = sfk_deduction_guide;
20163 ctor_dtor_or_conv_p = 1;
20164 }
20165 }
20166
20167 /* Check to see if the token indicates the start of a
20168 function-definition. */
20169 if (cp_parser_token_starts_function_definition_p (token))
20170 {
20171 if (!function_definition_allowed_p)
20172 {
20173 /* If a function-definition should not appear here, issue an
20174 error message. */
20175 cp_parser_error (parser,
20176 "a function-definition is not allowed here");
20177 return error_mark_node;
20178 }
20179
20180 location_t func_brace_location
20181 = cp_lexer_peek_token (parser->lexer)->location;
20182
20183 /* Neither attributes nor an asm-specification are allowed
20184 on a function-definition. */
20185 if (asm_specification)
20186 error_at (asm_spec_start_token->location,
20187 "an asm-specification is not allowed "
20188 "on a function-definition");
20189 if (attributes)
20190 error_at (attributes_start_token->location,
20191 "attributes are not allowed "
20192 "on a function-definition");
20193 /* This is a function-definition. */
20194 *function_definition_p = true;
20195
20196 /* Parse the function definition. */
20197 if (member_p)
20198 decl = cp_parser_save_member_function_body (parser,
20199 decl_specifiers,
20200 declarator,
20201 prefix_attributes);
20202 else
20203 decl =
20204 (cp_parser_function_definition_from_specifiers_and_declarator
20205 (parser, decl_specifiers, prefix_attributes, declarator));
20206
20207 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
20208 {
20209 /* This is where the prologue starts... */
20210 DECL_STRUCT_FUNCTION (decl)->function_start_locus
20211 = func_brace_location;
20212 }
20213
20214 return decl;
20215 }
20216 }
20217 else if (parser->fully_implicit_function_template_p)
20218 {
20219 /* A non-template declaration involving a function parameter list
20220 containing an implicit template parameter will be made into a
20221 template. If the resulting declaration is not going to be an
20222 actual function then finish the template scope here to prevent it.
20223 An error message will be issued once we have a decl to talk about.
20224
20225 FIXME probably we should do type deduction rather than create an
20226 implicit template, but the standard currently doesn't allow it. */
20227 bogus_implicit_tmpl = true;
20228 finish_fully_implicit_template (parser, NULL_TREE);
20229 }
20230
20231 /* [dcl.dcl]
20232
20233 Only in function declarations for constructors, destructors, type
20234 conversions, and deduction guides can the decl-specifier-seq be omitted.
20235
20236 We explicitly postpone this check past the point where we handle
20237 function-definitions because we tolerate function-definitions
20238 that are missing their return types in some modes. */
20239 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
20240 {
20241 cp_parser_error (parser,
20242 "expected constructor, destructor, or type conversion");
20243 return error_mark_node;
20244 }
20245
20246 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20247 if (token->type == CPP_EQ
20248 || token->type == CPP_OPEN_PAREN
20249 || token->type == CPP_OPEN_BRACE)
20250 {
20251 is_initialized = SD_INITIALIZED;
20252 initialization_kind = token->type;
20253 if (maybe_range_for_decl)
20254 *maybe_range_for_decl = error_mark_node;
20255 tmp_init_loc = token->location;
20256 if (init_loc && *init_loc == UNKNOWN_LOCATION)
20257 *init_loc = tmp_init_loc;
20258
20259 if (token->type == CPP_EQ
20260 && function_declarator_p (declarator))
20261 {
20262 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
20263 if (t2->keyword == RID_DEFAULT)
20264 is_initialized = SD_DEFAULTED;
20265 else if (t2->keyword == RID_DELETE)
20266 is_initialized = SD_DELETED;
20267 }
20268 }
20269 else
20270 {
20271 /* If the init-declarator isn't initialized and isn't followed by a
20272 `,' or `;', it's not a valid init-declarator. */
20273 if (token->type != CPP_COMMA
20274 && token->type != CPP_SEMICOLON)
20275 {
20276 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
20277 range_for_decl_p = true;
20278 else
20279 {
20280 if (!maybe_range_for_decl)
20281 cp_parser_error (parser, "expected initializer");
20282 return error_mark_node;
20283 }
20284 }
20285 is_initialized = SD_UNINITIALIZED;
20286 initialization_kind = CPP_EOF;
20287 }
20288
20289 /* Because start_decl has side-effects, we should only call it if we
20290 know we're going ahead. By this point, we know that we cannot
20291 possibly be looking at any other construct. */
20292 cp_parser_commit_to_tentative_parse (parser);
20293
20294 /* Enter the newly declared entry in the symbol table. If we're
20295 processing a declaration in a class-specifier, we wait until
20296 after processing the initializer. */
20297 if (!member_p)
20298 {
20299 if (parser->in_unbraced_linkage_specification_p)
20300 decl_specifiers->storage_class = sc_extern;
20301 decl = start_decl (declarator, decl_specifiers,
20302 range_for_decl_p? SD_INITIALIZED : is_initialized,
20303 attributes, prefix_attributes, &pushed_scope);
20304 cp_finalize_omp_declare_simd (parser, decl);
20305 cp_finalize_oacc_routine (parser, decl, false);
20306 /* Adjust location of decl if declarator->id_loc is more appropriate:
20307 set, and decl wasn't merged with another decl, in which case its
20308 location would be different from input_location, and more accurate. */
20309 if (DECL_P (decl)
20310 && declarator->id_loc != UNKNOWN_LOCATION
20311 && DECL_SOURCE_LOCATION (decl) == input_location)
20312 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
20313 }
20314 else if (scope)
20315 /* Enter the SCOPE. That way unqualified names appearing in the
20316 initializer will be looked up in SCOPE. */
20317 pushed_scope = push_scope (scope);
20318
20319 /* Perform deferred access control checks, now that we know in which
20320 SCOPE the declared entity resides. */
20321 if (!member_p && decl)
20322 {
20323 tree saved_current_function_decl = NULL_TREE;
20324
20325 /* If the entity being declared is a function, pretend that we
20326 are in its scope. If it is a `friend', it may have access to
20327 things that would not otherwise be accessible. */
20328 if (TREE_CODE (decl) == FUNCTION_DECL)
20329 {
20330 saved_current_function_decl = current_function_decl;
20331 current_function_decl = decl;
20332 }
20333
20334 /* Perform access checks for template parameters. */
20335 cp_parser_perform_template_parameter_access_checks (checks);
20336
20337 /* Perform the access control checks for the declarator and the
20338 decl-specifiers. */
20339 perform_deferred_access_checks (tf_warning_or_error);
20340
20341 /* Restore the saved value. */
20342 if (TREE_CODE (decl) == FUNCTION_DECL)
20343 current_function_decl = saved_current_function_decl;
20344 }
20345
20346 /* Parse the initializer. */
20347 initializer = NULL_TREE;
20348 is_direct_init = false;
20349 is_non_constant_init = true;
20350 if (is_initialized)
20351 {
20352 if (function_declarator_p (declarator))
20353 {
20354 if (initialization_kind == CPP_EQ)
20355 initializer = cp_parser_pure_specifier (parser);
20356 else
20357 {
20358 /* If the declaration was erroneous, we don't really
20359 know what the user intended, so just silently
20360 consume the initializer. */
20361 if (decl != error_mark_node)
20362 error_at (tmp_init_loc, "initializer provided for function");
20363 cp_parser_skip_to_closing_parenthesis (parser,
20364 /*recovering=*/true,
20365 /*or_comma=*/false,
20366 /*consume_paren=*/true);
20367 }
20368 }
20369 else
20370 {
20371 /* We want to record the extra mangling scope for in-class
20372 initializers of class members and initializers of static data
20373 member templates. The former involves deferring
20374 parsing of the initializer until end of class as with default
20375 arguments. So right here we only handle the latter. */
20376 if (!member_p && processing_template_decl && decl != error_mark_node)
20377 start_lambda_scope (decl);
20378 initializer = cp_parser_initializer (parser,
20379 &is_direct_init,
20380 &is_non_constant_init);
20381 if (!member_p && processing_template_decl && decl != error_mark_node)
20382 finish_lambda_scope ();
20383 if (initializer == error_mark_node)
20384 cp_parser_skip_to_end_of_statement (parser);
20385 }
20386 }
20387
20388 /* The old parser allows attributes to appear after a parenthesized
20389 initializer. Mark Mitchell proposed removing this functionality
20390 on the GCC mailing lists on 2002-08-13. This parser accepts the
20391 attributes -- but ignores them. Made a permerror in GCC 8. */
20392 if (cp_parser_allow_gnu_extensions_p (parser)
20393 && initialization_kind == CPP_OPEN_PAREN
20394 && cp_parser_attributes_opt (parser)
20395 && permerror (input_location,
20396 "attributes after parenthesized initializer ignored"))
20397 {
20398 static bool hint;
20399 if (flag_permissive && !hint)
20400 {
20401 hint = true;
20402 inform (input_location,
20403 "this flexibility is deprecated and will be removed");
20404 }
20405 }
20406
20407 /* And now complain about a non-function implicit template. */
20408 if (bogus_implicit_tmpl && decl != error_mark_node)
20409 error_at (DECL_SOURCE_LOCATION (decl),
20410 "non-function %qD declared as implicit template", decl);
20411
20412 /* For an in-class declaration, use `grokfield' to create the
20413 declaration. */
20414 if (member_p)
20415 {
20416 if (pushed_scope)
20417 {
20418 pop_scope (pushed_scope);
20419 pushed_scope = NULL_TREE;
20420 }
20421 decl = grokfield (declarator, decl_specifiers,
20422 initializer, !is_non_constant_init,
20423 /*asmspec=*/NULL_TREE,
20424 attr_chainon (attributes, prefix_attributes));
20425 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20426 cp_parser_save_default_args (parser, decl);
20427 cp_finalize_omp_declare_simd (parser, decl);
20428 cp_finalize_oacc_routine (parser, decl, false);
20429 }
20430
20431 /* Finish processing the declaration. But, skip member
20432 declarations. */
20433 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20434 {
20435 cp_finish_decl (decl,
20436 initializer, !is_non_constant_init,
20437 asm_specification,
20438 /* If the initializer is in parentheses, then this is
20439 a direct-initialization, which means that an
20440 `explicit' constructor is OK. Otherwise, an
20441 `explicit' constructor cannot be used. */
20442 ((is_direct_init || !is_initialized)
20443 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20444 }
20445 else if ((cxx_dialect != cxx98) && friend_p
20446 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20447 /* Core issue #226 (C++0x only): A default template-argument
20448 shall not be specified in a friend class template
20449 declaration. */
20450 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20451 /*is_partial=*/false, /*is_friend_decl=*/1);
20452
20453 if (!friend_p && pushed_scope)
20454 pop_scope (pushed_scope);
20455
20456 if (function_declarator_p (declarator)
20457 && parser->fully_implicit_function_template_p)
20458 {
20459 if (member_p)
20460 decl = finish_fully_implicit_template (parser, decl);
20461 else
20462 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20463 }
20464
20465 if (auto_result && is_initialized && decl_specifiers->type
20466 && type_uses_auto (decl_specifiers->type))
20467 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20468
20469 return decl;
20470 }
20471
20472 /* Parse a declarator.
20473
20474 declarator:
20475 direct-declarator
20476 ptr-operator declarator
20477
20478 abstract-declarator:
20479 ptr-operator abstract-declarator [opt]
20480 direct-abstract-declarator
20481
20482 GNU Extensions:
20483
20484 declarator:
20485 attributes [opt] direct-declarator
20486 attributes [opt] ptr-operator declarator
20487
20488 abstract-declarator:
20489 attributes [opt] ptr-operator abstract-declarator [opt]
20490 attributes [opt] direct-abstract-declarator
20491
20492 The parser flags FLAGS is used to control type-specifier parsing.
20493
20494 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20495 detect constructors, destructors, deduction guides, or conversion operators.
20496 It is set to -1 if the declarator is a name, and +1 if it is a
20497 function. Otherwise it is set to zero. Usually you just want to
20498 test for >0, but internally the negative value is used.
20499
20500 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20501 a decl-specifier-seq unless it declares a constructor, destructor,
20502 or conversion. It might seem that we could check this condition in
20503 semantic analysis, rather than parsing, but that makes it difficult
20504 to handle something like `f()'. We want to notice that there are
20505 no decl-specifiers, and therefore realize that this is an
20506 expression, not a declaration.)
20507
20508 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20509 the declarator is a direct-declarator of the form "(...)".
20510
20511 MEMBER_P is true iff this declarator is a member-declarator.
20512
20513 FRIEND_P is true iff this declarator is a friend.
20514
20515 STATIC_P is true iff the keyword static was seen. */
20516
20517 static cp_declarator *
20518 cp_parser_declarator (cp_parser* parser,
20519 cp_parser_declarator_kind dcl_kind,
20520 cp_parser_flags flags,
20521 int* ctor_dtor_or_conv_p,
20522 bool* parenthesized_p,
20523 bool member_p, bool friend_p, bool static_p)
20524 {
20525 cp_declarator *declarator;
20526 enum tree_code code;
20527 cp_cv_quals cv_quals;
20528 tree class_type;
20529 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20530
20531 /* Assume this is not a constructor, destructor, or type-conversion
20532 operator. */
20533 if (ctor_dtor_or_conv_p)
20534 *ctor_dtor_or_conv_p = 0;
20535
20536 if (cp_parser_allow_gnu_extensions_p (parser))
20537 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20538
20539 /* Check for the ptr-operator production. */
20540 cp_parser_parse_tentatively (parser);
20541 /* Parse the ptr-operator. */
20542 code = cp_parser_ptr_operator (parser,
20543 &class_type,
20544 &cv_quals,
20545 &std_attributes);
20546
20547 /* If that worked, then we have a ptr-operator. */
20548 if (cp_parser_parse_definitely (parser))
20549 {
20550 /* If a ptr-operator was found, then this declarator was not
20551 parenthesized. */
20552 if (parenthesized_p)
20553 *parenthesized_p = true;
20554 /* The dependent declarator is optional if we are parsing an
20555 abstract-declarator. */
20556 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20557 cp_parser_parse_tentatively (parser);
20558
20559 /* Parse the dependent declarator. */
20560 declarator = cp_parser_declarator (parser, dcl_kind,
20561 CP_PARSER_FLAGS_NONE,
20562 /*ctor_dtor_or_conv_p=*/NULL,
20563 /*parenthesized_p=*/NULL,
20564 /*member_p=*/false,
20565 friend_p, /*static_p=*/false);
20566
20567 /* If we are parsing an abstract-declarator, we must handle the
20568 case where the dependent declarator is absent. */
20569 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20570 && !cp_parser_parse_definitely (parser))
20571 declarator = NULL;
20572
20573 declarator = cp_parser_make_indirect_declarator
20574 (code, class_type, cv_quals, declarator, std_attributes);
20575 }
20576 /* Everything else is a direct-declarator. */
20577 else
20578 {
20579 if (parenthesized_p)
20580 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20581 CPP_OPEN_PAREN);
20582 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20583 flags, ctor_dtor_or_conv_p,
20584 member_p, friend_p, static_p);
20585 }
20586
20587 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20588 declarator->attributes = gnu_attributes;
20589 return declarator;
20590 }
20591
20592 /* Parse a direct-declarator or direct-abstract-declarator.
20593
20594 direct-declarator:
20595 declarator-id
20596 direct-declarator ( parameter-declaration-clause )
20597 cv-qualifier-seq [opt]
20598 ref-qualifier [opt]
20599 exception-specification [opt]
20600 direct-declarator [ constant-expression [opt] ]
20601 ( declarator )
20602
20603 direct-abstract-declarator:
20604 direct-abstract-declarator [opt]
20605 ( parameter-declaration-clause )
20606 cv-qualifier-seq [opt]
20607 ref-qualifier [opt]
20608 exception-specification [opt]
20609 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20610 ( abstract-declarator )
20611
20612 Returns a representation of the declarator. DCL_KIND is
20613 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20614 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20615 we are parsing a direct-declarator. It is
20616 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20617 of ambiguity we prefer an abstract declarator, as per
20618 [dcl.ambig.res].
20619 The parser flags FLAGS is used to control type-specifier parsing.
20620 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
20621 as for cp_parser_declarator. */
20622
20623 static cp_declarator *
20624 cp_parser_direct_declarator (cp_parser* parser,
20625 cp_parser_declarator_kind dcl_kind,
20626 cp_parser_flags flags,
20627 int* ctor_dtor_or_conv_p,
20628 bool member_p, bool friend_p, bool static_p)
20629 {
20630 cp_token *token;
20631 cp_declarator *declarator = NULL;
20632 tree scope = NULL_TREE;
20633 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20634 bool saved_in_declarator_p = parser->in_declarator_p;
20635 bool first = true;
20636 tree pushed_scope = NULL_TREE;
20637 cp_token *open_paren = NULL, *close_paren = NULL;
20638
20639 while (true)
20640 {
20641 /* Peek at the next token. */
20642 token = cp_lexer_peek_token (parser->lexer);
20643 if (token->type == CPP_OPEN_PAREN)
20644 {
20645 /* This is either a parameter-declaration-clause, or a
20646 parenthesized declarator. When we know we are parsing a
20647 named declarator, it must be a parenthesized declarator
20648 if FIRST is true. For instance, `(int)' is a
20649 parameter-declaration-clause, with an omitted
20650 direct-abstract-declarator. But `((*))', is a
20651 parenthesized abstract declarator. Finally, when T is a
20652 template parameter `(T)' is a
20653 parameter-declaration-clause, and not a parenthesized
20654 named declarator.
20655
20656 We first try and parse a parameter-declaration-clause,
20657 and then try a nested declarator (if FIRST is true).
20658
20659 It is not an error for it not to be a
20660 parameter-declaration-clause, even when FIRST is
20661 false. Consider,
20662
20663 int i (int);
20664 int i (3);
20665
20666 The first is the declaration of a function while the
20667 second is the definition of a variable, including its
20668 initializer.
20669
20670 Having seen only the parenthesis, we cannot know which of
20671 these two alternatives should be selected. Even more
20672 complex are examples like:
20673
20674 int i (int (a));
20675 int i (int (3));
20676
20677 The former is a function-declaration; the latter is a
20678 variable initialization.
20679
20680 Thus again, we try a parameter-declaration-clause, and if
20681 that fails, we back out and return. */
20682
20683 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20684 {
20685 tree params;
20686 bool is_declarator = false;
20687
20688 open_paren = NULL;
20689
20690 /* In a member-declarator, the only valid interpretation
20691 of a parenthesis is the start of a
20692 parameter-declaration-clause. (It is invalid to
20693 initialize a static data member with a parenthesized
20694 initializer; only the "=" form of initialization is
20695 permitted.) */
20696 if (!member_p)
20697 cp_parser_parse_tentatively (parser);
20698
20699 /* Consume the `('. */
20700 matching_parens parens;
20701 parens.consume_open (parser);
20702 if (first)
20703 {
20704 /* If this is going to be an abstract declarator, we're
20705 in a declarator and we can't have default args. */
20706 parser->default_arg_ok_p = false;
20707 parser->in_declarator_p = true;
20708 }
20709
20710 begin_scope (sk_function_parms, NULL_TREE);
20711
20712 /* Parse the parameter-declaration-clause. */
20713 params
20714 = cp_parser_parameter_declaration_clause (parser, flags);
20715
20716 /* Consume the `)'. */
20717 parens.require_close (parser);
20718
20719 /* If all went well, parse the cv-qualifier-seq,
20720 ref-qualifier and the exception-specification. */
20721 if (member_p || cp_parser_parse_definitely (parser))
20722 {
20723 cp_cv_quals cv_quals;
20724 cp_virt_specifiers virt_specifiers;
20725 cp_ref_qualifier ref_qual;
20726 tree exception_specification;
20727 tree late_return;
20728 tree attrs;
20729 bool memfn = (member_p || (pushed_scope
20730 && CLASS_TYPE_P (pushed_scope)));
20731 unsigned char local_variables_forbidden_p
20732 = parser->local_variables_forbidden_p;
20733 /* 'this' is not allowed in static member functions. */
20734 if (static_p || friend_p)
20735 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
20736
20737 is_declarator = true;
20738
20739 if (ctor_dtor_or_conv_p)
20740 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20741 first = false;
20742
20743 /* Parse the cv-qualifier-seq. */
20744 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20745 /* Parse the ref-qualifier. */
20746 ref_qual = cp_parser_ref_qualifier_opt (parser);
20747 /* Parse the tx-qualifier. */
20748 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20749 /* And the exception-specification. */
20750 exception_specification
20751 = cp_parser_exception_specification_opt (parser);
20752
20753 attrs = cp_parser_std_attribute_spec_seq (parser);
20754
20755 /* In here, we handle cases where attribute is used after
20756 the function declaration. For example:
20757 void func (int x) __attribute__((vector(..))); */
20758 tree gnu_attrs = NULL_TREE;
20759 tree requires_clause = NULL_TREE;
20760 late_return = (cp_parser_late_return_type_opt
20761 (parser, declarator, requires_clause,
20762 memfn ? cv_quals : -1));
20763
20764 /* Parse the virt-specifier-seq. */
20765 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20766
20767 /* Create the function-declarator. */
20768 declarator = make_call_declarator (declarator,
20769 params,
20770 cv_quals,
20771 virt_specifiers,
20772 ref_qual,
20773 tx_qual,
20774 exception_specification,
20775 late_return,
20776 requires_clause);
20777 declarator->std_attributes = attrs;
20778 declarator->attributes = gnu_attrs;
20779 /* Any subsequent parameter lists are to do with
20780 return type, so are not those of the declared
20781 function. */
20782 parser->default_arg_ok_p = false;
20783
20784 /* Restore the state of local_variables_forbidden_p. */
20785 parser->local_variables_forbidden_p
20786 = local_variables_forbidden_p;
20787 }
20788
20789 /* Remove the function parms from scope. */
20790 pop_bindings_and_leave_scope ();
20791
20792 if (is_declarator)
20793 /* Repeat the main loop. */
20794 continue;
20795 }
20796
20797 /* If this is the first, we can try a parenthesized
20798 declarator. */
20799 if (first)
20800 {
20801 bool saved_in_type_id_in_expr_p;
20802
20803 parser->default_arg_ok_p = saved_default_arg_ok_p;
20804 parser->in_declarator_p = saved_in_declarator_p;
20805
20806 open_paren = token;
20807 /* Consume the `('. */
20808 matching_parens parens;
20809 parens.consume_open (parser);
20810 /* Parse the nested declarator. */
20811 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20812 parser->in_type_id_in_expr_p = true;
20813 declarator
20814 = cp_parser_declarator (parser, dcl_kind, flags,
20815 ctor_dtor_or_conv_p,
20816 /*parenthesized_p=*/NULL,
20817 member_p, friend_p,
20818 /*static_p=*/false);
20819 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20820 first = false;
20821 /* Expect a `)'. */
20822 close_paren = cp_lexer_peek_token (parser->lexer);
20823 if (!parens.require_close (parser))
20824 declarator = cp_error_declarator;
20825 if (declarator == cp_error_declarator)
20826 break;
20827
20828 goto handle_declarator;
20829 }
20830 /* Otherwise, we must be done. */
20831 else
20832 break;
20833 }
20834 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20835 && token->type == CPP_OPEN_SQUARE
20836 && !cp_next_tokens_can_be_attribute_p (parser))
20837 {
20838 /* Parse an array-declarator. */
20839 tree bounds, attrs;
20840
20841 if (ctor_dtor_or_conv_p)
20842 *ctor_dtor_or_conv_p = 0;
20843
20844 open_paren = NULL;
20845 first = false;
20846 parser->default_arg_ok_p = false;
20847 parser->in_declarator_p = true;
20848 /* Consume the `['. */
20849 cp_lexer_consume_token (parser->lexer);
20850 /* Peek at the next token. */
20851 token = cp_lexer_peek_token (parser->lexer);
20852 /* If the next token is `]', then there is no
20853 constant-expression. */
20854 if (token->type != CPP_CLOSE_SQUARE)
20855 {
20856 bool non_constant_p;
20857 bounds
20858 = cp_parser_constant_expression (parser,
20859 /*allow_non_constant=*/true,
20860 &non_constant_p);
20861 if (!non_constant_p)
20862 /* OK */;
20863 else if (error_operand_p (bounds))
20864 /* Already gave an error. */;
20865 else if (!parser->in_function_body
20866 || current_binding_level->kind == sk_function_parms)
20867 {
20868 /* Normally, the array bound must be an integral constant
20869 expression. However, as an extension, we allow VLAs
20870 in function scopes as long as they aren't part of a
20871 parameter declaration. */
20872 cp_parser_error (parser,
20873 "array bound is not an integer constant");
20874 bounds = error_mark_node;
20875 }
20876 else if (processing_template_decl
20877 && !type_dependent_expression_p (bounds))
20878 {
20879 /* Remember this wasn't a constant-expression. */
20880 bounds = build_nop (TREE_TYPE (bounds), bounds);
20881 TREE_SIDE_EFFECTS (bounds) = 1;
20882 }
20883 }
20884 else
20885 bounds = NULL_TREE;
20886 /* Look for the closing `]'. */
20887 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20888 {
20889 declarator = cp_error_declarator;
20890 break;
20891 }
20892
20893 attrs = cp_parser_std_attribute_spec_seq (parser);
20894 declarator = make_array_declarator (declarator, bounds);
20895 declarator->std_attributes = attrs;
20896 }
20897 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20898 {
20899 {
20900 tree qualifying_scope;
20901 tree unqualified_name;
20902 tree attrs;
20903 special_function_kind sfk;
20904 bool abstract_ok;
20905 bool pack_expansion_p = false;
20906 cp_token *declarator_id_start_token;
20907
20908 /* Parse a declarator-id */
20909 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20910 if (abstract_ok)
20911 {
20912 cp_parser_parse_tentatively (parser);
20913
20914 /* If we see an ellipsis, we should be looking at a
20915 parameter pack. */
20916 if (token->type == CPP_ELLIPSIS)
20917 {
20918 /* Consume the `...' */
20919 cp_lexer_consume_token (parser->lexer);
20920
20921 pack_expansion_p = true;
20922 }
20923 }
20924
20925 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20926 unqualified_name
20927 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20928 qualifying_scope = parser->scope;
20929 if (abstract_ok)
20930 {
20931 bool okay = false;
20932
20933 if (!unqualified_name && pack_expansion_p)
20934 {
20935 /* Check whether an error occurred. */
20936 okay = !cp_parser_error_occurred (parser);
20937
20938 /* We already consumed the ellipsis to mark a
20939 parameter pack, but we have no way to report it,
20940 so abort the tentative parse. We will be exiting
20941 immediately anyway. */
20942 cp_parser_abort_tentative_parse (parser);
20943 }
20944 else
20945 okay = cp_parser_parse_definitely (parser);
20946
20947 if (!okay)
20948 unqualified_name = error_mark_node;
20949 else if (unqualified_name
20950 && (qualifying_scope
20951 || (!identifier_p (unqualified_name))))
20952 {
20953 cp_parser_error (parser, "expected unqualified-id");
20954 unqualified_name = error_mark_node;
20955 }
20956 }
20957
20958 if (!unqualified_name)
20959 return NULL;
20960 if (unqualified_name == error_mark_node)
20961 {
20962 declarator = cp_error_declarator;
20963 pack_expansion_p = false;
20964 declarator->parameter_pack_p = false;
20965 break;
20966 }
20967
20968 attrs = cp_parser_std_attribute_spec_seq (parser);
20969
20970 if (qualifying_scope && at_namespace_scope_p ()
20971 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20972 {
20973 /* In the declaration of a member of a template class
20974 outside of the class itself, the SCOPE will sometimes
20975 be a TYPENAME_TYPE. For example, given:
20976
20977 template <typename T>
20978 int S<T>::R::i = 3;
20979
20980 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20981 this context, we must resolve S<T>::R to an ordinary
20982 type, rather than a typename type.
20983
20984 The reason we normally avoid resolving TYPENAME_TYPEs
20985 is that a specialization of `S' might render
20986 `S<T>::R' not a type. However, if `S' is
20987 specialized, then this `i' will not be used, so there
20988 is no harm in resolving the types here. */
20989 tree type;
20990
20991 /* Resolve the TYPENAME_TYPE. */
20992 type = resolve_typename_type (qualifying_scope,
20993 /*only_current_p=*/false);
20994 /* If that failed, the declarator is invalid. */
20995 if (TREE_CODE (type) == TYPENAME_TYPE)
20996 {
20997 if (typedef_variant_p (type))
20998 error_at (declarator_id_start_token->location,
20999 "cannot define member of dependent typedef "
21000 "%qT", type);
21001 else
21002 error_at (declarator_id_start_token->location,
21003 "%<%T::%E%> is not a type",
21004 TYPE_CONTEXT (qualifying_scope),
21005 TYPE_IDENTIFIER (qualifying_scope));
21006 }
21007 qualifying_scope = type;
21008 }
21009
21010 sfk = sfk_none;
21011
21012 if (unqualified_name)
21013 {
21014 tree class_type;
21015
21016 if (qualifying_scope
21017 && CLASS_TYPE_P (qualifying_scope))
21018 class_type = qualifying_scope;
21019 else
21020 class_type = current_class_type;
21021
21022 if (TREE_CODE (unqualified_name) == TYPE_DECL)
21023 {
21024 tree name_type = TREE_TYPE (unqualified_name);
21025
21026 if (!class_type || !same_type_p (name_type, class_type))
21027 {
21028 /* We do not attempt to print the declarator
21029 here because we do not have enough
21030 information about its original syntactic
21031 form. */
21032 cp_parser_error (parser, "invalid declarator");
21033 declarator = cp_error_declarator;
21034 break;
21035 }
21036 else if (qualifying_scope
21037 && CLASSTYPE_USE_TEMPLATE (name_type))
21038 {
21039 error_at (declarator_id_start_token->location,
21040 "invalid use of constructor as a template");
21041 inform (declarator_id_start_token->location,
21042 "use %<%T::%D%> instead of %<%T::%D%> to "
21043 "name the constructor in a qualified name",
21044 class_type,
21045 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
21046 class_type, name_type);
21047 declarator = cp_error_declarator;
21048 break;
21049 }
21050 unqualified_name = constructor_name (class_type);
21051 }
21052
21053 if (class_type)
21054 {
21055 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
21056 sfk = sfk_destructor;
21057 else if (identifier_p (unqualified_name)
21058 && IDENTIFIER_CONV_OP_P (unqualified_name))
21059 sfk = sfk_conversion;
21060 else if (/* There's no way to declare a constructor
21061 for an unnamed type, even if the type
21062 got a name for linkage purposes. */
21063 !TYPE_WAS_UNNAMED (class_type)
21064 /* Handle correctly (c++/19200):
21065
21066 struct S {
21067 struct T{};
21068 friend void S(T);
21069 };
21070
21071 and also:
21072
21073 namespace N {
21074 void S();
21075 }
21076
21077 struct S {
21078 friend void N::S();
21079 }; */
21080 && (!friend_p || class_type == qualifying_scope)
21081 && constructor_name_p (unqualified_name,
21082 class_type))
21083 sfk = sfk_constructor;
21084 else if (is_overloaded_fn (unqualified_name)
21085 && DECL_CONSTRUCTOR_P (get_first_fn
21086 (unqualified_name)))
21087 sfk = sfk_constructor;
21088
21089 if (ctor_dtor_or_conv_p && sfk != sfk_none)
21090 *ctor_dtor_or_conv_p = -1;
21091 }
21092 }
21093 declarator = make_id_declarator (qualifying_scope,
21094 unqualified_name,
21095 sfk, token->location);
21096 declarator->std_attributes = attrs;
21097 declarator->parameter_pack_p = pack_expansion_p;
21098
21099 if (pack_expansion_p)
21100 maybe_warn_variadic_templates ();
21101
21102 /* We're looking for this case in [temp.res]:
21103 A qualified-id is assumed to name a type if [...]
21104 - it is a decl-specifier of the decl-specifier-seq of a
21105 parameter-declaration in a declarator of a function or
21106 function template declaration, ... */
21107 if (cxx_dialect >= cxx2a
21108 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
21109 && declarator->kind == cdk_id
21110 && !at_class_scope_p ()
21111 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21112 {
21113 /* ...whose declarator-id is qualified. If it isn't, never
21114 assume the parameters to refer to types. */
21115 if (qualifying_scope == NULL_TREE)
21116 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21117 else
21118 {
21119 /* Now we have something like
21120 template <typename T> int C::x(S::p);
21121 which can be a function template declaration or a
21122 variable template definition. If name lookup for
21123 the declarator-id C::x finds one or more function
21124 templates, assume S::p to name a type. Otherwise,
21125 don't. */
21126 tree decl
21127 = cp_parser_lookup_name_simple (parser, unqualified_name,
21128 token->location);
21129 if (!is_overloaded_fn (decl))
21130 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21131 }
21132 }
21133 }
21134
21135 handle_declarator:;
21136 scope = get_scope_of_declarator (declarator);
21137 if (scope)
21138 {
21139 /* Any names that appear after the declarator-id for a
21140 member are looked up in the containing scope. */
21141 if (at_function_scope_p ())
21142 {
21143 /* But declarations with qualified-ids can't appear in a
21144 function. */
21145 cp_parser_error (parser, "qualified-id in declaration");
21146 declarator = cp_error_declarator;
21147 break;
21148 }
21149 pushed_scope = push_scope (scope);
21150 }
21151 parser->in_declarator_p = true;
21152 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
21153 || (declarator && declarator->kind == cdk_id))
21154 /* Default args are only allowed on function
21155 declarations. */
21156 parser->default_arg_ok_p = saved_default_arg_ok_p;
21157 else
21158 parser->default_arg_ok_p = false;
21159
21160 first = false;
21161 }
21162 /* We're done. */
21163 else
21164 break;
21165 }
21166
21167 /* For an abstract declarator, we might wind up with nothing at this
21168 point. That's an error; the declarator is not optional. */
21169 if (!declarator)
21170 cp_parser_error (parser, "expected declarator");
21171 else if (open_paren)
21172 {
21173 /* Record overly parenthesized declarator so we can give a
21174 diagnostic about confusing decl/expr disambiguation. */
21175 if (declarator->kind == cdk_array)
21176 {
21177 /* If the open and close parens are on different lines, this
21178 is probably a formatting thing, so ignore. */
21179 expanded_location open = expand_location (open_paren->location);
21180 expanded_location close = expand_location (close_paren->location);
21181 if (open.line != close.line || open.file != close.file)
21182 open_paren = NULL;
21183 }
21184 if (open_paren)
21185 declarator->parenthesized = open_paren->location;
21186 }
21187
21188 /* If we entered a scope, we must exit it now. */
21189 if (pushed_scope)
21190 pop_scope (pushed_scope);
21191
21192 parser->default_arg_ok_p = saved_default_arg_ok_p;
21193 parser->in_declarator_p = saved_in_declarator_p;
21194
21195 return declarator;
21196 }
21197
21198 /* Parse a ptr-operator.
21199
21200 ptr-operator:
21201 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21202 * cv-qualifier-seq [opt]
21203 &
21204 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21205 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21206
21207 GNU Extension:
21208
21209 ptr-operator:
21210 & cv-qualifier-seq [opt]
21211
21212 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21213 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21214 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21215 filled in with the TYPE containing the member. *CV_QUALS is
21216 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21217 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21218 Note that the tree codes returned by this function have nothing
21219 to do with the types of trees that will be eventually be created
21220 to represent the pointer or reference type being parsed. They are
21221 just constants with suggestive names. */
21222 static enum tree_code
21223 cp_parser_ptr_operator (cp_parser* parser,
21224 tree* type,
21225 cp_cv_quals *cv_quals,
21226 tree *attributes)
21227 {
21228 enum tree_code code = ERROR_MARK;
21229 cp_token *token;
21230 tree attrs = NULL_TREE;
21231
21232 /* Assume that it's not a pointer-to-member. */
21233 *type = NULL_TREE;
21234 /* And that there are no cv-qualifiers. */
21235 *cv_quals = TYPE_UNQUALIFIED;
21236
21237 /* Peek at the next token. */
21238 token = cp_lexer_peek_token (parser->lexer);
21239
21240 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21241 if (token->type == CPP_MULT)
21242 code = INDIRECT_REF;
21243 else if (token->type == CPP_AND)
21244 code = ADDR_EXPR;
21245 else if ((cxx_dialect != cxx98) &&
21246 token->type == CPP_AND_AND) /* C++0x only */
21247 code = NON_LVALUE_EXPR;
21248
21249 if (code != ERROR_MARK)
21250 {
21251 /* Consume the `*', `&' or `&&'. */
21252 cp_lexer_consume_token (parser->lexer);
21253
21254 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21255 `&', if we are allowing GNU extensions. (The only qualifier
21256 that can legally appear after `&' is `restrict', but that is
21257 enforced during semantic analysis. */
21258 if (code == INDIRECT_REF
21259 || cp_parser_allow_gnu_extensions_p (parser))
21260 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21261
21262 attrs = cp_parser_std_attribute_spec_seq (parser);
21263 if (attributes != NULL)
21264 *attributes = attrs;
21265 }
21266 else
21267 {
21268 /* Try the pointer-to-member case. */
21269 cp_parser_parse_tentatively (parser);
21270 /* Look for the optional `::' operator. */
21271 cp_parser_global_scope_opt (parser,
21272 /*current_scope_valid_p=*/false);
21273 /* Look for the nested-name specifier. */
21274 token = cp_lexer_peek_token (parser->lexer);
21275 cp_parser_nested_name_specifier (parser,
21276 /*typename_keyword_p=*/false,
21277 /*check_dependency_p=*/true,
21278 /*type_p=*/false,
21279 /*is_declaration=*/false);
21280 /* If we found it, and the next token is a `*', then we are
21281 indeed looking at a pointer-to-member operator. */
21282 if (!cp_parser_error_occurred (parser)
21283 && cp_parser_require (parser, CPP_MULT, RT_MULT))
21284 {
21285 /* Indicate that the `*' operator was used. */
21286 code = INDIRECT_REF;
21287
21288 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21289 error_at (token->location, "%qD is a namespace", parser->scope);
21290 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
21291 error_at (token->location, "cannot form pointer to member of "
21292 "non-class %q#T", parser->scope);
21293 else
21294 {
21295 /* The type of which the member is a member is given by the
21296 current SCOPE. */
21297 *type = parser->scope;
21298 /* The next name will not be qualified. */
21299 parser->scope = NULL_TREE;
21300 parser->qualifying_scope = NULL_TREE;
21301 parser->object_scope = NULL_TREE;
21302 /* Look for optional c++11 attributes. */
21303 attrs = cp_parser_std_attribute_spec_seq (parser);
21304 if (attributes != NULL)
21305 *attributes = attrs;
21306 /* Look for the optional cv-qualifier-seq. */
21307 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21308 }
21309 }
21310 /* If that didn't work we don't have a ptr-operator. */
21311 if (!cp_parser_parse_definitely (parser))
21312 cp_parser_error (parser, "expected ptr-operator");
21313 }
21314
21315 return code;
21316 }
21317
21318 /* Parse an (optional) cv-qualifier-seq.
21319
21320 cv-qualifier-seq:
21321 cv-qualifier cv-qualifier-seq [opt]
21322
21323 cv-qualifier:
21324 const
21325 volatile
21326
21327 GNU Extension:
21328
21329 cv-qualifier:
21330 __restrict__
21331
21332 Returns a bitmask representing the cv-qualifiers. */
21333
21334 static cp_cv_quals
21335 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
21336 {
21337 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
21338
21339 while (true)
21340 {
21341 cp_token *token;
21342 cp_cv_quals cv_qualifier;
21343
21344 /* Peek at the next token. */
21345 token = cp_lexer_peek_token (parser->lexer);
21346 /* See if it's a cv-qualifier. */
21347 switch (token->keyword)
21348 {
21349 case RID_CONST:
21350 cv_qualifier = TYPE_QUAL_CONST;
21351 break;
21352
21353 case RID_VOLATILE:
21354 cv_qualifier = TYPE_QUAL_VOLATILE;
21355 break;
21356
21357 case RID_RESTRICT:
21358 cv_qualifier = TYPE_QUAL_RESTRICT;
21359 break;
21360
21361 default:
21362 cv_qualifier = TYPE_UNQUALIFIED;
21363 break;
21364 }
21365
21366 if (!cv_qualifier)
21367 break;
21368
21369 if (cv_quals & cv_qualifier)
21370 {
21371 gcc_rich_location richloc (token->location);
21372 richloc.add_fixit_remove ();
21373 error_at (&richloc, "duplicate cv-qualifier");
21374 cp_lexer_purge_token (parser->lexer);
21375 }
21376 else
21377 {
21378 cp_lexer_consume_token (parser->lexer);
21379 cv_quals |= cv_qualifier;
21380 }
21381 }
21382
21383 return cv_quals;
21384 }
21385
21386 /* Parse an (optional) ref-qualifier
21387
21388 ref-qualifier:
21389 &
21390 &&
21391
21392 Returns cp_ref_qualifier representing ref-qualifier. */
21393
21394 static cp_ref_qualifier
21395 cp_parser_ref_qualifier_opt (cp_parser* parser)
21396 {
21397 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
21398
21399 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21400 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
21401 return ref_qual;
21402
21403 while (true)
21404 {
21405 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
21406 cp_token *token = cp_lexer_peek_token (parser->lexer);
21407
21408 switch (token->type)
21409 {
21410 case CPP_AND:
21411 curr_ref_qual = REF_QUAL_LVALUE;
21412 break;
21413
21414 case CPP_AND_AND:
21415 curr_ref_qual = REF_QUAL_RVALUE;
21416 break;
21417
21418 default:
21419 curr_ref_qual = REF_QUAL_NONE;
21420 break;
21421 }
21422
21423 if (!curr_ref_qual)
21424 break;
21425 else if (ref_qual)
21426 {
21427 error_at (token->location, "multiple ref-qualifiers");
21428 cp_lexer_purge_token (parser->lexer);
21429 }
21430 else
21431 {
21432 ref_qual = curr_ref_qual;
21433 cp_lexer_consume_token (parser->lexer);
21434 }
21435 }
21436
21437 return ref_qual;
21438 }
21439
21440 /* Parse an optional tx-qualifier.
21441
21442 tx-qualifier:
21443 transaction_safe
21444 transaction_safe_dynamic */
21445
21446 static tree
21447 cp_parser_tx_qualifier_opt (cp_parser *parser)
21448 {
21449 cp_token *token = cp_lexer_peek_token (parser->lexer);
21450 if (token->type == CPP_NAME)
21451 {
21452 tree name = token->u.value;
21453 const char *p = IDENTIFIER_POINTER (name);
21454 const int len = strlen ("transaction_safe");
21455 if (!strncmp (p, "transaction_safe", len))
21456 {
21457 p += len;
21458 if (*p == '\0'
21459 || !strcmp (p, "_dynamic"))
21460 {
21461 cp_lexer_consume_token (parser->lexer);
21462 if (!flag_tm)
21463 {
21464 error ("%qE requires %<-fgnu-tm%>", name);
21465 return NULL_TREE;
21466 }
21467 else
21468 return name;
21469 }
21470 }
21471 }
21472 return NULL_TREE;
21473 }
21474
21475 /* Parse an (optional) virt-specifier-seq.
21476
21477 virt-specifier-seq:
21478 virt-specifier virt-specifier-seq [opt]
21479
21480 virt-specifier:
21481 override
21482 final
21483
21484 Returns a bitmask representing the virt-specifiers. */
21485
21486 static cp_virt_specifiers
21487 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21488 {
21489 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21490
21491 while (true)
21492 {
21493 cp_token *token;
21494 cp_virt_specifiers virt_specifier;
21495
21496 /* Peek at the next token. */
21497 token = cp_lexer_peek_token (parser->lexer);
21498 /* See if it's a virt-specifier-qualifier. */
21499 if (token->type != CPP_NAME)
21500 break;
21501 if (id_equal (token->u.value, "override"))
21502 {
21503 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21504 virt_specifier = VIRT_SPEC_OVERRIDE;
21505 }
21506 else if (id_equal (token->u.value, "final"))
21507 {
21508 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21509 virt_specifier = VIRT_SPEC_FINAL;
21510 }
21511 else if (id_equal (token->u.value, "__final"))
21512 {
21513 virt_specifier = VIRT_SPEC_FINAL;
21514 }
21515 else
21516 break;
21517
21518 if (virt_specifiers & virt_specifier)
21519 {
21520 gcc_rich_location richloc (token->location);
21521 richloc.add_fixit_remove ();
21522 error_at (&richloc, "duplicate virt-specifier");
21523 cp_lexer_purge_token (parser->lexer);
21524 }
21525 else
21526 {
21527 cp_lexer_consume_token (parser->lexer);
21528 virt_specifiers |= virt_specifier;
21529 }
21530 }
21531 return virt_specifiers;
21532 }
21533
21534 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21535 is in scope even though it isn't real. */
21536
21537 void
21538 inject_this_parameter (tree ctype, cp_cv_quals quals)
21539 {
21540 tree this_parm;
21541
21542 if (current_class_ptr)
21543 {
21544 /* We don't clear this between NSDMIs. Is it already what we want? */
21545 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21546 if (DECL_P (current_class_ptr)
21547 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21548 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21549 && cp_type_quals (type) == quals)
21550 return;
21551 }
21552
21553 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21554 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21555 current_class_ptr = NULL_TREE;
21556 current_class_ref
21557 = cp_build_fold_indirect_ref (this_parm);
21558 current_class_ptr = this_parm;
21559 }
21560
21561 /* Return true iff our current scope is a non-static data member
21562 initializer. */
21563
21564 bool
21565 parsing_nsdmi (void)
21566 {
21567 /* We recognize NSDMI context by the context-less 'this' pointer set up
21568 by the function above. */
21569 if (current_class_ptr
21570 && TREE_CODE (current_class_ptr) == PARM_DECL
21571 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21572 return true;
21573 return false;
21574 }
21575
21576 /* Parse a late-specified return type, if any. This is not a separate
21577 non-terminal, but part of a function declarator, which looks like
21578
21579 -> trailing-type-specifier-seq abstract-declarator(opt)
21580
21581 Returns the type indicated by the type-id.
21582
21583 In addition to this, parse any queued up #pragma omp declare simd
21584 clauses, and #pragma acc routine clauses.
21585
21586 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21587 function. */
21588
21589 static tree
21590 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21591 tree& requires_clause, cp_cv_quals quals)
21592 {
21593 cp_token *token;
21594 tree type = NULL_TREE;
21595 bool declare_simd_p = (parser->omp_declare_simd
21596 && declarator
21597 && declarator->kind == cdk_id);
21598
21599 bool oacc_routine_p = (parser->oacc_routine
21600 && declarator
21601 && declarator->kind == cdk_id);
21602
21603 /* Peek at the next token. */
21604 token = cp_lexer_peek_token (parser->lexer);
21605 /* A late-specified return type is indicated by an initial '->'. */
21606 if (token->type != CPP_DEREF
21607 && token->keyword != RID_REQUIRES
21608 && !(token->type == CPP_NAME
21609 && token->u.value == ridpointers[RID_REQUIRES])
21610 && !(declare_simd_p || oacc_routine_p))
21611 return NULL_TREE;
21612
21613 tree save_ccp = current_class_ptr;
21614 tree save_ccr = current_class_ref;
21615 if (quals >= 0)
21616 {
21617 /* DR 1207: 'this' is in scope in the trailing return type. */
21618 inject_this_parameter (current_class_type, quals);
21619 }
21620
21621 if (token->type == CPP_DEREF)
21622 {
21623 /* Consume the ->. */
21624 cp_lexer_consume_token (parser->lexer);
21625
21626 type = cp_parser_trailing_type_id (parser);
21627 }
21628
21629 /* Function declarations may be followed by a trailing
21630 requires-clause. */
21631 requires_clause = cp_parser_requires_clause_opt (parser);
21632
21633 if (declare_simd_p)
21634 declarator->attributes
21635 = cp_parser_late_parsing_omp_declare_simd (parser,
21636 declarator->attributes);
21637 if (oacc_routine_p)
21638 declarator->attributes
21639 = cp_parser_late_parsing_oacc_routine (parser,
21640 declarator->attributes);
21641
21642 if (quals >= 0)
21643 {
21644 current_class_ptr = save_ccp;
21645 current_class_ref = save_ccr;
21646 }
21647
21648 return type;
21649 }
21650
21651 /* Parse a declarator-id.
21652
21653 declarator-id:
21654 id-expression
21655 :: [opt] nested-name-specifier [opt] type-name
21656
21657 In the `id-expression' case, the value returned is as for
21658 cp_parser_id_expression if the id-expression was an unqualified-id.
21659 If the id-expression was a qualified-id, then a SCOPE_REF is
21660 returned. The first operand is the scope (either a NAMESPACE_DECL
21661 or TREE_TYPE), but the second is still just a representation of an
21662 unqualified-id. */
21663
21664 static tree
21665 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21666 {
21667 tree id;
21668 /* The expression must be an id-expression. Assume that qualified
21669 names are the names of types so that:
21670
21671 template <class T>
21672 int S<T>::R::i = 3;
21673
21674 will work; we must treat `S<T>::R' as the name of a type.
21675 Similarly, assume that qualified names are templates, where
21676 required, so that:
21677
21678 template <class T>
21679 int S<T>::R<T>::i = 3;
21680
21681 will work, too. */
21682 id = cp_parser_id_expression (parser,
21683 /*template_keyword_p=*/false,
21684 /*check_dependency_p=*/false,
21685 /*template_p=*/NULL,
21686 /*declarator_p=*/true,
21687 optional_p);
21688 if (id && BASELINK_P (id))
21689 id = BASELINK_FUNCTIONS (id);
21690 return id;
21691 }
21692
21693 /* Parse a type-id.
21694
21695 type-id:
21696 type-specifier-seq abstract-declarator [opt]
21697
21698 The parser flags FLAGS is used to control type-specifier parsing.
21699
21700 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
21701
21702 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21703 i.e. we've just seen "->".
21704
21705 Returns the TYPE specified. */
21706
21707 static tree
21708 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
21709 bool is_template_arg, bool is_trailing_return,
21710 location_t *type_location)
21711 {
21712 cp_decl_specifier_seq type_specifier_seq;
21713 cp_declarator *abstract_declarator;
21714
21715 /* Parse the type-specifier-seq. */
21716 cp_parser_type_specifier_seq (parser, flags,
21717 /*is_declaration=*/false,
21718 is_trailing_return,
21719 &type_specifier_seq);
21720 if (type_location)
21721 *type_location = type_specifier_seq.locations[ds_type_spec];
21722
21723 if (is_template_arg && type_specifier_seq.type
21724 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21725 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21726 /* A bare template name as a template argument is a template template
21727 argument, not a placeholder, so fail parsing it as a type argument. */
21728 {
21729 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21730 cp_parser_simulate_error (parser);
21731 return error_mark_node;
21732 }
21733 if (type_specifier_seq.type == error_mark_node)
21734 return error_mark_node;
21735
21736 /* There might or might not be an abstract declarator. */
21737 cp_parser_parse_tentatively (parser);
21738 /* Look for the declarator. */
21739 abstract_declarator
21740 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
21741 CP_PARSER_FLAGS_NONE, NULL,
21742 /*parenthesized_p=*/NULL,
21743 /*member_p=*/false,
21744 /*friend_p=*/false,
21745 /*static_p=*/false);
21746 /* Check to see if there really was a declarator. */
21747 if (!cp_parser_parse_definitely (parser))
21748 abstract_declarator = NULL;
21749
21750 if (type_specifier_seq.type
21751 /* The concepts TS allows 'auto' as a type-id. */
21752 && (!flag_concepts || parser->in_type_id_in_expr_p)
21753 /* None of the valid uses of 'auto' in C++14 involve the type-id
21754 nonterminal, but it is valid in a trailing-return-type. */
21755 && !(cxx_dialect >= cxx14 && is_trailing_return))
21756 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21757 {
21758 /* A type-id with type 'auto' is only ok if the abstract declarator
21759 is a function declarator with a late-specified return type.
21760
21761 A type-id with 'auto' is also valid in a trailing-return-type
21762 in a compound-requirement. */
21763 if (abstract_declarator
21764 && abstract_declarator->kind == cdk_function
21765 && abstract_declarator->u.function.late_return_type)
21766 /* OK */;
21767 else if (parser->in_result_type_constraint_p)
21768 /* OK */;
21769 else
21770 {
21771 location_t loc = type_specifier_seq.locations[ds_type_spec];
21772 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21773 {
21774 error_at (loc, "missing template arguments after %qT",
21775 auto_node);
21776 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21777 tmpl);
21778 }
21779 else
21780 error_at (loc, "invalid use of %qT", auto_node);
21781 return error_mark_node;
21782 }
21783 }
21784
21785 return groktypename (&type_specifier_seq, abstract_declarator,
21786 is_template_arg);
21787 }
21788
21789 /* Wrapper for cp_parser_type_id_1. */
21790
21791 static tree
21792 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
21793 location_t *type_location)
21794 {
21795 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
21796 }
21797
21798 /* Wrapper for cp_parser_type_id_1. */
21799
21800 static tree
21801 cp_parser_template_type_arg (cp_parser *parser)
21802 {
21803 tree r;
21804 const char *saved_message = parser->type_definition_forbidden_message;
21805 parser->type_definition_forbidden_message
21806 = G_("types may not be defined in template arguments");
21807 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
21808 parser->type_definition_forbidden_message = saved_message;
21809 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21810 {
21811 error ("invalid use of %<auto%> in template argument");
21812 r = error_mark_node;
21813 }
21814 return r;
21815 }
21816
21817 /* Wrapper for cp_parser_type_id_1. */
21818
21819 static tree
21820 cp_parser_trailing_type_id (cp_parser *parser)
21821 {
21822 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
21823 false, true, NULL);
21824 }
21825
21826 /* Parse a type-specifier-seq.
21827
21828 type-specifier-seq:
21829 type-specifier type-specifier-seq [opt]
21830
21831 GNU extension:
21832
21833 type-specifier-seq:
21834 attributes type-specifier-seq [opt]
21835
21836 The parser flags FLAGS is used to control type-specifier parsing.
21837
21838 If IS_DECLARATION is true, we are at the start of a "condition" or
21839 exception-declaration, so we might be followed by a declarator-id.
21840
21841 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21842 i.e. we've just seen "->".
21843
21844 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21845
21846 static void
21847 cp_parser_type_specifier_seq (cp_parser* parser,
21848 cp_parser_flags flags,
21849 bool is_declaration,
21850 bool is_trailing_return,
21851 cp_decl_specifier_seq *type_specifier_seq)
21852 {
21853 bool seen_type_specifier = false;
21854 cp_token *start_token = NULL;
21855
21856 /* Clear the TYPE_SPECIFIER_SEQ. */
21857 clear_decl_specs (type_specifier_seq);
21858
21859 flags |= CP_PARSER_FLAGS_OPTIONAL;
21860 /* In the context of a trailing return type, enum E { } is an
21861 elaborated-type-specifier followed by a function-body, not an
21862 enum-specifier. */
21863 if (is_trailing_return)
21864 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21865
21866 /* Parse the type-specifiers and attributes. */
21867 while (true)
21868 {
21869 tree type_specifier;
21870 bool is_cv_qualifier;
21871
21872 /* Check for attributes first. */
21873 if (cp_next_tokens_can_be_attribute_p (parser))
21874 {
21875 type_specifier_seq->attributes
21876 = attr_chainon (type_specifier_seq->attributes,
21877 cp_parser_attributes_opt (parser));
21878 continue;
21879 }
21880
21881 /* record the token of the beginning of the type specifier seq,
21882 for error reporting purposes*/
21883 if (!start_token)
21884 start_token = cp_lexer_peek_token (parser->lexer);
21885
21886 /* Look for the type-specifier. */
21887 type_specifier = cp_parser_type_specifier (parser,
21888 flags,
21889 type_specifier_seq,
21890 /*is_declaration=*/false,
21891 NULL,
21892 &is_cv_qualifier);
21893 if (!type_specifier)
21894 {
21895 /* If the first type-specifier could not be found, this is not a
21896 type-specifier-seq at all. */
21897 if (!seen_type_specifier)
21898 {
21899 /* Set in_declarator_p to avoid skipping to the semicolon. */
21900 int in_decl = parser->in_declarator_p;
21901 parser->in_declarator_p = true;
21902
21903 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21904 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21905 cp_parser_error (parser, "expected type-specifier");
21906
21907 parser->in_declarator_p = in_decl;
21908
21909 type_specifier_seq->type = error_mark_node;
21910 return;
21911 }
21912 /* If subsequent type-specifiers could not be found, the
21913 type-specifier-seq is complete. */
21914 break;
21915 }
21916
21917 seen_type_specifier = true;
21918 /* The standard says that a condition can be:
21919
21920 type-specifier-seq declarator = assignment-expression
21921
21922 However, given:
21923
21924 struct S {};
21925 if (int S = ...)
21926
21927 we should treat the "S" as a declarator, not as a
21928 type-specifier. The standard doesn't say that explicitly for
21929 type-specifier-seq, but it does say that for
21930 decl-specifier-seq in an ordinary declaration. Perhaps it
21931 would be clearer just to allow a decl-specifier-seq here, and
21932 then add a semantic restriction that if any decl-specifiers
21933 that are not type-specifiers appear, the program is invalid. */
21934 if (is_declaration && !is_cv_qualifier)
21935 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21936 }
21937 }
21938
21939 /* Return whether the function currently being declared has an associated
21940 template parameter list. */
21941
21942 static bool
21943 function_being_declared_is_template_p (cp_parser* parser)
21944 {
21945 if (!current_template_parms || processing_template_parmlist)
21946 return false;
21947
21948 if (parser->implicit_template_scope)
21949 return true;
21950
21951 if (at_class_scope_p ()
21952 && TYPE_BEING_DEFINED (current_class_type))
21953 return parser->num_template_parameter_lists != 0;
21954
21955 return ((int) parser->num_template_parameter_lists > template_class_depth
21956 (current_class_type));
21957 }
21958
21959 /* Parse a parameter-declaration-clause.
21960
21961 parameter-declaration-clause:
21962 parameter-declaration-list [opt] ... [opt]
21963 parameter-declaration-list , ...
21964
21965 The parser flags FLAGS is used to control type-specifier parsing.
21966
21967 Returns a representation for the parameter declarations. A return
21968 value of NULL indicates a parameter-declaration-clause consisting
21969 only of an ellipsis. */
21970
21971 static tree
21972 cp_parser_parameter_declaration_clause (cp_parser* parser,
21973 cp_parser_flags flags)
21974 {
21975 tree parameters;
21976 cp_token *token;
21977 bool ellipsis_p;
21978
21979 temp_override<bool> cleanup
21980 (parser->auto_is_implicit_function_template_parm_p);
21981
21982 if (!processing_specialization
21983 && !processing_template_parmlist
21984 && !processing_explicit_instantiation
21985 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21986 actual function or a random abstract declarator. */
21987 && parser->default_arg_ok_p)
21988 if (!current_function_decl
21989 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21990 parser->auto_is_implicit_function_template_parm_p = true;
21991
21992 /* Peek at the next token. */
21993 token = cp_lexer_peek_token (parser->lexer);
21994 /* Check for trivial parameter-declaration-clauses. */
21995 if (token->type == CPP_ELLIPSIS)
21996 {
21997 /* Consume the `...' token. */
21998 cp_lexer_consume_token (parser->lexer);
21999 return NULL_TREE;
22000 }
22001 else if (token->type == CPP_CLOSE_PAREN)
22002 /* There are no parameters. */
22003 return void_list_node;
22004 /* Check for `(void)', too, which is a special case. */
22005 else if (token->keyword == RID_VOID
22006 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22007 == CPP_CLOSE_PAREN))
22008 {
22009 /* Consume the `void' token. */
22010 cp_lexer_consume_token (parser->lexer);
22011 /* There are no parameters. */
22012 return void_list_node;
22013 }
22014
22015 /* Parse the parameter-declaration-list. */
22016 parameters = cp_parser_parameter_declaration_list (parser, flags);
22017 /* If a parse error occurred while parsing the
22018 parameter-declaration-list, then the entire
22019 parameter-declaration-clause is erroneous. */
22020 if (parameters == error_mark_node)
22021 return NULL_TREE;
22022
22023 /* Peek at the next token. */
22024 token = cp_lexer_peek_token (parser->lexer);
22025 /* If it's a `,', the clause should terminate with an ellipsis. */
22026 if (token->type == CPP_COMMA)
22027 {
22028 /* Consume the `,'. */
22029 cp_lexer_consume_token (parser->lexer);
22030 /* Expect an ellipsis. */
22031 ellipsis_p
22032 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
22033 }
22034 /* It might also be `...' if the optional trailing `,' was
22035 omitted. */
22036 else if (token->type == CPP_ELLIPSIS)
22037 {
22038 /* Consume the `...' token. */
22039 cp_lexer_consume_token (parser->lexer);
22040 /* And remember that we saw it. */
22041 ellipsis_p = true;
22042 }
22043 else
22044 ellipsis_p = false;
22045
22046 /* Finish the parameter list. */
22047 if (!ellipsis_p)
22048 parameters = chainon (parameters, void_list_node);
22049
22050 return parameters;
22051 }
22052
22053 /* Parse a parameter-declaration-list.
22054
22055 parameter-declaration-list:
22056 parameter-declaration
22057 parameter-declaration-list , parameter-declaration
22058
22059 The parser flags FLAGS is used to control type-specifier parsing.
22060
22061 Returns a representation of the parameter-declaration-list, as for
22062 cp_parser_parameter_declaration_clause. However, the
22063 `void_list_node' is never appended to the list. */
22064
22065 static tree
22066 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
22067 {
22068 tree parameters = NULL_TREE;
22069 tree *tail = &parameters;
22070 bool saved_in_unbraced_linkage_specification_p;
22071 int index = 0;
22072
22073 /* The special considerations that apply to a function within an
22074 unbraced linkage specifications do not apply to the parameters
22075 to the function. */
22076 saved_in_unbraced_linkage_specification_p
22077 = parser->in_unbraced_linkage_specification_p;
22078 parser->in_unbraced_linkage_specification_p = false;
22079
22080 /* Look for more parameters. */
22081 while (true)
22082 {
22083 cp_parameter_declarator *parameter;
22084 tree decl = error_mark_node;
22085 bool parenthesized_p = false;
22086
22087 /* Parse the parameter. */
22088 parameter
22089 = cp_parser_parameter_declaration (parser, flags,
22090 /*template_parm_p=*/false,
22091 &parenthesized_p);
22092
22093 /* We don't know yet if the enclosing context is deprecated, so wait
22094 and warn in grokparms if appropriate. */
22095 deprecated_state = DEPRECATED_SUPPRESS;
22096
22097 if (parameter)
22098 {
22099 decl = grokdeclarator (parameter->declarator,
22100 &parameter->decl_specifiers,
22101 PARM,
22102 parameter->default_argument != NULL_TREE,
22103 &parameter->decl_specifiers.attributes);
22104 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
22105 DECL_SOURCE_LOCATION (decl) = parameter->loc;
22106 }
22107
22108 deprecated_state = DEPRECATED_NORMAL;
22109
22110 /* If a parse error occurred parsing the parameter declaration,
22111 then the entire parameter-declaration-list is erroneous. */
22112 if (decl == error_mark_node)
22113 {
22114 parameters = error_mark_node;
22115 break;
22116 }
22117
22118 if (parameter->decl_specifiers.attributes)
22119 cplus_decl_attributes (&decl,
22120 parameter->decl_specifiers.attributes,
22121 0);
22122 if (DECL_NAME (decl))
22123 decl = pushdecl (decl);
22124
22125 if (decl != error_mark_node)
22126 {
22127 retrofit_lang_decl (decl);
22128 DECL_PARM_INDEX (decl) = ++index;
22129 DECL_PARM_LEVEL (decl) = function_parm_depth ();
22130 }
22131
22132 /* Add the new parameter to the list. */
22133 *tail = build_tree_list (parameter->default_argument, decl);
22134 tail = &TREE_CHAIN (*tail);
22135
22136 /* Peek at the next token. */
22137 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
22138 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
22139 /* These are for Objective-C++ */
22140 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22141 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22142 /* The parameter-declaration-list is complete. */
22143 break;
22144 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22145 {
22146 cp_token *token;
22147
22148 /* Peek at the next token. */
22149 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22150 /* If it's an ellipsis, then the list is complete. */
22151 if (token->type == CPP_ELLIPSIS)
22152 break;
22153 /* Otherwise, there must be more parameters. Consume the
22154 `,'. */
22155 cp_lexer_consume_token (parser->lexer);
22156 /* When parsing something like:
22157
22158 int i(float f, double d)
22159
22160 we can tell after seeing the declaration for "f" that we
22161 are not looking at an initialization of a variable "i",
22162 but rather at the declaration of a function "i".
22163
22164 Due to the fact that the parsing of template arguments
22165 (as specified to a template-id) requires backtracking we
22166 cannot use this technique when inside a template argument
22167 list. */
22168 if (!parser->in_template_argument_list_p
22169 && !parser->in_type_id_in_expr_p
22170 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22171 /* However, a parameter-declaration of the form
22172 "float(f)" (which is a valid declaration of a
22173 parameter "f") can also be interpreted as an
22174 expression (the conversion of "f" to "float"). */
22175 && !parenthesized_p)
22176 cp_parser_commit_to_tentative_parse (parser);
22177 }
22178 else
22179 {
22180 cp_parser_error (parser, "expected %<,%> or %<...%>");
22181 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
22182 cp_parser_skip_to_closing_parenthesis (parser,
22183 /*recovering=*/true,
22184 /*or_comma=*/false,
22185 /*consume_paren=*/false);
22186 break;
22187 }
22188 }
22189
22190 parser->in_unbraced_linkage_specification_p
22191 = saved_in_unbraced_linkage_specification_p;
22192
22193 /* Reset implicit_template_scope if we are about to leave the function
22194 parameter list that introduced it. Note that for out-of-line member
22195 definitions, there will be one or more class scopes before we get to
22196 the template parameter scope. */
22197
22198 if (cp_binding_level *its = parser->implicit_template_scope)
22199 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
22200 {
22201 while (maybe_its->kind == sk_class)
22202 maybe_its = maybe_its->level_chain;
22203 if (maybe_its == its)
22204 {
22205 parser->implicit_template_parms = 0;
22206 parser->implicit_template_scope = 0;
22207 }
22208 }
22209
22210 return parameters;
22211 }
22212
22213 /* Parse a parameter declaration.
22214
22215 parameter-declaration:
22216 decl-specifier-seq ... [opt] declarator
22217 decl-specifier-seq declarator = assignment-expression
22218 decl-specifier-seq ... [opt] abstract-declarator [opt]
22219 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22220
22221 The parser flags FLAGS is used to control type-specifier parsing.
22222
22223 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22224 declares a template parameter. (In that case, a non-nested `>'
22225 token encountered during the parsing of the assignment-expression
22226 is not interpreted as a greater-than operator.)
22227
22228 Returns a representation of the parameter, or NULL if an error
22229 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22230 true iff the declarator is of the form "(p)". */
22231
22232 static cp_parameter_declarator *
22233 cp_parser_parameter_declaration (cp_parser *parser,
22234 cp_parser_flags flags,
22235 bool template_parm_p,
22236 bool *parenthesized_p)
22237 {
22238 int declares_class_or_enum;
22239 cp_decl_specifier_seq decl_specifiers;
22240 cp_declarator *declarator;
22241 tree default_argument;
22242 cp_token *token = NULL, *declarator_token_start = NULL;
22243 const char *saved_message;
22244 bool template_parameter_pack_p = false;
22245
22246 /* In a template parameter, `>' is not an operator.
22247
22248 [temp.param]
22249
22250 When parsing a default template-argument for a non-type
22251 template-parameter, the first non-nested `>' is taken as the end
22252 of the template parameter-list rather than a greater-than
22253 operator. */
22254
22255 /* Type definitions may not appear in parameter types. */
22256 saved_message = parser->type_definition_forbidden_message;
22257 parser->type_definition_forbidden_message
22258 = G_("types may not be defined in parameter types");
22259
22260 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
22261 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22262 (current_template_parms)) : 0);
22263
22264 /* Parse the declaration-specifiers. */
22265 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22266 cp_parser_decl_specifier_seq (parser,
22267 flags,
22268 &decl_specifiers,
22269 &declares_class_or_enum);
22270
22271 /* Complain about missing 'typename' or other invalid type names. */
22272 if (!decl_specifiers.any_type_specifiers_p
22273 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22274 decl_specifiers.type = error_mark_node;
22275
22276 /* If an error occurred, there's no reason to attempt to parse the
22277 rest of the declaration. */
22278 if (cp_parser_error_occurred (parser))
22279 {
22280 parser->type_definition_forbidden_message = saved_message;
22281 return NULL;
22282 }
22283
22284 /* Peek at the next token. */
22285 token = cp_lexer_peek_token (parser->lexer);
22286
22287 /* If the next token is a `)', `,', `=', `>', or `...', then there
22288 is no declarator. However, when variadic templates are enabled,
22289 there may be a declarator following `...'. */
22290 if (token->type == CPP_CLOSE_PAREN
22291 || token->type == CPP_COMMA
22292 || token->type == CPP_EQ
22293 || token->type == CPP_GREATER)
22294 {
22295 declarator = NULL;
22296 if (parenthesized_p)
22297 *parenthesized_p = false;
22298 }
22299 /* Otherwise, there should be a declarator. */
22300 else
22301 {
22302 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22303 parser->default_arg_ok_p = false;
22304
22305 /* After seeing a decl-specifier-seq, if the next token is not a
22306 "(", there is no possibility that the code is a valid
22307 expression. Therefore, if parsing tentatively, we commit at
22308 this point. */
22309 if (!parser->in_template_argument_list_p
22310 /* In an expression context, having seen:
22311
22312 (int((char ...
22313
22314 we cannot be sure whether we are looking at a
22315 function-type (taking a "char" as a parameter) or a cast
22316 of some object of type "char" to "int". */
22317 && !parser->in_type_id_in_expr_p
22318 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22319 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22320 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
22321 cp_parser_commit_to_tentative_parse (parser);
22322 /* Parse the declarator. */
22323 declarator_token_start = token;
22324 declarator = cp_parser_declarator (parser,
22325 CP_PARSER_DECLARATOR_EITHER,
22326 CP_PARSER_FLAGS_NONE,
22327 /*ctor_dtor_or_conv_p=*/NULL,
22328 parenthesized_p,
22329 /*member_p=*/false,
22330 /*friend_p=*/false,
22331 /*static_p=*/false);
22332 parser->default_arg_ok_p = saved_default_arg_ok_p;
22333 /* After the declarator, allow more attributes. */
22334 decl_specifiers.attributes
22335 = attr_chainon (decl_specifiers.attributes,
22336 cp_parser_attributes_opt (parser));
22337
22338 /* If the declarator is a template parameter pack, remember that and
22339 clear the flag in the declarator itself so we don't get errors
22340 from grokdeclarator. */
22341 if (template_parm_p && declarator && declarator->parameter_pack_p)
22342 {
22343 declarator->parameter_pack_p = false;
22344 template_parameter_pack_p = true;
22345 }
22346 }
22347
22348 /* If the next token is an ellipsis, and we have not seen a declarator
22349 name, and if either the type of the declarator contains parameter
22350 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22351 for, eg, abbreviated integral type names), then we actually have a
22352 parameter pack expansion expression. Otherwise, leave the ellipsis
22353 for a C-style variadic function. */
22354 token = cp_lexer_peek_token (parser->lexer);
22355
22356 /* If a function parameter pack was specified and an implicit template
22357 parameter was introduced during cp_parser_parameter_declaration,
22358 change any implicit parameters introduced into packs. */
22359 if (parser->implicit_template_parms
22360 && ((token->type == CPP_ELLIPSIS
22361 && declarator_can_be_parameter_pack (declarator))
22362 || (declarator && declarator->parameter_pack_p)))
22363 {
22364 int latest_template_parm_idx = TREE_VEC_LENGTH
22365 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
22366
22367 if (latest_template_parm_idx != template_parm_idx)
22368 decl_specifiers.type = convert_generic_types_to_packs
22369 (decl_specifiers.type,
22370 template_parm_idx, latest_template_parm_idx);
22371 }
22372
22373 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22374 {
22375 tree type = decl_specifiers.type;
22376
22377 if (type && DECL_P (type))
22378 type = TREE_TYPE (type);
22379
22380 if (((type
22381 && TREE_CODE (type) != TYPE_PACK_EXPANSION
22382 && (template_parm_p || uses_parameter_packs (type)))
22383 || (!type && template_parm_p))
22384 && declarator_can_be_parameter_pack (declarator))
22385 {
22386 /* Consume the `...'. */
22387 cp_lexer_consume_token (parser->lexer);
22388 maybe_warn_variadic_templates ();
22389
22390 /* Build a pack expansion type */
22391 if (template_parm_p)
22392 template_parameter_pack_p = true;
22393 else if (declarator)
22394 declarator->parameter_pack_p = true;
22395 else
22396 decl_specifiers.type = make_pack_expansion (type);
22397 }
22398 }
22399
22400 /* The restriction on defining new types applies only to the type
22401 of the parameter, not to the default argument. */
22402 parser->type_definition_forbidden_message = saved_message;
22403
22404 /* If the next token is `=', then process a default argument. */
22405 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22406 {
22407 tree type = decl_specifiers.type;
22408 token = cp_lexer_peek_token (parser->lexer);
22409 /* If we are defining a class, then the tokens that make up the
22410 default argument must be saved and processed later. */
22411 if (!template_parm_p && at_class_scope_p ()
22412 && TYPE_BEING_DEFINED (current_class_type)
22413 && !LAMBDA_TYPE_P (current_class_type))
22414 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
22415
22416 // A constrained-type-specifier may declare a type template-parameter.
22417 else if (declares_constrained_type_template_parameter (type))
22418 default_argument
22419 = cp_parser_default_type_template_argument (parser);
22420
22421 // A constrained-type-specifier may declare a template-template-parameter.
22422 else if (declares_constrained_template_template_parameter (type))
22423 default_argument
22424 = cp_parser_default_template_template_argument (parser);
22425
22426 /* Outside of a class definition, we can just parse the
22427 assignment-expression. */
22428 else
22429 default_argument
22430 = cp_parser_default_argument (parser, template_parm_p);
22431
22432 if (!parser->default_arg_ok_p)
22433 {
22434 permerror (token->location,
22435 "default arguments are only "
22436 "permitted for function parameters");
22437 }
22438 else if ((declarator && declarator->parameter_pack_p)
22439 || template_parameter_pack_p
22440 || (decl_specifiers.type
22441 && PACK_EXPANSION_P (decl_specifiers.type)))
22442 {
22443 /* Find the name of the parameter pack. */
22444 cp_declarator *id_declarator = declarator;
22445 while (id_declarator && id_declarator->kind != cdk_id)
22446 id_declarator = id_declarator->declarator;
22447
22448 if (id_declarator && id_declarator->kind == cdk_id)
22449 error_at (declarator_token_start->location,
22450 template_parm_p
22451 ? G_("template parameter pack %qD "
22452 "cannot have a default argument")
22453 : G_("parameter pack %qD cannot have "
22454 "a default argument"),
22455 id_declarator->u.id.unqualified_name);
22456 else
22457 error_at (declarator_token_start->location,
22458 template_parm_p
22459 ? G_("template parameter pack cannot have "
22460 "a default argument")
22461 : G_("parameter pack cannot have a "
22462 "default argument"));
22463
22464 default_argument = NULL_TREE;
22465 }
22466 }
22467 else
22468 default_argument = NULL_TREE;
22469
22470 if (default_argument)
22471 STRIP_ANY_LOCATION_WRAPPER (default_argument);
22472
22473 /* Generate a location for the parameter, ranging from the start of the
22474 initial token to the end of the final token (using input_location for
22475 the latter, set up by cp_lexer_set_source_position_from_token when
22476 consuming tokens).
22477
22478 If we have a identifier, then use it for the caret location, e.g.
22479
22480 extern int callee (int one, int (*two)(int, int), float three);
22481 ~~~~~~^~~~~~~~~~~~~~
22482
22483 otherwise, reuse the start location for the caret location e.g.:
22484
22485 extern int callee (int one, int (*)(int, int), float three);
22486 ^~~~~~~~~~~~~~~~~
22487
22488 */
22489 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22490 ? declarator->id_loc
22491 : decl_spec_token_start->location);
22492 location_t param_loc = make_location (caret_loc,
22493 decl_spec_token_start->location,
22494 input_location);
22495
22496 return make_parameter_declarator (&decl_specifiers,
22497 declarator,
22498 default_argument,
22499 param_loc,
22500 template_parameter_pack_p);
22501 }
22502
22503 /* Parse a default argument and return it.
22504
22505 TEMPLATE_PARM_P is true if this is a default argument for a
22506 non-type template parameter. */
22507 static tree
22508 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22509 {
22510 tree default_argument = NULL_TREE;
22511 bool saved_greater_than_is_operator_p;
22512 unsigned char saved_local_variables_forbidden_p;
22513 bool non_constant_p, is_direct_init;
22514
22515 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22516 set correctly. */
22517 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22518 parser->greater_than_is_operator_p = !template_parm_p;
22519 /* Local variable names (and the `this' keyword) may not
22520 appear in a default argument. */
22521 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22522 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
22523 /* Parse the assignment-expression. */
22524 if (template_parm_p)
22525 push_deferring_access_checks (dk_no_deferred);
22526 tree saved_class_ptr = NULL_TREE;
22527 tree saved_class_ref = NULL_TREE;
22528 /* The "this" pointer is not valid in a default argument. */
22529 if (cfun)
22530 {
22531 saved_class_ptr = current_class_ptr;
22532 cp_function_chain->x_current_class_ptr = NULL_TREE;
22533 saved_class_ref = current_class_ref;
22534 cp_function_chain->x_current_class_ref = NULL_TREE;
22535 }
22536 default_argument
22537 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22538 /* Restore the "this" pointer. */
22539 if (cfun)
22540 {
22541 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22542 cp_function_chain->x_current_class_ref = saved_class_ref;
22543 }
22544 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22545 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22546 if (template_parm_p)
22547 pop_deferring_access_checks ();
22548 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22549 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22550
22551 return default_argument;
22552 }
22553
22554 /* Parse a function-body.
22555
22556 function-body:
22557 compound_statement */
22558
22559 static void
22560 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22561 {
22562 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22563 ? BCS_TRY_BLOCK : BCS_NORMAL),
22564 true);
22565 }
22566
22567 /* Parse a ctor-initializer-opt followed by a function-body. Return
22568 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22569 is true we are parsing a function-try-block. */
22570
22571 static void
22572 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22573 bool in_function_try_block)
22574 {
22575 tree body, list;
22576 const bool check_body_p =
22577 DECL_CONSTRUCTOR_P (current_function_decl)
22578 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
22579 tree last = NULL;
22580
22581 /* Begin the function body. */
22582 body = begin_function_body ();
22583 /* Parse the optional ctor-initializer. */
22584 cp_parser_ctor_initializer_opt (parser);
22585
22586 /* If we're parsing a constexpr constructor definition, we need
22587 to check that the constructor body is indeed empty. However,
22588 before we get to cp_parser_function_body lot of junk has been
22589 generated, so we can't just check that we have an empty block.
22590 Rather we take a snapshot of the outermost block, and check whether
22591 cp_parser_function_body changed its state. */
22592 if (check_body_p)
22593 {
22594 list = cur_stmt_list;
22595 if (STATEMENT_LIST_TAIL (list))
22596 last = STATEMENT_LIST_TAIL (list)->stmt;
22597 }
22598 /* Parse the function-body. */
22599 cp_parser_function_body (parser, in_function_try_block);
22600 if (check_body_p)
22601 check_constexpr_ctor_body (last, list, /*complain=*/true);
22602 /* Finish the function body. */
22603 finish_function_body (body);
22604 }
22605
22606 /* Parse an initializer.
22607
22608 initializer:
22609 = initializer-clause
22610 ( expression-list )
22611
22612 Returns an expression representing the initializer. If no
22613 initializer is present, NULL_TREE is returned.
22614
22615 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22616 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22617 set to TRUE if there is no initializer present. If there is an
22618 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22619 is set to true; otherwise it is set to false. */
22620
22621 static tree
22622 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22623 bool* non_constant_p, bool subexpression_p)
22624 {
22625 cp_token *token;
22626 tree init;
22627
22628 /* Peek at the next token. */
22629 token = cp_lexer_peek_token (parser->lexer);
22630
22631 /* Let our caller know whether or not this initializer was
22632 parenthesized. */
22633 *is_direct_init = (token->type != CPP_EQ);
22634 /* Assume that the initializer is constant. */
22635 *non_constant_p = false;
22636
22637 if (token->type == CPP_EQ)
22638 {
22639 /* Consume the `='. */
22640 cp_lexer_consume_token (parser->lexer);
22641 /* Parse the initializer-clause. */
22642 init = cp_parser_initializer_clause (parser, non_constant_p);
22643 }
22644 else if (token->type == CPP_OPEN_PAREN)
22645 {
22646 vec<tree, va_gc> *vec;
22647 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22648 /*cast_p=*/false,
22649 /*allow_expansion_p=*/true,
22650 non_constant_p);
22651 if (vec == NULL)
22652 return error_mark_node;
22653 init = build_tree_list_vec (vec);
22654 release_tree_vector (vec);
22655 }
22656 else if (token->type == CPP_OPEN_BRACE)
22657 {
22658 cp_lexer_set_source_position (parser->lexer);
22659 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22660 init = cp_parser_braced_list (parser, non_constant_p);
22661 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22662 }
22663 else
22664 {
22665 /* Anything else is an error. */
22666 cp_parser_error (parser, "expected initializer");
22667 init = error_mark_node;
22668 }
22669
22670 if (!subexpression_p && check_for_bare_parameter_packs (init))
22671 init = error_mark_node;
22672
22673 return init;
22674 }
22675
22676 /* Parse an initializer-clause.
22677
22678 initializer-clause:
22679 assignment-expression
22680 braced-init-list
22681
22682 Returns an expression representing the initializer.
22683
22684 If the `assignment-expression' production is used the value
22685 returned is simply a representation for the expression.
22686
22687 Otherwise, calls cp_parser_braced_list. */
22688
22689 static cp_expr
22690 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22691 {
22692 cp_expr initializer;
22693
22694 /* Assume the expression is constant. */
22695 *non_constant_p = false;
22696
22697 /* If it is not a `{', then we are looking at an
22698 assignment-expression. */
22699 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22700 {
22701 initializer
22702 = cp_parser_constant_expression (parser,
22703 /*allow_non_constant_p=*/true,
22704 non_constant_p);
22705 }
22706 else
22707 initializer = cp_parser_braced_list (parser, non_constant_p);
22708
22709 return initializer;
22710 }
22711
22712 /* Parse a brace-enclosed initializer list.
22713
22714 braced-init-list:
22715 { initializer-list , [opt] }
22716 { designated-initializer-list , [opt] }
22717 { }
22718
22719 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22720 the elements of the initializer-list (or NULL, if the last
22721 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22722 NULL_TREE. There is no way to detect whether or not the optional
22723 trailing `,' was provided. NON_CONSTANT_P is as for
22724 cp_parser_initializer. */
22725
22726 static cp_expr
22727 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22728 {
22729 tree initializer;
22730 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22731
22732 /* Consume the `{' token. */
22733 matching_braces braces;
22734 braces.require_open (parser);
22735 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22736 initializer = make_node (CONSTRUCTOR);
22737 /* If it's not a `}', then there is a non-trivial initializer. */
22738 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22739 {
22740 /* Parse the initializer list. */
22741 CONSTRUCTOR_ELTS (initializer)
22742 = cp_parser_initializer_list (parser, non_constant_p);
22743 /* A trailing `,' token is allowed. */
22744 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22745 cp_lexer_consume_token (parser->lexer);
22746 }
22747 else
22748 *non_constant_p = false;
22749 /* Now, there should be a trailing `}'. */
22750 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22751 braces.require_close (parser);
22752 TREE_TYPE (initializer) = init_list_type_node;
22753
22754 cp_expr result (initializer);
22755 /* Build a location of the form:
22756 { ... }
22757 ^~~~~~~
22758 with caret==start at the open brace, finish at the close brace. */
22759 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22760 result.set_location (combined_loc);
22761 return result;
22762 }
22763
22764 /* Consume tokens up to, and including, the next non-nested closing `]'.
22765 Returns true iff we found a closing `]'. */
22766
22767 static bool
22768 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22769 {
22770 unsigned square_depth = 0;
22771
22772 while (true)
22773 {
22774 cp_token * token = cp_lexer_peek_token (parser->lexer);
22775
22776 switch (token->type)
22777 {
22778 case CPP_PRAGMA_EOL:
22779 if (!parser->lexer->in_pragma)
22780 break;
22781 /* FALLTHRU */
22782 case CPP_EOF:
22783 /* If we've run out of tokens, then there is no closing `]'. */
22784 return false;
22785
22786 case CPP_OPEN_SQUARE:
22787 ++square_depth;
22788 break;
22789
22790 case CPP_CLOSE_SQUARE:
22791 if (!square_depth--)
22792 {
22793 cp_lexer_consume_token (parser->lexer);
22794 return true;
22795 }
22796 break;
22797
22798 default:
22799 break;
22800 }
22801
22802 /* Consume the token. */
22803 cp_lexer_consume_token (parser->lexer);
22804 }
22805 }
22806
22807 /* Return true if we are looking at an array-designator, false otherwise. */
22808
22809 static bool
22810 cp_parser_array_designator_p (cp_parser *parser)
22811 {
22812 /* Consume the `['. */
22813 cp_lexer_consume_token (parser->lexer);
22814
22815 cp_lexer_save_tokens (parser->lexer);
22816
22817 /* Skip tokens until the next token is a closing square bracket.
22818 If we find the closing `]', and the next token is a `=', then
22819 we are looking at an array designator. */
22820 bool array_designator_p
22821 = (cp_parser_skip_to_closing_square_bracket (parser)
22822 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22823
22824 /* Roll back the tokens we skipped. */
22825 cp_lexer_rollback_tokens (parser->lexer);
22826
22827 return array_designator_p;
22828 }
22829
22830 /* Parse an initializer-list.
22831
22832 initializer-list:
22833 initializer-clause ... [opt]
22834 initializer-list , initializer-clause ... [opt]
22835
22836 C++2A Extension:
22837
22838 designated-initializer-list:
22839 designated-initializer-clause
22840 designated-initializer-list , designated-initializer-clause
22841
22842 designated-initializer-clause:
22843 designator brace-or-equal-initializer
22844
22845 designator:
22846 . identifier
22847
22848 GNU Extension:
22849
22850 initializer-list:
22851 designation initializer-clause ...[opt]
22852 initializer-list , designation initializer-clause ...[opt]
22853
22854 designation:
22855 . identifier =
22856 identifier :
22857 [ constant-expression ] =
22858
22859 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22860 for the initializer. If the INDEX of the elt is non-NULL, it is the
22861 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22862 as for cp_parser_initializer. */
22863
22864 static vec<constructor_elt, va_gc> *
22865 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22866 {
22867 vec<constructor_elt, va_gc> *v = NULL;
22868 bool first_p = true;
22869 tree first_designator = NULL_TREE;
22870
22871 /* Assume all of the expressions are constant. */
22872 *non_constant_p = false;
22873
22874 /* Parse the rest of the list. */
22875 while (true)
22876 {
22877 cp_token *token;
22878 tree designator;
22879 tree initializer;
22880 bool clause_non_constant_p;
22881 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22882
22883 /* Handle the C++2A syntax, '. id ='. */
22884 if ((cxx_dialect >= cxx2a
22885 || cp_parser_allow_gnu_extensions_p (parser))
22886 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22887 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22888 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22889 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22890 == CPP_OPEN_BRACE)))
22891 {
22892 if (cxx_dialect < cxx2a)
22893 pedwarn (loc, OPT_Wpedantic,
22894 "C++ designated initializers only available with "
22895 "-std=c++2a or -std=gnu++2a");
22896 /* Consume the `.'. */
22897 cp_lexer_consume_token (parser->lexer);
22898 /* Consume the identifier. */
22899 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22900 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22901 /* Consume the `='. */
22902 cp_lexer_consume_token (parser->lexer);
22903 }
22904 /* Also, if the next token is an identifier and the following one is a
22905 colon, we are looking at the GNU designated-initializer
22906 syntax. */
22907 else if (cp_parser_allow_gnu_extensions_p (parser)
22908 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22909 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22910 == CPP_COLON))
22911 {
22912 /* Warn the user that they are using an extension. */
22913 pedwarn (loc, OPT_Wpedantic,
22914 "ISO C++ does not allow GNU designated initializers");
22915 /* Consume the identifier. */
22916 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22917 /* Consume the `:'. */
22918 cp_lexer_consume_token (parser->lexer);
22919 }
22920 /* Also handle C99 array designators, '[ const ] ='. */
22921 else if (cp_parser_allow_gnu_extensions_p (parser)
22922 && !c_dialect_objc ()
22923 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22924 {
22925 /* In C++11, [ could start a lambda-introducer. */
22926 bool non_const = false;
22927
22928 cp_parser_parse_tentatively (parser);
22929
22930 if (!cp_parser_array_designator_p (parser))
22931 {
22932 cp_parser_simulate_error (parser);
22933 designator = NULL_TREE;
22934 }
22935 else
22936 {
22937 designator = cp_parser_constant_expression (parser, true,
22938 &non_const);
22939 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22940 cp_parser_require (parser, CPP_EQ, RT_EQ);
22941 }
22942
22943 if (!cp_parser_parse_definitely (parser))
22944 designator = NULL_TREE;
22945 else if (non_const
22946 && (!require_potential_rvalue_constant_expression
22947 (designator)))
22948 designator = NULL_TREE;
22949 if (designator)
22950 /* Warn the user that they are using an extension. */
22951 pedwarn (loc, OPT_Wpedantic,
22952 "ISO C++ does not allow C99 designated initializers");
22953 }
22954 else
22955 designator = NULL_TREE;
22956
22957 if (first_p)
22958 {
22959 first_designator = designator;
22960 first_p = false;
22961 }
22962 else if (cxx_dialect >= cxx2a
22963 && first_designator != error_mark_node
22964 && (!first_designator != !designator))
22965 {
22966 error_at (loc, "either all initializer clauses should be designated "
22967 "or none of them should be");
22968 first_designator = error_mark_node;
22969 }
22970 else if (cxx_dialect < cxx2a && !first_designator)
22971 first_designator = designator;
22972
22973 /* Parse the initializer. */
22974 initializer = cp_parser_initializer_clause (parser,
22975 &clause_non_constant_p);
22976 /* If any clause is non-constant, so is the entire initializer. */
22977 if (clause_non_constant_p)
22978 *non_constant_p = true;
22979
22980 /* If we have an ellipsis, this is an initializer pack
22981 expansion. */
22982 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22983 {
22984 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22985
22986 /* Consume the `...'. */
22987 cp_lexer_consume_token (parser->lexer);
22988
22989 if (designator && cxx_dialect >= cxx2a)
22990 error_at (loc,
22991 "%<...%> not allowed in designated initializer list");
22992
22993 /* Turn the initializer into an initializer expansion. */
22994 initializer = make_pack_expansion (initializer);
22995 }
22996
22997 /* Add it to the vector. */
22998 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22999
23000 /* If the next token is not a comma, we have reached the end of
23001 the list. */
23002 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23003 break;
23004
23005 /* Peek at the next token. */
23006 token = cp_lexer_peek_nth_token (parser->lexer, 2);
23007 /* If the next token is a `}', then we're still done. An
23008 initializer-clause can have a trailing `,' after the
23009 initializer-list and before the closing `}'. */
23010 if (token->type == CPP_CLOSE_BRACE)
23011 break;
23012
23013 /* Consume the `,' token. */
23014 cp_lexer_consume_token (parser->lexer);
23015 }
23016
23017 /* The same identifier shall not appear in multiple designators
23018 of a designated-initializer-list. */
23019 if (first_designator)
23020 {
23021 unsigned int i;
23022 tree designator, val;
23023 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23024 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23025 {
23026 if (IDENTIFIER_MARKED (designator))
23027 {
23028 error_at (cp_expr_loc_or_loc (val, input_location),
23029 "%<.%s%> designator used multiple times in "
23030 "the same initializer list",
23031 IDENTIFIER_POINTER (designator));
23032 (*v)[i].index = error_mark_node;
23033 }
23034 else
23035 IDENTIFIER_MARKED (designator) = 1;
23036 }
23037 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23038 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23039 IDENTIFIER_MARKED (designator) = 0;
23040 }
23041
23042 return v;
23043 }
23044
23045 /* Classes [gram.class] */
23046
23047 /* Parse a class-name.
23048
23049 class-name:
23050 identifier
23051 template-id
23052
23053 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23054 to indicate that names looked up in dependent types should be
23055 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23056 keyword has been used to indicate that the name that appears next
23057 is a template. TAG_TYPE indicates the explicit tag given before
23058 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23059 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23060 is the class being defined in a class-head. If ENUM_OK is TRUE,
23061 enum-names are also accepted.
23062
23063 Returns the TYPE_DECL representing the class. */
23064
23065 static tree
23066 cp_parser_class_name (cp_parser *parser,
23067 bool typename_keyword_p,
23068 bool template_keyword_p,
23069 enum tag_types tag_type,
23070 bool check_dependency_p,
23071 bool class_head_p,
23072 bool is_declaration,
23073 bool enum_ok)
23074 {
23075 tree decl;
23076 tree scope;
23077 bool typename_p;
23078 cp_token *token;
23079 tree identifier = NULL_TREE;
23080
23081 /* All class-names start with an identifier. */
23082 token = cp_lexer_peek_token (parser->lexer);
23083 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
23084 {
23085 cp_parser_error (parser, "expected class-name");
23086 return error_mark_node;
23087 }
23088
23089 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23090 to a template-id, so we save it here. */
23091 scope = parser->scope;
23092 if (scope == error_mark_node)
23093 return error_mark_node;
23094
23095 /* Any name names a type if we're following the `typename' keyword
23096 in a qualified name where the enclosing scope is type-dependent. */
23097 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
23098 && dependent_type_p (scope));
23099 /* Handle the common case (an identifier, but not a template-id)
23100 efficiently. */
23101 if (token->type == CPP_NAME
23102 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
23103 {
23104 cp_token *identifier_token;
23105 bool ambiguous_p;
23106
23107 /* Look for the identifier. */
23108 identifier_token = cp_lexer_peek_token (parser->lexer);
23109 ambiguous_p = identifier_token->error_reported;
23110 identifier = cp_parser_identifier (parser);
23111 /* If the next token isn't an identifier, we are certainly not
23112 looking at a class-name. */
23113 if (identifier == error_mark_node)
23114 decl = error_mark_node;
23115 /* If we know this is a type-name, there's no need to look it
23116 up. */
23117 else if (typename_p)
23118 decl = identifier;
23119 else
23120 {
23121 tree ambiguous_decls;
23122 /* If we already know that this lookup is ambiguous, then
23123 we've already issued an error message; there's no reason
23124 to check again. */
23125 if (ambiguous_p)
23126 {
23127 cp_parser_simulate_error (parser);
23128 return error_mark_node;
23129 }
23130 /* If the next token is a `::', then the name must be a type
23131 name.
23132
23133 [basic.lookup.qual]
23134
23135 During the lookup for a name preceding the :: scope
23136 resolution operator, object, function, and enumerator
23137 names are ignored. */
23138 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23139 tag_type = scope_type;
23140 /* Look up the name. */
23141 decl = cp_parser_lookup_name (parser, identifier,
23142 tag_type,
23143 /*is_template=*/false,
23144 /*is_namespace=*/false,
23145 check_dependency_p,
23146 &ambiguous_decls,
23147 identifier_token->location);
23148 if (ambiguous_decls)
23149 {
23150 if (cp_parser_parsing_tentatively (parser))
23151 cp_parser_simulate_error (parser);
23152 return error_mark_node;
23153 }
23154 }
23155 }
23156 else
23157 {
23158 /* Try a template-id. */
23159 decl = cp_parser_template_id (parser, template_keyword_p,
23160 check_dependency_p,
23161 tag_type,
23162 is_declaration);
23163 if (decl == error_mark_node)
23164 return error_mark_node;
23165 }
23166
23167 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
23168
23169 /* If this is a typename, create a TYPENAME_TYPE. */
23170 if (typename_p && decl != error_mark_node)
23171 {
23172 decl = make_typename_type (scope, decl, typename_type,
23173 /*complain=*/tf_error);
23174 if (decl != error_mark_node)
23175 decl = TYPE_NAME (decl);
23176 }
23177
23178 decl = strip_using_decl (decl);
23179
23180 /* Check to see that it is really the name of a class. */
23181 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
23182 && identifier_p (TREE_OPERAND (decl, 0))
23183 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23184 /* Situations like this:
23185
23186 template <typename T> struct A {
23187 typename T::template X<int>::I i;
23188 };
23189
23190 are problematic. Is `T::template X<int>' a class-name? The
23191 standard does not seem to be definitive, but there is no other
23192 valid interpretation of the following `::'. Therefore, those
23193 names are considered class-names. */
23194 {
23195 decl = make_typename_type (scope, decl, tag_type, tf_error);
23196 if (decl != error_mark_node)
23197 decl = TYPE_NAME (decl);
23198 }
23199 else if (TREE_CODE (decl) != TYPE_DECL
23200 || TREE_TYPE (decl) == error_mark_node
23201 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
23202 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
23203 /* In Objective-C 2.0, a classname followed by '.' starts a
23204 dot-syntax expression, and it's not a type-name. */
23205 || (c_dialect_objc ()
23206 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
23207 && objc_is_class_name (decl)))
23208 decl = error_mark_node;
23209
23210 if (decl == error_mark_node)
23211 cp_parser_error (parser, "expected class-name");
23212 else if (identifier && !parser->scope)
23213 maybe_note_name_used_in_class (identifier, decl);
23214
23215 return decl;
23216 }
23217
23218 /* Parse a class-specifier.
23219
23220 class-specifier:
23221 class-head { member-specification [opt] }
23222
23223 Returns the TREE_TYPE representing the class. */
23224
23225 static tree
23226 cp_parser_class_specifier_1 (cp_parser* parser)
23227 {
23228 tree type;
23229 tree attributes = NULL_TREE;
23230 bool nested_name_specifier_p;
23231 unsigned saved_num_template_parameter_lists;
23232 bool saved_in_function_body;
23233 unsigned char in_statement;
23234 bool in_switch_statement_p;
23235 bool saved_in_unbraced_linkage_specification_p;
23236 tree old_scope = NULL_TREE;
23237 tree scope = NULL_TREE;
23238 cp_token *closing_brace;
23239
23240 push_deferring_access_checks (dk_no_deferred);
23241
23242 /* Parse the class-head. */
23243 type = cp_parser_class_head (parser,
23244 &nested_name_specifier_p);
23245 /* If the class-head was a semantic disaster, skip the entire body
23246 of the class. */
23247 if (!type)
23248 {
23249 cp_parser_skip_to_end_of_block_or_statement (parser);
23250 pop_deferring_access_checks ();
23251 return error_mark_node;
23252 }
23253
23254 /* Look for the `{'. */
23255 matching_braces braces;
23256 if (!braces.require_open (parser))
23257 {
23258 pop_deferring_access_checks ();
23259 return error_mark_node;
23260 }
23261
23262 cp_ensure_no_omp_declare_simd (parser);
23263 cp_ensure_no_oacc_routine (parser);
23264
23265 /* Issue an error message if type-definitions are forbidden here. */
23266 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
23267 /* Remember that we are defining one more class. */
23268 ++parser->num_classes_being_defined;
23269 /* Inside the class, surrounding template-parameter-lists do not
23270 apply. */
23271 saved_num_template_parameter_lists
23272 = parser->num_template_parameter_lists;
23273 parser->num_template_parameter_lists = 0;
23274 /* We are not in a function body. */
23275 saved_in_function_body = parser->in_function_body;
23276 parser->in_function_body = false;
23277 /* Or in a loop. */
23278 in_statement = parser->in_statement;
23279 parser->in_statement = 0;
23280 /* Or in a switch. */
23281 in_switch_statement_p = parser->in_switch_statement_p;
23282 parser->in_switch_statement_p = false;
23283 /* We are not immediately inside an extern "lang" block. */
23284 saved_in_unbraced_linkage_specification_p
23285 = parser->in_unbraced_linkage_specification_p;
23286 parser->in_unbraced_linkage_specification_p = false;
23287
23288 // Associate constraints with the type.
23289 if (flag_concepts)
23290 type = associate_classtype_constraints (type);
23291
23292 /* Start the class. */
23293 if (nested_name_specifier_p)
23294 {
23295 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
23296 old_scope = push_inner_scope (scope);
23297 }
23298 type = begin_class_definition (type);
23299
23300 if (type == error_mark_node)
23301 /* If the type is erroneous, skip the entire body of the class. */
23302 cp_parser_skip_to_closing_brace (parser);
23303 else
23304 /* Parse the member-specification. */
23305 cp_parser_member_specification_opt (parser);
23306
23307 /* Look for the trailing `}'. */
23308 closing_brace = braces.require_close (parser);
23309 /* Look for trailing attributes to apply to this class. */
23310 if (cp_parser_allow_gnu_extensions_p (parser))
23311 attributes = cp_parser_gnu_attributes_opt (parser);
23312 if (type != error_mark_node)
23313 type = finish_struct (type, attributes);
23314 if (nested_name_specifier_p)
23315 pop_inner_scope (old_scope, scope);
23316
23317 /* We've finished a type definition. Check for the common syntax
23318 error of forgetting a semicolon after the definition. We need to
23319 be careful, as we can't just check for not-a-semicolon and be done
23320 with it; the user might have typed:
23321
23322 class X { } c = ...;
23323 class X { } *p = ...;
23324
23325 and so forth. Instead, enumerate all the possible tokens that
23326 might follow this production; if we don't see one of them, then
23327 complain and silently insert the semicolon. */
23328 {
23329 cp_token *token = cp_lexer_peek_token (parser->lexer);
23330 bool want_semicolon = true;
23331
23332 if (cp_next_tokens_can_be_std_attribute_p (parser))
23333 /* Don't try to parse c++11 attributes here. As per the
23334 grammar, that should be a task for
23335 cp_parser_decl_specifier_seq. */
23336 want_semicolon = false;
23337
23338 switch (token->type)
23339 {
23340 case CPP_NAME:
23341 case CPP_SEMICOLON:
23342 case CPP_MULT:
23343 case CPP_AND:
23344 case CPP_OPEN_PAREN:
23345 case CPP_CLOSE_PAREN:
23346 case CPP_COMMA:
23347 want_semicolon = false;
23348 break;
23349
23350 /* While it's legal for type qualifiers and storage class
23351 specifiers to follow type definitions in the grammar, only
23352 compiler testsuites contain code like that. Assume that if
23353 we see such code, then what we're really seeing is a case
23354 like:
23355
23356 class X { }
23357 const <type> var = ...;
23358
23359 or
23360
23361 class Y { }
23362 static <type> func (...) ...
23363
23364 i.e. the qualifier or specifier applies to the next
23365 declaration. To do so, however, we need to look ahead one
23366 more token to see if *that* token is a type specifier.
23367
23368 This code could be improved to handle:
23369
23370 class Z { }
23371 static const <type> var = ...; */
23372 case CPP_KEYWORD:
23373 if (keyword_is_decl_specifier (token->keyword))
23374 {
23375 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
23376
23377 /* Handling user-defined types here would be nice, but very
23378 tricky. */
23379 want_semicolon
23380 = (lookahead->type == CPP_KEYWORD
23381 && keyword_begins_type_specifier (lookahead->keyword));
23382 }
23383 break;
23384 default:
23385 break;
23386 }
23387
23388 /* If we don't have a type, then something is very wrong and we
23389 shouldn't try to do anything clever. Likewise for not seeing the
23390 closing brace. */
23391 if (closing_brace && TYPE_P (type) && want_semicolon)
23392 {
23393 /* Locate the closing brace. */
23394 cp_token_position prev
23395 = cp_lexer_previous_token_position (parser->lexer);
23396 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
23397 location_t loc = prev_token->location;
23398
23399 /* We want to suggest insertion of a ';' immediately *after* the
23400 closing brace, so, if we can, offset the location by 1 column. */
23401 location_t next_loc = loc;
23402 if (!linemap_location_from_macro_expansion_p (line_table, loc))
23403 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
23404
23405 rich_location richloc (line_table, next_loc);
23406
23407 /* If we successfully offset the location, suggest the fix-it. */
23408 if (next_loc != loc)
23409 richloc.add_fixit_insert_before (next_loc, ";");
23410
23411 if (CLASSTYPE_DECLARED_CLASS (type))
23412 error_at (&richloc,
23413 "expected %<;%> after class definition");
23414 else if (TREE_CODE (type) == RECORD_TYPE)
23415 error_at (&richloc,
23416 "expected %<;%> after struct definition");
23417 else if (TREE_CODE (type) == UNION_TYPE)
23418 error_at (&richloc,
23419 "expected %<;%> after union definition");
23420 else
23421 gcc_unreachable ();
23422
23423 /* Unget one token and smash it to look as though we encountered
23424 a semicolon in the input stream. */
23425 cp_lexer_set_token_position (parser->lexer, prev);
23426 token = cp_lexer_peek_token (parser->lexer);
23427 token->type = CPP_SEMICOLON;
23428 token->keyword = RID_MAX;
23429 }
23430 }
23431
23432 /* If this class is not itself within the scope of another class,
23433 then we need to parse the bodies of all of the queued function
23434 definitions. Note that the queued functions defined in a class
23435 are not always processed immediately following the
23436 class-specifier for that class. Consider:
23437
23438 struct A {
23439 struct B { void f() { sizeof (A); } };
23440 };
23441
23442 If `f' were processed before the processing of `A' were
23443 completed, there would be no way to compute the size of `A'.
23444 Note that the nesting we are interested in here is lexical --
23445 not the semantic nesting given by TYPE_CONTEXT. In particular,
23446 for:
23447
23448 struct A { struct B; };
23449 struct A::B { void f() { } };
23450
23451 there is no need to delay the parsing of `A::B::f'. */
23452 if (--parser->num_classes_being_defined == 0)
23453 {
23454 tree decl;
23455 tree class_type = NULL_TREE;
23456 tree pushed_scope = NULL_TREE;
23457 unsigned ix;
23458 cp_default_arg_entry *e;
23459 tree save_ccp, save_ccr;
23460
23461 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
23462 {
23463 /* Skip default arguments, NSDMIs, etc, in order to improve
23464 error recovery (c++/71169, c++/71832). */
23465 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23466 vec_safe_truncate (unparsed_nsdmis, 0);
23467 vec_safe_truncate (unparsed_classes, 0);
23468 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23469 }
23470
23471 /* In a first pass, parse default arguments to the functions.
23472 Then, in a second pass, parse the bodies of the functions.
23473 This two-phased approach handles cases like:
23474
23475 struct S {
23476 void f() { g(); }
23477 void g(int i = 3);
23478 };
23479
23480 */
23481 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
23482 {
23483 decl = e->decl;
23484 /* If there are default arguments that have not yet been processed,
23485 take care of them now. */
23486 if (class_type != e->class_type)
23487 {
23488 if (pushed_scope)
23489 pop_scope (pushed_scope);
23490 class_type = e->class_type;
23491 pushed_scope = push_scope (class_type);
23492 }
23493 /* Make sure that any template parameters are in scope. */
23494 maybe_begin_member_template_processing (decl);
23495 /* Parse the default argument expressions. */
23496 cp_parser_late_parsing_default_args (parser, decl);
23497 /* Remove any template parameters from the symbol table. */
23498 maybe_end_member_template_processing ();
23499 }
23500 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23501 /* Now parse any NSDMIs. */
23502 save_ccp = current_class_ptr;
23503 save_ccr = current_class_ref;
23504 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23505 {
23506 if (class_type != DECL_CONTEXT (decl))
23507 {
23508 if (pushed_scope)
23509 pop_scope (pushed_scope);
23510 class_type = DECL_CONTEXT (decl);
23511 pushed_scope = push_scope (class_type);
23512 }
23513 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23514 cp_parser_late_parsing_nsdmi (parser, decl);
23515 }
23516 vec_safe_truncate (unparsed_nsdmis, 0);
23517 current_class_ptr = save_ccp;
23518 current_class_ref = save_ccr;
23519 if (pushed_scope)
23520 pop_scope (pushed_scope);
23521
23522 /* Now do some post-NSDMI bookkeeping. */
23523 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23524 after_nsdmi_defaulted_late_checks (class_type);
23525 vec_safe_truncate (unparsed_classes, 0);
23526 after_nsdmi_defaulted_late_checks (type);
23527
23528 /* Now parse the body of the functions. */
23529 if (flag_openmp)
23530 {
23531 /* OpenMP UDRs need to be parsed before all other functions. */
23532 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23533 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23534 cp_parser_late_parsing_for_member (parser, decl);
23535 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23536 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23537 cp_parser_late_parsing_for_member (parser, decl);
23538 }
23539 else
23540 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23541 cp_parser_late_parsing_for_member (parser, decl);
23542 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23543 }
23544 else
23545 vec_safe_push (unparsed_classes, type);
23546
23547 /* Put back any saved access checks. */
23548 pop_deferring_access_checks ();
23549
23550 /* Restore saved state. */
23551 parser->in_switch_statement_p = in_switch_statement_p;
23552 parser->in_statement = in_statement;
23553 parser->in_function_body = saved_in_function_body;
23554 parser->num_template_parameter_lists
23555 = saved_num_template_parameter_lists;
23556 parser->in_unbraced_linkage_specification_p
23557 = saved_in_unbraced_linkage_specification_p;
23558
23559 return type;
23560 }
23561
23562 static tree
23563 cp_parser_class_specifier (cp_parser* parser)
23564 {
23565 tree ret;
23566 timevar_push (TV_PARSE_STRUCT);
23567 ret = cp_parser_class_specifier_1 (parser);
23568 timevar_pop (TV_PARSE_STRUCT);
23569 return ret;
23570 }
23571
23572 /* Parse a class-head.
23573
23574 class-head:
23575 class-key identifier [opt] base-clause [opt]
23576 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23577 class-key nested-name-specifier [opt] template-id
23578 base-clause [opt]
23579
23580 class-virt-specifier:
23581 final
23582
23583 GNU Extensions:
23584 class-key attributes identifier [opt] base-clause [opt]
23585 class-key attributes nested-name-specifier identifier base-clause [opt]
23586 class-key attributes nested-name-specifier [opt] template-id
23587 base-clause [opt]
23588
23589 Upon return BASES is initialized to the list of base classes (or
23590 NULL, if there are none) in the same form returned by
23591 cp_parser_base_clause.
23592
23593 Returns the TYPE of the indicated class. Sets
23594 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23595 involving a nested-name-specifier was used, and FALSE otherwise.
23596
23597 Returns error_mark_node if this is not a class-head.
23598
23599 Returns NULL_TREE if the class-head is syntactically valid, but
23600 semantically invalid in a way that means we should skip the entire
23601 body of the class. */
23602
23603 static tree
23604 cp_parser_class_head (cp_parser* parser,
23605 bool* nested_name_specifier_p)
23606 {
23607 tree nested_name_specifier;
23608 enum tag_types class_key;
23609 tree id = NULL_TREE;
23610 tree type = NULL_TREE;
23611 tree attributes;
23612 tree bases;
23613 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23614 bool template_id_p = false;
23615 bool qualified_p = false;
23616 bool invalid_nested_name_p = false;
23617 bool invalid_explicit_specialization_p = false;
23618 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23619 tree pushed_scope = NULL_TREE;
23620 unsigned num_templates;
23621 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23622 /* Assume no nested-name-specifier will be present. */
23623 *nested_name_specifier_p = false;
23624 /* Assume no template parameter lists will be used in defining the
23625 type. */
23626 num_templates = 0;
23627 parser->colon_corrects_to_scope_p = false;
23628
23629 /* Look for the class-key. */
23630 class_key = cp_parser_class_key (parser);
23631 if (class_key == none_type)
23632 return error_mark_node;
23633
23634 location_t class_head_start_location = input_location;
23635
23636 /* Parse the attributes. */
23637 attributes = cp_parser_attributes_opt (parser);
23638
23639 /* If the next token is `::', that is invalid -- but sometimes
23640 people do try to write:
23641
23642 struct ::S {};
23643
23644 Handle this gracefully by accepting the extra qualifier, and then
23645 issuing an error about it later if this really is a
23646 class-head. If it turns out just to be an elaborated type
23647 specifier, remain silent. */
23648 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23649 qualified_p = true;
23650
23651 push_deferring_access_checks (dk_no_check);
23652
23653 /* Determine the name of the class. Begin by looking for an
23654 optional nested-name-specifier. */
23655 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23656 nested_name_specifier
23657 = cp_parser_nested_name_specifier_opt (parser,
23658 /*typename_keyword_p=*/false,
23659 /*check_dependency_p=*/false,
23660 /*type_p=*/true,
23661 /*is_declaration=*/false);
23662 /* If there was a nested-name-specifier, then there *must* be an
23663 identifier. */
23664
23665 cp_token *bad_template_keyword = NULL;
23666
23667 if (nested_name_specifier)
23668 {
23669 type_start_token = cp_lexer_peek_token (parser->lexer);
23670 /* Although the grammar says `identifier', it really means
23671 `class-name' or `template-name'. You are only allowed to
23672 define a class that has already been declared with this
23673 syntax.
23674
23675 The proposed resolution for Core Issue 180 says that wherever
23676 you see `class T::X' you should treat `X' as a type-name.
23677
23678 It is OK to define an inaccessible class; for example:
23679
23680 class A { class B; };
23681 class A::B {};
23682
23683 We do not know if we will see a class-name, or a
23684 template-name. We look for a class-name first, in case the
23685 class-name is a template-id; if we looked for the
23686 template-name first we would stop after the template-name. */
23687 cp_parser_parse_tentatively (parser);
23688 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23689 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23690 type = cp_parser_class_name (parser,
23691 /*typename_keyword_p=*/false,
23692 /*template_keyword_p=*/false,
23693 class_type,
23694 /*check_dependency_p=*/false,
23695 /*class_head_p=*/true,
23696 /*is_declaration=*/false);
23697 /* If that didn't work, ignore the nested-name-specifier. */
23698 if (!cp_parser_parse_definitely (parser))
23699 {
23700 invalid_nested_name_p = true;
23701 type_start_token = cp_lexer_peek_token (parser->lexer);
23702 id = cp_parser_identifier (parser);
23703 if (id == error_mark_node)
23704 id = NULL_TREE;
23705 }
23706 /* If we could not find a corresponding TYPE, treat this
23707 declaration like an unqualified declaration. */
23708 if (type == error_mark_node)
23709 nested_name_specifier = NULL_TREE;
23710 /* Otherwise, count the number of templates used in TYPE and its
23711 containing scopes. */
23712 else
23713 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23714 }
23715 /* Otherwise, the identifier is optional. */
23716 else
23717 {
23718 /* We don't know whether what comes next is a template-id,
23719 an identifier, or nothing at all. */
23720 cp_parser_parse_tentatively (parser);
23721 /* Check for a template-id. */
23722 type_start_token = cp_lexer_peek_token (parser->lexer);
23723 id = cp_parser_template_id (parser,
23724 /*template_keyword_p=*/false,
23725 /*check_dependency_p=*/true,
23726 class_key,
23727 /*is_declaration=*/true);
23728 /* If that didn't work, it could still be an identifier. */
23729 if (!cp_parser_parse_definitely (parser))
23730 {
23731 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23732 {
23733 type_start_token = cp_lexer_peek_token (parser->lexer);
23734 id = cp_parser_identifier (parser);
23735 }
23736 else
23737 id = NULL_TREE;
23738 }
23739 else
23740 {
23741 template_id_p = true;
23742 ++num_templates;
23743 }
23744 }
23745
23746 pop_deferring_access_checks ();
23747
23748 if (id)
23749 {
23750 cp_parser_check_for_invalid_template_id (parser, id,
23751 class_key,
23752 type_start_token->location);
23753 }
23754 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23755
23756 /* If it's not a `:' or a `{' then we can't really be looking at a
23757 class-head, since a class-head only appears as part of a
23758 class-specifier. We have to detect this situation before calling
23759 xref_tag, since that has irreversible side-effects. */
23760 if (!cp_parser_next_token_starts_class_definition_p (parser))
23761 {
23762 cp_parser_error (parser, "expected %<{%> or %<:%>");
23763 type = error_mark_node;
23764 goto out;
23765 }
23766
23767 /* At this point, we're going ahead with the class-specifier, even
23768 if some other problem occurs. */
23769 cp_parser_commit_to_tentative_parse (parser);
23770 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23771 {
23772 cp_parser_error (parser,
23773 "cannot specify %<override%> for a class");
23774 type = error_mark_node;
23775 goto out;
23776 }
23777 /* Issue the error about the overly-qualified name now. */
23778 if (qualified_p)
23779 {
23780 cp_parser_error (parser,
23781 "global qualification of class name is invalid");
23782 type = error_mark_node;
23783 goto out;
23784 }
23785 else if (invalid_nested_name_p)
23786 {
23787 cp_parser_error (parser,
23788 "qualified name does not name a class");
23789 type = error_mark_node;
23790 goto out;
23791 }
23792 else if (nested_name_specifier)
23793 {
23794 tree scope;
23795
23796 if (bad_template_keyword)
23797 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23798 keyword template shall not appear at the top level. */
23799 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23800 "keyword %<template%> not allowed in class-head-name");
23801
23802 /* Reject typedef-names in class heads. */
23803 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23804 {
23805 error_at (type_start_token->location,
23806 "invalid class name in declaration of %qD",
23807 type);
23808 type = NULL_TREE;
23809 goto done;
23810 }
23811
23812 /* Figure out in what scope the declaration is being placed. */
23813 scope = current_scope ();
23814 /* If that scope does not contain the scope in which the
23815 class was originally declared, the program is invalid. */
23816 if (scope && !is_ancestor (scope, nested_name_specifier))
23817 {
23818 if (at_namespace_scope_p ())
23819 error_at (type_start_token->location,
23820 "declaration of %qD in namespace %qD which does not "
23821 "enclose %qD",
23822 type, scope, nested_name_specifier);
23823 else
23824 error_at (type_start_token->location,
23825 "declaration of %qD in %qD which does not enclose %qD",
23826 type, scope, nested_name_specifier);
23827 type = NULL_TREE;
23828 goto done;
23829 }
23830 /* [dcl.meaning]
23831
23832 A declarator-id shall not be qualified except for the
23833 definition of a ... nested class outside of its class
23834 ... [or] the definition or explicit instantiation of a
23835 class member of a namespace outside of its namespace. */
23836 if (scope == nested_name_specifier)
23837 {
23838 permerror (nested_name_specifier_token_start->location,
23839 "extra qualification not allowed");
23840 nested_name_specifier = NULL_TREE;
23841 num_templates = 0;
23842 }
23843 }
23844 /* An explicit-specialization must be preceded by "template <>". If
23845 it is not, try to recover gracefully. */
23846 if (at_namespace_scope_p ()
23847 && parser->num_template_parameter_lists == 0
23848 && !processing_template_parmlist
23849 && template_id_p)
23850 {
23851 /* Build a location of this form:
23852 struct typename <ARGS>
23853 ^~~~~~~~~~~~~~~~~~~~~~
23854 with caret==start at the start token, and
23855 finishing at the end of the type. */
23856 location_t reported_loc
23857 = make_location (class_head_start_location,
23858 class_head_start_location,
23859 get_finish (type_start_token->location));
23860 rich_location richloc (line_table, reported_loc);
23861 richloc.add_fixit_insert_before (class_head_start_location,
23862 "template <> ");
23863 error_at (&richloc,
23864 "an explicit specialization must be preceded by"
23865 " %<template <>%>");
23866 invalid_explicit_specialization_p = true;
23867 /* Take the same action that would have been taken by
23868 cp_parser_explicit_specialization. */
23869 ++parser->num_template_parameter_lists;
23870 begin_specialization ();
23871 }
23872 /* There must be no "return" statements between this point and the
23873 end of this function; set "type "to the correct return value and
23874 use "goto done;" to return. */
23875 /* Make sure that the right number of template parameters were
23876 present. */
23877 if (!cp_parser_check_template_parameters (parser, num_templates,
23878 template_id_p,
23879 type_start_token->location,
23880 /*declarator=*/NULL))
23881 {
23882 /* If something went wrong, there is no point in even trying to
23883 process the class-definition. */
23884 type = NULL_TREE;
23885 goto done;
23886 }
23887
23888 /* Look up the type. */
23889 if (template_id_p)
23890 {
23891 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23892 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23893 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23894 {
23895 error_at (type_start_token->location,
23896 "function template %qD redeclared as a class template", id);
23897 type = error_mark_node;
23898 }
23899 else
23900 {
23901 type = TREE_TYPE (id);
23902 type = maybe_process_partial_specialization (type);
23903
23904 /* Check the scope while we still know whether or not we had a
23905 nested-name-specifier. */
23906 if (type != error_mark_node)
23907 check_unqualified_spec_or_inst (type, type_start_token->location);
23908 }
23909 if (nested_name_specifier)
23910 pushed_scope = push_scope (nested_name_specifier);
23911 }
23912 else if (nested_name_specifier)
23913 {
23914 tree class_type;
23915
23916 /* Given:
23917
23918 template <typename T> struct S { struct T };
23919 template <typename T> struct S<T>::T { };
23920
23921 we will get a TYPENAME_TYPE when processing the definition of
23922 `S::T'. We need to resolve it to the actual type before we
23923 try to define it. */
23924 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23925 {
23926 class_type = resolve_typename_type (TREE_TYPE (type),
23927 /*only_current_p=*/false);
23928 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23929 type = TYPE_NAME (class_type);
23930 else
23931 {
23932 cp_parser_error (parser, "could not resolve typename type");
23933 type = error_mark_node;
23934 }
23935 }
23936
23937 if (maybe_process_partial_specialization (TREE_TYPE (type))
23938 == error_mark_node)
23939 {
23940 type = NULL_TREE;
23941 goto done;
23942 }
23943
23944 class_type = current_class_type;
23945 /* Enter the scope indicated by the nested-name-specifier. */
23946 pushed_scope = push_scope (nested_name_specifier);
23947 /* Get the canonical version of this type. */
23948 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23949 /* Call push_template_decl if it seems like we should be defining a
23950 template either from the template headers or the type we're
23951 defining, so that we diagnose both extra and missing headers. */
23952 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23953 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23954 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23955 {
23956 type = push_template_decl (type);
23957 if (type == error_mark_node)
23958 {
23959 type = NULL_TREE;
23960 goto done;
23961 }
23962 }
23963
23964 type = TREE_TYPE (type);
23965 *nested_name_specifier_p = true;
23966 }
23967 else /* The name is not a nested name. */
23968 {
23969 /* If the class was unnamed, create a dummy name. */
23970 if (!id)
23971 id = make_anon_name ();
23972 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23973 ? ts_within_enclosing_non_class
23974 : ts_current);
23975 type = xref_tag (class_key, id, tag_scope,
23976 parser->num_template_parameter_lists);
23977 }
23978
23979 /* Indicate whether this class was declared as a `class' or as a
23980 `struct'. */
23981 if (TREE_CODE (type) == RECORD_TYPE)
23982 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23983 cp_parser_check_class_key (class_key, type);
23984
23985 /* If this type was already complete, and we see another definition,
23986 that's an error. */
23987 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23988 {
23989 error_at (type_start_token->location, "redefinition of %q#T",
23990 type);
23991 inform (location_of (type), "previous definition of %q#T",
23992 type);
23993 type = NULL_TREE;
23994 goto done;
23995 }
23996 else if (type == error_mark_node)
23997 type = NULL_TREE;
23998
23999 if (type)
24000 {
24001 /* Apply attributes now, before any use of the class as a template
24002 argument in its base list. */
24003 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
24004 fixup_attribute_variants (type);
24005 }
24006
24007 /* We will have entered the scope containing the class; the names of
24008 base classes should be looked up in that context. For example:
24009
24010 struct A { struct B {}; struct C; };
24011 struct A::C : B {};
24012
24013 is valid. */
24014
24015 /* Get the list of base-classes, if there is one. */
24016 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24017 {
24018 /* PR59482: enter the class scope so that base-specifiers are looked
24019 up correctly. */
24020 if (type)
24021 pushclass (type);
24022 bases = cp_parser_base_clause (parser);
24023 /* PR59482: get out of the previously pushed class scope so that the
24024 subsequent pops pop the right thing. */
24025 if (type)
24026 popclass ();
24027 }
24028 else
24029 bases = NULL_TREE;
24030
24031 /* If we're really defining a class, process the base classes.
24032 If they're invalid, fail. */
24033 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24034 xref_basetypes (type, bases);
24035
24036 done:
24037 /* Leave the scope given by the nested-name-specifier. We will
24038 enter the class scope itself while processing the members. */
24039 if (pushed_scope)
24040 pop_scope (pushed_scope);
24041
24042 if (invalid_explicit_specialization_p)
24043 {
24044 end_specialization ();
24045 --parser->num_template_parameter_lists;
24046 }
24047
24048 if (type)
24049 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
24050 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
24051 CLASSTYPE_FINAL (type) = 1;
24052 out:
24053 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24054 return type;
24055 }
24056
24057 /* Parse a class-key.
24058
24059 class-key:
24060 class
24061 struct
24062 union
24063
24064 Returns the kind of class-key specified, or none_type to indicate
24065 error. */
24066
24067 static enum tag_types
24068 cp_parser_class_key (cp_parser* parser)
24069 {
24070 cp_token *token;
24071 enum tag_types tag_type;
24072
24073 /* Look for the class-key. */
24074 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
24075 if (!token)
24076 return none_type;
24077
24078 /* Check to see if the TOKEN is a class-key. */
24079 tag_type = cp_parser_token_is_class_key (token);
24080 if (!tag_type)
24081 cp_parser_error (parser, "expected class-key");
24082 return tag_type;
24083 }
24084
24085 /* Parse a type-parameter-key.
24086
24087 type-parameter-key:
24088 class
24089 typename
24090 */
24091
24092 static void
24093 cp_parser_type_parameter_key (cp_parser* parser)
24094 {
24095 /* Look for the type-parameter-key. */
24096 enum tag_types tag_type = none_type;
24097 cp_token *token = cp_lexer_peek_token (parser->lexer);
24098 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
24099 {
24100 cp_lexer_consume_token (parser->lexer);
24101 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
24102 /* typename is not allowed in a template template parameter
24103 by the standard until C++17. */
24104 pedwarn (token->location, OPT_Wpedantic,
24105 "ISO C++ forbids typename key in template template parameter;"
24106 " use -std=c++17 or -std=gnu++17");
24107 }
24108 else
24109 cp_parser_error (parser, "expected %<class%> or %<typename%>");
24110
24111 return;
24112 }
24113
24114 /* Parse an (optional) member-specification.
24115
24116 member-specification:
24117 member-declaration member-specification [opt]
24118 access-specifier : member-specification [opt] */
24119
24120 static void
24121 cp_parser_member_specification_opt (cp_parser* parser)
24122 {
24123 while (true)
24124 {
24125 cp_token *token;
24126 enum rid keyword;
24127
24128 /* Peek at the next token. */
24129 token = cp_lexer_peek_token (parser->lexer);
24130 /* If it's a `}', or EOF then we've seen all the members. */
24131 if (token->type == CPP_CLOSE_BRACE
24132 || token->type == CPP_EOF
24133 || token->type == CPP_PRAGMA_EOL)
24134 break;
24135
24136 /* See if this token is a keyword. */
24137 keyword = token->keyword;
24138 switch (keyword)
24139 {
24140 case RID_PUBLIC:
24141 case RID_PROTECTED:
24142 case RID_PRIVATE:
24143 /* Consume the access-specifier. */
24144 cp_lexer_consume_token (parser->lexer);
24145 /* Remember which access-specifier is active. */
24146 current_access_specifier = token->u.value;
24147 /* Look for the `:'. */
24148 cp_parser_require (parser, CPP_COLON, RT_COLON);
24149 break;
24150
24151 default:
24152 /* Accept #pragmas at class scope. */
24153 if (token->type == CPP_PRAGMA)
24154 {
24155 cp_parser_pragma (parser, pragma_member, NULL);
24156 break;
24157 }
24158
24159 /* Otherwise, the next construction must be a
24160 member-declaration. */
24161 cp_parser_member_declaration (parser);
24162 }
24163 }
24164 }
24165
24166 /* Parse a member-declaration.
24167
24168 member-declaration:
24169 decl-specifier-seq [opt] member-declarator-list [opt] ;
24170 function-definition ; [opt]
24171 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24172 using-declaration
24173 template-declaration
24174 alias-declaration
24175
24176 member-declarator-list:
24177 member-declarator
24178 member-declarator-list , member-declarator
24179
24180 member-declarator:
24181 declarator pure-specifier [opt]
24182 declarator constant-initializer [opt]
24183 identifier [opt] : constant-expression
24184
24185 GNU Extensions:
24186
24187 member-declaration:
24188 __extension__ member-declaration
24189
24190 member-declarator:
24191 declarator attributes [opt] pure-specifier [opt]
24192 declarator attributes [opt] constant-initializer [opt]
24193 identifier [opt] attributes [opt] : constant-expression
24194
24195 C++0x Extensions:
24196
24197 member-declaration:
24198 static_assert-declaration */
24199
24200 static void
24201 cp_parser_member_declaration (cp_parser* parser)
24202 {
24203 cp_decl_specifier_seq decl_specifiers;
24204 tree prefix_attributes;
24205 tree decl;
24206 int declares_class_or_enum;
24207 bool friend_p;
24208 cp_token *token = NULL;
24209 cp_token *decl_spec_token_start = NULL;
24210 cp_token *initializer_token_start = NULL;
24211 int saved_pedantic;
24212 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24213
24214 /* Check for the `__extension__' keyword. */
24215 if (cp_parser_extension_opt (parser, &saved_pedantic))
24216 {
24217 /* Recurse. */
24218 cp_parser_member_declaration (parser);
24219 /* Restore the old value of the PEDANTIC flag. */
24220 pedantic = saved_pedantic;
24221
24222 return;
24223 }
24224
24225 /* Check for a template-declaration. */
24226 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24227 {
24228 /* An explicit specialization here is an error condition, and we
24229 expect the specialization handler to detect and report this. */
24230 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
24231 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
24232 cp_parser_explicit_specialization (parser);
24233 else
24234 cp_parser_template_declaration (parser, /*member_p=*/true);
24235
24236 return;
24237 }
24238 /* Check for a template introduction. */
24239 else if (cp_parser_template_declaration_after_export (parser, true))
24240 return;
24241
24242 /* Check for a using-declaration. */
24243 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24244 {
24245 if (cxx_dialect < cxx11)
24246 {
24247 /* Parse the using-declaration. */
24248 cp_parser_using_declaration (parser,
24249 /*access_declaration_p=*/false);
24250 return;
24251 }
24252 else
24253 {
24254 tree decl;
24255 bool alias_decl_expected;
24256 cp_parser_parse_tentatively (parser);
24257 decl = cp_parser_alias_declaration (parser);
24258 /* Note that if we actually see the '=' token after the
24259 identifier, cp_parser_alias_declaration commits the
24260 tentative parse. In that case, we really expect an
24261 alias-declaration. Otherwise, we expect a using
24262 declaration. */
24263 alias_decl_expected =
24264 !cp_parser_uncommitted_to_tentative_parse_p (parser);
24265 cp_parser_parse_definitely (parser);
24266
24267 if (alias_decl_expected)
24268 finish_member_declaration (decl);
24269 else
24270 cp_parser_using_declaration (parser,
24271 /*access_declaration_p=*/false);
24272 return;
24273 }
24274 }
24275
24276 /* Check for @defs. */
24277 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
24278 {
24279 tree ivar, member;
24280 tree ivar_chains = cp_parser_objc_defs_expression (parser);
24281 ivar = ivar_chains;
24282 while (ivar)
24283 {
24284 member = ivar;
24285 ivar = TREE_CHAIN (member);
24286 TREE_CHAIN (member) = NULL_TREE;
24287 finish_member_declaration (member);
24288 }
24289 return;
24290 }
24291
24292 /* If the next token is `static_assert' we have a static assertion. */
24293 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
24294 {
24295 cp_parser_static_assert (parser, /*member_p=*/true);
24296 return;
24297 }
24298
24299 parser->colon_corrects_to_scope_p = false;
24300
24301 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
24302 goto out;
24303
24304 /* Parse the decl-specifier-seq. */
24305 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24306 cp_parser_decl_specifier_seq (parser,
24307 (CP_PARSER_FLAGS_OPTIONAL
24308 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
24309 &decl_specifiers,
24310 &declares_class_or_enum);
24311 /* Check for an invalid type-name. */
24312 if (!decl_specifiers.any_type_specifiers_p
24313 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24314 goto out;
24315 /* If there is no declarator, then the decl-specifier-seq should
24316 specify a type. */
24317 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24318 {
24319 /* If there was no decl-specifier-seq, and the next token is a
24320 `;', then we have something like:
24321
24322 struct S { ; };
24323
24324 [class.mem]
24325
24326 Each member-declaration shall declare at least one member
24327 name of the class. */
24328 if (!decl_specifiers.any_specifiers_p)
24329 {
24330 cp_token *token = cp_lexer_peek_token (parser->lexer);
24331 if (!in_system_header_at (token->location))
24332 {
24333 gcc_rich_location richloc (token->location);
24334 richloc.add_fixit_remove ();
24335 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
24336 }
24337 }
24338 else
24339 {
24340 tree type;
24341
24342 /* See if this declaration is a friend. */
24343 friend_p = cp_parser_friend_p (&decl_specifiers);
24344 /* If there were decl-specifiers, check to see if there was
24345 a class-declaration. */
24346 type = check_tag_decl (&decl_specifiers,
24347 /*explicit_type_instantiation_p=*/false);
24348 /* Nested classes have already been added to the class, but
24349 a `friend' needs to be explicitly registered. */
24350 if (friend_p)
24351 {
24352 /* If the `friend' keyword was present, the friend must
24353 be introduced with a class-key. */
24354 if (!declares_class_or_enum && cxx_dialect < cxx11)
24355 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
24356 "in C++03 a class-key must be used "
24357 "when declaring a friend");
24358 /* In this case:
24359
24360 template <typename T> struct A {
24361 friend struct A<T>::B;
24362 };
24363
24364 A<T>::B will be represented by a TYPENAME_TYPE, and
24365 therefore not recognized by check_tag_decl. */
24366 if (!type)
24367 {
24368 type = decl_specifiers.type;
24369 if (type && TREE_CODE (type) == TYPE_DECL)
24370 type = TREE_TYPE (type);
24371 }
24372 if (!type || !TYPE_P (type))
24373 error_at (decl_spec_token_start->location,
24374 "friend declaration does not name a class or "
24375 "function");
24376 else
24377 make_friend_class (current_class_type, type,
24378 /*complain=*/true);
24379 }
24380 /* If there is no TYPE, an error message will already have
24381 been issued. */
24382 else if (!type || type == error_mark_node)
24383 ;
24384 /* An anonymous aggregate has to be handled specially; such
24385 a declaration really declares a data member (with a
24386 particular type), as opposed to a nested class. */
24387 else if (ANON_AGGR_TYPE_P (type))
24388 {
24389 /* C++11 9.5/6. */
24390 if (decl_specifiers.storage_class != sc_none)
24391 error_at (decl_spec_token_start->location,
24392 "a storage class on an anonymous aggregate "
24393 "in class scope is not allowed");
24394
24395 /* Remove constructors and such from TYPE, now that we
24396 know it is an anonymous aggregate. */
24397 fixup_anonymous_aggr (type);
24398 /* And make the corresponding data member. */
24399 decl = build_decl (decl_spec_token_start->location,
24400 FIELD_DECL, NULL_TREE, type);
24401 /* Add it to the class. */
24402 finish_member_declaration (decl);
24403 }
24404 else
24405 cp_parser_check_access_in_redeclaration
24406 (TYPE_NAME (type),
24407 decl_spec_token_start->location);
24408 }
24409 }
24410 else
24411 {
24412 bool assume_semicolon = false;
24413
24414 /* Clear attributes from the decl_specifiers but keep them
24415 around as prefix attributes that apply them to the entity
24416 being declared. */
24417 prefix_attributes = decl_specifiers.attributes;
24418 decl_specifiers.attributes = NULL_TREE;
24419
24420 /* See if these declarations will be friends. */
24421 friend_p = cp_parser_friend_p (&decl_specifiers);
24422
24423 /* Keep going until we hit the `;' at the end of the
24424 declaration. */
24425 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24426 {
24427 tree attributes = NULL_TREE;
24428 tree first_attribute;
24429 tree initializer;
24430 bool named_bitfld = false;
24431
24432 /* Peek at the next token. */
24433 token = cp_lexer_peek_token (parser->lexer);
24434
24435 /* The following code wants to know early if it is a bit-field
24436 or some other declaration. Attributes can appear before
24437 the `:' token. Skip over them without consuming any tokens
24438 to peek if they are followed by `:'. */
24439 if (cp_next_tokens_can_be_attribute_p (parser)
24440 || (token->type == CPP_NAME
24441 && cp_nth_tokens_can_be_attribute_p (parser, 2)
24442 && (named_bitfld = true)))
24443 {
24444 size_t n
24445 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
24446 token = cp_lexer_peek_nth_token (parser->lexer, n);
24447 }
24448
24449 /* Check for a bitfield declaration. */
24450 if (token->type == CPP_COLON
24451 || (token->type == CPP_NAME
24452 && token == cp_lexer_peek_token (parser->lexer)
24453 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
24454 && (named_bitfld = true)))
24455 {
24456 tree identifier;
24457 tree width;
24458 tree late_attributes = NULL_TREE;
24459 location_t id_location
24460 = cp_lexer_peek_token (parser->lexer)->location;
24461
24462 if (named_bitfld)
24463 identifier = cp_parser_identifier (parser);
24464 else
24465 identifier = NULL_TREE;
24466
24467 /* Look for attributes that apply to the bitfield. */
24468 attributes = cp_parser_attributes_opt (parser);
24469
24470 /* Consume the `:' token. */
24471 cp_lexer_consume_token (parser->lexer);
24472
24473 /* Get the width of the bitfield. */
24474 width = cp_parser_constant_expression (parser, false, NULL,
24475 cxx_dialect >= cxx11);
24476
24477 /* In C++2A and as extension for C++11 and above we allow
24478 default member initializers for bit-fields. */
24479 initializer = NULL_TREE;
24480 if (cxx_dialect >= cxx11
24481 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
24482 || cp_lexer_next_token_is (parser->lexer,
24483 CPP_OPEN_BRACE)))
24484 {
24485 location_t loc
24486 = cp_lexer_peek_token (parser->lexer)->location;
24487 if (cxx_dialect < cxx2a
24488 && !in_system_header_at (loc)
24489 && identifier != NULL_TREE)
24490 pedwarn (loc, 0,
24491 "default member initializers for bit-fields "
24492 "only available with -std=c++2a or "
24493 "-std=gnu++2a");
24494
24495 initializer = cp_parser_save_nsdmi (parser);
24496 if (identifier == NULL_TREE)
24497 {
24498 error_at (loc, "default member initializer for "
24499 "unnamed bit-field");
24500 initializer = NULL_TREE;
24501 }
24502 }
24503 else
24504 {
24505 /* Look for attributes that apply to the bitfield after
24506 the `:' token and width. This is where GCC used to
24507 parse attributes in the past, pedwarn if there is
24508 a std attribute. */
24509 if (cp_next_tokens_can_be_std_attribute_p (parser))
24510 pedwarn (input_location, OPT_Wpedantic,
24511 "ISO C++ allows bit-field attributes only "
24512 "before the %<:%> token");
24513
24514 late_attributes = cp_parser_attributes_opt (parser);
24515 }
24516
24517 attributes = attr_chainon (attributes, late_attributes);
24518
24519 /* Remember which attributes are prefix attributes and
24520 which are not. */
24521 first_attribute = attributes;
24522 /* Combine the attributes. */
24523 attributes = attr_chainon (prefix_attributes, attributes);
24524
24525 /* Create the bitfield declaration. */
24526 decl = grokbitfield (identifier
24527 ? make_id_declarator (NULL_TREE,
24528 identifier,
24529 sfk_none,
24530 id_location)
24531 : NULL,
24532 &decl_specifiers,
24533 width, initializer,
24534 attributes);
24535 }
24536 else
24537 {
24538 cp_declarator *declarator;
24539 tree asm_specification;
24540 int ctor_dtor_or_conv_p;
24541 bool static_p = (decl_specifiers.storage_class == sc_static);
24542
24543 /* Parse the declarator. */
24544 declarator
24545 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24546 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24547 &ctor_dtor_or_conv_p,
24548 /*parenthesized_p=*/NULL,
24549 /*member_p=*/true,
24550 friend_p, static_p);
24551
24552 /* If something went wrong parsing the declarator, make sure
24553 that we at least consume some tokens. */
24554 if (declarator == cp_error_declarator)
24555 {
24556 /* Skip to the end of the statement. */
24557 cp_parser_skip_to_end_of_statement (parser);
24558 /* If the next token is not a semicolon, that is
24559 probably because we just skipped over the body of
24560 a function. So, we consume a semicolon if
24561 present, but do not issue an error message if it
24562 is not present. */
24563 if (cp_lexer_next_token_is (parser->lexer,
24564 CPP_SEMICOLON))
24565 cp_lexer_consume_token (parser->lexer);
24566 goto out;
24567 }
24568
24569 if (declares_class_or_enum & 2)
24570 cp_parser_check_for_definition_in_return_type
24571 (declarator, decl_specifiers.type,
24572 decl_specifiers.locations[ds_type_spec]);
24573
24574 /* Look for an asm-specification. */
24575 asm_specification = cp_parser_asm_specification_opt (parser);
24576 /* Look for attributes that apply to the declaration. */
24577 attributes = cp_parser_attributes_opt (parser);
24578 /* Remember which attributes are prefix attributes and
24579 which are not. */
24580 first_attribute = attributes;
24581 /* Combine the attributes. */
24582 attributes = attr_chainon (prefix_attributes, attributes);
24583
24584 /* If it's an `=', then we have a constant-initializer or a
24585 pure-specifier. It is not correct to parse the
24586 initializer before registering the member declaration
24587 since the member declaration should be in scope while
24588 its initializer is processed. However, the rest of the
24589 front end does not yet provide an interface that allows
24590 us to handle this correctly. */
24591 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24592 {
24593 /* In [class.mem]:
24594
24595 A pure-specifier shall be used only in the declaration of
24596 a virtual function.
24597
24598 A member-declarator can contain a constant-initializer
24599 only if it declares a static member of integral or
24600 enumeration type.
24601
24602 Therefore, if the DECLARATOR is for a function, we look
24603 for a pure-specifier; otherwise, we look for a
24604 constant-initializer. When we call `grokfield', it will
24605 perform more stringent semantics checks. */
24606 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24607 if (function_declarator_p (declarator)
24608 || (decl_specifiers.type
24609 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24610 && declarator->kind == cdk_id
24611 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24612 == FUNCTION_TYPE)))
24613 initializer = cp_parser_pure_specifier (parser);
24614 else if (decl_specifiers.storage_class != sc_static)
24615 initializer = cp_parser_save_nsdmi (parser);
24616 else if (cxx_dialect >= cxx11)
24617 {
24618 bool nonconst;
24619 /* Don't require a constant rvalue in C++11, since we
24620 might want a reference constant. We'll enforce
24621 constancy later. */
24622 cp_lexer_consume_token (parser->lexer);
24623 /* Parse the initializer. */
24624 initializer = cp_parser_initializer_clause (parser,
24625 &nonconst);
24626 }
24627 else
24628 /* Parse the initializer. */
24629 initializer = cp_parser_constant_initializer (parser);
24630 }
24631 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24632 && !function_declarator_p (declarator))
24633 {
24634 bool x;
24635 if (decl_specifiers.storage_class != sc_static)
24636 initializer = cp_parser_save_nsdmi (parser);
24637 else
24638 initializer = cp_parser_initializer (parser, &x, &x);
24639 }
24640 /* Otherwise, there is no initializer. */
24641 else
24642 initializer = NULL_TREE;
24643
24644 /* See if we are probably looking at a function
24645 definition. We are certainly not looking at a
24646 member-declarator. Calling `grokfield' has
24647 side-effects, so we must not do it unless we are sure
24648 that we are looking at a member-declarator. */
24649 if (cp_parser_token_starts_function_definition_p
24650 (cp_lexer_peek_token (parser->lexer)))
24651 {
24652 /* The grammar does not allow a pure-specifier to be
24653 used when a member function is defined. (It is
24654 possible that this fact is an oversight in the
24655 standard, since a pure function may be defined
24656 outside of the class-specifier. */
24657 if (initializer && initializer_token_start)
24658 error_at (initializer_token_start->location,
24659 "pure-specifier on function-definition");
24660 decl = cp_parser_save_member_function_body (parser,
24661 &decl_specifiers,
24662 declarator,
24663 attributes);
24664 if (parser->fully_implicit_function_template_p)
24665 decl = finish_fully_implicit_template (parser, decl);
24666 /* If the member was not a friend, declare it here. */
24667 if (!friend_p)
24668 finish_member_declaration (decl);
24669 /* Peek at the next token. */
24670 token = cp_lexer_peek_token (parser->lexer);
24671 /* If the next token is a semicolon, consume it. */
24672 if (token->type == CPP_SEMICOLON)
24673 {
24674 location_t semicolon_loc
24675 = cp_lexer_consume_token (parser->lexer)->location;
24676 gcc_rich_location richloc (semicolon_loc);
24677 richloc.add_fixit_remove ();
24678 warning_at (&richloc, OPT_Wextra_semi,
24679 "extra %<;%> after in-class "
24680 "function definition");
24681 }
24682 goto out;
24683 }
24684 else
24685 if (declarator->kind == cdk_function)
24686 declarator->id_loc = token->location;
24687 /* Create the declaration. */
24688 decl = grokfield (declarator, &decl_specifiers,
24689 initializer, /*init_const_expr_p=*/true,
24690 asm_specification, attributes);
24691 if (parser->fully_implicit_function_template_p)
24692 {
24693 if (friend_p)
24694 finish_fully_implicit_template (parser, 0);
24695 else
24696 decl = finish_fully_implicit_template (parser, decl);
24697 }
24698 }
24699
24700 cp_finalize_omp_declare_simd (parser, decl);
24701 cp_finalize_oacc_routine (parser, decl, false);
24702
24703 /* Reset PREFIX_ATTRIBUTES. */
24704 if (attributes != error_mark_node)
24705 {
24706 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24707 attributes = TREE_CHAIN (attributes);
24708 if (attributes)
24709 TREE_CHAIN (attributes) = NULL_TREE;
24710 }
24711
24712 /* If there is any qualification still in effect, clear it
24713 now; we will be starting fresh with the next declarator. */
24714 parser->scope = NULL_TREE;
24715 parser->qualifying_scope = NULL_TREE;
24716 parser->object_scope = NULL_TREE;
24717 /* If it's a `,', then there are more declarators. */
24718 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24719 {
24720 cp_lexer_consume_token (parser->lexer);
24721 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24722 {
24723 cp_token *token = cp_lexer_previous_token (parser->lexer);
24724 gcc_rich_location richloc (token->location);
24725 richloc.add_fixit_remove ();
24726 error_at (&richloc, "stray %<,%> at end of "
24727 "member declaration");
24728 }
24729 }
24730 /* If the next token isn't a `;', then we have a parse error. */
24731 else if (cp_lexer_next_token_is_not (parser->lexer,
24732 CPP_SEMICOLON))
24733 {
24734 /* The next token might be a ways away from where the
24735 actual semicolon is missing. Find the previous token
24736 and use that for our error position. */
24737 cp_token *token = cp_lexer_previous_token (parser->lexer);
24738 gcc_rich_location richloc (token->location);
24739 richloc.add_fixit_insert_after (";");
24740 error_at (&richloc, "expected %<;%> at end of "
24741 "member declaration");
24742
24743 /* Assume that the user meant to provide a semicolon. If
24744 we were to cp_parser_skip_to_end_of_statement, we might
24745 skip to a semicolon inside a member function definition
24746 and issue nonsensical error messages. */
24747 assume_semicolon = true;
24748 }
24749
24750 if (decl)
24751 {
24752 /* Add DECL to the list of members. */
24753 if (!friend_p
24754 /* Explicitly include, eg, NSDMIs, for better error
24755 recovery (c++/58650). */
24756 || !DECL_DECLARES_FUNCTION_P (decl))
24757 finish_member_declaration (decl);
24758
24759 if (TREE_CODE (decl) == FUNCTION_DECL)
24760 cp_parser_save_default_args (parser, decl);
24761 else if (TREE_CODE (decl) == FIELD_DECL
24762 && DECL_INITIAL (decl))
24763 /* Add DECL to the queue of NSDMI to be parsed later. */
24764 vec_safe_push (unparsed_nsdmis, decl);
24765 }
24766
24767 if (assume_semicolon)
24768 goto out;
24769 }
24770 }
24771
24772 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24773 out:
24774 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24775 }
24776
24777 /* Parse a pure-specifier.
24778
24779 pure-specifier:
24780 = 0
24781
24782 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24783 Otherwise, ERROR_MARK_NODE is returned. */
24784
24785 static tree
24786 cp_parser_pure_specifier (cp_parser* parser)
24787 {
24788 cp_token *token;
24789
24790 /* Look for the `=' token. */
24791 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24792 return error_mark_node;
24793 /* Look for the `0' token. */
24794 token = cp_lexer_peek_token (parser->lexer);
24795
24796 if (token->type == CPP_EOF
24797 || token->type == CPP_PRAGMA_EOL)
24798 return error_mark_node;
24799
24800 cp_lexer_consume_token (parser->lexer);
24801
24802 /* Accept = default or = delete in c++0x mode. */
24803 if (token->keyword == RID_DEFAULT
24804 || token->keyword == RID_DELETE)
24805 {
24806 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24807 return token->u.value;
24808 }
24809
24810 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24811 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24812 {
24813 cp_parser_error (parser,
24814 "invalid pure specifier (only %<= 0%> is allowed)");
24815 cp_parser_skip_to_end_of_statement (parser);
24816 return error_mark_node;
24817 }
24818 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24819 {
24820 error_at (token->location, "templates may not be %<virtual%>");
24821 return error_mark_node;
24822 }
24823
24824 return integer_zero_node;
24825 }
24826
24827 /* Parse a constant-initializer.
24828
24829 constant-initializer:
24830 = constant-expression
24831
24832 Returns a representation of the constant-expression. */
24833
24834 static tree
24835 cp_parser_constant_initializer (cp_parser* parser)
24836 {
24837 /* Look for the `=' token. */
24838 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24839 return error_mark_node;
24840
24841 /* It is invalid to write:
24842
24843 struct S { static const int i = { 7 }; };
24844
24845 */
24846 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24847 {
24848 cp_parser_error (parser,
24849 "a brace-enclosed initializer is not allowed here");
24850 /* Consume the opening brace. */
24851 matching_braces braces;
24852 braces.consume_open (parser);
24853 /* Skip the initializer. */
24854 cp_parser_skip_to_closing_brace (parser);
24855 /* Look for the trailing `}'. */
24856 braces.require_close (parser);
24857
24858 return error_mark_node;
24859 }
24860
24861 return cp_parser_constant_expression (parser);
24862 }
24863
24864 /* Derived classes [gram.class.derived] */
24865
24866 /* Parse a base-clause.
24867
24868 base-clause:
24869 : base-specifier-list
24870
24871 base-specifier-list:
24872 base-specifier ... [opt]
24873 base-specifier-list , base-specifier ... [opt]
24874
24875 Returns a TREE_LIST representing the base-classes, in the order in
24876 which they were declared. The representation of each node is as
24877 described by cp_parser_base_specifier.
24878
24879 In the case that no bases are specified, this function will return
24880 NULL_TREE, not ERROR_MARK_NODE. */
24881
24882 static tree
24883 cp_parser_base_clause (cp_parser* parser)
24884 {
24885 tree bases = NULL_TREE;
24886
24887 /* Look for the `:' that begins the list. */
24888 cp_parser_require (parser, CPP_COLON, RT_COLON);
24889
24890 /* Scan the base-specifier-list. */
24891 while (true)
24892 {
24893 cp_token *token;
24894 tree base;
24895 bool pack_expansion_p = false;
24896
24897 /* Look for the base-specifier. */
24898 base = cp_parser_base_specifier (parser);
24899 /* Look for the (optional) ellipsis. */
24900 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24901 {
24902 /* Consume the `...'. */
24903 cp_lexer_consume_token (parser->lexer);
24904
24905 pack_expansion_p = true;
24906 }
24907
24908 /* Add BASE to the front of the list. */
24909 if (base && base != error_mark_node)
24910 {
24911 if (pack_expansion_p)
24912 /* Make this a pack expansion type. */
24913 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24914
24915 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24916 {
24917 TREE_CHAIN (base) = bases;
24918 bases = base;
24919 }
24920 }
24921 /* Peek at the next token. */
24922 token = cp_lexer_peek_token (parser->lexer);
24923 /* If it's not a comma, then the list is complete. */
24924 if (token->type != CPP_COMMA)
24925 break;
24926 /* Consume the `,'. */
24927 cp_lexer_consume_token (parser->lexer);
24928 }
24929
24930 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24931 base class had a qualified name. However, the next name that
24932 appears is certainly not qualified. */
24933 parser->scope = NULL_TREE;
24934 parser->qualifying_scope = NULL_TREE;
24935 parser->object_scope = NULL_TREE;
24936
24937 return nreverse (bases);
24938 }
24939
24940 /* Parse a base-specifier.
24941
24942 base-specifier:
24943 :: [opt] nested-name-specifier [opt] class-name
24944 virtual access-specifier [opt] :: [opt] nested-name-specifier
24945 [opt] class-name
24946 access-specifier virtual [opt] :: [opt] nested-name-specifier
24947 [opt] class-name
24948
24949 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24950 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24951 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24952 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24953
24954 static tree
24955 cp_parser_base_specifier (cp_parser* parser)
24956 {
24957 cp_token *token;
24958 bool done = false;
24959 bool virtual_p = false;
24960 bool duplicate_virtual_error_issued_p = false;
24961 bool duplicate_access_error_issued_p = false;
24962 bool class_scope_p, template_p;
24963 tree access = access_default_node;
24964 tree type;
24965
24966 /* Process the optional `virtual' and `access-specifier'. */
24967 while (!done)
24968 {
24969 /* Peek at the next token. */
24970 token = cp_lexer_peek_token (parser->lexer);
24971 /* Process `virtual'. */
24972 switch (token->keyword)
24973 {
24974 case RID_VIRTUAL:
24975 /* If `virtual' appears more than once, issue an error. */
24976 if (virtual_p && !duplicate_virtual_error_issued_p)
24977 {
24978 cp_parser_error (parser,
24979 "%<virtual%> specified more than once in base-specifier");
24980 duplicate_virtual_error_issued_p = true;
24981 }
24982
24983 virtual_p = true;
24984
24985 /* Consume the `virtual' token. */
24986 cp_lexer_consume_token (parser->lexer);
24987
24988 break;
24989
24990 case RID_PUBLIC:
24991 case RID_PROTECTED:
24992 case RID_PRIVATE:
24993 /* If more than one access specifier appears, issue an
24994 error. */
24995 if (access != access_default_node
24996 && !duplicate_access_error_issued_p)
24997 {
24998 cp_parser_error (parser,
24999 "more than one access specifier in base-specifier");
25000 duplicate_access_error_issued_p = true;
25001 }
25002
25003 access = ridpointers[(int) token->keyword];
25004
25005 /* Consume the access-specifier. */
25006 cp_lexer_consume_token (parser->lexer);
25007
25008 break;
25009
25010 default:
25011 done = true;
25012 break;
25013 }
25014 }
25015 /* It is not uncommon to see programs mechanically, erroneously, use
25016 the 'typename' keyword to denote (dependent) qualified types
25017 as base classes. */
25018 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25019 {
25020 token = cp_lexer_peek_token (parser->lexer);
25021 if (!processing_template_decl)
25022 error_at (token->location,
25023 "keyword %<typename%> not allowed outside of templates");
25024 else
25025 error_at (token->location,
25026 "keyword %<typename%> not allowed in this context "
25027 "(the base class is implicitly a type)");
25028 cp_lexer_consume_token (parser->lexer);
25029 }
25030
25031 /* Look for the optional `::' operator. */
25032 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
25033 /* Look for the nested-name-specifier. The simplest way to
25034 implement:
25035
25036 [temp.res]
25037
25038 The keyword `typename' is not permitted in a base-specifier or
25039 mem-initializer; in these contexts a qualified name that
25040 depends on a template-parameter is implicitly assumed to be a
25041 type name.
25042
25043 is to pretend that we have seen the `typename' keyword at this
25044 point. */
25045 cp_parser_nested_name_specifier_opt (parser,
25046 /*typename_keyword_p=*/true,
25047 /*check_dependency_p=*/true,
25048 /*type_p=*/true,
25049 /*is_declaration=*/true);
25050 /* If the base class is given by a qualified name, assume that names
25051 we see are type names or templates, as appropriate. */
25052 class_scope_p = (parser->scope && TYPE_P (parser->scope));
25053 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
25054
25055 if (!parser->scope
25056 && cp_lexer_next_token_is_decltype (parser->lexer))
25057 /* DR 950 allows decltype as a base-specifier. */
25058 type = cp_parser_decltype (parser);
25059 else
25060 {
25061 /* Otherwise, look for the class-name. */
25062 type = cp_parser_class_name (parser,
25063 class_scope_p,
25064 template_p,
25065 typename_type,
25066 /*check_dependency_p=*/true,
25067 /*class_head_p=*/false,
25068 /*is_declaration=*/true);
25069 type = TREE_TYPE (type);
25070 }
25071
25072 if (type == error_mark_node)
25073 return error_mark_node;
25074
25075 return finish_base_specifier (type, access, virtual_p);
25076 }
25077
25078 /* Exception handling [gram.exception] */
25079
25080 /* Parse an (optional) noexcept-specification.
25081
25082 noexcept-specification:
25083 noexcept ( constant-expression ) [opt]
25084
25085 If no noexcept-specification is present, returns NULL_TREE.
25086 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25087 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25088 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25089 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25090 in which case a boolean condition is returned instead. */
25091
25092 static tree
25093 cp_parser_noexcept_specification_opt (cp_parser* parser,
25094 bool require_constexpr,
25095 bool* consumed_expr,
25096 bool return_cond)
25097 {
25098 cp_token *token;
25099 const char *saved_message;
25100
25101 /* Peek at the next token. */
25102 token = cp_lexer_peek_token (parser->lexer);
25103
25104 /* Is it a noexcept-specification? */
25105 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
25106 {
25107 tree expr;
25108 cp_lexer_consume_token (parser->lexer);
25109
25110 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
25111 {
25112 matching_parens parens;
25113 parens.consume_open (parser);
25114
25115 tree save_ccp = current_class_ptr;
25116 tree save_ccr = current_class_ref;
25117
25118 if (current_class_type)
25119 inject_this_parameter (current_class_type, TYPE_UNQUALIFIED);
25120
25121 if (require_constexpr)
25122 {
25123 /* Types may not be defined in an exception-specification. */
25124 saved_message = parser->type_definition_forbidden_message;
25125 parser->type_definition_forbidden_message
25126 = G_("types may not be defined in an exception-specification");
25127
25128 expr = cp_parser_constant_expression (parser);
25129
25130 /* Restore the saved message. */
25131 parser->type_definition_forbidden_message = saved_message;
25132 }
25133 else
25134 {
25135 expr = cp_parser_expression (parser);
25136 *consumed_expr = true;
25137 }
25138
25139 parens.require_close (parser);
25140
25141 current_class_ptr = save_ccp;
25142 current_class_ref = save_ccr;
25143 }
25144 else
25145 {
25146 expr = boolean_true_node;
25147 if (!require_constexpr)
25148 *consumed_expr = false;
25149 }
25150
25151 /* We cannot build a noexcept-spec right away because this will check
25152 that expr is a constexpr. */
25153 if (!return_cond)
25154 return build_noexcept_spec (expr, tf_warning_or_error);
25155 else
25156 return expr;
25157 }
25158 else
25159 return NULL_TREE;
25160 }
25161
25162 /* Parse an (optional) exception-specification.
25163
25164 exception-specification:
25165 throw ( type-id-list [opt] )
25166
25167 Returns a TREE_LIST representing the exception-specification. The
25168 TREE_VALUE of each node is a type. */
25169
25170 static tree
25171 cp_parser_exception_specification_opt (cp_parser* parser)
25172 {
25173 cp_token *token;
25174 tree type_id_list;
25175 const char *saved_message;
25176
25177 /* Peek at the next token. */
25178 token = cp_lexer_peek_token (parser->lexer);
25179
25180 /* Is it a noexcept-specification? */
25181 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
25182 false);
25183 if (type_id_list != NULL_TREE)
25184 return type_id_list;
25185
25186 /* If it's not `throw', then there's no exception-specification. */
25187 if (!cp_parser_is_keyword (token, RID_THROW))
25188 return NULL_TREE;
25189
25190 location_t loc = token->location;
25191
25192 /* Consume the `throw'. */
25193 cp_lexer_consume_token (parser->lexer);
25194
25195 /* Look for the `('. */
25196 matching_parens parens;
25197 parens.require_open (parser);
25198
25199 /* Peek at the next token. */
25200 token = cp_lexer_peek_token (parser->lexer);
25201 /* If it's not a `)', then there is a type-id-list. */
25202 if (token->type != CPP_CLOSE_PAREN)
25203 {
25204 /* Types may not be defined in an exception-specification. */
25205 saved_message = parser->type_definition_forbidden_message;
25206 parser->type_definition_forbidden_message
25207 = G_("types may not be defined in an exception-specification");
25208 /* Parse the type-id-list. */
25209 type_id_list = cp_parser_type_id_list (parser);
25210 /* Restore the saved message. */
25211 parser->type_definition_forbidden_message = saved_message;
25212
25213 if (cxx_dialect >= cxx17)
25214 {
25215 error_at (loc, "ISO C++17 does not allow dynamic exception "
25216 "specifications");
25217 type_id_list = NULL_TREE;
25218 }
25219 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
25220 warning_at (loc, OPT_Wdeprecated,
25221 "dynamic exception specifications are deprecated in "
25222 "C++11");
25223 }
25224 /* In C++17, throw() is equivalent to noexcept (true). throw()
25225 is deprecated in C++11 and above as well, but is still widely used,
25226 so don't warn about it yet. */
25227 else if (cxx_dialect >= cxx17)
25228 type_id_list = noexcept_true_spec;
25229 else
25230 type_id_list = empty_except_spec;
25231
25232 /* Look for the `)'. */
25233 parens.require_close (parser);
25234
25235 return type_id_list;
25236 }
25237
25238 /* Parse an (optional) type-id-list.
25239
25240 type-id-list:
25241 type-id ... [opt]
25242 type-id-list , type-id ... [opt]
25243
25244 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25245 in the order that the types were presented. */
25246
25247 static tree
25248 cp_parser_type_id_list (cp_parser* parser)
25249 {
25250 tree types = NULL_TREE;
25251
25252 while (true)
25253 {
25254 cp_token *token;
25255 tree type;
25256
25257 token = cp_lexer_peek_token (parser->lexer);
25258
25259 /* Get the next type-id. */
25260 type = cp_parser_type_id (parser);
25261 /* Check for invalid 'auto'. */
25262 if (flag_concepts && type_uses_auto (type))
25263 {
25264 error_at (token->location,
25265 "invalid use of %<auto%> in exception-specification");
25266 type = error_mark_node;
25267 }
25268 /* Parse the optional ellipsis. */
25269 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25270 {
25271 /* Consume the `...'. */
25272 cp_lexer_consume_token (parser->lexer);
25273
25274 /* Turn the type into a pack expansion expression. */
25275 type = make_pack_expansion (type);
25276 }
25277 /* Add it to the list. */
25278 types = add_exception_specifier (types, type, /*complain=*/1);
25279 /* Peek at the next token. */
25280 token = cp_lexer_peek_token (parser->lexer);
25281 /* If it is not a `,', we are done. */
25282 if (token->type != CPP_COMMA)
25283 break;
25284 /* Consume the `,'. */
25285 cp_lexer_consume_token (parser->lexer);
25286 }
25287
25288 return nreverse (types);
25289 }
25290
25291 /* Parse a try-block.
25292
25293 try-block:
25294 try compound-statement handler-seq */
25295
25296 static tree
25297 cp_parser_try_block (cp_parser* parser)
25298 {
25299 tree try_block;
25300
25301 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
25302 if (parser->in_function_body
25303 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
25304 error ("%<try%> in %<constexpr%> function");
25305
25306 try_block = begin_try_block ();
25307 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
25308 finish_try_block (try_block);
25309 cp_parser_handler_seq (parser);
25310 finish_handler_sequence (try_block);
25311
25312 return try_block;
25313 }
25314
25315 /* Parse a function-try-block.
25316
25317 function-try-block:
25318 try ctor-initializer [opt] function-body handler-seq */
25319
25320 static void
25321 cp_parser_function_try_block (cp_parser* parser)
25322 {
25323 tree compound_stmt;
25324 tree try_block;
25325
25326 /* Look for the `try' keyword. */
25327 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
25328 return;
25329 /* Let the rest of the front end know where we are. */
25330 try_block = begin_function_try_block (&compound_stmt);
25331 /* Parse the function-body. */
25332 cp_parser_ctor_initializer_opt_and_function_body
25333 (parser, /*in_function_try_block=*/true);
25334 /* We're done with the `try' part. */
25335 finish_function_try_block (try_block);
25336 /* Parse the handlers. */
25337 cp_parser_handler_seq (parser);
25338 /* We're done with the handlers. */
25339 finish_function_handler_sequence (try_block, compound_stmt);
25340 }
25341
25342 /* Parse a handler-seq.
25343
25344 handler-seq:
25345 handler handler-seq [opt] */
25346
25347 static void
25348 cp_parser_handler_seq (cp_parser* parser)
25349 {
25350 while (true)
25351 {
25352 cp_token *token;
25353
25354 /* Parse the handler. */
25355 cp_parser_handler (parser);
25356 /* Peek at the next token. */
25357 token = cp_lexer_peek_token (parser->lexer);
25358 /* If it's not `catch' then there are no more handlers. */
25359 if (!cp_parser_is_keyword (token, RID_CATCH))
25360 break;
25361 }
25362 }
25363
25364 /* Parse a handler.
25365
25366 handler:
25367 catch ( exception-declaration ) compound-statement */
25368
25369 static void
25370 cp_parser_handler (cp_parser* parser)
25371 {
25372 tree handler;
25373 tree declaration;
25374
25375 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
25376 handler = begin_handler ();
25377 matching_parens parens;
25378 parens.require_open (parser);
25379 declaration = cp_parser_exception_declaration (parser);
25380 finish_handler_parms (declaration, handler);
25381 parens.require_close (parser);
25382 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
25383 finish_handler (handler);
25384 }
25385
25386 /* Parse an exception-declaration.
25387
25388 exception-declaration:
25389 type-specifier-seq declarator
25390 type-specifier-seq abstract-declarator
25391 type-specifier-seq
25392 ...
25393
25394 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25395 ellipsis variant is used. */
25396
25397 static tree
25398 cp_parser_exception_declaration (cp_parser* parser)
25399 {
25400 cp_decl_specifier_seq type_specifiers;
25401 cp_declarator *declarator;
25402 const char *saved_message;
25403
25404 /* If it's an ellipsis, it's easy to handle. */
25405 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25406 {
25407 /* Consume the `...' token. */
25408 cp_lexer_consume_token (parser->lexer);
25409 return NULL_TREE;
25410 }
25411
25412 /* Types may not be defined in exception-declarations. */
25413 saved_message = parser->type_definition_forbidden_message;
25414 parser->type_definition_forbidden_message
25415 = G_("types may not be defined in exception-declarations");
25416
25417 /* Parse the type-specifier-seq. */
25418 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
25419 /*is_declaration=*/true,
25420 /*is_trailing_return=*/false,
25421 &type_specifiers);
25422 /* If it's a `)', then there is no declarator. */
25423 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25424 declarator = NULL;
25425 else
25426 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
25427 CP_PARSER_FLAGS_NONE,
25428 /*ctor_dtor_or_conv_p=*/NULL,
25429 /*parenthesized_p=*/NULL,
25430 /*member_p=*/false,
25431 /*friend_p=*/false,
25432 /*static_p=*/false);
25433
25434 /* Restore the saved message. */
25435 parser->type_definition_forbidden_message = saved_message;
25436
25437 if (!type_specifiers.any_specifiers_p)
25438 return error_mark_node;
25439
25440 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
25441 }
25442
25443 /* Parse a throw-expression.
25444
25445 throw-expression:
25446 throw assignment-expression [opt]
25447
25448 Returns a THROW_EXPR representing the throw-expression. */
25449
25450 static tree
25451 cp_parser_throw_expression (cp_parser* parser)
25452 {
25453 tree expression;
25454 cp_token* token;
25455
25456 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
25457 token = cp_lexer_peek_token (parser->lexer);
25458 /* Figure out whether or not there is an assignment-expression
25459 following the "throw" keyword. */
25460 if (token->type == CPP_COMMA
25461 || token->type == CPP_SEMICOLON
25462 || token->type == CPP_CLOSE_PAREN
25463 || token->type == CPP_CLOSE_SQUARE
25464 || token->type == CPP_CLOSE_BRACE
25465 || token->type == CPP_COLON)
25466 expression = NULL_TREE;
25467 else
25468 expression = cp_parser_assignment_expression (parser);
25469
25470 return build_throw (expression);
25471 }
25472
25473 /* GNU Extensions */
25474
25475 /* Parse an (optional) asm-specification.
25476
25477 asm-specification:
25478 asm ( string-literal )
25479
25480 If the asm-specification is present, returns a STRING_CST
25481 corresponding to the string-literal. Otherwise, returns
25482 NULL_TREE. */
25483
25484 static tree
25485 cp_parser_asm_specification_opt (cp_parser* parser)
25486 {
25487 cp_token *token;
25488 tree asm_specification;
25489
25490 /* Peek at the next token. */
25491 token = cp_lexer_peek_token (parser->lexer);
25492 /* If the next token isn't the `asm' keyword, then there's no
25493 asm-specification. */
25494 if (!cp_parser_is_keyword (token, RID_ASM))
25495 return NULL_TREE;
25496
25497 /* Consume the `asm' token. */
25498 cp_lexer_consume_token (parser->lexer);
25499 /* Look for the `('. */
25500 matching_parens parens;
25501 parens.require_open (parser);
25502
25503 /* Look for the string-literal. */
25504 asm_specification = cp_parser_string_literal (parser, false, false);
25505
25506 /* Look for the `)'. */
25507 parens.require_close (parser);
25508
25509 return asm_specification;
25510 }
25511
25512 /* Parse an asm-operand-list.
25513
25514 asm-operand-list:
25515 asm-operand
25516 asm-operand-list , asm-operand
25517
25518 asm-operand:
25519 string-literal ( expression )
25520 [ string-literal ] string-literal ( expression )
25521
25522 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25523 each node is the expression. The TREE_PURPOSE is itself a
25524 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25525 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25526 is a STRING_CST for the string literal before the parenthesis. Returns
25527 ERROR_MARK_NODE if any of the operands are invalid. */
25528
25529 static tree
25530 cp_parser_asm_operand_list (cp_parser* parser)
25531 {
25532 tree asm_operands = NULL_TREE;
25533 bool invalid_operands = false;
25534
25535 while (true)
25536 {
25537 tree string_literal;
25538 tree expression;
25539 tree name;
25540
25541 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25542 {
25543 /* Consume the `[' token. */
25544 cp_lexer_consume_token (parser->lexer);
25545 /* Read the operand name. */
25546 name = cp_parser_identifier (parser);
25547 if (name != error_mark_node)
25548 name = build_string (IDENTIFIER_LENGTH (name),
25549 IDENTIFIER_POINTER (name));
25550 /* Look for the closing `]'. */
25551 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25552 }
25553 else
25554 name = NULL_TREE;
25555 /* Look for the string-literal. */
25556 string_literal = cp_parser_string_literal (parser, false, false);
25557
25558 /* Look for the `('. */
25559 matching_parens parens;
25560 parens.require_open (parser);
25561 /* Parse the expression. */
25562 expression = cp_parser_expression (parser);
25563 /* Look for the `)'. */
25564 parens.require_close (parser);
25565
25566 if (name == error_mark_node
25567 || string_literal == error_mark_node
25568 || expression == error_mark_node)
25569 invalid_operands = true;
25570
25571 /* Add this operand to the list. */
25572 asm_operands = tree_cons (build_tree_list (name, string_literal),
25573 expression,
25574 asm_operands);
25575 /* If the next token is not a `,', there are no more
25576 operands. */
25577 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25578 break;
25579 /* Consume the `,'. */
25580 cp_lexer_consume_token (parser->lexer);
25581 }
25582
25583 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25584 }
25585
25586 /* Parse an asm-clobber-list.
25587
25588 asm-clobber-list:
25589 string-literal
25590 asm-clobber-list , string-literal
25591
25592 Returns a TREE_LIST, indicating the clobbers in the order that they
25593 appeared. The TREE_VALUE of each node is a STRING_CST. */
25594
25595 static tree
25596 cp_parser_asm_clobber_list (cp_parser* parser)
25597 {
25598 tree clobbers = NULL_TREE;
25599
25600 while (true)
25601 {
25602 tree string_literal;
25603
25604 /* Look for the string literal. */
25605 string_literal = cp_parser_string_literal (parser, false, false);
25606 /* Add it to the list. */
25607 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25608 /* If the next token is not a `,', then the list is
25609 complete. */
25610 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25611 break;
25612 /* Consume the `,' token. */
25613 cp_lexer_consume_token (parser->lexer);
25614 }
25615
25616 return clobbers;
25617 }
25618
25619 /* Parse an asm-label-list.
25620
25621 asm-label-list:
25622 identifier
25623 asm-label-list , identifier
25624
25625 Returns a TREE_LIST, indicating the labels in the order that they
25626 appeared. The TREE_VALUE of each node is a label. */
25627
25628 static tree
25629 cp_parser_asm_label_list (cp_parser* parser)
25630 {
25631 tree labels = NULL_TREE;
25632
25633 while (true)
25634 {
25635 tree identifier, label, name;
25636
25637 /* Look for the identifier. */
25638 identifier = cp_parser_identifier (parser);
25639 if (!error_operand_p (identifier))
25640 {
25641 label = lookup_label (identifier);
25642 if (TREE_CODE (label) == LABEL_DECL)
25643 {
25644 TREE_USED (label) = 1;
25645 check_goto (label);
25646 name = build_string (IDENTIFIER_LENGTH (identifier),
25647 IDENTIFIER_POINTER (identifier));
25648 labels = tree_cons (name, label, labels);
25649 }
25650 }
25651 /* If the next token is not a `,', then the list is
25652 complete. */
25653 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25654 break;
25655 /* Consume the `,' token. */
25656 cp_lexer_consume_token (parser->lexer);
25657 }
25658
25659 return nreverse (labels);
25660 }
25661
25662 /* Return TRUE iff the next tokens in the stream are possibly the
25663 beginning of a GNU extension attribute. */
25664
25665 static bool
25666 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25667 {
25668 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25669 }
25670
25671 /* Return TRUE iff the next tokens in the stream are possibly the
25672 beginning of a standard C++-11 attribute specifier. */
25673
25674 static bool
25675 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25676 {
25677 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25678 }
25679
25680 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25681 beginning of a standard C++-11 attribute specifier. */
25682
25683 static bool
25684 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25685 {
25686 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25687
25688 return (cxx_dialect >= cxx11
25689 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25690 || (token->type == CPP_OPEN_SQUARE
25691 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25692 && token->type == CPP_OPEN_SQUARE)));
25693 }
25694
25695 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25696 beginning of a GNU extension attribute. */
25697
25698 static bool
25699 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25700 {
25701 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25702
25703 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25704 }
25705
25706 /* Return true iff the next tokens can be the beginning of either a
25707 GNU attribute list, or a standard C++11 attribute sequence. */
25708
25709 static bool
25710 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25711 {
25712 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25713 || cp_next_tokens_can_be_std_attribute_p (parser));
25714 }
25715
25716 /* Return true iff the next Nth tokens can be the beginning of either
25717 a GNU attribute list, or a standard C++11 attribute sequence. */
25718
25719 static bool
25720 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25721 {
25722 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25723 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25724 }
25725
25726 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25727 of GNU attributes, or return NULL. */
25728
25729 static tree
25730 cp_parser_attributes_opt (cp_parser *parser)
25731 {
25732 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25733 return cp_parser_gnu_attributes_opt (parser);
25734 return cp_parser_std_attribute_spec_seq (parser);
25735 }
25736
25737 /* Parse an (optional) series of attributes.
25738
25739 attributes:
25740 attributes attribute
25741
25742 attribute:
25743 __attribute__ (( attribute-list [opt] ))
25744
25745 The return value is as for cp_parser_gnu_attribute_list. */
25746
25747 static tree
25748 cp_parser_gnu_attributes_opt (cp_parser* parser)
25749 {
25750 tree attributes = NULL_TREE;
25751
25752 temp_override<bool> cleanup
25753 (parser->auto_is_implicit_function_template_parm_p, false);
25754
25755 while (true)
25756 {
25757 cp_token *token;
25758 tree attribute_list;
25759 bool ok = true;
25760
25761 /* Peek at the next token. */
25762 token = cp_lexer_peek_token (parser->lexer);
25763 /* If it's not `__attribute__', then we're done. */
25764 if (token->keyword != RID_ATTRIBUTE)
25765 break;
25766
25767 /* Consume the `__attribute__' keyword. */
25768 cp_lexer_consume_token (parser->lexer);
25769 /* Look for the two `(' tokens. */
25770 matching_parens outer_parens;
25771 outer_parens.require_open (parser);
25772 matching_parens inner_parens;
25773 inner_parens.require_open (parser);
25774
25775 /* Peek at the next token. */
25776 token = cp_lexer_peek_token (parser->lexer);
25777 if (token->type != CPP_CLOSE_PAREN)
25778 /* Parse the attribute-list. */
25779 attribute_list = cp_parser_gnu_attribute_list (parser);
25780 else
25781 /* If the next token is a `)', then there is no attribute
25782 list. */
25783 attribute_list = NULL;
25784
25785 /* Look for the two `)' tokens. */
25786 if (!inner_parens.require_close (parser))
25787 ok = false;
25788 if (!outer_parens.require_close (parser))
25789 ok = false;
25790 if (!ok)
25791 cp_parser_skip_to_end_of_statement (parser);
25792
25793 /* Add these new attributes to the list. */
25794 attributes = attr_chainon (attributes, attribute_list);
25795 }
25796
25797 return attributes;
25798 }
25799
25800 /* Parse a GNU attribute-list.
25801
25802 attribute-list:
25803 attribute
25804 attribute-list , attribute
25805
25806 attribute:
25807 identifier
25808 identifier ( identifier )
25809 identifier ( identifier , expression-list )
25810 identifier ( expression-list )
25811
25812 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25813 to an attribute. The TREE_PURPOSE of each node is the identifier
25814 indicating which attribute is in use. The TREE_VALUE represents
25815 the arguments, if any. */
25816
25817 static tree
25818 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
25819 {
25820 tree attribute_list = NULL_TREE;
25821 bool save_translate_strings_p = parser->translate_strings_p;
25822
25823 /* Don't create wrapper nodes within attributes: the
25824 handlers don't know how to handle them. */
25825 auto_suppress_location_wrappers sentinel;
25826
25827 parser->translate_strings_p = false;
25828 while (true)
25829 {
25830 cp_token *token;
25831 tree identifier;
25832 tree attribute;
25833
25834 /* Look for the identifier. We also allow keywords here; for
25835 example `__attribute__ ((const))' is legal. */
25836 token = cp_lexer_peek_token (parser->lexer);
25837 if (token->type == CPP_NAME
25838 || token->type == CPP_KEYWORD)
25839 {
25840 tree arguments = NULL_TREE;
25841
25842 /* Consume the token, but save it since we need it for the
25843 SIMD enabled function parsing. */
25844 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25845
25846 /* Save away the identifier that indicates which attribute
25847 this is. */
25848 identifier = (token->type == CPP_KEYWORD)
25849 /* For keywords, use the canonical spelling, not the
25850 parsed identifier. */
25851 ? ridpointers[(int) token->keyword]
25852 : id_token->u.value;
25853
25854 identifier = canonicalize_attr_name (identifier);
25855 attribute = build_tree_list (identifier, NULL_TREE);
25856
25857 /* Peek at the next token. */
25858 token = cp_lexer_peek_token (parser->lexer);
25859 /* If it's an `(', then parse the attribute arguments. */
25860 if (token->type == CPP_OPEN_PAREN)
25861 {
25862 vec<tree, va_gc> *vec;
25863 int attr_flag = (attribute_takes_identifier_p (identifier)
25864 ? id_attr : normal_attr);
25865 vec = cp_parser_parenthesized_expression_list
25866 (parser, attr_flag, /*cast_p=*/false,
25867 /*allow_expansion_p=*/false,
25868 /*non_constant_p=*/NULL);
25869 if (vec == NULL)
25870 arguments = error_mark_node;
25871 else
25872 {
25873 arguments = build_tree_list_vec (vec);
25874 release_tree_vector (vec);
25875 }
25876 /* Save the arguments away. */
25877 TREE_VALUE (attribute) = arguments;
25878 }
25879
25880 if (arguments != error_mark_node)
25881 {
25882 /* Add this attribute to the list. */
25883 TREE_CHAIN (attribute) = attribute_list;
25884 attribute_list = attribute;
25885 }
25886
25887 token = cp_lexer_peek_token (parser->lexer);
25888 }
25889 /* Unless EXACTLY_ONE is set look for more attributes.
25890 If the next token isn't a `,', we're done. */
25891 if (exactly_one || token->type != CPP_COMMA)
25892 break;
25893
25894 /* Consume the comma and keep going. */
25895 cp_lexer_consume_token (parser->lexer);
25896 }
25897 parser->translate_strings_p = save_translate_strings_p;
25898
25899 /* We built up the list in reverse order. */
25900 return nreverse (attribute_list);
25901 }
25902
25903 /* Parse a standard C++11 attribute.
25904
25905 The returned representation is a TREE_LIST which TREE_PURPOSE is
25906 the scoped name of the attribute, and the TREE_VALUE is its
25907 arguments list.
25908
25909 Note that the scoped name of the attribute is itself a TREE_LIST
25910 which TREE_PURPOSE is the namespace of the attribute, and
25911 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25912 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25913 and which TREE_PURPOSE is directly the attribute name.
25914
25915 Clients of the attribute code should use get_attribute_namespace
25916 and get_attribute_name to get the actual namespace and name of
25917 attributes, regardless of their being GNU or C++11 attributes.
25918
25919 attribute:
25920 attribute-token attribute-argument-clause [opt]
25921
25922 attribute-token:
25923 identifier
25924 attribute-scoped-token
25925
25926 attribute-scoped-token:
25927 attribute-namespace :: identifier
25928
25929 attribute-namespace:
25930 identifier
25931
25932 attribute-argument-clause:
25933 ( balanced-token-seq )
25934
25935 balanced-token-seq:
25936 balanced-token [opt]
25937 balanced-token-seq balanced-token
25938
25939 balanced-token:
25940 ( balanced-token-seq )
25941 [ balanced-token-seq ]
25942 { balanced-token-seq }. */
25943
25944 static tree
25945 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25946 {
25947 tree attribute, attr_id = NULL_TREE, arguments;
25948 cp_token *token;
25949
25950 temp_override<bool> cleanup
25951 (parser->auto_is_implicit_function_template_parm_p, false);
25952
25953 /* First, parse name of the attribute, a.k.a attribute-token. */
25954
25955 token = cp_lexer_peek_token (parser->lexer);
25956 if (token->type == CPP_NAME)
25957 attr_id = token->u.value;
25958 else if (token->type == CPP_KEYWORD)
25959 attr_id = ridpointers[(int) token->keyword];
25960 else if (token->flags & NAMED_OP)
25961 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25962
25963 if (attr_id == NULL_TREE)
25964 return NULL_TREE;
25965
25966 cp_lexer_consume_token (parser->lexer);
25967
25968 token = cp_lexer_peek_token (parser->lexer);
25969 if (token->type == CPP_SCOPE)
25970 {
25971 /* We are seeing a scoped attribute token. */
25972
25973 cp_lexer_consume_token (parser->lexer);
25974 if (attr_ns)
25975 error_at (token->location, "attribute using prefix used together "
25976 "with scoped attribute token");
25977 attr_ns = attr_id;
25978
25979 token = cp_lexer_consume_token (parser->lexer);
25980 if (token->type == CPP_NAME)
25981 attr_id = token->u.value;
25982 else if (token->type == CPP_KEYWORD)
25983 attr_id = ridpointers[(int) token->keyword];
25984 else if (token->flags & NAMED_OP)
25985 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25986 else
25987 {
25988 error_at (token->location,
25989 "expected an identifier for the attribute name");
25990 return error_mark_node;
25991 }
25992
25993 attr_ns = canonicalize_attr_name (attr_ns);
25994 attr_id = canonicalize_attr_name (attr_id);
25995 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25996 NULL_TREE);
25997 token = cp_lexer_peek_token (parser->lexer);
25998 }
25999 else if (attr_ns)
26000 {
26001 attr_ns = canonicalize_attr_name (attr_ns);
26002 attr_id = canonicalize_attr_name (attr_id);
26003 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26004 NULL_TREE);
26005 }
26006 else
26007 {
26008 attr_id = canonicalize_attr_name (attr_id);
26009 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
26010 NULL_TREE);
26011 /* C++11 noreturn attribute is equivalent to GNU's. */
26012 if (is_attribute_p ("noreturn", attr_id))
26013 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26014 /* C++14 deprecated attribute is equivalent to GNU's. */
26015 else if (is_attribute_p ("deprecated", attr_id))
26016 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26017 /* C++17 fallthrough attribute is equivalent to GNU's. */
26018 else if (is_attribute_p ("fallthrough", attr_id))
26019 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26020 /* Transactional Memory TS optimize_for_synchronized attribute is
26021 equivalent to GNU transaction_callable. */
26022 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
26023 TREE_PURPOSE (attribute)
26024 = get_identifier ("transaction_callable");
26025 /* Transactional Memory attributes are GNU attributes. */
26026 else if (tm_attr_to_mask (attr_id))
26027 TREE_PURPOSE (attribute) = attr_id;
26028 }
26029
26030 /* Now parse the optional argument clause of the attribute. */
26031
26032 if (token->type != CPP_OPEN_PAREN)
26033 return attribute;
26034
26035 {
26036 vec<tree, va_gc> *vec;
26037 int attr_flag = normal_attr;
26038
26039 if (attr_ns == gnu_identifier
26040 && attribute_takes_identifier_p (attr_id))
26041 /* A GNU attribute that takes an identifier in parameter. */
26042 attr_flag = id_attr;
26043
26044 vec = cp_parser_parenthesized_expression_list
26045 (parser, attr_flag, /*cast_p=*/false,
26046 /*allow_expansion_p=*/true,
26047 /*non_constant_p=*/NULL);
26048 if (vec == NULL)
26049 arguments = error_mark_node;
26050 else
26051 {
26052 arguments = build_tree_list_vec (vec);
26053 release_tree_vector (vec);
26054 }
26055
26056 if (arguments == error_mark_node)
26057 attribute = error_mark_node;
26058 else
26059 TREE_VALUE (attribute) = arguments;
26060 }
26061
26062 return attribute;
26063 }
26064
26065 /* Check that the attribute ATTRIBUTE appears at most once in the
26066 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
26067 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
26068 isn't implemented yet in GCC. */
26069
26070 static void
26071 cp_parser_check_std_attribute (tree attributes, tree attribute)
26072 {
26073 if (attributes)
26074 {
26075 tree name = get_attribute_name (attribute);
26076 if (is_attribute_p ("noreturn", name)
26077 && lookup_attribute ("noreturn", attributes))
26078 error ("attribute %<noreturn%> can appear at most once "
26079 "in an attribute-list");
26080 else if (is_attribute_p ("deprecated", name)
26081 && lookup_attribute ("deprecated", attributes))
26082 error ("attribute %<deprecated%> can appear at most once "
26083 "in an attribute-list");
26084 }
26085 }
26086
26087 /* Parse a list of standard C++-11 attributes.
26088
26089 attribute-list:
26090 attribute [opt]
26091 attribute-list , attribute[opt]
26092 attribute ...
26093 attribute-list , attribute ...
26094 */
26095
26096 static tree
26097 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
26098 {
26099 tree attributes = NULL_TREE, attribute = NULL_TREE;
26100 cp_token *token = NULL;
26101
26102 while (true)
26103 {
26104 attribute = cp_parser_std_attribute (parser, attr_ns);
26105 if (attribute == error_mark_node)
26106 break;
26107 if (attribute != NULL_TREE)
26108 {
26109 cp_parser_check_std_attribute (attributes, attribute);
26110 TREE_CHAIN (attribute) = attributes;
26111 attributes = attribute;
26112 }
26113 token = cp_lexer_peek_token (parser->lexer);
26114 if (token->type == CPP_ELLIPSIS)
26115 {
26116 cp_lexer_consume_token (parser->lexer);
26117 if (attribute == NULL_TREE)
26118 error_at (token->location,
26119 "expected attribute before %<...%>");
26120 else
26121 {
26122 tree pack = make_pack_expansion (TREE_VALUE (attribute));
26123 if (pack == error_mark_node)
26124 return error_mark_node;
26125 TREE_VALUE (attribute) = pack;
26126 }
26127 token = cp_lexer_peek_token (parser->lexer);
26128 }
26129 if (token->type != CPP_COMMA)
26130 break;
26131 cp_lexer_consume_token (parser->lexer);
26132 }
26133 attributes = nreverse (attributes);
26134 return attributes;
26135 }
26136
26137 /* Parse a standard C++-11 attribute specifier.
26138
26139 attribute-specifier:
26140 [ [ attribute-using-prefix [opt] attribute-list ] ]
26141 alignment-specifier
26142
26143 attribute-using-prefix:
26144 using attribute-namespace :
26145
26146 alignment-specifier:
26147 alignas ( type-id ... [opt] )
26148 alignas ( alignment-expression ... [opt] ). */
26149
26150 static tree
26151 cp_parser_std_attribute_spec (cp_parser *parser)
26152 {
26153 tree attributes = NULL_TREE;
26154 cp_token *token = cp_lexer_peek_token (parser->lexer);
26155
26156 if (token->type == CPP_OPEN_SQUARE
26157 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
26158 {
26159 tree attr_ns = NULL_TREE;
26160
26161 cp_lexer_consume_token (parser->lexer);
26162 cp_lexer_consume_token (parser->lexer);
26163
26164 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26165 {
26166 token = cp_lexer_peek_nth_token (parser->lexer, 2);
26167 if (token->type == CPP_NAME)
26168 attr_ns = token->u.value;
26169 else if (token->type == CPP_KEYWORD)
26170 attr_ns = ridpointers[(int) token->keyword];
26171 else if (token->flags & NAMED_OP)
26172 attr_ns = get_identifier (cpp_type2name (token->type,
26173 token->flags));
26174 if (attr_ns
26175 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
26176 {
26177 if (cxx_dialect < cxx17
26178 && !in_system_header_at (input_location))
26179 pedwarn (input_location, 0,
26180 "attribute using prefix only available "
26181 "with -std=c++17 or -std=gnu++17");
26182
26183 cp_lexer_consume_token (parser->lexer);
26184 cp_lexer_consume_token (parser->lexer);
26185 cp_lexer_consume_token (parser->lexer);
26186 }
26187 else
26188 attr_ns = NULL_TREE;
26189 }
26190
26191 attributes = cp_parser_std_attribute_list (parser, attr_ns);
26192
26193 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
26194 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
26195 cp_parser_skip_to_end_of_statement (parser);
26196 else
26197 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26198 when we are sure that we have actually parsed them. */
26199 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26200 }
26201 else
26202 {
26203 tree alignas_expr;
26204
26205 /* Look for an alignment-specifier. */
26206
26207 token = cp_lexer_peek_token (parser->lexer);
26208
26209 if (token->type != CPP_KEYWORD
26210 || token->keyword != RID_ALIGNAS)
26211 return NULL_TREE;
26212
26213 cp_lexer_consume_token (parser->lexer);
26214 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26215
26216 matching_parens parens;
26217 if (!parens.require_open (parser))
26218 return error_mark_node;
26219
26220 cp_parser_parse_tentatively (parser);
26221 alignas_expr = cp_parser_type_id (parser);
26222
26223 if (!cp_parser_parse_definitely (parser))
26224 {
26225 alignas_expr = cp_parser_assignment_expression (parser);
26226 if (alignas_expr == error_mark_node)
26227 cp_parser_skip_to_end_of_statement (parser);
26228 if (alignas_expr == NULL_TREE
26229 || alignas_expr == error_mark_node)
26230 return alignas_expr;
26231 }
26232
26233 alignas_expr = cxx_alignas_expr (alignas_expr);
26234 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
26235
26236 /* Handle alignas (pack...). */
26237 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26238 {
26239 cp_lexer_consume_token (parser->lexer);
26240 alignas_expr = make_pack_expansion (alignas_expr);
26241 }
26242
26243 /* Something went wrong, so don't build the attribute. */
26244 if (alignas_expr == error_mark_node)
26245 return error_mark_node;
26246
26247 if (!parens.require_close (parser))
26248 return error_mark_node;
26249
26250 /* Build the C++-11 representation of an 'aligned'
26251 attribute. */
26252 attributes
26253 = build_tree_list (build_tree_list (gnu_identifier,
26254 aligned_identifier), alignas_expr);
26255 }
26256
26257 return attributes;
26258 }
26259
26260 /* Parse a standard C++-11 attribute-specifier-seq.
26261
26262 attribute-specifier-seq:
26263 attribute-specifier-seq [opt] attribute-specifier
26264 */
26265
26266 static tree
26267 cp_parser_std_attribute_spec_seq (cp_parser *parser)
26268 {
26269 tree attr_specs = NULL_TREE;
26270 tree attr_last = NULL_TREE;
26271
26272 /* Don't create wrapper nodes within attributes: the
26273 handlers don't know how to handle them. */
26274 auto_suppress_location_wrappers sentinel;
26275
26276 while (true)
26277 {
26278 tree attr_spec = cp_parser_std_attribute_spec (parser);
26279 if (attr_spec == NULL_TREE)
26280 break;
26281 if (attr_spec == error_mark_node)
26282 return error_mark_node;
26283
26284 if (attr_last)
26285 TREE_CHAIN (attr_last) = attr_spec;
26286 else
26287 attr_specs = attr_last = attr_spec;
26288 attr_last = tree_last (attr_last);
26289 }
26290
26291 return attr_specs;
26292 }
26293
26294 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26295 return index of the first token after balanced-token, or N on failure. */
26296
26297 static size_t
26298 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
26299 {
26300 size_t orig_n = n;
26301 int nparens = 0, nbraces = 0, nsquares = 0;
26302 do
26303 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
26304 {
26305 case CPP_PRAGMA_EOL:
26306 if (!parser->lexer->in_pragma)
26307 break;
26308 /* FALLTHRU */
26309 case CPP_EOF:
26310 /* Ran out of tokens. */
26311 return orig_n;
26312 case CPP_OPEN_PAREN:
26313 ++nparens;
26314 break;
26315 case CPP_OPEN_BRACE:
26316 ++nbraces;
26317 break;
26318 case CPP_OPEN_SQUARE:
26319 ++nsquares;
26320 break;
26321 case CPP_CLOSE_PAREN:
26322 --nparens;
26323 break;
26324 case CPP_CLOSE_BRACE:
26325 --nbraces;
26326 break;
26327 case CPP_CLOSE_SQUARE:
26328 --nsquares;
26329 break;
26330 default:
26331 break;
26332 }
26333 while (nparens || nbraces || nsquares);
26334 return n;
26335 }
26336
26337 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26338 return index of the first token after the GNU attribute tokens, or N on
26339 failure. */
26340
26341 static size_t
26342 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
26343 {
26344 while (true)
26345 {
26346 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
26347 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
26348 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
26349 break;
26350
26351 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
26352 if (n2 == n + 2)
26353 break;
26354 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
26355 break;
26356 n = n2 + 1;
26357 }
26358 return n;
26359 }
26360
26361 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26362 next token), return index of the first token after the standard C++11
26363 attribute tokens, or N on failure. */
26364
26365 static size_t
26366 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
26367 {
26368 while (true)
26369 {
26370 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
26371 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
26372 {
26373 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26374 if (n2 == n + 1)
26375 break;
26376 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
26377 break;
26378 n = n2 + 1;
26379 }
26380 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
26381 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
26382 {
26383 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26384 if (n2 == n + 1)
26385 break;
26386 n = n2;
26387 }
26388 else
26389 break;
26390 }
26391 return n;
26392 }
26393
26394 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26395 as the next token), return index of the first token after the attribute
26396 tokens, or N on failure. */
26397
26398 static size_t
26399 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
26400 {
26401 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
26402 return cp_parser_skip_gnu_attributes_opt (parser, n);
26403 return cp_parser_skip_std_attribute_spec_seq (parser, n);
26404 }
26405
26406 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26407 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26408 current value of the PEDANTIC flag, regardless of whether or not
26409 the `__extension__' keyword is present. The caller is responsible
26410 for restoring the value of the PEDANTIC flag. */
26411
26412 static bool
26413 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
26414 {
26415 /* Save the old value of the PEDANTIC flag. */
26416 *saved_pedantic = pedantic;
26417
26418 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
26419 {
26420 /* Consume the `__extension__' token. */
26421 cp_lexer_consume_token (parser->lexer);
26422 /* We're not being pedantic while the `__extension__' keyword is
26423 in effect. */
26424 pedantic = 0;
26425
26426 return true;
26427 }
26428
26429 return false;
26430 }
26431
26432 /* Parse a label declaration.
26433
26434 label-declaration:
26435 __label__ label-declarator-seq ;
26436
26437 label-declarator-seq:
26438 identifier , label-declarator-seq
26439 identifier */
26440
26441 static void
26442 cp_parser_label_declaration (cp_parser* parser)
26443 {
26444 /* Look for the `__label__' keyword. */
26445 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
26446
26447 while (true)
26448 {
26449 tree identifier;
26450
26451 /* Look for an identifier. */
26452 identifier = cp_parser_identifier (parser);
26453 /* If we failed, stop. */
26454 if (identifier == error_mark_node)
26455 break;
26456 /* Declare it as a label. */
26457 finish_label_decl (identifier);
26458 /* If the next token is a `;', stop. */
26459 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26460 break;
26461 /* Look for the `,' separating the label declarations. */
26462 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
26463 }
26464
26465 /* Look for the final `;'. */
26466 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26467 }
26468
26469 // -------------------------------------------------------------------------- //
26470 // Requires Clause
26471
26472 // Parse a requires clause.
26473 //
26474 // requires-clause:
26475 // 'requires' logical-or-expression
26476 //
26477 // The required logical-or-expression must be a constant expression. Note
26478 // that we don't check that the expression is constepxr here. We defer until
26479 // we analyze constraints and then, we only check atomic constraints.
26480 static tree
26481 cp_parser_requires_clause (cp_parser *parser)
26482 {
26483 // Parse the requires clause so that it is not automatically folded.
26484 ++processing_template_decl;
26485 tree expr = cp_parser_binary_expression (parser, false, false,
26486 PREC_NOT_OPERATOR, NULL);
26487 if (check_for_bare_parameter_packs (expr))
26488 expr = error_mark_node;
26489 --processing_template_decl;
26490 return expr;
26491 }
26492
26493 // Optionally parse a requires clause:
26494 static tree
26495 cp_parser_requires_clause_opt (cp_parser *parser)
26496 {
26497 cp_token *tok = cp_lexer_peek_token (parser->lexer);
26498 if (tok->keyword != RID_REQUIRES)
26499 {
26500 if (!flag_concepts && tok->type == CPP_NAME
26501 && tok->u.value == ridpointers[RID_REQUIRES])
26502 {
26503 error_at (cp_lexer_peek_token (parser->lexer)->location,
26504 "%<requires%> only available with -fconcepts");
26505 /* Parse and discard the requires-clause. */
26506 cp_lexer_consume_token (parser->lexer);
26507 cp_parser_requires_clause (parser);
26508 }
26509 return NULL_TREE;
26510 }
26511 cp_lexer_consume_token (parser->lexer);
26512 return cp_parser_requires_clause (parser);
26513 }
26514
26515
26516 /*---------------------------------------------------------------------------
26517 Requires expressions
26518 ---------------------------------------------------------------------------*/
26519
26520 /* Parse a requires expression
26521
26522 requirement-expression:
26523 'requires' requirement-parameter-list [opt] requirement-body */
26524 static tree
26525 cp_parser_requires_expression (cp_parser *parser)
26526 {
26527 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26528 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26529
26530 /* A requires-expression shall appear only within a concept
26531 definition or a requires-clause.
26532
26533 TODO: Implement this diagnostic correctly. */
26534 if (!processing_template_decl)
26535 {
26536 error_at (loc, "a requires expression cannot appear outside a template");
26537 cp_parser_skip_to_end_of_statement (parser);
26538 return error_mark_node;
26539 }
26540
26541 tree parms, reqs;
26542 {
26543 /* Local parameters are delared as variables within the scope
26544 of the expression. They are not visible past the end of
26545 the expression. Expressions within the requires-expression
26546 are unevaluated. */
26547 struct scope_sentinel
26548 {
26549 scope_sentinel ()
26550 {
26551 ++cp_unevaluated_operand;
26552 begin_scope (sk_block, NULL_TREE);
26553 }
26554
26555 ~scope_sentinel ()
26556 {
26557 pop_bindings_and_leave_scope ();
26558 --cp_unevaluated_operand;
26559 }
26560 } s;
26561
26562 /* Parse the optional parameter list. */
26563 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26564 {
26565 parms = cp_parser_requirement_parameter_list (parser);
26566 if (parms == error_mark_node)
26567 return error_mark_node;
26568 }
26569 else
26570 parms = NULL_TREE;
26571
26572 /* Parse the requirement body. */
26573 reqs = cp_parser_requirement_body (parser);
26574 if (reqs == error_mark_node)
26575 return error_mark_node;
26576 }
26577
26578 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26579 the parm chain. */
26580 grokparms (parms, &parms);
26581 return finish_requires_expr (parms, reqs);
26582 }
26583
26584 /* Parse a parameterized requirement.
26585
26586 requirement-parameter-list:
26587 '(' parameter-declaration-clause ')' */
26588 static tree
26589 cp_parser_requirement_parameter_list (cp_parser *parser)
26590 {
26591 matching_parens parens;
26592 if (!parens.require_open (parser))
26593 return error_mark_node;
26594
26595 tree parms
26596 = cp_parser_parameter_declaration_clause (parser, CP_PARSER_FLAGS_NONE);
26597
26598 if (!parens.require_close (parser))
26599 return error_mark_node;
26600
26601 return parms;
26602 }
26603
26604 /* Parse the body of a requirement.
26605
26606 requirement-body:
26607 '{' requirement-list '}' */
26608 static tree
26609 cp_parser_requirement_body (cp_parser *parser)
26610 {
26611 matching_braces braces;
26612 if (!braces.require_open (parser))
26613 return error_mark_node;
26614
26615 tree reqs = cp_parser_requirement_list (parser);
26616
26617 if (!braces.require_close (parser))
26618 return error_mark_node;
26619
26620 return reqs;
26621 }
26622
26623 /* Parse a list of requirements.
26624
26625 requirement-list:
26626 requirement
26627 requirement-list ';' requirement[opt] */
26628 static tree
26629 cp_parser_requirement_list (cp_parser *parser)
26630 {
26631 tree result = NULL_TREE;
26632 while (true)
26633 {
26634 tree req = cp_parser_requirement (parser);
26635 if (req == error_mark_node)
26636 return error_mark_node;
26637
26638 result = tree_cons (NULL_TREE, req, result);
26639
26640 /* If we see a semi-colon, consume it. */
26641 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26642 cp_lexer_consume_token (parser->lexer);
26643
26644 /* Stop processing at the end of the list. */
26645 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26646 break;
26647 }
26648
26649 /* Reverse the order of requirements so they are analyzed in
26650 declaration order. */
26651 return nreverse (result);
26652 }
26653
26654 /* Parse a syntactic requirement or type requirement.
26655
26656 requirement:
26657 simple-requirement
26658 compound-requirement
26659 type-requirement
26660 nested-requirement */
26661 static tree
26662 cp_parser_requirement (cp_parser *parser)
26663 {
26664 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26665 return cp_parser_compound_requirement (parser);
26666 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26667 return cp_parser_type_requirement (parser);
26668 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26669 return cp_parser_nested_requirement (parser);
26670 else
26671 return cp_parser_simple_requirement (parser);
26672 }
26673
26674 /* Parse a simple requirement.
26675
26676 simple-requirement:
26677 expression ';' */
26678 static tree
26679 cp_parser_simple_requirement (cp_parser *parser)
26680 {
26681 tree expr = cp_parser_expression (parser, NULL, false, false);
26682 if (!expr || expr == error_mark_node)
26683 return error_mark_node;
26684
26685 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26686 return error_mark_node;
26687
26688 return finish_simple_requirement (expr);
26689 }
26690
26691 /* Parse a type requirement
26692
26693 type-requirement
26694 nested-name-specifier [opt] required-type-name ';'
26695
26696 required-type-name:
26697 type-name
26698 'template' [opt] simple-template-id */
26699 static tree
26700 cp_parser_type_requirement (cp_parser *parser)
26701 {
26702 cp_lexer_consume_token (parser->lexer);
26703
26704 // Save the scope before parsing name specifiers.
26705 tree saved_scope = parser->scope;
26706 tree saved_object_scope = parser->object_scope;
26707 tree saved_qualifying_scope = parser->qualifying_scope;
26708 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26709 cp_parser_nested_name_specifier_opt (parser,
26710 /*typename_keyword_p=*/true,
26711 /*check_dependency_p=*/false,
26712 /*type_p=*/true,
26713 /*is_declaration=*/false);
26714
26715 tree type;
26716 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26717 {
26718 cp_lexer_consume_token (parser->lexer);
26719 type = cp_parser_template_id (parser,
26720 /*template_keyword_p=*/true,
26721 /*check_dependency=*/false,
26722 /*tag_type=*/none_type,
26723 /*is_declaration=*/false);
26724 type = make_typename_type (parser->scope, type, typename_type,
26725 /*complain=*/tf_error);
26726 }
26727 else
26728 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26729
26730 if (TREE_CODE (type) == TYPE_DECL)
26731 type = TREE_TYPE (type);
26732
26733 parser->scope = saved_scope;
26734 parser->object_scope = saved_object_scope;
26735 parser->qualifying_scope = saved_qualifying_scope;
26736
26737 if (type == error_mark_node)
26738 cp_parser_skip_to_end_of_statement (parser);
26739
26740 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26741 return error_mark_node;
26742 if (type == error_mark_node)
26743 return error_mark_node;
26744
26745 return finish_type_requirement (type);
26746 }
26747
26748 /* Parse a compound requirement
26749
26750 compound-requirement:
26751 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26752 static tree
26753 cp_parser_compound_requirement (cp_parser *parser)
26754 {
26755 /* Parse an expression enclosed in '{ }'s. */
26756 matching_braces braces;
26757 if (!braces.require_open (parser))
26758 return error_mark_node;
26759
26760 tree expr = cp_parser_expression (parser, NULL, false, false);
26761 if (!expr || expr == error_mark_node)
26762 return error_mark_node;
26763
26764 if (!braces.require_close (parser))
26765 return error_mark_node;
26766
26767 /* Parse the optional noexcept. */
26768 bool noexcept_p = false;
26769 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26770 {
26771 cp_lexer_consume_token (parser->lexer);
26772 noexcept_p = true;
26773 }
26774
26775 /* Parse the optional trailing return type. */
26776 tree type = NULL_TREE;
26777 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26778 {
26779 cp_lexer_consume_token (parser->lexer);
26780 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26781 parser->in_result_type_constraint_p = true;
26782 type = cp_parser_trailing_type_id (parser);
26783 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26784 if (type == error_mark_node)
26785 return error_mark_node;
26786 }
26787
26788 return finish_compound_requirement (expr, type, noexcept_p);
26789 }
26790
26791 /* Parse a nested requirement. This is the same as a requires clause.
26792
26793 nested-requirement:
26794 requires-clause */
26795 static tree
26796 cp_parser_nested_requirement (cp_parser *parser)
26797 {
26798 cp_lexer_consume_token (parser->lexer);
26799 tree req = cp_parser_requires_clause (parser);
26800 if (req == error_mark_node)
26801 return error_mark_node;
26802 return finish_nested_requirement (req);
26803 }
26804
26805 /* Support Functions */
26806
26807 /* Return the appropriate prefer_type argument for lookup_name_real based on
26808 tag_type and template_mem_access. */
26809
26810 static inline int
26811 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26812 {
26813 /* DR 141: When looking in the current enclosing context for a template-name
26814 after -> or ., only consider class templates. */
26815 if (template_mem_access)
26816 return 2;
26817 switch (tag_type)
26818 {
26819 case none_type: return 0; // No preference.
26820 case scope_type: return 1; // Type or namespace.
26821 default: return 2; // Type only.
26822 }
26823 }
26824
26825 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26826 NAME should have one of the representations used for an
26827 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26828 is returned. If PARSER->SCOPE is a dependent type, then a
26829 SCOPE_REF is returned.
26830
26831 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26832 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26833 was formed. Abstractly, such entities should not be passed to this
26834 function, because they do not need to be looked up, but it is
26835 simpler to check for this special case here, rather than at the
26836 call-sites.
26837
26838 In cases not explicitly covered above, this function returns a
26839 DECL, OVERLOAD, or baselink representing the result of the lookup.
26840 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26841 is returned.
26842
26843 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26844 (e.g., "struct") that was used. In that case bindings that do not
26845 refer to types are ignored.
26846
26847 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26848 ignored.
26849
26850 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26851 are ignored.
26852
26853 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26854 types.
26855
26856 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26857 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26858 NULL_TREE otherwise. */
26859
26860 static cp_expr
26861 cp_parser_lookup_name (cp_parser *parser, tree name,
26862 enum tag_types tag_type,
26863 bool is_template,
26864 bool is_namespace,
26865 bool check_dependency,
26866 tree *ambiguous_decls,
26867 location_t name_location)
26868 {
26869 tree decl;
26870 tree object_type = parser->context->object_type;
26871
26872 /* Assume that the lookup will be unambiguous. */
26873 if (ambiguous_decls)
26874 *ambiguous_decls = NULL_TREE;
26875
26876 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26877 no longer valid. Note that if we are parsing tentatively, and
26878 the parse fails, OBJECT_TYPE will be automatically restored. */
26879 parser->context->object_type = NULL_TREE;
26880
26881 if (name == error_mark_node)
26882 return error_mark_node;
26883
26884 /* A template-id has already been resolved; there is no lookup to
26885 do. */
26886 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26887 return name;
26888 if (BASELINK_P (name))
26889 {
26890 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26891 == TEMPLATE_ID_EXPR);
26892 return name;
26893 }
26894
26895 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26896 it should already have been checked to make sure that the name
26897 used matches the type being destroyed. */
26898 if (TREE_CODE (name) == BIT_NOT_EXPR)
26899 {
26900 tree type;
26901
26902 /* Figure out to which type this destructor applies. */
26903 if (parser->scope)
26904 type = parser->scope;
26905 else if (object_type)
26906 type = object_type;
26907 else
26908 type = current_class_type;
26909 /* If that's not a class type, there is no destructor. */
26910 if (!type || !CLASS_TYPE_P (type))
26911 return error_mark_node;
26912
26913 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26914 lazily_declare_fn (sfk_destructor, type);
26915
26916 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26917 return dtor;
26918
26919 return error_mark_node;
26920 }
26921
26922 /* By this point, the NAME should be an ordinary identifier. If
26923 the id-expression was a qualified name, the qualifying scope is
26924 stored in PARSER->SCOPE at this point. */
26925 gcc_assert (identifier_p (name));
26926
26927 /* Perform the lookup. */
26928 if (parser->scope)
26929 {
26930 bool dependent_p;
26931
26932 if (parser->scope == error_mark_node)
26933 return error_mark_node;
26934
26935 /* If the SCOPE is dependent, the lookup must be deferred until
26936 the template is instantiated -- unless we are explicitly
26937 looking up names in uninstantiated templates. Even then, we
26938 cannot look up the name if the scope is not a class type; it
26939 might, for example, be a template type parameter. */
26940 dependent_p = (TYPE_P (parser->scope)
26941 && dependent_scope_p (parser->scope));
26942 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26943 && dependent_p)
26944 /* Defer lookup. */
26945 decl = error_mark_node;
26946 else
26947 {
26948 tree pushed_scope = NULL_TREE;
26949
26950 /* If PARSER->SCOPE is a dependent type, then it must be a
26951 class type, and we must not be checking dependencies;
26952 otherwise, we would have processed this lookup above. So
26953 that PARSER->SCOPE is not considered a dependent base by
26954 lookup_member, we must enter the scope here. */
26955 if (dependent_p)
26956 pushed_scope = push_scope (parser->scope);
26957
26958 /* If the PARSER->SCOPE is a template specialization, it
26959 may be instantiated during name lookup. In that case,
26960 errors may be issued. Even if we rollback the current
26961 tentative parse, those errors are valid. */
26962 decl = lookup_qualified_name (parser->scope, name,
26963 prefer_type_arg (tag_type),
26964 /*complain=*/true);
26965
26966 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26967 lookup result and the nested-name-specifier nominates a class C:
26968 * if the name specified after the nested-name-specifier, when
26969 looked up in C, is the injected-class-name of C (Clause 9), or
26970 * if the name specified after the nested-name-specifier is the
26971 same as the identifier or the simple-template-id's template-
26972 name in the last component of the nested-name-specifier,
26973 the name is instead considered to name the constructor of
26974 class C. [ Note: for example, the constructor is not an
26975 acceptable lookup result in an elaborated-type-specifier so
26976 the constructor would not be used in place of the
26977 injected-class-name. --end note ] Such a constructor name
26978 shall be used only in the declarator-id of a declaration that
26979 names a constructor or in a using-declaration. */
26980 if (tag_type == none_type
26981 && DECL_SELF_REFERENCE_P (decl)
26982 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26983 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26984 prefer_type_arg (tag_type),
26985 /*complain=*/true);
26986
26987 /* If we have a single function from a using decl, pull it out. */
26988 if (TREE_CODE (decl) == OVERLOAD
26989 && !really_overloaded_fn (decl))
26990 decl = OVL_FUNCTION (decl);
26991
26992 if (pushed_scope)
26993 pop_scope (pushed_scope);
26994 }
26995
26996 /* If the scope is a dependent type and either we deferred lookup or
26997 we did lookup but didn't find the name, rememeber the name. */
26998 if (decl == error_mark_node && TYPE_P (parser->scope)
26999 && dependent_type_p (parser->scope))
27000 {
27001 if (tag_type)
27002 {
27003 tree type;
27004
27005 /* The resolution to Core Issue 180 says that `struct
27006 A::B' should be considered a type-name, even if `A'
27007 is dependent. */
27008 type = make_typename_type (parser->scope, name, tag_type,
27009 /*complain=*/tf_error);
27010 if (type != error_mark_node)
27011 decl = TYPE_NAME (type);
27012 }
27013 else if (is_template
27014 && (cp_parser_next_token_ends_template_argument_p (parser)
27015 || cp_lexer_next_token_is (parser->lexer,
27016 CPP_CLOSE_PAREN)))
27017 decl = make_unbound_class_template (parser->scope,
27018 name, NULL_TREE,
27019 /*complain=*/tf_error);
27020 else
27021 decl = build_qualified_name (/*type=*/NULL_TREE,
27022 parser->scope, name,
27023 is_template);
27024 }
27025 parser->qualifying_scope = parser->scope;
27026 parser->object_scope = NULL_TREE;
27027 }
27028 else if (object_type)
27029 {
27030 /* Look up the name in the scope of the OBJECT_TYPE, unless the
27031 OBJECT_TYPE is not a class. */
27032 if (CLASS_TYPE_P (object_type))
27033 /* If the OBJECT_TYPE is a template specialization, it may
27034 be instantiated during name lookup. In that case, errors
27035 may be issued. Even if we rollback the current tentative
27036 parse, those errors are valid. */
27037 decl = lookup_member (object_type,
27038 name,
27039 /*protect=*/0,
27040 prefer_type_arg (tag_type),
27041 tf_warning_or_error);
27042 else
27043 decl = NULL_TREE;
27044
27045 if (!decl)
27046 /* Look it up in the enclosing context. DR 141: When looking for a
27047 template-name after -> or ., only consider class templates. */
27048 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
27049 /*nonclass=*/0,
27050 /*block_p=*/true, is_namespace, 0);
27051 if (object_type == unknown_type_node)
27052 /* The object is type-dependent, so we can't look anything up; we used
27053 this to get the DR 141 behavior. */
27054 object_type = NULL_TREE;
27055 parser->object_scope = object_type;
27056 parser->qualifying_scope = NULL_TREE;
27057 }
27058 else
27059 {
27060 decl = lookup_name_real (name, prefer_type_arg (tag_type),
27061 /*nonclass=*/0,
27062 /*block_p=*/true, is_namespace, 0);
27063 parser->qualifying_scope = NULL_TREE;
27064 parser->object_scope = NULL_TREE;
27065 }
27066
27067 /* If the lookup failed, let our caller know. */
27068 if (!decl || decl == error_mark_node)
27069 return error_mark_node;
27070
27071 /* Pull out the template from an injected-class-name (or multiple). */
27072 if (is_template)
27073 decl = maybe_get_template_decl_from_type_decl (decl);
27074
27075 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
27076 if (TREE_CODE (decl) == TREE_LIST)
27077 {
27078 if (ambiguous_decls)
27079 *ambiguous_decls = decl;
27080 /* The error message we have to print is too complicated for
27081 cp_parser_error, so we incorporate its actions directly. */
27082 if (!cp_parser_simulate_error (parser))
27083 {
27084 error_at (name_location, "reference to %qD is ambiguous",
27085 name);
27086 print_candidates (decl);
27087 }
27088 return error_mark_node;
27089 }
27090
27091 gcc_assert (DECL_P (decl)
27092 || TREE_CODE (decl) == OVERLOAD
27093 || TREE_CODE (decl) == SCOPE_REF
27094 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
27095 || BASELINK_P (decl));
27096
27097 /* If we have resolved the name of a member declaration, check to
27098 see if the declaration is accessible. When the name resolves to
27099 set of overloaded functions, accessibility is checked when
27100 overload resolution is done.
27101
27102 During an explicit instantiation, access is not checked at all,
27103 as per [temp.explicit]. */
27104 if (DECL_P (decl))
27105 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
27106
27107 maybe_record_typedef_use (decl);
27108
27109 return cp_expr (decl, name_location);
27110 }
27111
27112 /* Like cp_parser_lookup_name, but for use in the typical case where
27113 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
27114 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
27115
27116 static tree
27117 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
27118 {
27119 return cp_parser_lookup_name (parser, name,
27120 none_type,
27121 /*is_template=*/false,
27122 /*is_namespace=*/false,
27123 /*check_dependency=*/true,
27124 /*ambiguous_decls=*/NULL,
27125 location);
27126 }
27127
27128 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
27129 the current context, return the TYPE_DECL. If TAG_NAME_P is
27130 true, the DECL indicates the class being defined in a class-head,
27131 or declared in an elaborated-type-specifier.
27132
27133 Otherwise, return DECL. */
27134
27135 static tree
27136 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
27137 {
27138 /* If the TEMPLATE_DECL is being declared as part of a class-head,
27139 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
27140
27141 struct A {
27142 template <typename T> struct B;
27143 };
27144
27145 template <typename T> struct A::B {};
27146
27147 Similarly, in an elaborated-type-specifier:
27148
27149 namespace N { struct X{}; }
27150
27151 struct A {
27152 template <typename T> friend struct N::X;
27153 };
27154
27155 However, if the DECL refers to a class type, and we are in
27156 the scope of the class, then the name lookup automatically
27157 finds the TYPE_DECL created by build_self_reference rather
27158 than a TEMPLATE_DECL. For example, in:
27159
27160 template <class T> struct S {
27161 S s;
27162 };
27163
27164 there is no need to handle such case. */
27165
27166 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
27167 return DECL_TEMPLATE_RESULT (decl);
27168
27169 return decl;
27170 }
27171
27172 /* If too many, or too few, template-parameter lists apply to the
27173 declarator, issue an error message. Returns TRUE if all went well,
27174 and FALSE otherwise. */
27175
27176 static bool
27177 cp_parser_check_declarator_template_parameters (cp_parser* parser,
27178 cp_declarator *declarator,
27179 location_t declarator_location)
27180 {
27181 switch (declarator->kind)
27182 {
27183 case cdk_id:
27184 {
27185 unsigned num_templates = 0;
27186 tree scope = declarator->u.id.qualifying_scope;
27187 bool template_id_p = false;
27188
27189 if (scope)
27190 num_templates = num_template_headers_for_class (scope);
27191 else if (TREE_CODE (declarator->u.id.unqualified_name)
27192 == TEMPLATE_ID_EXPR)
27193 {
27194 /* If the DECLARATOR has the form `X<y>' then it uses one
27195 additional level of template parameters. */
27196 ++num_templates;
27197 template_id_p = true;
27198 }
27199
27200 return cp_parser_check_template_parameters
27201 (parser, num_templates, template_id_p, declarator_location,
27202 declarator);
27203 }
27204
27205 case cdk_function:
27206 case cdk_array:
27207 case cdk_pointer:
27208 case cdk_reference:
27209 case cdk_ptrmem:
27210 return (cp_parser_check_declarator_template_parameters
27211 (parser, declarator->declarator, declarator_location));
27212
27213 case cdk_decomp:
27214 case cdk_error:
27215 return true;
27216
27217 default:
27218 gcc_unreachable ();
27219 }
27220 return false;
27221 }
27222
27223 /* NUM_TEMPLATES were used in the current declaration. If that is
27224 invalid, return FALSE and issue an error messages. Otherwise,
27225 return TRUE. If DECLARATOR is non-NULL, then we are checking a
27226 declarator and we can print more accurate diagnostics. */
27227
27228 static bool
27229 cp_parser_check_template_parameters (cp_parser* parser,
27230 unsigned num_templates,
27231 bool template_id_p,
27232 location_t location,
27233 cp_declarator *declarator)
27234 {
27235 /* If there are the same number of template classes and parameter
27236 lists, that's OK. */
27237 if (parser->num_template_parameter_lists == num_templates)
27238 return true;
27239 /* If there are more, but only one more, and the name ends in an identifier,
27240 then we are declaring a primary template. That's OK too. */
27241 if (!template_id_p
27242 && parser->num_template_parameter_lists == num_templates + 1)
27243 return true;
27244 /* If there are more template classes than parameter lists, we have
27245 something like:
27246
27247 template <class T> void S<T>::R<T>::f (); */
27248 if (parser->num_template_parameter_lists < num_templates)
27249 {
27250 if (declarator && !current_function_decl)
27251 error_at (location, "specializing member %<%T::%E%> "
27252 "requires %<template<>%> syntax",
27253 declarator->u.id.qualifying_scope,
27254 declarator->u.id.unqualified_name);
27255 else if (declarator)
27256 error_at (location, "invalid declaration of %<%T::%E%>",
27257 declarator->u.id.qualifying_scope,
27258 declarator->u.id.unqualified_name);
27259 else
27260 error_at (location, "too few template-parameter-lists");
27261 return false;
27262 }
27263 /* Otherwise, there are too many template parameter lists. We have
27264 something like:
27265
27266 template <class T> template <class U> void S::f(); */
27267 error_at (location, "too many template-parameter-lists");
27268 return false;
27269 }
27270
27271 /* Parse an optional `::' token indicating that the following name is
27272 from the global namespace. If so, PARSER->SCOPE is set to the
27273 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
27274 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
27275 Returns the new value of PARSER->SCOPE, if the `::' token is
27276 present, and NULL_TREE otherwise. */
27277
27278 static tree
27279 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
27280 {
27281 cp_token *token;
27282
27283 /* Peek at the next token. */
27284 token = cp_lexer_peek_token (parser->lexer);
27285 /* If we're looking at a `::' token then we're starting from the
27286 global namespace, not our current location. */
27287 if (token->type == CPP_SCOPE)
27288 {
27289 /* Consume the `::' token. */
27290 cp_lexer_consume_token (parser->lexer);
27291 /* Set the SCOPE so that we know where to start the lookup. */
27292 parser->scope = global_namespace;
27293 parser->qualifying_scope = global_namespace;
27294 parser->object_scope = NULL_TREE;
27295
27296 return parser->scope;
27297 }
27298 else if (!current_scope_valid_p)
27299 {
27300 parser->scope = NULL_TREE;
27301 parser->qualifying_scope = NULL_TREE;
27302 parser->object_scope = NULL_TREE;
27303 }
27304
27305 return NULL_TREE;
27306 }
27307
27308 /* Returns TRUE if the upcoming token sequence is the start of a
27309 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27310 declarator is preceded by the `friend' specifier. */
27311
27312 static bool
27313 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
27314 {
27315 bool constructor_p;
27316 bool outside_class_specifier_p;
27317 tree nested_name_specifier;
27318 cp_token *next_token;
27319
27320 /* The common case is that this is not a constructor declarator, so
27321 try to avoid doing lots of work if at all possible. It's not
27322 valid declare a constructor at function scope. */
27323 if (parser->in_function_body)
27324 return false;
27325 /* And only certain tokens can begin a constructor declarator. */
27326 next_token = cp_lexer_peek_token (parser->lexer);
27327 if (next_token->type != CPP_NAME
27328 && next_token->type != CPP_SCOPE
27329 && next_token->type != CPP_NESTED_NAME_SPECIFIER
27330 && next_token->type != CPP_TEMPLATE_ID)
27331 return false;
27332
27333 /* Parse tentatively; we are going to roll back all of the tokens
27334 consumed here. */
27335 cp_parser_parse_tentatively (parser);
27336 /* Assume that we are looking at a constructor declarator. */
27337 constructor_p = true;
27338
27339 /* Look for the optional `::' operator. */
27340 cp_parser_global_scope_opt (parser,
27341 /*current_scope_valid_p=*/false);
27342 /* Look for the nested-name-specifier. */
27343 nested_name_specifier
27344 = (cp_parser_nested_name_specifier_opt (parser,
27345 /*typename_keyword_p=*/false,
27346 /*check_dependency_p=*/false,
27347 /*type_p=*/false,
27348 /*is_declaration=*/false));
27349
27350 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
27351 if (nested_name_specifier
27352 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
27353 {
27354 tree s = resolve_typename_type (nested_name_specifier,
27355 /*only_current_p=*/false);
27356 if (TREE_CODE (s) != TYPENAME_TYPE)
27357 nested_name_specifier = s;
27358 }
27359
27360 outside_class_specifier_p = (!at_class_scope_p ()
27361 || !TYPE_BEING_DEFINED (current_class_type)
27362 || friend_p);
27363
27364 /* Outside of a class-specifier, there must be a
27365 nested-name-specifier. Except in C++17 mode, where we
27366 might be declaring a guiding declaration. */
27367 if (!nested_name_specifier && outside_class_specifier_p
27368 && cxx_dialect < cxx17)
27369 constructor_p = false;
27370 else if (nested_name_specifier == error_mark_node)
27371 constructor_p = false;
27372
27373 /* If we have a class scope, this is easy; DR 147 says that S::S always
27374 names the constructor, and no other qualified name could. */
27375 if (constructor_p && nested_name_specifier
27376 && CLASS_TYPE_P (nested_name_specifier))
27377 {
27378 tree id = cp_parser_unqualified_id (parser,
27379 /*template_keyword_p=*/false,
27380 /*check_dependency_p=*/false,
27381 /*declarator_p=*/true,
27382 /*optional_p=*/false);
27383 if (is_overloaded_fn (id))
27384 id = DECL_NAME (get_first_fn (id));
27385 if (!constructor_name_p (id, nested_name_specifier))
27386 constructor_p = false;
27387 }
27388 /* If we still think that this might be a constructor-declarator,
27389 look for a class-name. */
27390 else if (constructor_p)
27391 {
27392 /* If we have:
27393
27394 template <typename T> struct S {
27395 S();
27396 };
27397
27398 we must recognize that the nested `S' names a class. */
27399 if (cxx_dialect >= cxx17)
27400 cp_parser_parse_tentatively (parser);
27401
27402 tree type_decl;
27403 type_decl = cp_parser_class_name (parser,
27404 /*typename_keyword_p=*/false,
27405 /*template_keyword_p=*/false,
27406 none_type,
27407 /*check_dependency_p=*/false,
27408 /*class_head_p=*/false,
27409 /*is_declaration=*/false);
27410
27411 if (cxx_dialect >= cxx17
27412 && !cp_parser_parse_definitely (parser))
27413 {
27414 type_decl = NULL_TREE;
27415 tree tmpl = cp_parser_template_name (parser,
27416 /*template_keyword*/false,
27417 /*check_dependency_p*/false,
27418 /*is_declaration*/false,
27419 none_type,
27420 /*is_identifier*/NULL);
27421 if (DECL_CLASS_TEMPLATE_P (tmpl)
27422 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27423 /* It's a deduction guide, return true. */;
27424 else
27425 cp_parser_simulate_error (parser);
27426 }
27427
27428 /* If there was no class-name, then this is not a constructor.
27429 Otherwise, if we are in a class-specifier and we aren't
27430 handling a friend declaration, check that its type matches
27431 current_class_type (c++/38313). Note: error_mark_node
27432 is left alone for error recovery purposes. */
27433 constructor_p = (!cp_parser_error_occurred (parser)
27434 && (outside_class_specifier_p
27435 || type_decl == NULL_TREE
27436 || type_decl == error_mark_node
27437 || same_type_p (current_class_type,
27438 TREE_TYPE (type_decl))));
27439
27440 /* If we're still considering a constructor, we have to see a `(',
27441 to begin the parameter-declaration-clause, followed by either a
27442 `)', an `...', or a decl-specifier. We need to check for a
27443 type-specifier to avoid being fooled into thinking that:
27444
27445 S (f) (int);
27446
27447 is a constructor. (It is actually a function named `f' that
27448 takes one parameter (of type `int') and returns a value of type
27449 `S'. */
27450 if (constructor_p
27451 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27452 constructor_p = false;
27453
27454 if (constructor_p
27455 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
27456 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
27457 /* A parameter declaration begins with a decl-specifier,
27458 which is either the "attribute" keyword, a storage class
27459 specifier, or (usually) a type-specifier. */
27460 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
27461 {
27462 tree type;
27463 tree pushed_scope = NULL_TREE;
27464 unsigned saved_num_template_parameter_lists;
27465
27466 /* Names appearing in the type-specifier should be looked up
27467 in the scope of the class. */
27468 if (current_class_type)
27469 type = NULL_TREE;
27470 else if (type_decl)
27471 {
27472 type = TREE_TYPE (type_decl);
27473 if (TREE_CODE (type) == TYPENAME_TYPE)
27474 {
27475 type = resolve_typename_type (type,
27476 /*only_current_p=*/false);
27477 if (TREE_CODE (type) == TYPENAME_TYPE)
27478 {
27479 cp_parser_abort_tentative_parse (parser);
27480 return false;
27481 }
27482 }
27483 pushed_scope = push_scope (type);
27484 }
27485
27486 /* Inside the constructor parameter list, surrounding
27487 template-parameter-lists do not apply. */
27488 saved_num_template_parameter_lists
27489 = parser->num_template_parameter_lists;
27490 parser->num_template_parameter_lists = 0;
27491
27492 /* Look for the type-specifier. */
27493 cp_parser_type_specifier (parser,
27494 CP_PARSER_FLAGS_NONE,
27495 /*decl_specs=*/NULL,
27496 /*is_declarator=*/true,
27497 /*declares_class_or_enum=*/NULL,
27498 /*is_cv_qualifier=*/NULL);
27499
27500 parser->num_template_parameter_lists
27501 = saved_num_template_parameter_lists;
27502
27503 /* Leave the scope of the class. */
27504 if (pushed_scope)
27505 pop_scope (pushed_scope);
27506
27507 constructor_p = !cp_parser_error_occurred (parser);
27508 }
27509 }
27510
27511 /* We did not really want to consume any tokens. */
27512 cp_parser_abort_tentative_parse (parser);
27513
27514 return constructor_p;
27515 }
27516
27517 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27518 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27519 they must be performed once we are in the scope of the function.
27520
27521 Returns the function defined. */
27522
27523 static tree
27524 cp_parser_function_definition_from_specifiers_and_declarator
27525 (cp_parser* parser,
27526 cp_decl_specifier_seq *decl_specifiers,
27527 tree attributes,
27528 const cp_declarator *declarator)
27529 {
27530 tree fn;
27531 bool success_p;
27532
27533 /* Begin the function-definition. */
27534 success_p = start_function (decl_specifiers, declarator, attributes);
27535
27536 /* The things we're about to see are not directly qualified by any
27537 template headers we've seen thus far. */
27538 reset_specialization ();
27539
27540 /* If there were names looked up in the decl-specifier-seq that we
27541 did not check, check them now. We must wait until we are in the
27542 scope of the function to perform the checks, since the function
27543 might be a friend. */
27544 perform_deferred_access_checks (tf_warning_or_error);
27545
27546 if (success_p)
27547 {
27548 cp_finalize_omp_declare_simd (parser, current_function_decl);
27549 parser->omp_declare_simd = NULL;
27550 cp_finalize_oacc_routine (parser, current_function_decl, true);
27551 parser->oacc_routine = NULL;
27552 }
27553
27554 if (!success_p)
27555 {
27556 /* Skip the entire function. */
27557 cp_parser_skip_to_end_of_block_or_statement (parser);
27558 fn = error_mark_node;
27559 }
27560 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27561 {
27562 /* Seen already, skip it. An error message has already been output. */
27563 cp_parser_skip_to_end_of_block_or_statement (parser);
27564 fn = current_function_decl;
27565 current_function_decl = NULL_TREE;
27566 /* If this is a function from a class, pop the nested class. */
27567 if (current_class_name)
27568 pop_nested_class ();
27569 }
27570 else
27571 {
27572 timevar_id_t tv;
27573 if (DECL_DECLARED_INLINE_P (current_function_decl))
27574 tv = TV_PARSE_INLINE;
27575 else
27576 tv = TV_PARSE_FUNC;
27577 timevar_push (tv);
27578 fn = cp_parser_function_definition_after_declarator (parser,
27579 /*inline_p=*/false);
27580 timevar_pop (tv);
27581 }
27582
27583 return fn;
27584 }
27585
27586 /* Parse the part of a function-definition that follows the
27587 declarator. INLINE_P is TRUE iff this function is an inline
27588 function defined within a class-specifier.
27589
27590 Returns the function defined. */
27591
27592 static tree
27593 cp_parser_function_definition_after_declarator (cp_parser* parser,
27594 bool inline_p)
27595 {
27596 tree fn;
27597 bool saved_in_unbraced_linkage_specification_p;
27598 bool saved_in_function_body;
27599 unsigned saved_num_template_parameter_lists;
27600 cp_token *token;
27601 bool fully_implicit_function_template_p
27602 = parser->fully_implicit_function_template_p;
27603 parser->fully_implicit_function_template_p = false;
27604 tree implicit_template_parms
27605 = parser->implicit_template_parms;
27606 parser->implicit_template_parms = 0;
27607 cp_binding_level* implicit_template_scope
27608 = parser->implicit_template_scope;
27609 parser->implicit_template_scope = 0;
27610
27611 saved_in_function_body = parser->in_function_body;
27612 parser->in_function_body = true;
27613 /* If the next token is `return', then the code may be trying to
27614 make use of the "named return value" extension that G++ used to
27615 support. */
27616 token = cp_lexer_peek_token (parser->lexer);
27617 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27618 {
27619 /* Consume the `return' keyword. */
27620 cp_lexer_consume_token (parser->lexer);
27621 /* Look for the identifier that indicates what value is to be
27622 returned. */
27623 cp_parser_identifier (parser);
27624 /* Issue an error message. */
27625 error_at (token->location,
27626 "named return values are no longer supported");
27627 /* Skip tokens until we reach the start of the function body. */
27628 while (true)
27629 {
27630 cp_token *token = cp_lexer_peek_token (parser->lexer);
27631 if (token->type == CPP_OPEN_BRACE
27632 || token->type == CPP_EOF
27633 || token->type == CPP_PRAGMA_EOL)
27634 break;
27635 cp_lexer_consume_token (parser->lexer);
27636 }
27637 }
27638 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27639 anything declared inside `f'. */
27640 saved_in_unbraced_linkage_specification_p
27641 = parser->in_unbraced_linkage_specification_p;
27642 parser->in_unbraced_linkage_specification_p = false;
27643 /* Inside the function, surrounding template-parameter-lists do not
27644 apply. */
27645 saved_num_template_parameter_lists
27646 = parser->num_template_parameter_lists;
27647 parser->num_template_parameter_lists = 0;
27648
27649 /* If the next token is `try', `__transaction_atomic', or
27650 `__transaction_relaxed`, then we are looking at either function-try-block
27651 or function-transaction-block. Note that all of these include the
27652 function-body. */
27653 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27654 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27655 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27656 RID_TRANSACTION_RELAXED))
27657 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27658 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27659 cp_parser_function_try_block (parser);
27660 else
27661 cp_parser_ctor_initializer_opt_and_function_body
27662 (parser, /*in_function_try_block=*/false);
27663
27664 /* Finish the function. */
27665 fn = finish_function (inline_p);
27666 /* Generate code for it, if necessary. */
27667 expand_or_defer_fn (fn);
27668 /* Restore the saved values. */
27669 parser->in_unbraced_linkage_specification_p
27670 = saved_in_unbraced_linkage_specification_p;
27671 parser->num_template_parameter_lists
27672 = saved_num_template_parameter_lists;
27673 parser->in_function_body = saved_in_function_body;
27674
27675 parser->fully_implicit_function_template_p
27676 = fully_implicit_function_template_p;
27677 parser->implicit_template_parms
27678 = implicit_template_parms;
27679 parser->implicit_template_scope
27680 = implicit_template_scope;
27681
27682 if (parser->fully_implicit_function_template_p)
27683 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27684
27685 return fn;
27686 }
27687
27688 /* Parse a template-declaration body (following argument list). */
27689
27690 static void
27691 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27692 tree parameter_list,
27693 bool member_p)
27694 {
27695 tree decl = NULL_TREE;
27696 bool friend_p = false;
27697
27698 /* We just processed one more parameter list. */
27699 ++parser->num_template_parameter_lists;
27700
27701 /* Get the deferred access checks from the parameter list. These
27702 will be checked once we know what is being declared, as for a
27703 member template the checks must be performed in the scope of the
27704 class containing the member. */
27705 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27706
27707 /* Tentatively parse for a new template parameter list, which can either be
27708 the template keyword or a template introduction. */
27709 if (cp_parser_template_declaration_after_export (parser, member_p))
27710 /* OK */;
27711 else if (cxx_dialect >= cxx11
27712 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27713 decl = cp_parser_alias_declaration (parser);
27714 else
27715 {
27716 /* There are no access checks when parsing a template, as we do not
27717 know if a specialization will be a friend. */
27718 push_deferring_access_checks (dk_no_check);
27719 cp_token *token = cp_lexer_peek_token (parser->lexer);
27720 decl = cp_parser_single_declaration (parser,
27721 checks,
27722 member_p,
27723 /*explicit_specialization_p=*/false,
27724 &friend_p);
27725 pop_deferring_access_checks ();
27726
27727 /* If this is a member template declaration, let the front
27728 end know. */
27729 if (member_p && !friend_p && decl)
27730 {
27731 if (TREE_CODE (decl) == TYPE_DECL)
27732 cp_parser_check_access_in_redeclaration (decl, token->location);
27733
27734 decl = finish_member_template_decl (decl);
27735 }
27736 else if (friend_p && decl
27737 && DECL_DECLARES_TYPE_P (decl))
27738 make_friend_class (current_class_type, TREE_TYPE (decl),
27739 /*complain=*/true);
27740 }
27741 /* We are done with the current parameter list. */
27742 --parser->num_template_parameter_lists;
27743
27744 pop_deferring_access_checks ();
27745
27746 /* Finish up. */
27747 finish_template_decl (parameter_list);
27748
27749 /* Check the template arguments for a literal operator template. */
27750 if (decl
27751 && DECL_DECLARES_FUNCTION_P (decl)
27752 && UDLIT_OPER_P (DECL_NAME (decl)))
27753 {
27754 bool ok = true;
27755 if (parameter_list == NULL_TREE)
27756 ok = false;
27757 else
27758 {
27759 int num_parms = TREE_VEC_LENGTH (parameter_list);
27760 if (num_parms == 1)
27761 {
27762 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27763 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27764 if (CLASS_TYPE_P (TREE_TYPE (parm)))
27765 /* OK, C++20 string literal operator template. We don't need
27766 to warn in lower dialects here because we will have already
27767 warned about the template parameter. */;
27768 else if (TREE_TYPE (parm) != char_type_node
27769 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27770 ok = false;
27771 }
27772 else if (num_parms == 2 && cxx_dialect >= cxx14)
27773 {
27774 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27775 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27776 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27777 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27778 if (parm == error_mark_node
27779 || TREE_TYPE (parm) != TREE_TYPE (type)
27780 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27781 ok = false;
27782 else
27783 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27784 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
27785 "ISO C++ did not adopt string literal operator templa"
27786 "tes taking an argument pack of characters");
27787 }
27788 else
27789 ok = false;
27790 }
27791 if (!ok)
27792 {
27793 if (cxx_dialect > cxx17)
27794 error ("literal operator template %qD has invalid parameter list;"
27795 " Expected non-type template parameter pack <char...> "
27796 " or single non-type parameter of class type",
27797 decl);
27798 else
27799 error ("literal operator template %qD has invalid parameter list."
27800 " Expected non-type template parameter pack <char...>",
27801 decl);
27802 }
27803 }
27804
27805 /* Register member declarations. */
27806 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27807 finish_member_declaration (decl);
27808 /* If DECL is a function template, we must return to parse it later.
27809 (Even though there is no definition, there might be default
27810 arguments that need handling.) */
27811 if (member_p && decl
27812 && DECL_DECLARES_FUNCTION_P (decl))
27813 vec_safe_push (unparsed_funs_with_definitions, decl);
27814 }
27815
27816 /* Parse a template introduction header for a template-declaration. Returns
27817 false if tentative parse fails. */
27818
27819 static bool
27820 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27821 {
27822 cp_parser_parse_tentatively (parser);
27823
27824 tree saved_scope = parser->scope;
27825 tree saved_object_scope = parser->object_scope;
27826 tree saved_qualifying_scope = parser->qualifying_scope;
27827
27828 /* Look for the optional `::' operator. */
27829 cp_parser_global_scope_opt (parser,
27830 /*current_scope_valid_p=*/false);
27831 /* Look for the nested-name-specifier. */
27832 cp_parser_nested_name_specifier_opt (parser,
27833 /*typename_keyword_p=*/false,
27834 /*check_dependency_p=*/true,
27835 /*type_p=*/false,
27836 /*is_declaration=*/false);
27837
27838 cp_token *token = cp_lexer_peek_token (parser->lexer);
27839 tree concept_name = cp_parser_identifier (parser);
27840
27841 /* Look up the concept for which we will be matching
27842 template parameters. */
27843 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27844 token->location);
27845 parser->scope = saved_scope;
27846 parser->object_scope = saved_object_scope;
27847 parser->qualifying_scope = saved_qualifying_scope;
27848
27849 if (concept_name == error_mark_node)
27850 cp_parser_simulate_error (parser);
27851
27852 /* Look for opening brace for introduction. */
27853 matching_braces braces;
27854 braces.require_open (parser);
27855
27856 if (!cp_parser_parse_definitely (parser))
27857 return false;
27858
27859 push_deferring_access_checks (dk_deferred);
27860
27861 /* Build vector of placeholder parameters and grab
27862 matching identifiers. */
27863 tree introduction_list = cp_parser_introduction_list (parser);
27864
27865 /* Look for closing brace for introduction. */
27866 if (!braces.require_close (parser))
27867 return true;
27868
27869 /* The introduction-list shall not be empty. */
27870 int nargs = TREE_VEC_LENGTH (introduction_list);
27871 if (nargs == 0)
27872 {
27873 /* In cp_parser_introduction_list we have already issued an error. */
27874 return true;
27875 }
27876
27877 if (tmpl_decl == error_mark_node)
27878 {
27879 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27880 token->location);
27881 return true;
27882 }
27883
27884 /* Build and associate the constraint. */
27885 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27886 if (parms && parms != error_mark_node)
27887 {
27888 cp_parser_template_declaration_after_parameters (parser, parms,
27889 member_p);
27890 return true;
27891 }
27892
27893 error_at (token->location, "no matching concept for template-introduction");
27894 return true;
27895 }
27896
27897 /* Parse a normal template-declaration following the template keyword. */
27898
27899 static void
27900 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27901 {
27902 tree parameter_list;
27903 bool need_lang_pop;
27904 location_t location = input_location;
27905
27906 /* Look for the `<' token. */
27907 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27908 return;
27909 if (at_class_scope_p () && current_function_decl)
27910 {
27911 /* 14.5.2.2 [temp.mem]
27912
27913 A local class shall not have member templates. */
27914 error_at (location,
27915 "invalid declaration of member template in local class");
27916 cp_parser_skip_to_end_of_block_or_statement (parser);
27917 return;
27918 }
27919 /* [temp]
27920
27921 A template ... shall not have C linkage. */
27922 if (current_lang_name == lang_name_c)
27923 {
27924 error_at (location, "template with C linkage");
27925 maybe_show_extern_c_location ();
27926 /* Give it C++ linkage to avoid confusing other parts of the
27927 front end. */
27928 push_lang_context (lang_name_cplusplus);
27929 need_lang_pop = true;
27930 }
27931 else
27932 need_lang_pop = false;
27933
27934 /* We cannot perform access checks on the template parameter
27935 declarations until we know what is being declared, just as we
27936 cannot check the decl-specifier list. */
27937 push_deferring_access_checks (dk_deferred);
27938
27939 /* If the next token is `>', then we have an invalid
27940 specialization. Rather than complain about an invalid template
27941 parameter, issue an error message here. */
27942 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27943 {
27944 cp_parser_error (parser, "invalid explicit specialization");
27945 begin_specialization ();
27946 parameter_list = NULL_TREE;
27947 }
27948 else
27949 {
27950 /* Parse the template parameters. */
27951 parameter_list = cp_parser_template_parameter_list (parser);
27952 }
27953
27954 /* Look for the `>'. */
27955 cp_parser_skip_to_end_of_template_parameter_list (parser);
27956
27957 /* Manage template requirements */
27958 if (flag_concepts)
27959 {
27960 tree reqs = get_shorthand_constraints (current_template_parms);
27961 if (tree r = cp_parser_requires_clause_opt (parser))
27962 reqs = conjoin_constraints (reqs, normalize_expression (r));
27963 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27964 }
27965
27966 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27967 member_p);
27968
27969 /* For the erroneous case of a template with C linkage, we pushed an
27970 implicit C++ linkage scope; exit that scope now. */
27971 if (need_lang_pop)
27972 pop_lang_context ();
27973 }
27974
27975 /* Parse a template-declaration, assuming that the `export' (and
27976 `extern') keywords, if present, has already been scanned. MEMBER_P
27977 is as for cp_parser_template_declaration. */
27978
27979 static bool
27980 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27981 {
27982 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27983 {
27984 cp_lexer_consume_token (parser->lexer);
27985 cp_parser_explicit_template_declaration (parser, member_p);
27986 return true;
27987 }
27988 else if (flag_concepts)
27989 return cp_parser_template_introduction (parser, member_p);
27990
27991 return false;
27992 }
27993
27994 /* Perform the deferred access checks from a template-parameter-list.
27995 CHECKS is a TREE_LIST of access checks, as returned by
27996 get_deferred_access_checks. */
27997
27998 static void
27999 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
28000 {
28001 ++processing_template_parmlist;
28002 perform_access_checks (checks, tf_warning_or_error);
28003 --processing_template_parmlist;
28004 }
28005
28006 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
28007 `function-definition' sequence that follows a template header.
28008 If MEMBER_P is true, this declaration appears in a class scope.
28009
28010 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
28011 *FRIEND_P is set to TRUE iff the declaration is a friend. */
28012
28013 static tree
28014 cp_parser_single_declaration (cp_parser* parser,
28015 vec<deferred_access_check, va_gc> *checks,
28016 bool member_p,
28017 bool explicit_specialization_p,
28018 bool* friend_p)
28019 {
28020 int declares_class_or_enum;
28021 tree decl = NULL_TREE;
28022 cp_decl_specifier_seq decl_specifiers;
28023 bool function_definition_p = false;
28024 cp_token *decl_spec_token_start;
28025
28026 /* This function is only used when processing a template
28027 declaration. */
28028 gcc_assert (innermost_scope_kind () == sk_template_parms
28029 || innermost_scope_kind () == sk_template_spec);
28030
28031 /* Defer access checks until we know what is being declared. */
28032 push_deferring_access_checks (dk_deferred);
28033
28034 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
28035 alternative. */
28036 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
28037 cp_parser_decl_specifier_seq (parser,
28038 (CP_PARSER_FLAGS_OPTIONAL
28039 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
28040 &decl_specifiers,
28041 &declares_class_or_enum);
28042 if (friend_p)
28043 *friend_p = cp_parser_friend_p (&decl_specifiers);
28044
28045 /* There are no template typedefs. */
28046 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
28047 {
28048 error_at (decl_spec_token_start->location,
28049 "template declaration of %<typedef%>");
28050 decl = error_mark_node;
28051 }
28052
28053 /* Gather up the access checks that occurred the
28054 decl-specifier-seq. */
28055 stop_deferring_access_checks ();
28056
28057 /* Check for the declaration of a template class. */
28058 if (declares_class_or_enum)
28059 {
28060 if (cp_parser_declares_only_class_p (parser)
28061 || (declares_class_or_enum & 2))
28062 {
28063 // If this is a declaration, but not a definition, associate
28064 // any constraints with the type declaration. Constraints
28065 // are associated with definitions in cp_parser_class_specifier.
28066 if (declares_class_or_enum == 1)
28067 associate_classtype_constraints (decl_specifiers.type);
28068
28069 decl = shadow_tag (&decl_specifiers);
28070
28071 /* In this case:
28072
28073 struct C {
28074 friend template <typename T> struct A<T>::B;
28075 };
28076
28077 A<T>::B will be represented by a TYPENAME_TYPE, and
28078 therefore not recognized by shadow_tag. */
28079 if (friend_p && *friend_p
28080 && !decl
28081 && decl_specifiers.type
28082 && TYPE_P (decl_specifiers.type))
28083 decl = decl_specifiers.type;
28084
28085 if (decl && decl != error_mark_node)
28086 decl = TYPE_NAME (decl);
28087 else
28088 decl = error_mark_node;
28089
28090 /* Perform access checks for template parameters. */
28091 cp_parser_perform_template_parameter_access_checks (checks);
28092
28093 /* Give a helpful diagnostic for
28094 template <class T> struct A { } a;
28095 if we aren't already recovering from an error. */
28096 if (!cp_parser_declares_only_class_p (parser)
28097 && !seen_error ())
28098 {
28099 error_at (cp_lexer_peek_token (parser->lexer)->location,
28100 "a class template declaration must not declare "
28101 "anything else");
28102 cp_parser_skip_to_end_of_block_or_statement (parser);
28103 goto out;
28104 }
28105 }
28106 }
28107
28108 /* Complain about missing 'typename' or other invalid type names. */
28109 if (!decl_specifiers.any_type_specifiers_p
28110 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
28111 {
28112 /* cp_parser_parse_and_diagnose_invalid_type_name calls
28113 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
28114 the rest of this declaration. */
28115 decl = error_mark_node;
28116 goto out;
28117 }
28118
28119 /* If it's not a template class, try for a template function. If
28120 the next token is a `;', then this declaration does not declare
28121 anything. But, if there were errors in the decl-specifiers, then
28122 the error might well have come from an attempted class-specifier.
28123 In that case, there's no need to warn about a missing declarator. */
28124 if (!decl
28125 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
28126 || decl_specifiers.type != error_mark_node))
28127 {
28128 decl = cp_parser_init_declarator (parser,
28129 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
28130 &decl_specifiers,
28131 checks,
28132 /*function_definition_allowed_p=*/true,
28133 member_p,
28134 declares_class_or_enum,
28135 &function_definition_p,
28136 NULL, NULL, NULL);
28137
28138 /* 7.1.1-1 [dcl.stc]
28139
28140 A storage-class-specifier shall not be specified in an explicit
28141 specialization... */
28142 if (decl
28143 && explicit_specialization_p
28144 && decl_specifiers.storage_class != sc_none)
28145 {
28146 error_at (decl_spec_token_start->location,
28147 "explicit template specialization cannot have a storage class");
28148 decl = error_mark_node;
28149 }
28150
28151 if (decl && VAR_P (decl))
28152 check_template_variable (decl);
28153 }
28154
28155 /* Look for a trailing `;' after the declaration. */
28156 if (!function_definition_p
28157 && (decl == error_mark_node
28158 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
28159 cp_parser_skip_to_end_of_block_or_statement (parser);
28160
28161 out:
28162 pop_deferring_access_checks ();
28163
28164 /* Clear any current qualification; whatever comes next is the start
28165 of something new. */
28166 parser->scope = NULL_TREE;
28167 parser->qualifying_scope = NULL_TREE;
28168 parser->object_scope = NULL_TREE;
28169
28170 return decl;
28171 }
28172
28173 /* Parse a cast-expression that is not the operand of a unary "&". */
28174
28175 static cp_expr
28176 cp_parser_simple_cast_expression (cp_parser *parser)
28177 {
28178 return cp_parser_cast_expression (parser, /*address_p=*/false,
28179 /*cast_p=*/false, /*decltype*/false, NULL);
28180 }
28181
28182 /* Parse a functional cast to TYPE. Returns an expression
28183 representing the cast. */
28184
28185 static cp_expr
28186 cp_parser_functional_cast (cp_parser* parser, tree type)
28187 {
28188 vec<tree, va_gc> *vec;
28189 tree expression_list;
28190 cp_expr cast;
28191 bool nonconst_p;
28192
28193 location_t start_loc = input_location;
28194
28195 if (!type)
28196 type = error_mark_node;
28197
28198 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28199 {
28200 cp_lexer_set_source_position (parser->lexer);
28201 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28202 expression_list = cp_parser_braced_list (parser, &nonconst_p);
28203 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
28204 if (TREE_CODE (type) == TYPE_DECL)
28205 type = TREE_TYPE (type);
28206
28207 cast = finish_compound_literal (type, expression_list,
28208 tf_warning_or_error, fcl_functional);
28209 /* Create a location of the form:
28210 type_name{i, f}
28211 ^~~~~~~~~~~~~~~
28212 with caret == start at the start of the type name,
28213 finishing at the closing brace. */
28214 location_t finish_loc
28215 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28216 location_t combined_loc = make_location (start_loc, start_loc,
28217 finish_loc);
28218 cast.set_location (combined_loc);
28219 return cast;
28220 }
28221
28222
28223 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
28224 /*cast_p=*/true,
28225 /*allow_expansion_p=*/true,
28226 /*non_constant_p=*/NULL);
28227 if (vec == NULL)
28228 expression_list = error_mark_node;
28229 else
28230 {
28231 expression_list = build_tree_list_vec (vec);
28232 release_tree_vector (vec);
28233 }
28234
28235 cast = build_functional_cast (type, expression_list,
28236 tf_warning_or_error);
28237 /* [expr.const]/1: In an integral constant expression "only type
28238 conversions to integral or enumeration type can be used". */
28239 if (TREE_CODE (type) == TYPE_DECL)
28240 type = TREE_TYPE (type);
28241 if (cast != error_mark_node
28242 && !cast_valid_in_integral_constant_expression_p (type)
28243 && cp_parser_non_integral_constant_expression (parser,
28244 NIC_CONSTRUCTOR))
28245 return error_mark_node;
28246
28247 /* Create a location of the form:
28248 float(i)
28249 ^~~~~~~~
28250 with caret == start at the start of the type name,
28251 finishing at the closing paren. */
28252 location_t finish_loc
28253 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28254 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
28255 cast.set_location (combined_loc);
28256 return cast;
28257 }
28258
28259 /* Save the tokens that make up the body of a member function defined
28260 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
28261 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
28262 specifiers applied to the declaration. Returns the FUNCTION_DECL
28263 for the member function. */
28264
28265 static tree
28266 cp_parser_save_member_function_body (cp_parser* parser,
28267 cp_decl_specifier_seq *decl_specifiers,
28268 cp_declarator *declarator,
28269 tree attributes)
28270 {
28271 cp_token *first;
28272 cp_token *last;
28273 tree fn;
28274 bool function_try_block = false;
28275
28276 /* Create the FUNCTION_DECL. */
28277 fn = grokmethod (decl_specifiers, declarator, attributes);
28278 cp_finalize_omp_declare_simd (parser, fn);
28279 cp_finalize_oacc_routine (parser, fn, true);
28280 /* If something went badly wrong, bail out now. */
28281 if (fn == error_mark_node)
28282 {
28283 /* If there's a function-body, skip it. */
28284 if (cp_parser_token_starts_function_definition_p
28285 (cp_lexer_peek_token (parser->lexer)))
28286 cp_parser_skip_to_end_of_block_or_statement (parser);
28287 return error_mark_node;
28288 }
28289
28290 /* Remember it, if there default args to post process. */
28291 cp_parser_save_default_args (parser, fn);
28292
28293 /* Save away the tokens that make up the body of the
28294 function. */
28295 first = parser->lexer->next_token;
28296
28297 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
28298 cp_lexer_consume_token (parser->lexer);
28299 else if (cp_lexer_next_token_is_keyword (parser->lexer,
28300 RID_TRANSACTION_ATOMIC))
28301 {
28302 cp_lexer_consume_token (parser->lexer);
28303 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
28304 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
28305 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
28306 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
28307 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28308 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
28309 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
28310 {
28311 cp_lexer_consume_token (parser->lexer);
28312 cp_lexer_consume_token (parser->lexer);
28313 cp_lexer_consume_token (parser->lexer);
28314 cp_lexer_consume_token (parser->lexer);
28315 cp_lexer_consume_token (parser->lexer);
28316 }
28317 else
28318 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
28319 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
28320 {
28321 cp_lexer_consume_token (parser->lexer);
28322 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28323 break;
28324 }
28325 }
28326
28327 /* Handle function try blocks. */
28328 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28329 {
28330 cp_lexer_consume_token (parser->lexer);
28331 function_try_block = true;
28332 }
28333 /* We can have braced-init-list mem-initializers before the fn body. */
28334 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28335 {
28336 cp_lexer_consume_token (parser->lexer);
28337 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
28338 {
28339 /* cache_group will stop after an un-nested { } pair, too. */
28340 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28341 break;
28342
28343 /* variadic mem-inits have ... after the ')'. */
28344 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28345 cp_lexer_consume_token (parser->lexer);
28346 }
28347 }
28348 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28349 /* Handle function try blocks. */
28350 if (function_try_block)
28351 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
28352 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28353 last = parser->lexer->next_token;
28354
28355 /* Save away the inline definition; we will process it when the
28356 class is complete. */
28357 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
28358 DECL_PENDING_INLINE_P (fn) = 1;
28359
28360 /* We need to know that this was defined in the class, so that
28361 friend templates are handled correctly. */
28362 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
28363
28364 /* Add FN to the queue of functions to be parsed later. */
28365 vec_safe_push (unparsed_funs_with_definitions, fn);
28366
28367 return fn;
28368 }
28369
28370 /* Save the tokens that make up the in-class initializer for a non-static
28371 data member. Returns a DEFAULT_ARG. */
28372
28373 static tree
28374 cp_parser_save_nsdmi (cp_parser* parser)
28375 {
28376 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
28377 }
28378
28379 /* Parse a template-argument-list, as well as the trailing ">" (but
28380 not the opening "<"). See cp_parser_template_argument_list for the
28381 return value. */
28382
28383 static tree
28384 cp_parser_enclosed_template_argument_list (cp_parser* parser)
28385 {
28386 tree arguments;
28387 tree saved_scope;
28388 tree saved_qualifying_scope;
28389 tree saved_object_scope;
28390 bool saved_greater_than_is_operator_p;
28391
28392 /* [temp.names]
28393
28394 When parsing a template-id, the first non-nested `>' is taken as
28395 the end of the template-argument-list rather than a greater-than
28396 operator. */
28397 saved_greater_than_is_operator_p
28398 = parser->greater_than_is_operator_p;
28399 parser->greater_than_is_operator_p = false;
28400 /* Parsing the argument list may modify SCOPE, so we save it
28401 here. */
28402 saved_scope = parser->scope;
28403 saved_qualifying_scope = parser->qualifying_scope;
28404 saved_object_scope = parser->object_scope;
28405 /* We need to evaluate the template arguments, even though this
28406 template-id may be nested within a "sizeof". */
28407 cp_evaluated ev;
28408 /* Parse the template-argument-list itself. */
28409 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
28410 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28411 arguments = NULL_TREE;
28412 else
28413 arguments = cp_parser_template_argument_list (parser);
28414 /* Look for the `>' that ends the template-argument-list. If we find
28415 a '>>' instead, it's probably just a typo. */
28416 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28417 {
28418 if (cxx_dialect != cxx98)
28419 {
28420 /* In C++0x, a `>>' in a template argument list or cast
28421 expression is considered to be two separate `>'
28422 tokens. So, change the current token to a `>', but don't
28423 consume it: it will be consumed later when the outer
28424 template argument list (or cast expression) is parsed.
28425 Note that this replacement of `>' for `>>' is necessary
28426 even if we are parsing tentatively: in the tentative
28427 case, after calling
28428 cp_parser_enclosed_template_argument_list we will always
28429 throw away all of the template arguments and the first
28430 closing `>', either because the template argument list
28431 was erroneous or because we are replacing those tokens
28432 with a CPP_TEMPLATE_ID token. The second `>' (which will
28433 not have been thrown away) is needed either to close an
28434 outer template argument list or to complete a new-style
28435 cast. */
28436 cp_token *token = cp_lexer_peek_token (parser->lexer);
28437 token->type = CPP_GREATER;
28438 }
28439 else if (!saved_greater_than_is_operator_p)
28440 {
28441 /* If we're in a nested template argument list, the '>>' has
28442 to be a typo for '> >'. We emit the error message, but we
28443 continue parsing and we push a '>' as next token, so that
28444 the argument list will be parsed correctly. Note that the
28445 global source location is still on the token before the
28446 '>>', so we need to say explicitly where we want it. */
28447 cp_token *token = cp_lexer_peek_token (parser->lexer);
28448 gcc_rich_location richloc (token->location);
28449 richloc.add_fixit_replace ("> >");
28450 error_at (&richloc, "%<>>%> should be %<> >%> "
28451 "within a nested template argument list");
28452
28453 token->type = CPP_GREATER;
28454 }
28455 else
28456 {
28457 /* If this is not a nested template argument list, the '>>'
28458 is a typo for '>'. Emit an error message and continue.
28459 Same deal about the token location, but here we can get it
28460 right by consuming the '>>' before issuing the diagnostic. */
28461 cp_token *token = cp_lexer_consume_token (parser->lexer);
28462 error_at (token->location,
28463 "spurious %<>>%>, use %<>%> to terminate "
28464 "a template argument list");
28465 }
28466 }
28467 else
28468 cp_parser_skip_to_end_of_template_parameter_list (parser);
28469 /* The `>' token might be a greater-than operator again now. */
28470 parser->greater_than_is_operator_p
28471 = saved_greater_than_is_operator_p;
28472 /* Restore the SAVED_SCOPE. */
28473 parser->scope = saved_scope;
28474 parser->qualifying_scope = saved_qualifying_scope;
28475 parser->object_scope = saved_object_scope;
28476
28477 return arguments;
28478 }
28479
28480 /* MEMBER_FUNCTION is a member function, or a friend. If default
28481 arguments, or the body of the function have not yet been parsed,
28482 parse them now. */
28483
28484 static void
28485 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
28486 {
28487 timevar_push (TV_PARSE_INMETH);
28488 /* If this member is a template, get the underlying
28489 FUNCTION_DECL. */
28490 if (DECL_FUNCTION_TEMPLATE_P (member_function))
28491 member_function = DECL_TEMPLATE_RESULT (member_function);
28492
28493 /* There should not be any class definitions in progress at this
28494 point; the bodies of members are only parsed outside of all class
28495 definitions. */
28496 gcc_assert (parser->num_classes_being_defined == 0);
28497 /* While we're parsing the member functions we might encounter more
28498 classes. We want to handle them right away, but we don't want
28499 them getting mixed up with functions that are currently in the
28500 queue. */
28501 push_unparsed_function_queues (parser);
28502
28503 /* Make sure that any template parameters are in scope. */
28504 maybe_begin_member_template_processing (member_function);
28505
28506 /* If the body of the function has not yet been parsed, parse it
28507 now. */
28508 if (DECL_PENDING_INLINE_P (member_function))
28509 {
28510 tree function_scope;
28511 cp_token_cache *tokens;
28512
28513 /* The function is no longer pending; we are processing it. */
28514 tokens = DECL_PENDING_INLINE_INFO (member_function);
28515 DECL_PENDING_INLINE_INFO (member_function) = NULL;
28516 DECL_PENDING_INLINE_P (member_function) = 0;
28517
28518 /* If this is a local class, enter the scope of the containing
28519 function. */
28520 function_scope = current_function_decl;
28521 if (function_scope)
28522 push_function_context ();
28523
28524 /* Push the body of the function onto the lexer stack. */
28525 cp_parser_push_lexer_for_tokens (parser, tokens);
28526
28527 /* Let the front end know that we going to be defining this
28528 function. */
28529 start_preparsed_function (member_function, NULL_TREE,
28530 SF_PRE_PARSED | SF_INCLASS_INLINE);
28531
28532 /* Don't do access checking if it is a templated function. */
28533 if (processing_template_decl)
28534 push_deferring_access_checks (dk_no_check);
28535
28536 /* #pragma omp declare reduction needs special parsing. */
28537 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28538 {
28539 parser->lexer->in_pragma = true;
28540 cp_parser_omp_declare_reduction_exprs (member_function, parser);
28541 finish_function (/*inline_p=*/true);
28542 cp_check_omp_declare_reduction (member_function);
28543 }
28544 else
28545 /* Now, parse the body of the function. */
28546 cp_parser_function_definition_after_declarator (parser,
28547 /*inline_p=*/true);
28548
28549 if (processing_template_decl)
28550 pop_deferring_access_checks ();
28551
28552 /* Leave the scope of the containing function. */
28553 if (function_scope)
28554 pop_function_context ();
28555 cp_parser_pop_lexer (parser);
28556 }
28557
28558 /* Remove any template parameters from the symbol table. */
28559 maybe_end_member_template_processing ();
28560
28561 /* Restore the queue. */
28562 pop_unparsed_function_queues (parser);
28563 timevar_pop (TV_PARSE_INMETH);
28564 }
28565
28566 /* If DECL contains any default args, remember it on the unparsed
28567 functions queue. */
28568
28569 static void
28570 cp_parser_save_default_args (cp_parser* parser, tree decl)
28571 {
28572 tree probe;
28573
28574 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28575 probe;
28576 probe = TREE_CHAIN (probe))
28577 if (TREE_PURPOSE (probe))
28578 {
28579 cp_default_arg_entry entry = {current_class_type, decl};
28580 vec_safe_push (unparsed_funs_with_default_args, entry);
28581 break;
28582 }
28583 }
28584
28585 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28586 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28587 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28588 from the parameter-type-list. */
28589
28590 static tree
28591 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28592 tree default_arg, tree parmtype)
28593 {
28594 cp_token_cache *tokens;
28595 tree parsed_arg;
28596 bool dummy;
28597
28598 if (default_arg == error_mark_node)
28599 return error_mark_node;
28600
28601 /* Push the saved tokens for the default argument onto the parser's
28602 lexer stack. */
28603 tokens = DEFARG_TOKENS (default_arg);
28604 cp_parser_push_lexer_for_tokens (parser, tokens);
28605
28606 start_lambda_scope (decl);
28607
28608 /* Parse the default argument. */
28609 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28610 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28611 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28612
28613 finish_lambda_scope ();
28614
28615 if (parsed_arg == error_mark_node)
28616 cp_parser_skip_to_end_of_statement (parser);
28617
28618 if (!processing_template_decl)
28619 {
28620 /* In a non-template class, check conversions now. In a template,
28621 we'll wait and instantiate these as needed. */
28622 if (TREE_CODE (decl) == PARM_DECL)
28623 parsed_arg = check_default_argument (parmtype, parsed_arg,
28624 tf_warning_or_error);
28625 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28626 parsed_arg = error_mark_node;
28627 else
28628 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28629 }
28630
28631 /* If the token stream has not been completely used up, then
28632 there was extra junk after the end of the default
28633 argument. */
28634 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28635 {
28636 if (TREE_CODE (decl) == PARM_DECL)
28637 cp_parser_error (parser, "expected %<,%>");
28638 else
28639 cp_parser_error (parser, "expected %<;%>");
28640 }
28641
28642 /* Revert to the main lexer. */
28643 cp_parser_pop_lexer (parser);
28644
28645 return parsed_arg;
28646 }
28647
28648 /* FIELD is a non-static data member with an initializer which we saved for
28649 later; parse it now. */
28650
28651 static void
28652 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28653 {
28654 tree def;
28655
28656 maybe_begin_member_template_processing (field);
28657
28658 push_unparsed_function_queues (parser);
28659 def = cp_parser_late_parse_one_default_arg (parser, field,
28660 DECL_INITIAL (field),
28661 NULL_TREE);
28662 pop_unparsed_function_queues (parser);
28663
28664 maybe_end_member_template_processing ();
28665
28666 DECL_INITIAL (field) = def;
28667 }
28668
28669 /* FN is a FUNCTION_DECL which may contains a parameter with an
28670 unparsed DEFAULT_ARG. Parse the default args now. This function
28671 assumes that the current scope is the scope in which the default
28672 argument should be processed. */
28673
28674 static void
28675 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28676 {
28677 unsigned char saved_local_variables_forbidden_p;
28678 tree parm, parmdecl;
28679
28680 /* While we're parsing the default args, we might (due to the
28681 statement expression extension) encounter more classes. We want
28682 to handle them right away, but we don't want them getting mixed
28683 up with default args that are currently in the queue. */
28684 push_unparsed_function_queues (parser);
28685
28686 /* Local variable names (and the `this' keyword) may not appear
28687 in a default argument. */
28688 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28689 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
28690
28691 push_defarg_context (fn);
28692
28693 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28694 parmdecl = DECL_ARGUMENTS (fn);
28695 parm && parm != void_list_node;
28696 parm = TREE_CHAIN (parm),
28697 parmdecl = DECL_CHAIN (parmdecl))
28698 {
28699 tree default_arg = TREE_PURPOSE (parm);
28700 tree parsed_arg;
28701 vec<tree, va_gc> *insts;
28702 tree copy;
28703 unsigned ix;
28704
28705 if (!default_arg)
28706 continue;
28707
28708 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28709 /* This can happen for a friend declaration for a function
28710 already declared with default arguments. */
28711 continue;
28712
28713 parsed_arg
28714 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28715 default_arg,
28716 TREE_VALUE (parm));
28717 TREE_PURPOSE (parm) = parsed_arg;
28718
28719 /* Update any instantiations we've already created. */
28720 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28721 vec_safe_iterate (insts, ix, &copy); ix++)
28722 TREE_PURPOSE (copy) = parsed_arg;
28723 }
28724
28725 pop_defarg_context ();
28726
28727 /* Make sure no default arg is missing. */
28728 check_default_args (fn);
28729
28730 /* Restore the state of local_variables_forbidden_p. */
28731 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28732
28733 /* Restore the queue. */
28734 pop_unparsed_function_queues (parser);
28735 }
28736
28737 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28738
28739 sizeof ... ( identifier )
28740
28741 where the 'sizeof' token has already been consumed. */
28742
28743 static tree
28744 cp_parser_sizeof_pack (cp_parser *parser)
28745 {
28746 /* Consume the `...'. */
28747 cp_lexer_consume_token (parser->lexer);
28748 maybe_warn_variadic_templates ();
28749
28750 matching_parens parens;
28751 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28752 if (paren)
28753 parens.consume_open (parser);
28754 else
28755 permerror (cp_lexer_peek_token (parser->lexer)->location,
28756 "%<sizeof...%> argument must be surrounded by parentheses");
28757
28758 cp_token *token = cp_lexer_peek_token (parser->lexer);
28759 tree name = cp_parser_identifier (parser);
28760 if (name == error_mark_node)
28761 return error_mark_node;
28762 /* The name is not qualified. */
28763 parser->scope = NULL_TREE;
28764 parser->qualifying_scope = NULL_TREE;
28765 parser->object_scope = NULL_TREE;
28766 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28767 if (expr == error_mark_node)
28768 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28769 token->location);
28770 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28771 expr = TREE_TYPE (expr);
28772 else if (TREE_CODE (expr) == CONST_DECL)
28773 expr = DECL_INITIAL (expr);
28774 expr = make_pack_expansion (expr);
28775 PACK_EXPANSION_SIZEOF_P (expr) = true;
28776
28777 if (paren)
28778 parens.require_close (parser);
28779
28780 return expr;
28781 }
28782
28783 /* Parse the operand of `sizeof' (or a similar operator). Returns
28784 either a TYPE or an expression, depending on the form of the
28785 input. The KEYWORD indicates which kind of expression we have
28786 encountered. */
28787
28788 static tree
28789 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28790 {
28791 tree expr = NULL_TREE;
28792 const char *saved_message;
28793 char *tmp;
28794 bool saved_integral_constant_expression_p;
28795 bool saved_non_integral_constant_expression_p;
28796
28797 /* If it's a `...', then we are computing the length of a parameter
28798 pack. */
28799 if (keyword == RID_SIZEOF
28800 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28801 return cp_parser_sizeof_pack (parser);
28802
28803 /* Types cannot be defined in a `sizeof' expression. Save away the
28804 old message. */
28805 saved_message = parser->type_definition_forbidden_message;
28806 /* And create the new one. */
28807 tmp = concat ("types may not be defined in %<",
28808 IDENTIFIER_POINTER (ridpointers[keyword]),
28809 "%> expressions", NULL);
28810 parser->type_definition_forbidden_message = tmp;
28811
28812 /* The restrictions on constant-expressions do not apply inside
28813 sizeof expressions. */
28814 saved_integral_constant_expression_p
28815 = parser->integral_constant_expression_p;
28816 saved_non_integral_constant_expression_p
28817 = parser->non_integral_constant_expression_p;
28818 parser->integral_constant_expression_p = false;
28819
28820 /* Do not actually evaluate the expression. */
28821 ++cp_unevaluated_operand;
28822 ++c_inhibit_evaluation_warnings;
28823 /* If it's a `(', then we might be looking at the type-id
28824 construction. */
28825 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28826 {
28827 tree type = NULL_TREE;
28828
28829 /* We can't be sure yet whether we're looking at a type-id or an
28830 expression. */
28831 cp_parser_parse_tentatively (parser);
28832
28833 matching_parens parens;
28834 parens.consume_open (parser);
28835
28836 /* Note: as a GNU Extension, compound literals are considered
28837 postfix-expressions as they are in C99, so they are valid
28838 arguments to sizeof. See comment in cp_parser_cast_expression
28839 for details. */
28840 if (cp_parser_compound_literal_p (parser))
28841 cp_parser_simulate_error (parser);
28842 else
28843 {
28844 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28845 parser->in_type_id_in_expr_p = true;
28846 /* Look for the type-id. */
28847 type = cp_parser_type_id (parser);
28848 /* Look for the closing `)'. */
28849 parens.require_close (parser);
28850 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28851 }
28852
28853 /* If all went well, then we're done. */
28854 if (cp_parser_parse_definitely (parser))
28855 expr = type;
28856 }
28857
28858 /* If the type-id production did not work out, then we must be
28859 looking at the unary-expression production. */
28860 if (!expr)
28861 expr = cp_parser_unary_expression (parser);
28862
28863 /* Go back to evaluating expressions. */
28864 --cp_unevaluated_operand;
28865 --c_inhibit_evaluation_warnings;
28866
28867 /* Free the message we created. */
28868 free (tmp);
28869 /* And restore the old one. */
28870 parser->type_definition_forbidden_message = saved_message;
28871 parser->integral_constant_expression_p
28872 = saved_integral_constant_expression_p;
28873 parser->non_integral_constant_expression_p
28874 = saved_non_integral_constant_expression_p;
28875
28876 return expr;
28877 }
28878
28879 /* If the current declaration has no declarator, return true. */
28880
28881 static bool
28882 cp_parser_declares_only_class_p (cp_parser *parser)
28883 {
28884 /* If the next token is a `;' or a `,' then there is no
28885 declarator. */
28886 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28887 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28888 }
28889
28890 /* Update the DECL_SPECS to reflect the storage class indicated by
28891 KEYWORD. */
28892
28893 static void
28894 cp_parser_set_storage_class (cp_parser *parser,
28895 cp_decl_specifier_seq *decl_specs,
28896 enum rid keyword,
28897 cp_token *token)
28898 {
28899 cp_storage_class storage_class;
28900
28901 if (parser->in_unbraced_linkage_specification_p)
28902 {
28903 error_at (token->location, "invalid use of %qD in linkage specification",
28904 ridpointers[keyword]);
28905 return;
28906 }
28907 else if (decl_specs->storage_class != sc_none)
28908 {
28909 decl_specs->conflicting_specifiers_p = true;
28910 return;
28911 }
28912
28913 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28914 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28915 && decl_specs->gnu_thread_keyword_p)
28916 {
28917 pedwarn (decl_specs->locations[ds_thread], 0,
28918 "%<__thread%> before %qD", ridpointers[keyword]);
28919 }
28920
28921 switch (keyword)
28922 {
28923 case RID_AUTO:
28924 storage_class = sc_auto;
28925 break;
28926 case RID_REGISTER:
28927 storage_class = sc_register;
28928 break;
28929 case RID_STATIC:
28930 storage_class = sc_static;
28931 break;
28932 case RID_EXTERN:
28933 storage_class = sc_extern;
28934 break;
28935 case RID_MUTABLE:
28936 storage_class = sc_mutable;
28937 break;
28938 default:
28939 gcc_unreachable ();
28940 }
28941 decl_specs->storage_class = storage_class;
28942 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28943
28944 /* A storage class specifier cannot be applied alongside a typedef
28945 specifier. If there is a typedef specifier present then set
28946 conflicting_specifiers_p which will trigger an error later
28947 on in grokdeclarator. */
28948 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28949 decl_specs->conflicting_specifiers_p = true;
28950 }
28951
28952 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28953 is true, the type is a class or enum definition. */
28954
28955 static void
28956 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28957 tree type_spec,
28958 cp_token *token,
28959 bool type_definition_p)
28960 {
28961 decl_specs->any_specifiers_p = true;
28962
28963 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
28964 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
28965 this is what happened. In system headers, we ignore these
28966 declarations so that G++ can work with system headers that are not
28967 C++-safe. */
28968 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28969 && !type_definition_p
28970 && (type_spec == boolean_type_node
28971 || type_spec == char8_type_node
28972 || type_spec == char16_type_node
28973 || type_spec == char32_type_node
28974 || type_spec == wchar_type_node)
28975 && (decl_specs->type
28976 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28977 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28978 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28979 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28980 {
28981 decl_specs->redefined_builtin_type = type_spec;
28982 set_and_check_decl_spec_loc (decl_specs,
28983 ds_redefined_builtin_type_spec,
28984 token);
28985 if (!decl_specs->type)
28986 {
28987 decl_specs->type = type_spec;
28988 decl_specs->type_definition_p = false;
28989 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28990 }
28991 }
28992 else if (decl_specs->type)
28993 decl_specs->multiple_types_p = true;
28994 else
28995 {
28996 decl_specs->type = type_spec;
28997 decl_specs->type_definition_p = type_definition_p;
28998 decl_specs->redefined_builtin_type = NULL_TREE;
28999 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
29000 }
29001 }
29002
29003 /* True iff TOKEN is the GNU keyword __thread. */
29004
29005 static bool
29006 token_is__thread (cp_token *token)
29007 {
29008 gcc_assert (token->keyword == RID_THREAD);
29009 return id_equal (token->u.value, "__thread");
29010 }
29011
29012 /* Set the location for a declarator specifier and check if it is
29013 duplicated.
29014
29015 DECL_SPECS is the sequence of declarator specifiers onto which to
29016 set the location.
29017
29018 DS is the single declarator specifier to set which location is to
29019 be set onto the existing sequence of declarators.
29020
29021 LOCATION is the location for the declarator specifier to
29022 consider. */
29023
29024 static void
29025 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
29026 cp_decl_spec ds, cp_token *token)
29027 {
29028 gcc_assert (ds < ds_last);
29029
29030 if (decl_specs == NULL)
29031 return;
29032
29033 location_t location = token->location;
29034
29035 if (decl_specs->locations[ds] == 0)
29036 {
29037 decl_specs->locations[ds] = location;
29038 if (ds == ds_thread)
29039 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
29040 }
29041 else
29042 {
29043 if (ds == ds_long)
29044 {
29045 if (decl_specs->locations[ds_long_long] != 0)
29046 error_at (location,
29047 "%<long long long%> is too long for GCC");
29048 else
29049 {
29050 decl_specs->locations[ds_long_long] = location;
29051 pedwarn_cxx98 (location,
29052 OPT_Wlong_long,
29053 "ISO C++ 1998 does not support %<long long%>");
29054 }
29055 }
29056 else if (ds == ds_thread)
29057 {
29058 bool gnu = token_is__thread (token);
29059 gcc_rich_location richloc (location);
29060 if (gnu != decl_specs->gnu_thread_keyword_p)
29061 {
29062 richloc.add_range (decl_specs->locations[ds_thread]);
29063 error_at (&richloc,
29064 "both %<__thread%> and %<thread_local%> specified");
29065 }
29066 else
29067 {
29068 richloc.add_fixit_remove ();
29069 error_at (&richloc, "duplicate %qD", token->u.value);
29070 }
29071 }
29072 else
29073 {
29074 static const char *const decl_spec_names[] = {
29075 "signed",
29076 "unsigned",
29077 "short",
29078 "long",
29079 "const",
29080 "volatile",
29081 "restrict",
29082 "inline",
29083 "virtual",
29084 "explicit",
29085 "friend",
29086 "typedef",
29087 "using",
29088 "constexpr",
29089 "__complex"
29090 };
29091 gcc_rich_location richloc (location);
29092 richloc.add_fixit_remove ();
29093 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
29094 }
29095 }
29096 }
29097
29098 /* Return true iff the declarator specifier DS is present in the
29099 sequence of declarator specifiers DECL_SPECS. */
29100
29101 bool
29102 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
29103 cp_decl_spec ds)
29104 {
29105 gcc_assert (ds < ds_last);
29106
29107 if (decl_specs == NULL)
29108 return false;
29109
29110 return decl_specs->locations[ds] != 0;
29111 }
29112
29113 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
29114 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
29115
29116 static bool
29117 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
29118 {
29119 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
29120 }
29121
29122 /* Issue an error message indicating that TOKEN_DESC was expected.
29123 If KEYWORD is true, it indicated this function is called by
29124 cp_parser_require_keword and the required token can only be
29125 a indicated keyword.
29126
29127 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29128 within any error as the location of an "opening" token matching
29129 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29130 RT_CLOSE_PAREN). */
29131
29132 static void
29133 cp_parser_required_error (cp_parser *parser,
29134 required_token token_desc,
29135 bool keyword,
29136 location_t matching_location)
29137 {
29138 if (cp_parser_simulate_error (parser))
29139 return;
29140
29141 const char *gmsgid = NULL;
29142 switch (token_desc)
29143 {
29144 case RT_NEW:
29145 gmsgid = G_("expected %<new%>");
29146 break;
29147 case RT_DELETE:
29148 gmsgid = G_("expected %<delete%>");
29149 break;
29150 case RT_RETURN:
29151 gmsgid = G_("expected %<return%>");
29152 break;
29153 case RT_WHILE:
29154 gmsgid = G_("expected %<while%>");
29155 break;
29156 case RT_EXTERN:
29157 gmsgid = G_("expected %<extern%>");
29158 break;
29159 case RT_STATIC_ASSERT:
29160 gmsgid = G_("expected %<static_assert%>");
29161 break;
29162 case RT_DECLTYPE:
29163 gmsgid = G_("expected %<decltype%>");
29164 break;
29165 case RT_OPERATOR:
29166 gmsgid = G_("expected %<operator%>");
29167 break;
29168 case RT_CLASS:
29169 gmsgid = G_("expected %<class%>");
29170 break;
29171 case RT_TEMPLATE:
29172 gmsgid = G_("expected %<template%>");
29173 break;
29174 case RT_NAMESPACE:
29175 gmsgid = G_("expected %<namespace%>");
29176 break;
29177 case RT_USING:
29178 gmsgid = G_("expected %<using%>");
29179 break;
29180 case RT_ASM:
29181 gmsgid = G_("expected %<asm%>");
29182 break;
29183 case RT_TRY:
29184 gmsgid = G_("expected %<try%>");
29185 break;
29186 case RT_CATCH:
29187 gmsgid = G_("expected %<catch%>");
29188 break;
29189 case RT_THROW:
29190 gmsgid = G_("expected %<throw%>");
29191 break;
29192 case RT_LABEL:
29193 gmsgid = G_("expected %<__label__%>");
29194 break;
29195 case RT_AT_TRY:
29196 gmsgid = G_("expected %<@try%>");
29197 break;
29198 case RT_AT_SYNCHRONIZED:
29199 gmsgid = G_("expected %<@synchronized%>");
29200 break;
29201 case RT_AT_THROW:
29202 gmsgid = G_("expected %<@throw%>");
29203 break;
29204 case RT_TRANSACTION_ATOMIC:
29205 gmsgid = G_("expected %<__transaction_atomic%>");
29206 break;
29207 case RT_TRANSACTION_RELAXED:
29208 gmsgid = G_("expected %<__transaction_relaxed%>");
29209 break;
29210 default:
29211 break;
29212 }
29213
29214 if (!gmsgid && !keyword)
29215 {
29216 switch (token_desc)
29217 {
29218 case RT_SEMICOLON:
29219 gmsgid = G_("expected %<;%>");
29220 break;
29221 case RT_OPEN_PAREN:
29222 gmsgid = G_("expected %<(%>");
29223 break;
29224 case RT_CLOSE_BRACE:
29225 gmsgid = G_("expected %<}%>");
29226 break;
29227 case RT_OPEN_BRACE:
29228 gmsgid = G_("expected %<{%>");
29229 break;
29230 case RT_CLOSE_SQUARE:
29231 gmsgid = G_("expected %<]%>");
29232 break;
29233 case RT_OPEN_SQUARE:
29234 gmsgid = G_("expected %<[%>");
29235 break;
29236 case RT_COMMA:
29237 gmsgid = G_("expected %<,%>");
29238 break;
29239 case RT_SCOPE:
29240 gmsgid = G_("expected %<::%>");
29241 break;
29242 case RT_LESS:
29243 gmsgid = G_("expected %<<%>");
29244 break;
29245 case RT_GREATER:
29246 gmsgid = G_("expected %<>%>");
29247 break;
29248 case RT_EQ:
29249 gmsgid = G_("expected %<=%>");
29250 break;
29251 case RT_ELLIPSIS:
29252 gmsgid = G_("expected %<...%>");
29253 break;
29254 case RT_MULT:
29255 gmsgid = G_("expected %<*%>");
29256 break;
29257 case RT_COMPL:
29258 gmsgid = G_("expected %<~%>");
29259 break;
29260 case RT_COLON:
29261 gmsgid = G_("expected %<:%>");
29262 break;
29263 case RT_COLON_SCOPE:
29264 gmsgid = G_("expected %<:%> or %<::%>");
29265 break;
29266 case RT_CLOSE_PAREN:
29267 gmsgid = G_("expected %<)%>");
29268 break;
29269 case RT_COMMA_CLOSE_PAREN:
29270 gmsgid = G_("expected %<,%> or %<)%>");
29271 break;
29272 case RT_PRAGMA_EOL:
29273 gmsgid = G_("expected end of line");
29274 break;
29275 case RT_NAME:
29276 gmsgid = G_("expected identifier");
29277 break;
29278 case RT_SELECT:
29279 gmsgid = G_("expected selection-statement");
29280 break;
29281 case RT_ITERATION:
29282 gmsgid = G_("expected iteration-statement");
29283 break;
29284 case RT_JUMP:
29285 gmsgid = G_("expected jump-statement");
29286 break;
29287 case RT_CLASS_KEY:
29288 gmsgid = G_("expected class-key");
29289 break;
29290 case RT_CLASS_TYPENAME_TEMPLATE:
29291 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
29292 break;
29293 default:
29294 gcc_unreachable ();
29295 }
29296 }
29297
29298 if (gmsgid)
29299 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
29300 }
29301
29302
29303 /* If the next token is of the indicated TYPE, consume it. Otherwise,
29304 issue an error message indicating that TOKEN_DESC was expected.
29305
29306 Returns the token consumed, if the token had the appropriate type.
29307 Otherwise, returns NULL.
29308
29309 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29310 within any error as the location of an "opening" token matching
29311 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29312 RT_CLOSE_PAREN). */
29313
29314 static cp_token *
29315 cp_parser_require (cp_parser* parser,
29316 enum cpp_ttype type,
29317 required_token token_desc,
29318 location_t matching_location)
29319 {
29320 if (cp_lexer_next_token_is (parser->lexer, type))
29321 return cp_lexer_consume_token (parser->lexer);
29322 else
29323 {
29324 /* Output the MESSAGE -- unless we're parsing tentatively. */
29325 if (!cp_parser_simulate_error (parser))
29326 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
29327 matching_location);
29328 return NULL;
29329 }
29330 }
29331
29332 /* An error message is produced if the next token is not '>'.
29333 All further tokens are skipped until the desired token is
29334 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29335
29336 static void
29337 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
29338 {
29339 /* Current level of '< ... >'. */
29340 unsigned level = 0;
29341 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29342 unsigned nesting_depth = 0;
29343
29344 /* Are we ready, yet? If not, issue error message. */
29345 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
29346 return;
29347
29348 /* Skip tokens until the desired token is found. */
29349 while (true)
29350 {
29351 /* Peek at the next token. */
29352 switch (cp_lexer_peek_token (parser->lexer)->type)
29353 {
29354 case CPP_LESS:
29355 if (!nesting_depth)
29356 ++level;
29357 break;
29358
29359 case CPP_RSHIFT:
29360 if (cxx_dialect == cxx98)
29361 /* C++0x views the `>>' operator as two `>' tokens, but
29362 C++98 does not. */
29363 break;
29364 else if (!nesting_depth && level-- == 0)
29365 {
29366 /* We've hit a `>>' where the first `>' closes the
29367 template argument list, and the second `>' is
29368 spurious. Just consume the `>>' and stop; we've
29369 already produced at least one error. */
29370 cp_lexer_consume_token (parser->lexer);
29371 return;
29372 }
29373 /* Fall through for C++0x, so we handle the second `>' in
29374 the `>>'. */
29375 gcc_fallthrough ();
29376
29377 case CPP_GREATER:
29378 if (!nesting_depth && level-- == 0)
29379 {
29380 /* We've reached the token we want, consume it and stop. */
29381 cp_lexer_consume_token (parser->lexer);
29382 return;
29383 }
29384 break;
29385
29386 case CPP_OPEN_PAREN:
29387 case CPP_OPEN_SQUARE:
29388 ++nesting_depth;
29389 break;
29390
29391 case CPP_CLOSE_PAREN:
29392 case CPP_CLOSE_SQUARE:
29393 if (nesting_depth-- == 0)
29394 return;
29395 break;
29396
29397 case CPP_EOF:
29398 case CPP_PRAGMA_EOL:
29399 case CPP_SEMICOLON:
29400 case CPP_OPEN_BRACE:
29401 case CPP_CLOSE_BRACE:
29402 /* The '>' was probably forgotten, don't look further. */
29403 return;
29404
29405 default:
29406 break;
29407 }
29408
29409 /* Consume this token. */
29410 cp_lexer_consume_token (parser->lexer);
29411 }
29412 }
29413
29414 /* If the next token is the indicated keyword, consume it. Otherwise,
29415 issue an error message indicating that TOKEN_DESC was expected.
29416
29417 Returns the token consumed, if the token had the appropriate type.
29418 Otherwise, returns NULL. */
29419
29420 static cp_token *
29421 cp_parser_require_keyword (cp_parser* parser,
29422 enum rid keyword,
29423 required_token token_desc)
29424 {
29425 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
29426
29427 if (token && token->keyword != keyword)
29428 {
29429 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
29430 UNKNOWN_LOCATION);
29431 return NULL;
29432 }
29433
29434 return token;
29435 }
29436
29437 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29438 function-definition. */
29439
29440 static bool
29441 cp_parser_token_starts_function_definition_p (cp_token* token)
29442 {
29443 return (/* An ordinary function-body begins with an `{'. */
29444 token->type == CPP_OPEN_BRACE
29445 /* A ctor-initializer begins with a `:'. */
29446 || token->type == CPP_COLON
29447 /* A function-try-block begins with `try'. */
29448 || token->keyword == RID_TRY
29449 /* A function-transaction-block begins with `__transaction_atomic'
29450 or `__transaction_relaxed'. */
29451 || token->keyword == RID_TRANSACTION_ATOMIC
29452 || token->keyword == RID_TRANSACTION_RELAXED
29453 /* The named return value extension begins with `return'. */
29454 || token->keyword == RID_RETURN);
29455 }
29456
29457 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29458 definition. */
29459
29460 static bool
29461 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
29462 {
29463 cp_token *token;
29464
29465 token = cp_lexer_peek_token (parser->lexer);
29466 return (token->type == CPP_OPEN_BRACE
29467 || (token->type == CPP_COLON
29468 && !parser->colon_doesnt_start_class_def_p));
29469 }
29470
29471 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29472 C++0x) ending a template-argument. */
29473
29474 static bool
29475 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
29476 {
29477 cp_token *token;
29478
29479 token = cp_lexer_peek_token (parser->lexer);
29480 return (token->type == CPP_COMMA
29481 || token->type == CPP_GREATER
29482 || token->type == CPP_ELLIPSIS
29483 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
29484 }
29485
29486 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29487 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29488
29489 static bool
29490 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
29491 size_t n)
29492 {
29493 cp_token *token;
29494
29495 token = cp_lexer_peek_nth_token (parser->lexer, n);
29496 if (token->type == CPP_LESS)
29497 return true;
29498 /* Check for the sequence `<::' in the original code. It would be lexed as
29499 `[:', where `[' is a digraph, and there is no whitespace before
29500 `:'. */
29501 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
29502 {
29503 cp_token *token2;
29504 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
29505 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
29506 return true;
29507 }
29508 return false;
29509 }
29510
29511 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29512 or none_type otherwise. */
29513
29514 static enum tag_types
29515 cp_parser_token_is_class_key (cp_token* token)
29516 {
29517 switch (token->keyword)
29518 {
29519 case RID_CLASS:
29520 return class_type;
29521 case RID_STRUCT:
29522 return record_type;
29523 case RID_UNION:
29524 return union_type;
29525
29526 default:
29527 return none_type;
29528 }
29529 }
29530
29531 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29532 or none_type otherwise or if the token is null. */
29533
29534 static enum tag_types
29535 cp_parser_token_is_type_parameter_key (cp_token* token)
29536 {
29537 if (!token)
29538 return none_type;
29539
29540 switch (token->keyword)
29541 {
29542 case RID_CLASS:
29543 return class_type;
29544 case RID_TYPENAME:
29545 return typename_type;
29546
29547 default:
29548 return none_type;
29549 }
29550 }
29551
29552 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29553
29554 static void
29555 cp_parser_check_class_key (enum tag_types class_key, tree type)
29556 {
29557 if (type == error_mark_node)
29558 return;
29559 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29560 {
29561 if (permerror (input_location, "%qs tag used in naming %q#T",
29562 class_key == union_type ? "union"
29563 : class_key == record_type ? "struct" : "class",
29564 type))
29565 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29566 "%q#T was previously declared here", type);
29567 }
29568 }
29569
29570 /* Issue an error message if DECL is redeclared with different
29571 access than its original declaration [class.access.spec/3].
29572 This applies to nested classes, nested class templates and
29573 enumerations [class.mem/1]. */
29574
29575 static void
29576 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29577 {
29578 if (!decl
29579 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29580 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29581 return;
29582
29583 if ((TREE_PRIVATE (decl)
29584 != (current_access_specifier == access_private_node))
29585 || (TREE_PROTECTED (decl)
29586 != (current_access_specifier == access_protected_node)))
29587 error_at (location, "%qD redeclared with different access", decl);
29588 }
29589
29590 /* Look for the `template' keyword, as a syntactic disambiguator.
29591 Return TRUE iff it is present, in which case it will be
29592 consumed. */
29593
29594 static bool
29595 cp_parser_optional_template_keyword (cp_parser *parser)
29596 {
29597 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29598 {
29599 /* In C++98 the `template' keyword can only be used within templates;
29600 outside templates the parser can always figure out what is a
29601 template and what is not. In C++11, per the resolution of DR 468,
29602 `template' is allowed in cases where it is not strictly necessary. */
29603 if (!processing_template_decl
29604 && pedantic && cxx_dialect == cxx98)
29605 {
29606 cp_token *token = cp_lexer_peek_token (parser->lexer);
29607 pedwarn (token->location, OPT_Wpedantic,
29608 "in C++98 %<template%> (as a disambiguator) is only "
29609 "allowed within templates");
29610 /* If this part of the token stream is rescanned, the same
29611 error message would be generated. So, we purge the token
29612 from the stream. */
29613 cp_lexer_purge_token (parser->lexer);
29614 return false;
29615 }
29616 else
29617 {
29618 /* Consume the `template' keyword. */
29619 cp_lexer_consume_token (parser->lexer);
29620 return true;
29621 }
29622 }
29623 return false;
29624 }
29625
29626 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29627 set PARSER->SCOPE, and perform other related actions. */
29628
29629 static void
29630 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29631 {
29632 struct tree_check *check_value;
29633
29634 /* Get the stored value. */
29635 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29636 /* Set the scope from the stored value. */
29637 parser->scope = saved_checks_value (check_value);
29638 parser->qualifying_scope = check_value->qualifying_scope;
29639 parser->object_scope = NULL_TREE;
29640 }
29641
29642 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29643 encounter the end of a block before what we were looking for. */
29644
29645 static bool
29646 cp_parser_cache_group (cp_parser *parser,
29647 enum cpp_ttype end,
29648 unsigned depth)
29649 {
29650 while (true)
29651 {
29652 cp_token *token = cp_lexer_peek_token (parser->lexer);
29653
29654 /* Abort a parenthesized expression if we encounter a semicolon. */
29655 if ((end == CPP_CLOSE_PAREN || depth == 0)
29656 && token->type == CPP_SEMICOLON)
29657 return true;
29658 /* If we've reached the end of the file, stop. */
29659 if (token->type == CPP_EOF
29660 || (end != CPP_PRAGMA_EOL
29661 && token->type == CPP_PRAGMA_EOL))
29662 return true;
29663 if (token->type == CPP_CLOSE_BRACE && depth == 0)
29664 /* We've hit the end of an enclosing block, so there's been some
29665 kind of syntax error. */
29666 return true;
29667
29668 /* Consume the token. */
29669 cp_lexer_consume_token (parser->lexer);
29670 /* See if it starts a new group. */
29671 if (token->type == CPP_OPEN_BRACE)
29672 {
29673 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29674 /* In theory this should probably check end == '}', but
29675 cp_parser_save_member_function_body needs it to exit
29676 after either '}' or ')' when called with ')'. */
29677 if (depth == 0)
29678 return false;
29679 }
29680 else if (token->type == CPP_OPEN_PAREN)
29681 {
29682 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29683 if (depth == 0 && end == CPP_CLOSE_PAREN)
29684 return false;
29685 }
29686 else if (token->type == CPP_PRAGMA)
29687 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29688 else if (token->type == end)
29689 return false;
29690 }
29691 }
29692
29693 /* Like above, for caching a default argument or NSDMI. Both of these are
29694 terminated by a non-nested comma, but it can be unclear whether or not a
29695 comma is nested in a template argument list unless we do more parsing.
29696 In order to handle this ambiguity, when we encounter a ',' after a '<'
29697 we try to parse what follows as a parameter-declaration-list (in the
29698 case of a default argument) or a member-declarator (in the case of an
29699 NSDMI). If that succeeds, then we stop caching. */
29700
29701 static tree
29702 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29703 {
29704 unsigned depth = 0;
29705 int maybe_template_id = 0;
29706 cp_token *first_token;
29707 cp_token *token;
29708 tree default_argument;
29709
29710 /* Add tokens until we have processed the entire default
29711 argument. We add the range [first_token, token). */
29712 first_token = cp_lexer_peek_token (parser->lexer);
29713 if (first_token->type == CPP_OPEN_BRACE)
29714 {
29715 /* For list-initialization, this is straightforward. */
29716 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29717 token = cp_lexer_peek_token (parser->lexer);
29718 }
29719 else while (true)
29720 {
29721 bool done = false;
29722
29723 /* Peek at the next token. */
29724 token = cp_lexer_peek_token (parser->lexer);
29725 /* What we do depends on what token we have. */
29726 switch (token->type)
29727 {
29728 /* In valid code, a default argument must be
29729 immediately followed by a `,' `)', or `...'. */
29730 case CPP_COMMA:
29731 if (depth == 0 && maybe_template_id)
29732 {
29733 /* If we've seen a '<', we might be in a
29734 template-argument-list. Until Core issue 325 is
29735 resolved, we don't know how this situation ought
29736 to be handled, so try to DTRT. We check whether
29737 what comes after the comma is a valid parameter
29738 declaration list. If it is, then the comma ends
29739 the default argument; otherwise the default
29740 argument continues. */
29741 bool error = false;
29742 cp_token *peek;
29743
29744 /* Set ITALP so cp_parser_parameter_declaration_list
29745 doesn't decide to commit to this parse. */
29746 bool saved_italp = parser->in_template_argument_list_p;
29747 parser->in_template_argument_list_p = true;
29748
29749 cp_parser_parse_tentatively (parser);
29750
29751 if (nsdmi)
29752 {
29753 /* Parse declarators until we reach a non-comma or
29754 somthing that cannot be an initializer.
29755 Just checking whether we're looking at a single
29756 declarator is insufficient. Consider:
29757 int var = tuple<T,U>::x;
29758 The template parameter 'U' looks exactly like a
29759 declarator. */
29760 do
29761 {
29762 int ctor_dtor_or_conv_p;
29763 cp_lexer_consume_token (parser->lexer);
29764 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29765 CP_PARSER_FLAGS_NONE,
29766 &ctor_dtor_or_conv_p,
29767 /*parenthesized_p=*/NULL,
29768 /*member_p=*/true,
29769 /*friend_p=*/false,
29770 /*static_p=*/false);
29771 peek = cp_lexer_peek_token (parser->lexer);
29772 if (cp_parser_error_occurred (parser))
29773 break;
29774 }
29775 while (peek->type == CPP_COMMA);
29776 /* If we met an '=' or ';' then the original comma
29777 was the end of the NSDMI. Otherwise assume
29778 we're still in the NSDMI. */
29779 error = (peek->type != CPP_EQ
29780 && peek->type != CPP_SEMICOLON);
29781 }
29782 else
29783 {
29784 cp_lexer_consume_token (parser->lexer);
29785 begin_scope (sk_function_parms, NULL_TREE);
29786 tree t = cp_parser_parameter_declaration_list
29787 (parser, CP_PARSER_FLAGS_NONE);
29788 if (t == error_mark_node)
29789 error = true;
29790 pop_bindings_and_leave_scope ();
29791 }
29792 if (!cp_parser_error_occurred (parser) && !error)
29793 done = true;
29794 cp_parser_abort_tentative_parse (parser);
29795
29796 parser->in_template_argument_list_p = saved_italp;
29797 break;
29798 }
29799 /* FALLTHRU */
29800 case CPP_CLOSE_PAREN:
29801 case CPP_ELLIPSIS:
29802 /* If we run into a non-nested `;', `}', or `]',
29803 then the code is invalid -- but the default
29804 argument is certainly over. */
29805 case CPP_SEMICOLON:
29806 case CPP_CLOSE_BRACE:
29807 case CPP_CLOSE_SQUARE:
29808 if (depth == 0
29809 /* Handle correctly int n = sizeof ... ( p ); */
29810 && token->type != CPP_ELLIPSIS)
29811 done = true;
29812 /* Update DEPTH, if necessary. */
29813 else if (token->type == CPP_CLOSE_PAREN
29814 || token->type == CPP_CLOSE_BRACE
29815 || token->type == CPP_CLOSE_SQUARE)
29816 --depth;
29817 break;
29818
29819 case CPP_OPEN_PAREN:
29820 case CPP_OPEN_SQUARE:
29821 case CPP_OPEN_BRACE:
29822 ++depth;
29823 break;
29824
29825 case CPP_LESS:
29826 if (depth == 0)
29827 /* This might be the comparison operator, or it might
29828 start a template argument list. */
29829 ++maybe_template_id;
29830 break;
29831
29832 case CPP_RSHIFT:
29833 if (cxx_dialect == cxx98)
29834 break;
29835 /* Fall through for C++0x, which treats the `>>'
29836 operator like two `>' tokens in certain
29837 cases. */
29838 gcc_fallthrough ();
29839
29840 case CPP_GREATER:
29841 if (depth == 0)
29842 {
29843 /* This might be an operator, or it might close a
29844 template argument list. But if a previous '<'
29845 started a template argument list, this will have
29846 closed it, so we can't be in one anymore. */
29847 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29848 if (maybe_template_id < 0)
29849 maybe_template_id = 0;
29850 }
29851 break;
29852
29853 /* If we run out of tokens, issue an error message. */
29854 case CPP_EOF:
29855 case CPP_PRAGMA_EOL:
29856 error_at (token->location, "file ends in default argument");
29857 return error_mark_node;
29858
29859 case CPP_NAME:
29860 case CPP_SCOPE:
29861 /* In these cases, we should look for template-ids.
29862 For example, if the default argument is
29863 `X<int, double>()', we need to do name lookup to
29864 figure out whether or not `X' is a template; if
29865 so, the `,' does not end the default argument.
29866
29867 That is not yet done. */
29868 break;
29869
29870 default:
29871 break;
29872 }
29873
29874 /* If we've reached the end, stop. */
29875 if (done)
29876 break;
29877
29878 /* Add the token to the token block. */
29879 token = cp_lexer_consume_token (parser->lexer);
29880 }
29881
29882 /* Create a DEFAULT_ARG to represent the unparsed default
29883 argument. */
29884 default_argument = make_node (DEFAULT_ARG);
29885 DEFARG_TOKENS (default_argument)
29886 = cp_token_cache_new (first_token, token);
29887 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29888
29889 return default_argument;
29890 }
29891
29892 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29893
29894 location_t
29895 defarg_location (tree default_argument)
29896 {
29897 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29898 location_t start = tokens->first->location;
29899 location_t end = tokens->last->location;
29900 return make_location (start, start, end);
29901 }
29902
29903 /* Begin parsing tentatively. We always save tokens while parsing
29904 tentatively so that if the tentative parsing fails we can restore the
29905 tokens. */
29906
29907 static void
29908 cp_parser_parse_tentatively (cp_parser* parser)
29909 {
29910 /* Enter a new parsing context. */
29911 parser->context = cp_parser_context_new (parser->context);
29912 /* Begin saving tokens. */
29913 cp_lexer_save_tokens (parser->lexer);
29914 /* In order to avoid repetitive access control error messages,
29915 access checks are queued up until we are no longer parsing
29916 tentatively. */
29917 push_deferring_access_checks (dk_deferred);
29918 }
29919
29920 /* Commit to the currently active tentative parse. */
29921
29922 static void
29923 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29924 {
29925 cp_parser_context *context;
29926 cp_lexer *lexer;
29927
29928 /* Mark all of the levels as committed. */
29929 lexer = parser->lexer;
29930 for (context = parser->context; context->next; context = context->next)
29931 {
29932 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29933 break;
29934 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29935 while (!cp_lexer_saving_tokens (lexer))
29936 lexer = lexer->next;
29937 cp_lexer_commit_tokens (lexer);
29938 }
29939 }
29940
29941 /* Commit to the topmost currently active tentative parse.
29942
29943 Note that this function shouldn't be called when there are
29944 irreversible side-effects while in a tentative state. For
29945 example, we shouldn't create a permanent entry in the symbol
29946 table, or issue an error message that might not apply if the
29947 tentative parse is aborted. */
29948
29949 static void
29950 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29951 {
29952 cp_parser_context *context = parser->context;
29953 cp_lexer *lexer = parser->lexer;
29954
29955 if (context)
29956 {
29957 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29958 return;
29959 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29960
29961 while (!cp_lexer_saving_tokens (lexer))
29962 lexer = lexer->next;
29963 cp_lexer_commit_tokens (lexer);
29964 }
29965 }
29966
29967 /* Abort the currently active tentative parse. All consumed tokens
29968 will be rolled back, and no diagnostics will be issued. */
29969
29970 static void
29971 cp_parser_abort_tentative_parse (cp_parser* parser)
29972 {
29973 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29974 || errorcount > 0);
29975 cp_parser_simulate_error (parser);
29976 /* Now, pretend that we want to see if the construct was
29977 successfully parsed. */
29978 cp_parser_parse_definitely (parser);
29979 }
29980
29981 /* Stop parsing tentatively. If a parse error has occurred, restore the
29982 token stream. Otherwise, commit to the tokens we have consumed.
29983 Returns true if no error occurred; false otherwise. */
29984
29985 static bool
29986 cp_parser_parse_definitely (cp_parser* parser)
29987 {
29988 bool error_occurred;
29989 cp_parser_context *context;
29990
29991 /* Remember whether or not an error occurred, since we are about to
29992 destroy that information. */
29993 error_occurred = cp_parser_error_occurred (parser);
29994 /* Remove the topmost context from the stack. */
29995 context = parser->context;
29996 parser->context = context->next;
29997 /* If no parse errors occurred, commit to the tentative parse. */
29998 if (!error_occurred)
29999 {
30000 /* Commit to the tokens read tentatively, unless that was
30001 already done. */
30002 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
30003 cp_lexer_commit_tokens (parser->lexer);
30004
30005 pop_to_parent_deferring_access_checks ();
30006 }
30007 /* Otherwise, if errors occurred, roll back our state so that things
30008 are just as they were before we began the tentative parse. */
30009 else
30010 {
30011 cp_lexer_rollback_tokens (parser->lexer);
30012 pop_deferring_access_checks ();
30013 }
30014 /* Add the context to the front of the free list. */
30015 context->next = cp_parser_context_free_list;
30016 cp_parser_context_free_list = context;
30017
30018 return !error_occurred;
30019 }
30020
30021 /* Returns true if we are parsing tentatively and are not committed to
30022 this tentative parse. */
30023
30024 static bool
30025 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
30026 {
30027 return (cp_parser_parsing_tentatively (parser)
30028 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
30029 }
30030
30031 /* Returns nonzero iff an error has occurred during the most recent
30032 tentative parse. */
30033
30034 static bool
30035 cp_parser_error_occurred (cp_parser* parser)
30036 {
30037 return (cp_parser_parsing_tentatively (parser)
30038 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
30039 }
30040
30041 /* Returns nonzero if GNU extensions are allowed. */
30042
30043 static bool
30044 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
30045 {
30046 return parser->allow_gnu_extensions_p;
30047 }
30048 \f
30049 /* Objective-C++ Productions */
30050
30051
30052 /* Parse an Objective-C expression, which feeds into a primary-expression
30053 above.
30054
30055 objc-expression:
30056 objc-message-expression
30057 objc-string-literal
30058 objc-encode-expression
30059 objc-protocol-expression
30060 objc-selector-expression
30061
30062 Returns a tree representation of the expression. */
30063
30064 static cp_expr
30065 cp_parser_objc_expression (cp_parser* parser)
30066 {
30067 /* Try to figure out what kind of declaration is present. */
30068 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30069
30070 switch (kwd->type)
30071 {
30072 case CPP_OPEN_SQUARE:
30073 return cp_parser_objc_message_expression (parser);
30074
30075 case CPP_OBJC_STRING:
30076 kwd = cp_lexer_consume_token (parser->lexer);
30077 return objc_build_string_object (kwd->u.value);
30078
30079 case CPP_KEYWORD:
30080 switch (kwd->keyword)
30081 {
30082 case RID_AT_ENCODE:
30083 return cp_parser_objc_encode_expression (parser);
30084
30085 case RID_AT_PROTOCOL:
30086 return cp_parser_objc_protocol_expression (parser);
30087
30088 case RID_AT_SELECTOR:
30089 return cp_parser_objc_selector_expression (parser);
30090
30091 default:
30092 break;
30093 }
30094 /* FALLTHRU */
30095 default:
30096 error_at (kwd->location,
30097 "misplaced %<@%D%> Objective-C++ construct",
30098 kwd->u.value);
30099 cp_parser_skip_to_end_of_block_or_statement (parser);
30100 }
30101
30102 return error_mark_node;
30103 }
30104
30105 /* Parse an Objective-C message expression.
30106
30107 objc-message-expression:
30108 [ objc-message-receiver objc-message-args ]
30109
30110 Returns a representation of an Objective-C message. */
30111
30112 static tree
30113 cp_parser_objc_message_expression (cp_parser* parser)
30114 {
30115 tree receiver, messageargs;
30116
30117 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30118 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
30119 receiver = cp_parser_objc_message_receiver (parser);
30120 messageargs = cp_parser_objc_message_args (parser);
30121 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
30122 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
30123
30124 tree result = objc_build_message_expr (receiver, messageargs);
30125
30126 /* Construct a location e.g.
30127 [self func1:5]
30128 ^~~~~~~~~~~~~~
30129 ranging from the '[' to the ']', with the caret at the start. */
30130 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
30131 protected_set_expr_location (result, combined_loc);
30132
30133 return result;
30134 }
30135
30136 /* Parse an objc-message-receiver.
30137
30138 objc-message-receiver:
30139 expression
30140 simple-type-specifier
30141
30142 Returns a representation of the type or expression. */
30143
30144 static tree
30145 cp_parser_objc_message_receiver (cp_parser* parser)
30146 {
30147 tree rcv;
30148
30149 /* An Objective-C message receiver may be either (1) a type
30150 or (2) an expression. */
30151 cp_parser_parse_tentatively (parser);
30152 rcv = cp_parser_expression (parser);
30153
30154 /* If that worked out, fine. */
30155 if (cp_parser_parse_definitely (parser))
30156 return rcv;
30157
30158 cp_parser_parse_tentatively (parser);
30159 rcv = cp_parser_simple_type_specifier (parser,
30160 /*decl_specs=*/NULL,
30161 CP_PARSER_FLAGS_NONE);
30162
30163 if (cp_parser_parse_definitely (parser))
30164 return objc_get_class_reference (rcv);
30165
30166 cp_parser_error (parser, "objective-c++ message receiver expected");
30167 return error_mark_node;
30168 }
30169
30170 /* Parse the arguments and selectors comprising an Objective-C message.
30171
30172 objc-message-args:
30173 objc-selector
30174 objc-selector-args
30175 objc-selector-args , objc-comma-args
30176
30177 objc-selector-args:
30178 objc-selector [opt] : assignment-expression
30179 objc-selector-args objc-selector [opt] : assignment-expression
30180
30181 objc-comma-args:
30182 assignment-expression
30183 objc-comma-args , assignment-expression
30184
30185 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
30186 selector arguments and TREE_VALUE containing a list of comma
30187 arguments. */
30188
30189 static tree
30190 cp_parser_objc_message_args (cp_parser* parser)
30191 {
30192 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
30193 bool maybe_unary_selector_p = true;
30194 cp_token *token = cp_lexer_peek_token (parser->lexer);
30195
30196 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30197 {
30198 tree selector = NULL_TREE, arg;
30199
30200 if (token->type != CPP_COLON)
30201 selector = cp_parser_objc_selector (parser);
30202
30203 /* Detect if we have a unary selector. */
30204 if (maybe_unary_selector_p
30205 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30206 return build_tree_list (selector, NULL_TREE);
30207
30208 maybe_unary_selector_p = false;
30209 cp_parser_require (parser, CPP_COLON, RT_COLON);
30210 arg = cp_parser_assignment_expression (parser);
30211
30212 sel_args
30213 = chainon (sel_args,
30214 build_tree_list (selector, arg));
30215
30216 token = cp_lexer_peek_token (parser->lexer);
30217 }
30218
30219 /* Handle non-selector arguments, if any. */
30220 while (token->type == CPP_COMMA)
30221 {
30222 tree arg;
30223
30224 cp_lexer_consume_token (parser->lexer);
30225 arg = cp_parser_assignment_expression (parser);
30226
30227 addl_args
30228 = chainon (addl_args,
30229 build_tree_list (NULL_TREE, arg));
30230
30231 token = cp_lexer_peek_token (parser->lexer);
30232 }
30233
30234 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
30235 {
30236 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
30237 return build_tree_list (error_mark_node, error_mark_node);
30238 }
30239
30240 return build_tree_list (sel_args, addl_args);
30241 }
30242
30243 /* Parse an Objective-C encode expression.
30244
30245 objc-encode-expression:
30246 @encode objc-typename
30247
30248 Returns an encoded representation of the type argument. */
30249
30250 static cp_expr
30251 cp_parser_objc_encode_expression (cp_parser* parser)
30252 {
30253 tree type;
30254 cp_token *token;
30255 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30256
30257 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
30258 matching_parens parens;
30259 parens.require_open (parser);
30260 token = cp_lexer_peek_token (parser->lexer);
30261 type = complete_type (cp_parser_type_id (parser));
30262 parens.require_close (parser);
30263
30264 if (!type)
30265 {
30266 error_at (token->location,
30267 "%<@encode%> must specify a type as an argument");
30268 return error_mark_node;
30269 }
30270
30271 /* This happens if we find @encode(T) (where T is a template
30272 typename or something dependent on a template typename) when
30273 parsing a template. In that case, we can't compile it
30274 immediately, but we rather create an AT_ENCODE_EXPR which will
30275 need to be instantiated when the template is used.
30276 */
30277 if (dependent_type_p (type))
30278 {
30279 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
30280 TREE_READONLY (value) = 1;
30281 return value;
30282 }
30283
30284
30285 /* Build a location of the form:
30286 @encode(int)
30287 ^~~~~~~~~~~~
30288 with caret==start at the @ token, finishing at the close paren. */
30289 location_t combined_loc
30290 = make_location (start_loc, start_loc,
30291 cp_lexer_previous_token (parser->lexer)->location);
30292
30293 return cp_expr (objc_build_encode_expr (type), combined_loc);
30294 }
30295
30296 /* Parse an Objective-C @defs expression. */
30297
30298 static tree
30299 cp_parser_objc_defs_expression (cp_parser *parser)
30300 {
30301 tree name;
30302
30303 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
30304 matching_parens parens;
30305 parens.require_open (parser);
30306 name = cp_parser_identifier (parser);
30307 parens.require_close (parser);
30308
30309 return objc_get_class_ivars (name);
30310 }
30311
30312 /* Parse an Objective-C protocol expression.
30313
30314 objc-protocol-expression:
30315 @protocol ( identifier )
30316
30317 Returns a representation of the protocol expression. */
30318
30319 static tree
30320 cp_parser_objc_protocol_expression (cp_parser* parser)
30321 {
30322 tree proto;
30323 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30324
30325 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30326 matching_parens parens;
30327 parens.require_open (parser);
30328 proto = cp_parser_identifier (parser);
30329 parens.require_close (parser);
30330
30331 /* Build a location of the form:
30332 @protocol(prot)
30333 ^~~~~~~~~~~~~~~
30334 with caret==start at the @ token, finishing at the close paren. */
30335 location_t combined_loc
30336 = make_location (start_loc, start_loc,
30337 cp_lexer_previous_token (parser->lexer)->location);
30338 tree result = objc_build_protocol_expr (proto);
30339 protected_set_expr_location (result, combined_loc);
30340 return result;
30341 }
30342
30343 /* Parse an Objective-C selector expression.
30344
30345 objc-selector-expression:
30346 @selector ( objc-method-signature )
30347
30348 objc-method-signature:
30349 objc-selector
30350 objc-selector-seq
30351
30352 objc-selector-seq:
30353 objc-selector :
30354 objc-selector-seq objc-selector :
30355
30356 Returns a representation of the method selector. */
30357
30358 static tree
30359 cp_parser_objc_selector_expression (cp_parser* parser)
30360 {
30361 tree sel_seq = NULL_TREE;
30362 bool maybe_unary_selector_p = true;
30363 cp_token *token;
30364 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30365
30366 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
30367 matching_parens parens;
30368 parens.require_open (parser);
30369 token = cp_lexer_peek_token (parser->lexer);
30370
30371 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
30372 || token->type == CPP_SCOPE)
30373 {
30374 tree selector = NULL_TREE;
30375
30376 if (token->type != CPP_COLON
30377 || token->type == CPP_SCOPE)
30378 selector = cp_parser_objc_selector (parser);
30379
30380 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
30381 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
30382 {
30383 /* Detect if we have a unary selector. */
30384 if (maybe_unary_selector_p)
30385 {
30386 sel_seq = selector;
30387 goto finish_selector;
30388 }
30389 else
30390 {
30391 cp_parser_error (parser, "expected %<:%>");
30392 }
30393 }
30394 maybe_unary_selector_p = false;
30395 token = cp_lexer_consume_token (parser->lexer);
30396
30397 if (token->type == CPP_SCOPE)
30398 {
30399 sel_seq
30400 = chainon (sel_seq,
30401 build_tree_list (selector, NULL_TREE));
30402 sel_seq
30403 = chainon (sel_seq,
30404 build_tree_list (NULL_TREE, NULL_TREE));
30405 }
30406 else
30407 sel_seq
30408 = chainon (sel_seq,
30409 build_tree_list (selector, NULL_TREE));
30410
30411 token = cp_lexer_peek_token (parser->lexer);
30412 }
30413
30414 finish_selector:
30415 parens.require_close (parser);
30416
30417
30418 /* Build a location of the form:
30419 @selector(func)
30420 ^~~~~~~~~~~~~~~
30421 with caret==start at the @ token, finishing at the close paren. */
30422 location_t combined_loc
30423 = make_location (loc, loc,
30424 cp_lexer_previous_token (parser->lexer)->location);
30425 tree result = objc_build_selector_expr (combined_loc, sel_seq);
30426 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30427 protected_set_expr_location (result, combined_loc);
30428 return result;
30429 }
30430
30431 /* Parse a list of identifiers.
30432
30433 objc-identifier-list:
30434 identifier
30435 objc-identifier-list , identifier
30436
30437 Returns a TREE_LIST of identifier nodes. */
30438
30439 static tree
30440 cp_parser_objc_identifier_list (cp_parser* parser)
30441 {
30442 tree identifier;
30443 tree list;
30444 cp_token *sep;
30445
30446 identifier = cp_parser_identifier (parser);
30447 if (identifier == error_mark_node)
30448 return error_mark_node;
30449
30450 list = build_tree_list (NULL_TREE, identifier);
30451 sep = cp_lexer_peek_token (parser->lexer);
30452
30453 while (sep->type == CPP_COMMA)
30454 {
30455 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30456 identifier = cp_parser_identifier (parser);
30457 if (identifier == error_mark_node)
30458 return list;
30459
30460 list = chainon (list, build_tree_list (NULL_TREE,
30461 identifier));
30462 sep = cp_lexer_peek_token (parser->lexer);
30463 }
30464
30465 return list;
30466 }
30467
30468 /* Parse an Objective-C alias declaration.
30469
30470 objc-alias-declaration:
30471 @compatibility_alias identifier identifier ;
30472
30473 This function registers the alias mapping with the Objective-C front end.
30474 It returns nothing. */
30475
30476 static void
30477 cp_parser_objc_alias_declaration (cp_parser* parser)
30478 {
30479 tree alias, orig;
30480
30481 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
30482 alias = cp_parser_identifier (parser);
30483 orig = cp_parser_identifier (parser);
30484 objc_declare_alias (alias, orig);
30485 cp_parser_consume_semicolon_at_end_of_statement (parser);
30486 }
30487
30488 /* Parse an Objective-C class forward-declaration.
30489
30490 objc-class-declaration:
30491 @class objc-identifier-list ;
30492
30493 The function registers the forward declarations with the Objective-C
30494 front end. It returns nothing. */
30495
30496 static void
30497 cp_parser_objc_class_declaration (cp_parser* parser)
30498 {
30499 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
30500 while (true)
30501 {
30502 tree id;
30503
30504 id = cp_parser_identifier (parser);
30505 if (id == error_mark_node)
30506 break;
30507
30508 objc_declare_class (id);
30509
30510 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30511 cp_lexer_consume_token (parser->lexer);
30512 else
30513 break;
30514 }
30515 cp_parser_consume_semicolon_at_end_of_statement (parser);
30516 }
30517
30518 /* Parse a list of Objective-C protocol references.
30519
30520 objc-protocol-refs-opt:
30521 objc-protocol-refs [opt]
30522
30523 objc-protocol-refs:
30524 < objc-identifier-list >
30525
30526 Returns a TREE_LIST of identifiers, if any. */
30527
30528 static tree
30529 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30530 {
30531 tree protorefs = NULL_TREE;
30532
30533 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30534 {
30535 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
30536 protorefs = cp_parser_objc_identifier_list (parser);
30537 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30538 }
30539
30540 return protorefs;
30541 }
30542
30543 /* Parse a Objective-C visibility specification. */
30544
30545 static void
30546 cp_parser_objc_visibility_spec (cp_parser* parser)
30547 {
30548 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30549
30550 switch (vis->keyword)
30551 {
30552 case RID_AT_PRIVATE:
30553 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30554 break;
30555 case RID_AT_PROTECTED:
30556 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30557 break;
30558 case RID_AT_PUBLIC:
30559 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30560 break;
30561 case RID_AT_PACKAGE:
30562 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30563 break;
30564 default:
30565 return;
30566 }
30567
30568 /* Eat '@private'/'@protected'/'@public'. */
30569 cp_lexer_consume_token (parser->lexer);
30570 }
30571
30572 /* Parse an Objective-C method type. Return 'true' if it is a class
30573 (+) method, and 'false' if it is an instance (-) method. */
30574
30575 static inline bool
30576 cp_parser_objc_method_type (cp_parser* parser)
30577 {
30578 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30579 return true;
30580 else
30581 return false;
30582 }
30583
30584 /* Parse an Objective-C protocol qualifier. */
30585
30586 static tree
30587 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30588 {
30589 tree quals = NULL_TREE, node;
30590 cp_token *token = cp_lexer_peek_token (parser->lexer);
30591
30592 node = token->u.value;
30593
30594 while (node && identifier_p (node)
30595 && (node == ridpointers [(int) RID_IN]
30596 || node == ridpointers [(int) RID_OUT]
30597 || node == ridpointers [(int) RID_INOUT]
30598 || node == ridpointers [(int) RID_BYCOPY]
30599 || node == ridpointers [(int) RID_BYREF]
30600 || node == ridpointers [(int) RID_ONEWAY]))
30601 {
30602 quals = tree_cons (NULL_TREE, node, quals);
30603 cp_lexer_consume_token (parser->lexer);
30604 token = cp_lexer_peek_token (parser->lexer);
30605 node = token->u.value;
30606 }
30607
30608 return quals;
30609 }
30610
30611 /* Parse an Objective-C typename. */
30612
30613 static tree
30614 cp_parser_objc_typename (cp_parser* parser)
30615 {
30616 tree type_name = NULL_TREE;
30617
30618 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30619 {
30620 tree proto_quals, cp_type = NULL_TREE;
30621
30622 matching_parens parens;
30623 parens.consume_open (parser); /* Eat '('. */
30624 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30625
30626 /* An ObjC type name may consist of just protocol qualifiers, in which
30627 case the type shall default to 'id'. */
30628 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30629 {
30630 cp_type = cp_parser_type_id (parser);
30631
30632 /* If the type could not be parsed, an error has already
30633 been produced. For error recovery, behave as if it had
30634 not been specified, which will use the default type
30635 'id'. */
30636 if (cp_type == error_mark_node)
30637 {
30638 cp_type = NULL_TREE;
30639 /* We need to skip to the closing parenthesis as
30640 cp_parser_type_id() does not seem to do it for
30641 us. */
30642 cp_parser_skip_to_closing_parenthesis (parser,
30643 /*recovering=*/true,
30644 /*or_comma=*/false,
30645 /*consume_paren=*/false);
30646 }
30647 }
30648
30649 parens.require_close (parser);
30650 type_name = build_tree_list (proto_quals, cp_type);
30651 }
30652
30653 return type_name;
30654 }
30655
30656 /* Check to see if TYPE refers to an Objective-C selector name. */
30657
30658 static bool
30659 cp_parser_objc_selector_p (enum cpp_ttype type)
30660 {
30661 return (type == CPP_NAME || type == CPP_KEYWORD
30662 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30663 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30664 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30665 || type == CPP_XOR || type == CPP_XOR_EQ);
30666 }
30667
30668 /* Parse an Objective-C selector. */
30669
30670 static tree
30671 cp_parser_objc_selector (cp_parser* parser)
30672 {
30673 cp_token *token = cp_lexer_consume_token (parser->lexer);
30674
30675 if (!cp_parser_objc_selector_p (token->type))
30676 {
30677 error_at (token->location, "invalid Objective-C++ selector name");
30678 return error_mark_node;
30679 }
30680
30681 /* C++ operator names are allowed to appear in ObjC selectors. */
30682 switch (token->type)
30683 {
30684 case CPP_AND_AND: return get_identifier ("and");
30685 case CPP_AND_EQ: return get_identifier ("and_eq");
30686 case CPP_AND: return get_identifier ("bitand");
30687 case CPP_OR: return get_identifier ("bitor");
30688 case CPP_COMPL: return get_identifier ("compl");
30689 case CPP_NOT: return get_identifier ("not");
30690 case CPP_NOT_EQ: return get_identifier ("not_eq");
30691 case CPP_OR_OR: return get_identifier ("or");
30692 case CPP_OR_EQ: return get_identifier ("or_eq");
30693 case CPP_XOR: return get_identifier ("xor");
30694 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30695 default: return token->u.value;
30696 }
30697 }
30698
30699 /* Parse an Objective-C params list. */
30700
30701 static tree
30702 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30703 {
30704 tree params = NULL_TREE;
30705 bool maybe_unary_selector_p = true;
30706 cp_token *token = cp_lexer_peek_token (parser->lexer);
30707
30708 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30709 {
30710 tree selector = NULL_TREE, type_name, identifier;
30711 tree parm_attr = NULL_TREE;
30712
30713 if (token->keyword == RID_ATTRIBUTE)
30714 break;
30715
30716 if (token->type != CPP_COLON)
30717 selector = cp_parser_objc_selector (parser);
30718
30719 /* Detect if we have a unary selector. */
30720 if (maybe_unary_selector_p
30721 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30722 {
30723 params = selector; /* Might be followed by attributes. */
30724 break;
30725 }
30726
30727 maybe_unary_selector_p = false;
30728 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30729 {
30730 /* Something went quite wrong. There should be a colon
30731 here, but there is not. Stop parsing parameters. */
30732 break;
30733 }
30734 type_name = cp_parser_objc_typename (parser);
30735 /* New ObjC allows attributes on parameters too. */
30736 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30737 parm_attr = cp_parser_attributes_opt (parser);
30738 identifier = cp_parser_identifier (parser);
30739
30740 params
30741 = chainon (params,
30742 objc_build_keyword_decl (selector,
30743 type_name,
30744 identifier,
30745 parm_attr));
30746
30747 token = cp_lexer_peek_token (parser->lexer);
30748 }
30749
30750 if (params == NULL_TREE)
30751 {
30752 cp_parser_error (parser, "objective-c++ method declaration is expected");
30753 return error_mark_node;
30754 }
30755
30756 /* We allow tail attributes for the method. */
30757 if (token->keyword == RID_ATTRIBUTE)
30758 {
30759 *attributes = cp_parser_attributes_opt (parser);
30760 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30761 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30762 return params;
30763 cp_parser_error (parser,
30764 "method attributes must be specified at the end");
30765 return error_mark_node;
30766 }
30767
30768 if (params == NULL_TREE)
30769 {
30770 cp_parser_error (parser, "objective-c++ method declaration is expected");
30771 return error_mark_node;
30772 }
30773 return params;
30774 }
30775
30776 /* Parse the non-keyword Objective-C params. */
30777
30778 static tree
30779 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30780 tree* attributes)
30781 {
30782 tree params = make_node (TREE_LIST);
30783 cp_token *token = cp_lexer_peek_token (parser->lexer);
30784 *ellipsisp = false; /* Initially, assume no ellipsis. */
30785
30786 while (token->type == CPP_COMMA)
30787 {
30788 cp_parameter_declarator *parmdecl;
30789 tree parm;
30790
30791 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30792 token = cp_lexer_peek_token (parser->lexer);
30793
30794 if (token->type == CPP_ELLIPSIS)
30795 {
30796 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30797 *ellipsisp = true;
30798 token = cp_lexer_peek_token (parser->lexer);
30799 break;
30800 }
30801
30802 /* TODO: parse attributes for tail parameters. */
30803 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
30804 false, NULL);
30805 parm = grokdeclarator (parmdecl->declarator,
30806 &parmdecl->decl_specifiers,
30807 PARM, /*initialized=*/0,
30808 /*attrlist=*/NULL);
30809
30810 chainon (params, build_tree_list (NULL_TREE, parm));
30811 token = cp_lexer_peek_token (parser->lexer);
30812 }
30813
30814 /* We allow tail attributes for the method. */
30815 if (token->keyword == RID_ATTRIBUTE)
30816 {
30817 if (*attributes == NULL_TREE)
30818 {
30819 *attributes = cp_parser_attributes_opt (parser);
30820 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30821 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30822 return params;
30823 }
30824 else
30825 /* We have an error, but parse the attributes, so that we can
30826 carry on. */
30827 *attributes = cp_parser_attributes_opt (parser);
30828
30829 cp_parser_error (parser,
30830 "method attributes must be specified at the end");
30831 return error_mark_node;
30832 }
30833
30834 return params;
30835 }
30836
30837 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30838
30839 static void
30840 cp_parser_objc_interstitial_code (cp_parser* parser)
30841 {
30842 cp_token *token = cp_lexer_peek_token (parser->lexer);
30843
30844 /* If the next token is `extern' and the following token is a string
30845 literal, then we have a linkage specification. */
30846 if (token->keyword == RID_EXTERN
30847 && cp_parser_is_pure_string_literal
30848 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30849 cp_parser_linkage_specification (parser);
30850 /* Handle #pragma, if any. */
30851 else if (token->type == CPP_PRAGMA)
30852 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30853 /* Allow stray semicolons. */
30854 else if (token->type == CPP_SEMICOLON)
30855 cp_lexer_consume_token (parser->lexer);
30856 /* Mark methods as optional or required, when building protocols. */
30857 else if (token->keyword == RID_AT_OPTIONAL)
30858 {
30859 cp_lexer_consume_token (parser->lexer);
30860 objc_set_method_opt (true);
30861 }
30862 else if (token->keyword == RID_AT_REQUIRED)
30863 {
30864 cp_lexer_consume_token (parser->lexer);
30865 objc_set_method_opt (false);
30866 }
30867 else if (token->keyword == RID_NAMESPACE)
30868 cp_parser_namespace_definition (parser);
30869 /* Other stray characters must generate errors. */
30870 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30871 {
30872 cp_lexer_consume_token (parser->lexer);
30873 error ("stray %qs between Objective-C++ methods",
30874 token->type == CPP_OPEN_BRACE ? "{" : "}");
30875 }
30876 /* Finally, try to parse a block-declaration, or a function-definition. */
30877 else
30878 cp_parser_block_declaration (parser, /*statement_p=*/false);
30879 }
30880
30881 /* Parse a method signature. */
30882
30883 static tree
30884 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30885 {
30886 tree rettype, kwdparms, optparms;
30887 bool ellipsis = false;
30888 bool is_class_method;
30889
30890 is_class_method = cp_parser_objc_method_type (parser);
30891 rettype = cp_parser_objc_typename (parser);
30892 *attributes = NULL_TREE;
30893 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30894 if (kwdparms == error_mark_node)
30895 return error_mark_node;
30896 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30897 if (optparms == error_mark_node)
30898 return error_mark_node;
30899
30900 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30901 }
30902
30903 static bool
30904 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30905 {
30906 tree tattr;
30907 cp_lexer_save_tokens (parser->lexer);
30908 tattr = cp_parser_attributes_opt (parser);
30909 gcc_assert (tattr) ;
30910
30911 /* If the attributes are followed by a method introducer, this is not allowed.
30912 Dump the attributes and flag the situation. */
30913 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30914 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30915 return true;
30916
30917 /* Otherwise, the attributes introduce some interstitial code, possibly so
30918 rewind to allow that check. */
30919 cp_lexer_rollback_tokens (parser->lexer);
30920 return false;
30921 }
30922
30923 /* Parse an Objective-C method prototype list. */
30924
30925 static void
30926 cp_parser_objc_method_prototype_list (cp_parser* parser)
30927 {
30928 cp_token *token = cp_lexer_peek_token (parser->lexer);
30929
30930 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30931 {
30932 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30933 {
30934 tree attributes, sig;
30935 bool is_class_method;
30936 if (token->type == CPP_PLUS)
30937 is_class_method = true;
30938 else
30939 is_class_method = false;
30940 sig = cp_parser_objc_method_signature (parser, &attributes);
30941 if (sig == error_mark_node)
30942 {
30943 cp_parser_skip_to_end_of_block_or_statement (parser);
30944 token = cp_lexer_peek_token (parser->lexer);
30945 continue;
30946 }
30947 objc_add_method_declaration (is_class_method, sig, attributes);
30948 cp_parser_consume_semicolon_at_end_of_statement (parser);
30949 }
30950 else if (token->keyword == RID_AT_PROPERTY)
30951 cp_parser_objc_at_property_declaration (parser);
30952 else if (token->keyword == RID_ATTRIBUTE
30953 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30954 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30955 OPT_Wattributes,
30956 "prefix attributes are ignored for methods");
30957 else
30958 /* Allow for interspersed non-ObjC++ code. */
30959 cp_parser_objc_interstitial_code (parser);
30960
30961 token = cp_lexer_peek_token (parser->lexer);
30962 }
30963
30964 if (token->type != CPP_EOF)
30965 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30966 else
30967 cp_parser_error (parser, "expected %<@end%>");
30968
30969 objc_finish_interface ();
30970 }
30971
30972 /* Parse an Objective-C method definition list. */
30973
30974 static void
30975 cp_parser_objc_method_definition_list (cp_parser* parser)
30976 {
30977 cp_token *token = cp_lexer_peek_token (parser->lexer);
30978
30979 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30980 {
30981 tree meth;
30982
30983 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30984 {
30985 cp_token *ptk;
30986 tree sig, attribute;
30987 bool is_class_method;
30988 if (token->type == CPP_PLUS)
30989 is_class_method = true;
30990 else
30991 is_class_method = false;
30992 push_deferring_access_checks (dk_deferred);
30993 sig = cp_parser_objc_method_signature (parser, &attribute);
30994 if (sig == error_mark_node)
30995 {
30996 cp_parser_skip_to_end_of_block_or_statement (parser);
30997 token = cp_lexer_peek_token (parser->lexer);
30998 continue;
30999 }
31000 objc_start_method_definition (is_class_method, sig, attribute,
31001 NULL_TREE);
31002
31003 /* For historical reasons, we accept an optional semicolon. */
31004 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31005 cp_lexer_consume_token (parser->lexer);
31006
31007 ptk = cp_lexer_peek_token (parser->lexer);
31008 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
31009 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
31010 {
31011 perform_deferred_access_checks (tf_warning_or_error);
31012 stop_deferring_access_checks ();
31013 meth = cp_parser_function_definition_after_declarator (parser,
31014 false);
31015 pop_deferring_access_checks ();
31016 objc_finish_method_definition (meth);
31017 }
31018 }
31019 /* The following case will be removed once @synthesize is
31020 completely implemented. */
31021 else if (token->keyword == RID_AT_PROPERTY)
31022 cp_parser_objc_at_property_declaration (parser);
31023 else if (token->keyword == RID_AT_SYNTHESIZE)
31024 cp_parser_objc_at_synthesize_declaration (parser);
31025 else if (token->keyword == RID_AT_DYNAMIC)
31026 cp_parser_objc_at_dynamic_declaration (parser);
31027 else if (token->keyword == RID_ATTRIBUTE
31028 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
31029 warning_at (token->location, OPT_Wattributes,
31030 "prefix attributes are ignored for methods");
31031 else
31032 /* Allow for interspersed non-ObjC++ code. */
31033 cp_parser_objc_interstitial_code (parser);
31034
31035 token = cp_lexer_peek_token (parser->lexer);
31036 }
31037
31038 if (token->type != CPP_EOF)
31039 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31040 else
31041 cp_parser_error (parser, "expected %<@end%>");
31042
31043 objc_finish_implementation ();
31044 }
31045
31046 /* Parse Objective-C ivars. */
31047
31048 static void
31049 cp_parser_objc_class_ivars (cp_parser* parser)
31050 {
31051 cp_token *token = cp_lexer_peek_token (parser->lexer);
31052
31053 if (token->type != CPP_OPEN_BRACE)
31054 return; /* No ivars specified. */
31055
31056 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
31057 token = cp_lexer_peek_token (parser->lexer);
31058
31059 while (token->type != CPP_CLOSE_BRACE
31060 && token->keyword != RID_AT_END && token->type != CPP_EOF)
31061 {
31062 cp_decl_specifier_seq declspecs;
31063 int decl_class_or_enum_p;
31064 tree prefix_attributes;
31065
31066 cp_parser_objc_visibility_spec (parser);
31067
31068 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31069 break;
31070
31071 cp_parser_decl_specifier_seq (parser,
31072 CP_PARSER_FLAGS_OPTIONAL,
31073 &declspecs,
31074 &decl_class_or_enum_p);
31075
31076 /* auto, register, static, extern, mutable. */
31077 if (declspecs.storage_class != sc_none)
31078 {
31079 cp_parser_error (parser, "invalid type for instance variable");
31080 declspecs.storage_class = sc_none;
31081 }
31082
31083 /* thread_local. */
31084 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31085 {
31086 cp_parser_error (parser, "invalid type for instance variable");
31087 declspecs.locations[ds_thread] = 0;
31088 }
31089
31090 /* typedef. */
31091 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31092 {
31093 cp_parser_error (parser, "invalid type for instance variable");
31094 declspecs.locations[ds_typedef] = 0;
31095 }
31096
31097 prefix_attributes = declspecs.attributes;
31098 declspecs.attributes = NULL_TREE;
31099
31100 /* Keep going until we hit the `;' at the end of the
31101 declaration. */
31102 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31103 {
31104 tree width = NULL_TREE, attributes, first_attribute, decl;
31105 cp_declarator *declarator = NULL;
31106 int ctor_dtor_or_conv_p;
31107
31108 /* Check for a (possibly unnamed) bitfield declaration. */
31109 token = cp_lexer_peek_token (parser->lexer);
31110 if (token->type == CPP_COLON)
31111 goto eat_colon;
31112
31113 if (token->type == CPP_NAME
31114 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
31115 == CPP_COLON))
31116 {
31117 /* Get the name of the bitfield. */
31118 declarator = make_id_declarator (NULL_TREE,
31119 cp_parser_identifier (parser),
31120 sfk_none, token->location);
31121
31122 eat_colon:
31123 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31124 /* Get the width of the bitfield. */
31125 width
31126 = cp_parser_constant_expression (parser);
31127 }
31128 else
31129 {
31130 /* Parse the declarator. */
31131 declarator
31132 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31133 CP_PARSER_FLAGS_NONE,
31134 &ctor_dtor_or_conv_p,
31135 /*parenthesized_p=*/NULL,
31136 /*member_p=*/false,
31137 /*friend_p=*/false,
31138 /*static_p=*/false);
31139 }
31140
31141 /* Look for attributes that apply to the ivar. */
31142 attributes = cp_parser_attributes_opt (parser);
31143 /* Remember which attributes are prefix attributes and
31144 which are not. */
31145 first_attribute = attributes;
31146 /* Combine the attributes. */
31147 attributes = attr_chainon (prefix_attributes, attributes);
31148
31149 if (width)
31150 /* Create the bitfield declaration. */
31151 decl = grokbitfield (declarator, &declspecs,
31152 width, NULL_TREE, attributes);
31153 else
31154 decl = grokfield (declarator, &declspecs,
31155 NULL_TREE, /*init_const_expr_p=*/false,
31156 NULL_TREE, attributes);
31157
31158 /* Add the instance variable. */
31159 if (decl != error_mark_node && decl != NULL_TREE)
31160 objc_add_instance_variable (decl);
31161
31162 /* Reset PREFIX_ATTRIBUTES. */
31163 if (attributes != error_mark_node)
31164 {
31165 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31166 attributes = TREE_CHAIN (attributes);
31167 if (attributes)
31168 TREE_CHAIN (attributes) = NULL_TREE;
31169 }
31170
31171 token = cp_lexer_peek_token (parser->lexer);
31172
31173 if (token->type == CPP_COMMA)
31174 {
31175 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31176 continue;
31177 }
31178 break;
31179 }
31180
31181 cp_parser_consume_semicolon_at_end_of_statement (parser);
31182 token = cp_lexer_peek_token (parser->lexer);
31183 }
31184
31185 if (token->keyword == RID_AT_END)
31186 cp_parser_error (parser, "expected %<}%>");
31187
31188 /* Do not consume the RID_AT_END, so it will be read again as terminating
31189 the @interface of @implementation. */
31190 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
31191 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
31192
31193 /* For historical reasons, we accept an optional semicolon. */
31194 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31195 cp_lexer_consume_token (parser->lexer);
31196 }
31197
31198 /* Parse an Objective-C protocol declaration. */
31199
31200 static void
31201 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
31202 {
31203 tree proto, protorefs;
31204 cp_token *tok;
31205
31206 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
31207 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31208 {
31209 tok = cp_lexer_peek_token (parser->lexer);
31210 error_at (tok->location, "identifier expected after %<@protocol%>");
31211 cp_parser_consume_semicolon_at_end_of_statement (parser);
31212 return;
31213 }
31214
31215 /* See if we have a forward declaration or a definition. */
31216 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
31217
31218 /* Try a forward declaration first. */
31219 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
31220 {
31221 while (true)
31222 {
31223 tree id;
31224
31225 id = cp_parser_identifier (parser);
31226 if (id == error_mark_node)
31227 break;
31228
31229 objc_declare_protocol (id, attributes);
31230
31231 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31232 cp_lexer_consume_token (parser->lexer);
31233 else
31234 break;
31235 }
31236 cp_parser_consume_semicolon_at_end_of_statement (parser);
31237 }
31238
31239 /* Ok, we got a full-fledged definition (or at least should). */
31240 else
31241 {
31242 proto = cp_parser_identifier (parser);
31243 protorefs = cp_parser_objc_protocol_refs_opt (parser);
31244 objc_start_protocol (proto, protorefs, attributes);
31245 cp_parser_objc_method_prototype_list (parser);
31246 }
31247 }
31248
31249 /* Parse an Objective-C superclass or category. */
31250
31251 static void
31252 cp_parser_objc_superclass_or_category (cp_parser *parser,
31253 bool iface_p,
31254 tree *super,
31255 tree *categ, bool *is_class_extension)
31256 {
31257 cp_token *next = cp_lexer_peek_token (parser->lexer);
31258
31259 *super = *categ = NULL_TREE;
31260 *is_class_extension = false;
31261 if (next->type == CPP_COLON)
31262 {
31263 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31264 *super = cp_parser_identifier (parser);
31265 }
31266 else if (next->type == CPP_OPEN_PAREN)
31267 {
31268 matching_parens parens;
31269 parens.consume_open (parser); /* Eat '('. */
31270
31271 /* If there is no category name, and this is an @interface, we
31272 have a class extension. */
31273 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31274 {
31275 *categ = NULL_TREE;
31276 *is_class_extension = true;
31277 }
31278 else
31279 *categ = cp_parser_identifier (parser);
31280
31281 parens.require_close (parser);
31282 }
31283 }
31284
31285 /* Parse an Objective-C class interface. */
31286
31287 static void
31288 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
31289 {
31290 tree name, super, categ, protos;
31291 bool is_class_extension;
31292
31293 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
31294 name = cp_parser_identifier (parser);
31295 if (name == error_mark_node)
31296 {
31297 /* It's hard to recover because even if valid @interface stuff
31298 is to follow, we can't compile it (or validate it) if we
31299 don't even know which class it refers to. Let's assume this
31300 was a stray '@interface' token in the stream and skip it.
31301 */
31302 return;
31303 }
31304 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
31305 &is_class_extension);
31306 protos = cp_parser_objc_protocol_refs_opt (parser);
31307
31308 /* We have either a class or a category on our hands. */
31309 if (categ || is_class_extension)
31310 objc_start_category_interface (name, categ, protos, attributes);
31311 else
31312 {
31313 objc_start_class_interface (name, super, protos, attributes);
31314 /* Handle instance variable declarations, if any. */
31315 cp_parser_objc_class_ivars (parser);
31316 objc_continue_interface ();
31317 }
31318
31319 cp_parser_objc_method_prototype_list (parser);
31320 }
31321
31322 /* Parse an Objective-C class implementation. */
31323
31324 static void
31325 cp_parser_objc_class_implementation (cp_parser* parser)
31326 {
31327 tree name, super, categ;
31328 bool is_class_extension;
31329
31330 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
31331 name = cp_parser_identifier (parser);
31332 if (name == error_mark_node)
31333 {
31334 /* It's hard to recover because even if valid @implementation
31335 stuff is to follow, we can't compile it (or validate it) if
31336 we don't even know which class it refers to. Let's assume
31337 this was a stray '@implementation' token in the stream and
31338 skip it.
31339 */
31340 return;
31341 }
31342 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
31343 &is_class_extension);
31344
31345 /* We have either a class or a category on our hands. */
31346 if (categ)
31347 objc_start_category_implementation (name, categ);
31348 else
31349 {
31350 objc_start_class_implementation (name, super);
31351 /* Handle instance variable declarations, if any. */
31352 cp_parser_objc_class_ivars (parser);
31353 objc_continue_implementation ();
31354 }
31355
31356 cp_parser_objc_method_definition_list (parser);
31357 }
31358
31359 /* Consume the @end token and finish off the implementation. */
31360
31361 static void
31362 cp_parser_objc_end_implementation (cp_parser* parser)
31363 {
31364 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31365 objc_finish_implementation ();
31366 }
31367
31368 /* Parse an Objective-C declaration. */
31369
31370 static void
31371 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
31372 {
31373 /* Try to figure out what kind of declaration is present. */
31374 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31375
31376 if (attributes)
31377 switch (kwd->keyword)
31378 {
31379 case RID_AT_ALIAS:
31380 case RID_AT_CLASS:
31381 case RID_AT_END:
31382 error_at (kwd->location, "attributes may not be specified before"
31383 " the %<@%D%> Objective-C++ keyword",
31384 kwd->u.value);
31385 attributes = NULL;
31386 break;
31387 case RID_AT_IMPLEMENTATION:
31388 warning_at (kwd->location, OPT_Wattributes,
31389 "prefix attributes are ignored before %<@%D%>",
31390 kwd->u.value);
31391 attributes = NULL;
31392 default:
31393 break;
31394 }
31395
31396 switch (kwd->keyword)
31397 {
31398 case RID_AT_ALIAS:
31399 cp_parser_objc_alias_declaration (parser);
31400 break;
31401 case RID_AT_CLASS:
31402 cp_parser_objc_class_declaration (parser);
31403 break;
31404 case RID_AT_PROTOCOL:
31405 cp_parser_objc_protocol_declaration (parser, attributes);
31406 break;
31407 case RID_AT_INTERFACE:
31408 cp_parser_objc_class_interface (parser, attributes);
31409 break;
31410 case RID_AT_IMPLEMENTATION:
31411 cp_parser_objc_class_implementation (parser);
31412 break;
31413 case RID_AT_END:
31414 cp_parser_objc_end_implementation (parser);
31415 break;
31416 default:
31417 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31418 kwd->u.value);
31419 cp_parser_skip_to_end_of_block_or_statement (parser);
31420 }
31421 }
31422
31423 /* Parse an Objective-C try-catch-finally statement.
31424
31425 objc-try-catch-finally-stmt:
31426 @try compound-statement objc-catch-clause-seq [opt]
31427 objc-finally-clause [opt]
31428
31429 objc-catch-clause-seq:
31430 objc-catch-clause objc-catch-clause-seq [opt]
31431
31432 objc-catch-clause:
31433 @catch ( objc-exception-declaration ) compound-statement
31434
31435 objc-finally-clause:
31436 @finally compound-statement
31437
31438 objc-exception-declaration:
31439 parameter-declaration
31440 '...'
31441
31442 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31443
31444 Returns NULL_TREE.
31445
31446 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31447 for C. Keep them in sync. */
31448
31449 static tree
31450 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
31451 {
31452 location_t location;
31453 tree stmt;
31454
31455 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
31456 location = cp_lexer_peek_token (parser->lexer)->location;
31457 objc_maybe_warn_exceptions (location);
31458 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31459 node, lest it get absorbed into the surrounding block. */
31460 stmt = push_stmt_list ();
31461 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31462 objc_begin_try_stmt (location, pop_stmt_list (stmt));
31463
31464 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
31465 {
31466 cp_parameter_declarator *parm;
31467 tree parameter_declaration = error_mark_node;
31468 bool seen_open_paren = false;
31469 matching_parens parens;
31470
31471 cp_lexer_consume_token (parser->lexer);
31472 if (parens.require_open (parser))
31473 seen_open_paren = true;
31474 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31475 {
31476 /* We have "@catch (...)" (where the '...' are literally
31477 what is in the code). Skip the '...'.
31478 parameter_declaration is set to NULL_TREE, and
31479 objc_being_catch_clauses() knows that that means
31480 '...'. */
31481 cp_lexer_consume_token (parser->lexer);
31482 parameter_declaration = NULL_TREE;
31483 }
31484 else
31485 {
31486 /* We have "@catch (NSException *exception)" or something
31487 like that. Parse the parameter declaration. */
31488 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
31489 false, NULL);
31490 if (parm == NULL)
31491 parameter_declaration = error_mark_node;
31492 else
31493 parameter_declaration = grokdeclarator (parm->declarator,
31494 &parm->decl_specifiers,
31495 PARM, /*initialized=*/0,
31496 /*attrlist=*/NULL);
31497 }
31498 if (seen_open_paren)
31499 parens.require_close (parser);
31500 else
31501 {
31502 /* If there was no open parenthesis, we are recovering from
31503 an error, and we are trying to figure out what mistake
31504 the user has made. */
31505
31506 /* If there is an immediate closing parenthesis, the user
31507 probably forgot the opening one (ie, they typed "@catch
31508 NSException *e)". Parse the closing parenthesis and keep
31509 going. */
31510 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31511 cp_lexer_consume_token (parser->lexer);
31512
31513 /* If these is no immediate closing parenthesis, the user
31514 probably doesn't know that parenthesis are required at
31515 all (ie, they typed "@catch NSException *e"). So, just
31516 forget about the closing parenthesis and keep going. */
31517 }
31518 objc_begin_catch_clause (parameter_declaration);
31519 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31520 objc_finish_catch_clause ();
31521 }
31522 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
31523 {
31524 cp_lexer_consume_token (parser->lexer);
31525 location = cp_lexer_peek_token (parser->lexer)->location;
31526 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31527 node, lest it get absorbed into the surrounding block. */
31528 stmt = push_stmt_list ();
31529 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31530 objc_build_finally_clause (location, pop_stmt_list (stmt));
31531 }
31532
31533 return objc_finish_try_stmt ();
31534 }
31535
31536 /* Parse an Objective-C synchronized statement.
31537
31538 objc-synchronized-stmt:
31539 @synchronized ( expression ) compound-statement
31540
31541 Returns NULL_TREE. */
31542
31543 static tree
31544 cp_parser_objc_synchronized_statement (cp_parser *parser)
31545 {
31546 location_t location;
31547 tree lock, stmt;
31548
31549 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31550
31551 location = cp_lexer_peek_token (parser->lexer)->location;
31552 objc_maybe_warn_exceptions (location);
31553 matching_parens parens;
31554 parens.require_open (parser);
31555 lock = cp_parser_expression (parser);
31556 parens.require_close (parser);
31557
31558 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31559 node, lest it get absorbed into the surrounding block. */
31560 stmt = push_stmt_list ();
31561 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31562
31563 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31564 }
31565
31566 /* Parse an Objective-C throw statement.
31567
31568 objc-throw-stmt:
31569 @throw assignment-expression [opt] ;
31570
31571 Returns a constructed '@throw' statement. */
31572
31573 static tree
31574 cp_parser_objc_throw_statement (cp_parser *parser)
31575 {
31576 tree expr = NULL_TREE;
31577 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31578
31579 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31580
31581 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31582 expr = cp_parser_expression (parser);
31583
31584 cp_parser_consume_semicolon_at_end_of_statement (parser);
31585
31586 return objc_build_throw_stmt (loc, expr);
31587 }
31588
31589 /* Parse an Objective-C statement. */
31590
31591 static tree
31592 cp_parser_objc_statement (cp_parser * parser)
31593 {
31594 /* Try to figure out what kind of declaration is present. */
31595 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31596
31597 switch (kwd->keyword)
31598 {
31599 case RID_AT_TRY:
31600 return cp_parser_objc_try_catch_finally_statement (parser);
31601 case RID_AT_SYNCHRONIZED:
31602 return cp_parser_objc_synchronized_statement (parser);
31603 case RID_AT_THROW:
31604 return cp_parser_objc_throw_statement (parser);
31605 default:
31606 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31607 kwd->u.value);
31608 cp_parser_skip_to_end_of_block_or_statement (parser);
31609 }
31610
31611 return error_mark_node;
31612 }
31613
31614 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31615 look ahead to see if an objc keyword follows the attributes. This
31616 is to detect the use of prefix attributes on ObjC @interface and
31617 @protocol. */
31618
31619 static bool
31620 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31621 {
31622 cp_lexer_save_tokens (parser->lexer);
31623 *attrib = cp_parser_attributes_opt (parser);
31624 gcc_assert (*attrib);
31625 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31626 {
31627 cp_lexer_commit_tokens (parser->lexer);
31628 return true;
31629 }
31630 cp_lexer_rollback_tokens (parser->lexer);
31631 return false;
31632 }
31633
31634 /* This routine is a minimal replacement for
31635 c_parser_struct_declaration () used when parsing the list of
31636 types/names or ObjC++ properties. For example, when parsing the
31637 code
31638
31639 @property (readonly) int a, b, c;
31640
31641 this function is responsible for parsing "int a, int b, int c" and
31642 returning the declarations as CHAIN of DECLs.
31643
31644 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31645 similar parsing. */
31646 static tree
31647 cp_parser_objc_struct_declaration (cp_parser *parser)
31648 {
31649 tree decls = NULL_TREE;
31650 cp_decl_specifier_seq declspecs;
31651 int decl_class_or_enum_p;
31652 tree prefix_attributes;
31653
31654 cp_parser_decl_specifier_seq (parser,
31655 CP_PARSER_FLAGS_NONE,
31656 &declspecs,
31657 &decl_class_or_enum_p);
31658
31659 if (declspecs.type == error_mark_node)
31660 return error_mark_node;
31661
31662 /* auto, register, static, extern, mutable. */
31663 if (declspecs.storage_class != sc_none)
31664 {
31665 cp_parser_error (parser, "invalid type for property");
31666 declspecs.storage_class = sc_none;
31667 }
31668
31669 /* thread_local. */
31670 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31671 {
31672 cp_parser_error (parser, "invalid type for property");
31673 declspecs.locations[ds_thread] = 0;
31674 }
31675
31676 /* typedef. */
31677 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31678 {
31679 cp_parser_error (parser, "invalid type for property");
31680 declspecs.locations[ds_typedef] = 0;
31681 }
31682
31683 prefix_attributes = declspecs.attributes;
31684 declspecs.attributes = NULL_TREE;
31685
31686 /* Keep going until we hit the `;' at the end of the declaration. */
31687 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31688 {
31689 tree attributes, first_attribute, decl;
31690 cp_declarator *declarator;
31691 cp_token *token;
31692
31693 /* Parse the declarator. */
31694 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31695 CP_PARSER_FLAGS_NONE,
31696 NULL, NULL, false, false, false);
31697
31698 /* Look for attributes that apply to the ivar. */
31699 attributes = cp_parser_attributes_opt (parser);
31700 /* Remember which attributes are prefix attributes and
31701 which are not. */
31702 first_attribute = attributes;
31703 /* Combine the attributes. */
31704 attributes = attr_chainon (prefix_attributes, attributes);
31705
31706 decl = grokfield (declarator, &declspecs,
31707 NULL_TREE, /*init_const_expr_p=*/false,
31708 NULL_TREE, attributes);
31709
31710 if (decl == error_mark_node || decl == NULL_TREE)
31711 return error_mark_node;
31712
31713 /* Reset PREFIX_ATTRIBUTES. */
31714 if (attributes != error_mark_node)
31715 {
31716 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31717 attributes = TREE_CHAIN (attributes);
31718 if (attributes)
31719 TREE_CHAIN (attributes) = NULL_TREE;
31720 }
31721
31722 DECL_CHAIN (decl) = decls;
31723 decls = decl;
31724
31725 token = cp_lexer_peek_token (parser->lexer);
31726 if (token->type == CPP_COMMA)
31727 {
31728 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31729 continue;
31730 }
31731 else
31732 break;
31733 }
31734 return decls;
31735 }
31736
31737 /* Parse an Objective-C @property declaration. The syntax is:
31738
31739 objc-property-declaration:
31740 '@property' objc-property-attributes[opt] struct-declaration ;
31741
31742 objc-property-attributes:
31743 '(' objc-property-attribute-list ')'
31744
31745 objc-property-attribute-list:
31746 objc-property-attribute
31747 objc-property-attribute-list, objc-property-attribute
31748
31749 objc-property-attribute
31750 'getter' = identifier
31751 'setter' = identifier
31752 'readonly'
31753 'readwrite'
31754 'assign'
31755 'retain'
31756 'copy'
31757 'nonatomic'
31758
31759 For example:
31760 @property NSString *name;
31761 @property (readonly) id object;
31762 @property (retain, nonatomic, getter=getTheName) id name;
31763 @property int a, b, c;
31764
31765 PS: This function is identical to
31766 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31767 static void
31768 cp_parser_objc_at_property_declaration (cp_parser *parser)
31769 {
31770 /* The following variables hold the attributes of the properties as
31771 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31772 seen. When we see an attribute, we set them to 'true' (if they
31773 are boolean properties) or to the identifier (if they have an
31774 argument, ie, for getter and setter). Note that here we only
31775 parse the list of attributes, check the syntax and accumulate the
31776 attributes that we find. objc_add_property_declaration() will
31777 then process the information. */
31778 bool property_assign = false;
31779 bool property_copy = false;
31780 tree property_getter_ident = NULL_TREE;
31781 bool property_nonatomic = false;
31782 bool property_readonly = false;
31783 bool property_readwrite = false;
31784 bool property_retain = false;
31785 tree property_setter_ident = NULL_TREE;
31786
31787 /* 'properties' is the list of properties that we read. Usually a
31788 single one, but maybe more (eg, in "@property int a, b, c;" there
31789 are three). */
31790 tree properties;
31791 location_t loc;
31792
31793 loc = cp_lexer_peek_token (parser->lexer)->location;
31794
31795 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31796
31797 /* Parse the optional attribute list... */
31798 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31799 {
31800 /* Eat the '('. */
31801 matching_parens parens;
31802 parens.consume_open (parser);
31803
31804 while (true)
31805 {
31806 bool syntax_error = false;
31807 cp_token *token = cp_lexer_peek_token (parser->lexer);
31808 enum rid keyword;
31809
31810 if (token->type != CPP_NAME)
31811 {
31812 cp_parser_error (parser, "expected identifier");
31813 break;
31814 }
31815 keyword = C_RID_CODE (token->u.value);
31816 cp_lexer_consume_token (parser->lexer);
31817 switch (keyword)
31818 {
31819 case RID_ASSIGN: property_assign = true; break;
31820 case RID_COPY: property_copy = true; break;
31821 case RID_NONATOMIC: property_nonatomic = true; break;
31822 case RID_READONLY: property_readonly = true; break;
31823 case RID_READWRITE: property_readwrite = true; break;
31824 case RID_RETAIN: property_retain = true; break;
31825
31826 case RID_GETTER:
31827 case RID_SETTER:
31828 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31829 {
31830 if (keyword == RID_GETTER)
31831 cp_parser_error (parser,
31832 "missing %<=%> (after %<getter%> attribute)");
31833 else
31834 cp_parser_error (parser,
31835 "missing %<=%> (after %<setter%> attribute)");
31836 syntax_error = true;
31837 break;
31838 }
31839 cp_lexer_consume_token (parser->lexer); /* eat the = */
31840 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31841 {
31842 cp_parser_error (parser, "expected identifier");
31843 syntax_error = true;
31844 break;
31845 }
31846 if (keyword == RID_SETTER)
31847 {
31848 if (property_setter_ident != NULL_TREE)
31849 {
31850 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31851 cp_lexer_consume_token (parser->lexer);
31852 }
31853 else
31854 property_setter_ident = cp_parser_objc_selector (parser);
31855 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31856 cp_parser_error (parser, "setter name must terminate with %<:%>");
31857 else
31858 cp_lexer_consume_token (parser->lexer);
31859 }
31860 else
31861 {
31862 if (property_getter_ident != NULL_TREE)
31863 {
31864 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31865 cp_lexer_consume_token (parser->lexer);
31866 }
31867 else
31868 property_getter_ident = cp_parser_objc_selector (parser);
31869 }
31870 break;
31871 default:
31872 cp_parser_error (parser, "unknown property attribute");
31873 syntax_error = true;
31874 break;
31875 }
31876
31877 if (syntax_error)
31878 break;
31879
31880 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31881 cp_lexer_consume_token (parser->lexer);
31882 else
31883 break;
31884 }
31885
31886 /* FIXME: "@property (setter, assign);" will generate a spurious
31887 "error: expected ‘)’ before ‘,’ token". This is because
31888 cp_parser_require, unlike the C counterpart, will produce an
31889 error even if we are in error recovery. */
31890 if (!parens.require_close (parser))
31891 {
31892 cp_parser_skip_to_closing_parenthesis (parser,
31893 /*recovering=*/true,
31894 /*or_comma=*/false,
31895 /*consume_paren=*/true);
31896 }
31897 }
31898
31899 /* ... and the property declaration(s). */
31900 properties = cp_parser_objc_struct_declaration (parser);
31901
31902 if (properties == error_mark_node)
31903 {
31904 cp_parser_skip_to_end_of_statement (parser);
31905 /* If the next token is now a `;', consume it. */
31906 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31907 cp_lexer_consume_token (parser->lexer);
31908 return;
31909 }
31910
31911 if (properties == NULL_TREE)
31912 cp_parser_error (parser, "expected identifier");
31913 else
31914 {
31915 /* Comma-separated properties are chained together in
31916 reverse order; add them one by one. */
31917 properties = nreverse (properties);
31918
31919 for (; properties; properties = TREE_CHAIN (properties))
31920 objc_add_property_declaration (loc, copy_node (properties),
31921 property_readonly, property_readwrite,
31922 property_assign, property_retain,
31923 property_copy, property_nonatomic,
31924 property_getter_ident, property_setter_ident);
31925 }
31926
31927 cp_parser_consume_semicolon_at_end_of_statement (parser);
31928 }
31929
31930 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31931
31932 objc-synthesize-declaration:
31933 @synthesize objc-synthesize-identifier-list ;
31934
31935 objc-synthesize-identifier-list:
31936 objc-synthesize-identifier
31937 objc-synthesize-identifier-list, objc-synthesize-identifier
31938
31939 objc-synthesize-identifier
31940 identifier
31941 identifier = identifier
31942
31943 For example:
31944 @synthesize MyProperty;
31945 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31946
31947 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31948 for C. Keep them in sync.
31949 */
31950 static void
31951 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31952 {
31953 tree list = NULL_TREE;
31954 location_t loc;
31955 loc = cp_lexer_peek_token (parser->lexer)->location;
31956
31957 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31958 while (true)
31959 {
31960 tree property, ivar;
31961 property = cp_parser_identifier (parser);
31962 if (property == error_mark_node)
31963 {
31964 cp_parser_consume_semicolon_at_end_of_statement (parser);
31965 return;
31966 }
31967 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31968 {
31969 cp_lexer_consume_token (parser->lexer);
31970 ivar = cp_parser_identifier (parser);
31971 if (ivar == error_mark_node)
31972 {
31973 cp_parser_consume_semicolon_at_end_of_statement (parser);
31974 return;
31975 }
31976 }
31977 else
31978 ivar = NULL_TREE;
31979 list = chainon (list, build_tree_list (ivar, property));
31980 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31981 cp_lexer_consume_token (parser->lexer);
31982 else
31983 break;
31984 }
31985 cp_parser_consume_semicolon_at_end_of_statement (parser);
31986 objc_add_synthesize_declaration (loc, list);
31987 }
31988
31989 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31990
31991 objc-dynamic-declaration:
31992 @dynamic identifier-list ;
31993
31994 For example:
31995 @dynamic MyProperty;
31996 @dynamic MyProperty, AnotherProperty;
31997
31998 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31999 for C. Keep them in sync.
32000 */
32001 static void
32002 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
32003 {
32004 tree list = NULL_TREE;
32005 location_t loc;
32006 loc = cp_lexer_peek_token (parser->lexer)->location;
32007
32008 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
32009 while (true)
32010 {
32011 tree property;
32012 property = cp_parser_identifier (parser);
32013 if (property == error_mark_node)
32014 {
32015 cp_parser_consume_semicolon_at_end_of_statement (parser);
32016 return;
32017 }
32018 list = chainon (list, build_tree_list (NULL, property));
32019 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32020 cp_lexer_consume_token (parser->lexer);
32021 else
32022 break;
32023 }
32024 cp_parser_consume_semicolon_at_end_of_statement (parser);
32025 objc_add_dynamic_declaration (loc, list);
32026 }
32027
32028 \f
32029 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
32030
32031 /* Returns name of the next clause.
32032 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
32033 the token is not consumed. Otherwise appropriate pragma_omp_clause is
32034 returned and the token is consumed. */
32035
32036 static pragma_omp_clause
32037 cp_parser_omp_clause_name (cp_parser *parser)
32038 {
32039 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
32040
32041 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32042 result = PRAGMA_OACC_CLAUSE_AUTO;
32043 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
32044 result = PRAGMA_OMP_CLAUSE_IF;
32045 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
32046 result = PRAGMA_OMP_CLAUSE_DEFAULT;
32047 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
32048 result = PRAGMA_OACC_CLAUSE_DELETE;
32049 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
32050 result = PRAGMA_OMP_CLAUSE_PRIVATE;
32051 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
32052 result = PRAGMA_OMP_CLAUSE_FOR;
32053 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32054 {
32055 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32056 const char *p = IDENTIFIER_POINTER (id);
32057
32058 switch (p[0])
32059 {
32060 case 'a':
32061 if (!strcmp ("aligned", p))
32062 result = PRAGMA_OMP_CLAUSE_ALIGNED;
32063 else if (!strcmp ("async", p))
32064 result = PRAGMA_OACC_CLAUSE_ASYNC;
32065 break;
32066 case 'c':
32067 if (!strcmp ("collapse", p))
32068 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
32069 else if (!strcmp ("copy", p))
32070 result = PRAGMA_OACC_CLAUSE_COPY;
32071 else if (!strcmp ("copyin", p))
32072 result = PRAGMA_OMP_CLAUSE_COPYIN;
32073 else if (!strcmp ("copyout", p))
32074 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32075 else if (!strcmp ("copyprivate", p))
32076 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
32077 else if (!strcmp ("create", p))
32078 result = PRAGMA_OACC_CLAUSE_CREATE;
32079 break;
32080 case 'd':
32081 if (!strcmp ("defaultmap", p))
32082 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
32083 else if (!strcmp ("depend", p))
32084 result = PRAGMA_OMP_CLAUSE_DEPEND;
32085 else if (!strcmp ("device", p))
32086 result = PRAGMA_OMP_CLAUSE_DEVICE;
32087 else if (!strcmp ("deviceptr", p))
32088 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
32089 else if (!strcmp ("device_resident", p))
32090 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
32091 else if (!strcmp ("dist_schedule", p))
32092 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
32093 break;
32094 case 'f':
32095 if (!strcmp ("final", p))
32096 result = PRAGMA_OMP_CLAUSE_FINAL;
32097 else if (!strcmp ("finalize", p))
32098 result = PRAGMA_OACC_CLAUSE_FINALIZE;
32099 else if (!strcmp ("firstprivate", p))
32100 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
32101 else if (!strcmp ("from", p))
32102 result = PRAGMA_OMP_CLAUSE_FROM;
32103 break;
32104 case 'g':
32105 if (!strcmp ("gang", p))
32106 result = PRAGMA_OACC_CLAUSE_GANG;
32107 else if (!strcmp ("grainsize", p))
32108 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
32109 break;
32110 case 'h':
32111 if (!strcmp ("hint", p))
32112 result = PRAGMA_OMP_CLAUSE_HINT;
32113 else if (!strcmp ("host", p))
32114 result = PRAGMA_OACC_CLAUSE_HOST;
32115 break;
32116 case 'i':
32117 if (!strcmp ("if_present", p))
32118 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
32119 else if (!strcmp ("in_reduction", p))
32120 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
32121 else if (!strcmp ("inbranch", p))
32122 result = PRAGMA_OMP_CLAUSE_INBRANCH;
32123 else if (!strcmp ("independent", p))
32124 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
32125 else if (!strcmp ("is_device_ptr", p))
32126 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
32127 break;
32128 case 'l':
32129 if (!strcmp ("lastprivate", p))
32130 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
32131 else if (!strcmp ("linear", p))
32132 result = PRAGMA_OMP_CLAUSE_LINEAR;
32133 else if (!strcmp ("link", p))
32134 result = PRAGMA_OMP_CLAUSE_LINK;
32135 break;
32136 case 'm':
32137 if (!strcmp ("map", p))
32138 result = PRAGMA_OMP_CLAUSE_MAP;
32139 else if (!strcmp ("mergeable", p))
32140 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
32141 break;
32142 case 'n':
32143 if (!strcmp ("nogroup", p))
32144 result = PRAGMA_OMP_CLAUSE_NOGROUP;
32145 else if (!strcmp ("nontemporal", p))
32146 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
32147 else if (!strcmp ("notinbranch", p))
32148 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
32149 else if (!strcmp ("nowait", p))
32150 result = PRAGMA_OMP_CLAUSE_NOWAIT;
32151 else if (!strcmp ("num_gangs", p))
32152 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
32153 else if (!strcmp ("num_tasks", p))
32154 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
32155 else if (!strcmp ("num_teams", p))
32156 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
32157 else if (!strcmp ("num_threads", p))
32158 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
32159 else if (!strcmp ("num_workers", p))
32160 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
32161 break;
32162 case 'o':
32163 if (!strcmp ("ordered", p))
32164 result = PRAGMA_OMP_CLAUSE_ORDERED;
32165 break;
32166 case 'p':
32167 if (!strcmp ("parallel", p))
32168 result = PRAGMA_OMP_CLAUSE_PARALLEL;
32169 else if (!strcmp ("present", p))
32170 result = PRAGMA_OACC_CLAUSE_PRESENT;
32171 else if (!strcmp ("present_or_copy", p)
32172 || !strcmp ("pcopy", p))
32173 result = PRAGMA_OACC_CLAUSE_COPY;
32174 else if (!strcmp ("present_or_copyin", p)
32175 || !strcmp ("pcopyin", p))
32176 result = PRAGMA_OACC_CLAUSE_COPYIN;
32177 else if (!strcmp ("present_or_copyout", p)
32178 || !strcmp ("pcopyout", p))
32179 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32180 else if (!strcmp ("present_or_create", p)
32181 || !strcmp ("pcreate", p))
32182 result = PRAGMA_OACC_CLAUSE_CREATE;
32183 else if (!strcmp ("priority", p))
32184 result = PRAGMA_OMP_CLAUSE_PRIORITY;
32185 else if (!strcmp ("proc_bind", p))
32186 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
32187 break;
32188 case 'r':
32189 if (!strcmp ("reduction", p))
32190 result = PRAGMA_OMP_CLAUSE_REDUCTION;
32191 break;
32192 case 's':
32193 if (!strcmp ("safelen", p))
32194 result = PRAGMA_OMP_CLAUSE_SAFELEN;
32195 else if (!strcmp ("schedule", p))
32196 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
32197 else if (!strcmp ("sections", p))
32198 result = PRAGMA_OMP_CLAUSE_SECTIONS;
32199 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
32200 result = PRAGMA_OACC_CLAUSE_HOST;
32201 else if (!strcmp ("seq", p))
32202 result = PRAGMA_OACC_CLAUSE_SEQ;
32203 else if (!strcmp ("shared", p))
32204 result = PRAGMA_OMP_CLAUSE_SHARED;
32205 else if (!strcmp ("simd", p))
32206 result = PRAGMA_OMP_CLAUSE_SIMD;
32207 else if (!strcmp ("simdlen", p))
32208 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
32209 break;
32210 case 't':
32211 if (!strcmp ("task_reduction", p))
32212 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
32213 else if (!strcmp ("taskgroup", p))
32214 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
32215 else if (!strcmp ("thread_limit", p))
32216 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
32217 else if (!strcmp ("threads", p))
32218 result = PRAGMA_OMP_CLAUSE_THREADS;
32219 else if (!strcmp ("tile", p))
32220 result = PRAGMA_OACC_CLAUSE_TILE;
32221 else if (!strcmp ("to", p))
32222 result = PRAGMA_OMP_CLAUSE_TO;
32223 break;
32224 case 'u':
32225 if (!strcmp ("uniform", p))
32226 result = PRAGMA_OMP_CLAUSE_UNIFORM;
32227 else if (!strcmp ("untied", p))
32228 result = PRAGMA_OMP_CLAUSE_UNTIED;
32229 else if (!strcmp ("use_device", p))
32230 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
32231 else if (!strcmp ("use_device_ptr", p))
32232 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
32233 break;
32234 case 'v':
32235 if (!strcmp ("vector", p))
32236 result = PRAGMA_OACC_CLAUSE_VECTOR;
32237 else if (!strcmp ("vector_length", p))
32238 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
32239 break;
32240 case 'w':
32241 if (!strcmp ("wait", p))
32242 result = PRAGMA_OACC_CLAUSE_WAIT;
32243 else if (!strcmp ("worker", p))
32244 result = PRAGMA_OACC_CLAUSE_WORKER;
32245 break;
32246 }
32247 }
32248
32249 if (result != PRAGMA_OMP_CLAUSE_NONE)
32250 cp_lexer_consume_token (parser->lexer);
32251
32252 return result;
32253 }
32254
32255 /* Validate that a clause of the given type does not already exist. */
32256
32257 static void
32258 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
32259 const char *name, location_t location)
32260 {
32261 tree c;
32262
32263 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
32264 if (OMP_CLAUSE_CODE (c) == code)
32265 {
32266 error_at (location, "too many %qs clauses", name);
32267 break;
32268 }
32269 }
32270
32271 /* OpenMP 2.5:
32272 variable-list:
32273 identifier
32274 variable-list , identifier
32275
32276 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
32277 colon). An opening parenthesis will have been consumed by the caller.
32278
32279 If KIND is nonzero, create the appropriate node and install the decl
32280 in OMP_CLAUSE_DECL and add the node to the head of the list.
32281
32282 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
32283 return the list created.
32284
32285 COLON can be NULL if only closing parenthesis should end the list,
32286 or pointer to bool which will receive false if the list is terminated
32287 by closing parenthesis or true if the list is terminated by colon. */
32288
32289 static tree
32290 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
32291 tree list, bool *colon)
32292 {
32293 cp_token *token;
32294 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32295 if (colon)
32296 {
32297 parser->colon_corrects_to_scope_p = false;
32298 *colon = false;
32299 }
32300 while (1)
32301 {
32302 tree name, decl;
32303
32304 if (kind == OMP_CLAUSE_DEPEND)
32305 cp_parser_parse_tentatively (parser);
32306 token = cp_lexer_peek_token (parser->lexer);
32307 if (kind != 0
32308 && current_class_ptr
32309 && cp_parser_is_keyword (token, RID_THIS))
32310 {
32311 decl = finish_this_expr ();
32312 if (TREE_CODE (decl) == NON_LVALUE_EXPR
32313 || CONVERT_EXPR_P (decl))
32314 decl = TREE_OPERAND (decl, 0);
32315 cp_lexer_consume_token (parser->lexer);
32316 }
32317 else
32318 {
32319 name = cp_parser_id_expression (parser, /*template_p=*/false,
32320 /*check_dependency_p=*/true,
32321 /*template_p=*/NULL,
32322 /*declarator_p=*/false,
32323 /*optional_p=*/false);
32324 if (name == error_mark_node)
32325 {
32326 if (kind == OMP_CLAUSE_DEPEND
32327 && cp_parser_simulate_error (parser))
32328 goto depend_lvalue;
32329 goto skip_comma;
32330 }
32331
32332 if (identifier_p (name))
32333 decl = cp_parser_lookup_name_simple (parser, name, token->location);
32334 else
32335 decl = name;
32336 if (decl == error_mark_node)
32337 {
32338 if (kind == OMP_CLAUSE_DEPEND
32339 && cp_parser_simulate_error (parser))
32340 goto depend_lvalue;
32341 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
32342 token->location);
32343 }
32344 }
32345 if (decl == error_mark_node)
32346 ;
32347 else if (kind != 0)
32348 {
32349 switch (kind)
32350 {
32351 case OMP_CLAUSE__CACHE_:
32352 /* The OpenACC cache directive explicitly only allows "array
32353 elements or subarrays". */
32354 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
32355 {
32356 error_at (token->location, "expected %<[%>");
32357 decl = error_mark_node;
32358 break;
32359 }
32360 /* FALLTHROUGH. */
32361 case OMP_CLAUSE_MAP:
32362 case OMP_CLAUSE_FROM:
32363 case OMP_CLAUSE_TO:
32364 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
32365 {
32366 location_t loc
32367 = cp_lexer_peek_token (parser->lexer)->location;
32368 cp_id_kind idk = CP_ID_KIND_NONE;
32369 cp_lexer_consume_token (parser->lexer);
32370 decl = convert_from_reference (decl);
32371 decl
32372 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
32373 decl, false,
32374 &idk, loc);
32375 }
32376 /* FALLTHROUGH. */
32377 case OMP_CLAUSE_DEPEND:
32378 case OMP_CLAUSE_REDUCTION:
32379 case OMP_CLAUSE_IN_REDUCTION:
32380 case OMP_CLAUSE_TASK_REDUCTION:
32381 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
32382 {
32383 tree low_bound = NULL_TREE, length = NULL_TREE;
32384
32385 parser->colon_corrects_to_scope_p = false;
32386 cp_lexer_consume_token (parser->lexer);
32387 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32388 low_bound = cp_parser_expression (parser);
32389 if (!colon)
32390 parser->colon_corrects_to_scope_p
32391 = saved_colon_corrects_to_scope_p;
32392 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
32393 length = integer_one_node;
32394 else
32395 {
32396 /* Look for `:'. */
32397 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32398 {
32399 if (kind == OMP_CLAUSE_DEPEND
32400 && cp_parser_simulate_error (parser))
32401 goto depend_lvalue;
32402 goto skip_comma;
32403 }
32404 if (kind == OMP_CLAUSE_DEPEND)
32405 cp_parser_commit_to_tentative_parse (parser);
32406 if (!cp_lexer_next_token_is (parser->lexer,
32407 CPP_CLOSE_SQUARE))
32408 length = cp_parser_expression (parser);
32409 }
32410 /* Look for the closing `]'. */
32411 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
32412 RT_CLOSE_SQUARE))
32413 {
32414 if (kind == OMP_CLAUSE_DEPEND
32415 && cp_parser_simulate_error (parser))
32416 goto depend_lvalue;
32417 goto skip_comma;
32418 }
32419
32420 decl = tree_cons (low_bound, length, decl);
32421 }
32422 break;
32423 default:
32424 break;
32425 }
32426
32427 if (kind == OMP_CLAUSE_DEPEND)
32428 {
32429 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
32430 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32431 && cp_parser_simulate_error (parser))
32432 {
32433 depend_lvalue:
32434 cp_parser_abort_tentative_parse (parser);
32435 decl = cp_parser_assignment_expression (parser, NULL,
32436 false, false);
32437 }
32438 else
32439 cp_parser_parse_definitely (parser);
32440 }
32441
32442 tree u = build_omp_clause (token->location, kind);
32443 OMP_CLAUSE_DECL (u) = decl;
32444 OMP_CLAUSE_CHAIN (u) = list;
32445 list = u;
32446 }
32447 else
32448 list = tree_cons (decl, NULL_TREE, list);
32449
32450 get_comma:
32451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32452 break;
32453 cp_lexer_consume_token (parser->lexer);
32454 }
32455
32456 if (colon)
32457 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32458
32459 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32460 {
32461 *colon = true;
32462 cp_parser_require (parser, CPP_COLON, RT_COLON);
32463 return list;
32464 }
32465
32466 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32467 {
32468 int ending;
32469
32470 /* Try to resync to an unnested comma. Copied from
32471 cp_parser_parenthesized_expression_list. */
32472 skip_comma:
32473 if (colon)
32474 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32475 ending = cp_parser_skip_to_closing_parenthesis (parser,
32476 /*recovering=*/true,
32477 /*or_comma=*/true,
32478 /*consume_paren=*/true);
32479 if (ending < 0)
32480 goto get_comma;
32481 }
32482
32483 return list;
32484 }
32485
32486 /* Similarly, but expect leading and trailing parenthesis. This is a very
32487 common case for omp clauses. */
32488
32489 static tree
32490 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
32491 {
32492 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32493 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
32494 return list;
32495 }
32496
32497 /* OpenACC 2.0:
32498 copy ( variable-list )
32499 copyin ( variable-list )
32500 copyout ( variable-list )
32501 create ( variable-list )
32502 delete ( variable-list )
32503 present ( variable-list ) */
32504
32505 static tree
32506 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
32507 tree list)
32508 {
32509 enum gomp_map_kind kind;
32510 switch (c_kind)
32511 {
32512 case PRAGMA_OACC_CLAUSE_COPY:
32513 kind = GOMP_MAP_TOFROM;
32514 break;
32515 case PRAGMA_OACC_CLAUSE_COPYIN:
32516 kind = GOMP_MAP_TO;
32517 break;
32518 case PRAGMA_OACC_CLAUSE_COPYOUT:
32519 kind = GOMP_MAP_FROM;
32520 break;
32521 case PRAGMA_OACC_CLAUSE_CREATE:
32522 kind = GOMP_MAP_ALLOC;
32523 break;
32524 case PRAGMA_OACC_CLAUSE_DELETE:
32525 kind = GOMP_MAP_RELEASE;
32526 break;
32527 case PRAGMA_OACC_CLAUSE_DEVICE:
32528 kind = GOMP_MAP_FORCE_TO;
32529 break;
32530 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32531 kind = GOMP_MAP_DEVICE_RESIDENT;
32532 break;
32533 case PRAGMA_OACC_CLAUSE_HOST:
32534 kind = GOMP_MAP_FORCE_FROM;
32535 break;
32536 case PRAGMA_OACC_CLAUSE_LINK:
32537 kind = GOMP_MAP_LINK;
32538 break;
32539 case PRAGMA_OACC_CLAUSE_PRESENT:
32540 kind = GOMP_MAP_FORCE_PRESENT;
32541 break;
32542 default:
32543 gcc_unreachable ();
32544 }
32545 tree nl, c;
32546 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32547
32548 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32549 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32550
32551 return nl;
32552 }
32553
32554 /* OpenACC 2.0:
32555 deviceptr ( variable-list ) */
32556
32557 static tree
32558 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32559 {
32560 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32561 tree vars, t;
32562
32563 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32564 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32565 variable-list must only allow for pointer variables. */
32566 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32567 for (t = vars; t; t = TREE_CHAIN (t))
32568 {
32569 tree v = TREE_PURPOSE (t);
32570 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32571 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32572 OMP_CLAUSE_DECL (u) = v;
32573 OMP_CLAUSE_CHAIN (u) = list;
32574 list = u;
32575 }
32576
32577 return list;
32578 }
32579
32580 /* OpenACC 2.5:
32581 auto
32582 finalize
32583 independent
32584 nohost
32585 seq */
32586
32587 static tree
32588 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
32589 enum omp_clause_code code,
32590 tree list, location_t location)
32591 {
32592 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32593 tree c = build_omp_clause (location, code);
32594 OMP_CLAUSE_CHAIN (c) = list;
32595 return c;
32596 }
32597
32598 /* OpenACC:
32599 num_gangs ( expression )
32600 num_workers ( expression )
32601 vector_length ( expression ) */
32602
32603 static tree
32604 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32605 const char *str, tree list)
32606 {
32607 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32608
32609 matching_parens parens;
32610 if (!parens.require_open (parser))
32611 return list;
32612
32613 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32614
32615 if (t == error_mark_node
32616 || !parens.require_close (parser))
32617 {
32618 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32619 /*or_comma=*/false,
32620 /*consume_paren=*/true);
32621 return list;
32622 }
32623
32624 check_no_duplicate_clause (list, code, str, loc);
32625
32626 tree c = build_omp_clause (loc, code);
32627 OMP_CLAUSE_OPERAND (c, 0) = t;
32628 OMP_CLAUSE_CHAIN (c) = list;
32629 return c;
32630 }
32631
32632 /* OpenACC:
32633
32634 gang [( gang-arg-list )]
32635 worker [( [num:] int-expr )]
32636 vector [( [length:] int-expr )]
32637
32638 where gang-arg is one of:
32639
32640 [num:] int-expr
32641 static: size-expr
32642
32643 and size-expr may be:
32644
32645 *
32646 int-expr
32647 */
32648
32649 static tree
32650 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
32651 const char *str, tree list)
32652 {
32653 const char *id = "num";
32654 cp_lexer *lexer = parser->lexer;
32655 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
32656 location_t loc = cp_lexer_peek_token (lexer)->location;
32657
32658 if (kind == OMP_CLAUSE_VECTOR)
32659 id = "length";
32660
32661 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32662 {
32663 matching_parens parens;
32664 parens.consume_open (parser);
32665
32666 do
32667 {
32668 cp_token *next = cp_lexer_peek_token (lexer);
32669 int idx = 0;
32670
32671 /* Gang static argument. */
32672 if (kind == OMP_CLAUSE_GANG
32673 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32674 {
32675 cp_lexer_consume_token (lexer);
32676
32677 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32678 goto cleanup_error;
32679
32680 idx = 1;
32681 if (ops[idx] != NULL)
32682 {
32683 cp_parser_error (parser, "too many %<static%> arguments");
32684 goto cleanup_error;
32685 }
32686
32687 /* Check for the '*' argument. */
32688 if (cp_lexer_next_token_is (lexer, CPP_MULT)
32689 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32690 || cp_lexer_nth_token_is (parser->lexer, 2,
32691 CPP_CLOSE_PAREN)))
32692 {
32693 cp_lexer_consume_token (lexer);
32694 ops[idx] = integer_minus_one_node;
32695
32696 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32697 {
32698 cp_lexer_consume_token (lexer);
32699 continue;
32700 }
32701 else break;
32702 }
32703 }
32704 /* Worker num: argument and vector length: arguments. */
32705 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32706 && id_equal (next->u.value, id)
32707 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32708 {
32709 cp_lexer_consume_token (lexer); /* id */
32710 cp_lexer_consume_token (lexer); /* ':' */
32711 }
32712
32713 /* Now collect the actual argument. */
32714 if (ops[idx] != NULL_TREE)
32715 {
32716 cp_parser_error (parser, "unexpected argument");
32717 goto cleanup_error;
32718 }
32719
32720 tree expr = cp_parser_assignment_expression (parser, NULL, false,
32721 false);
32722 if (expr == error_mark_node)
32723 goto cleanup_error;
32724
32725 mark_exp_read (expr);
32726 ops[idx] = expr;
32727
32728 if (kind == OMP_CLAUSE_GANG
32729 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32730 {
32731 cp_lexer_consume_token (lexer);
32732 continue;
32733 }
32734 break;
32735 }
32736 while (1);
32737
32738 if (!parens.require_close (parser))
32739 goto cleanup_error;
32740 }
32741
32742 check_no_duplicate_clause (list, kind, str, loc);
32743
32744 c = build_omp_clause (loc, kind);
32745
32746 if (ops[1])
32747 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32748
32749 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32750 OMP_CLAUSE_CHAIN (c) = list;
32751
32752 return c;
32753
32754 cleanup_error:
32755 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32756 return list;
32757 }
32758
32759 /* OpenACC 2.0:
32760 tile ( size-expr-list ) */
32761
32762 static tree
32763 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32764 {
32765 tree c, expr = error_mark_node;
32766 tree tile = NULL_TREE;
32767
32768 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32769 so, but the spec authors never considered such a case and have
32770 differing opinions on what it might mean, including 'not
32771 allowed'.) */
32772 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32773 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32774 clause_loc);
32775
32776 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32777 return list;
32778
32779 do
32780 {
32781 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32782 return list;
32783
32784 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32785 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32786 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32787 {
32788 cp_lexer_consume_token (parser->lexer);
32789 expr = integer_zero_node;
32790 }
32791 else
32792 expr = cp_parser_constant_expression (parser);
32793
32794 tile = tree_cons (NULL_TREE, expr, tile);
32795 }
32796 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32797
32798 /* Consume the trailing ')'. */
32799 cp_lexer_consume_token (parser->lexer);
32800
32801 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32802 tile = nreverse (tile);
32803 OMP_CLAUSE_TILE_LIST (c) = tile;
32804 OMP_CLAUSE_CHAIN (c) = list;
32805 return c;
32806 }
32807
32808 /* OpenACC 2.0
32809 Parse wait clause or directive parameters. */
32810
32811 static tree
32812 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32813 {
32814 vec<tree, va_gc> *args;
32815 tree t, args_tree;
32816
32817 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32818 /*cast_p=*/false,
32819 /*allow_expansion_p=*/true,
32820 /*non_constant_p=*/NULL);
32821
32822 if (args == NULL || args->length () == 0)
32823 {
32824 if (args != NULL)
32825 {
32826 cp_parser_error (parser, "expected integer expression list");
32827 release_tree_vector (args);
32828 }
32829 return list;
32830 }
32831
32832 args_tree = build_tree_list_vec (args);
32833
32834 release_tree_vector (args);
32835
32836 for (t = args_tree; t; t = TREE_CHAIN (t))
32837 {
32838 tree targ = TREE_VALUE (t);
32839
32840 if (targ != error_mark_node)
32841 {
32842 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32843 error ("%<wait%> expression must be integral");
32844 else
32845 {
32846 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32847
32848 targ = mark_rvalue_use (targ);
32849 OMP_CLAUSE_DECL (c) = targ;
32850 OMP_CLAUSE_CHAIN (c) = list;
32851 list = c;
32852 }
32853 }
32854 }
32855
32856 return list;
32857 }
32858
32859 /* OpenACC:
32860 wait ( int-expr-list ) */
32861
32862 static tree
32863 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32864 {
32865 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32866
32867 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32868 return list;
32869
32870 list = cp_parser_oacc_wait_list (parser, location, list);
32871
32872 return list;
32873 }
32874
32875 /* OpenMP 3.0:
32876 collapse ( constant-expression ) */
32877
32878 static tree
32879 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32880 {
32881 tree c, num;
32882 location_t loc;
32883 HOST_WIDE_INT n;
32884
32885 loc = cp_lexer_peek_token (parser->lexer)->location;
32886 matching_parens parens;
32887 if (!parens.require_open (parser))
32888 return list;
32889
32890 num = cp_parser_constant_expression (parser);
32891
32892 if (!parens.require_close (parser))
32893 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32894 /*or_comma=*/false,
32895 /*consume_paren=*/true);
32896
32897 if (num == error_mark_node)
32898 return list;
32899 num = fold_non_dependent_expr (num);
32900 if (!tree_fits_shwi_p (num)
32901 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32902 || (n = tree_to_shwi (num)) <= 0
32903 || (int) n != n)
32904 {
32905 error_at (loc, "collapse argument needs positive constant integer expression");
32906 return list;
32907 }
32908
32909 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32910 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32911 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32912 OMP_CLAUSE_CHAIN (c) = list;
32913 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32914
32915 return c;
32916 }
32917
32918 /* OpenMP 2.5:
32919 default ( none | shared )
32920
32921 OpenACC:
32922 default ( none | present ) */
32923
32924 static tree
32925 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32926 location_t location, bool is_oacc)
32927 {
32928 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32929 tree c;
32930
32931 matching_parens parens;
32932 if (!parens.require_open (parser))
32933 return list;
32934 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32935 {
32936 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32937 const char *p = IDENTIFIER_POINTER (id);
32938
32939 switch (p[0])
32940 {
32941 case 'n':
32942 if (strcmp ("none", p) != 0)
32943 goto invalid_kind;
32944 kind = OMP_CLAUSE_DEFAULT_NONE;
32945 break;
32946
32947 case 'p':
32948 if (strcmp ("present", p) != 0 || !is_oacc)
32949 goto invalid_kind;
32950 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32951 break;
32952
32953 case 's':
32954 if (strcmp ("shared", p) != 0 || is_oacc)
32955 goto invalid_kind;
32956 kind = OMP_CLAUSE_DEFAULT_SHARED;
32957 break;
32958
32959 default:
32960 goto invalid_kind;
32961 }
32962
32963 cp_lexer_consume_token (parser->lexer);
32964 }
32965 else
32966 {
32967 invalid_kind:
32968 if (is_oacc)
32969 cp_parser_error (parser, "expected %<none%> or %<present%>");
32970 else
32971 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32972 }
32973
32974 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32975 || !parens.require_close (parser))
32976 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32977 /*or_comma=*/false,
32978 /*consume_paren=*/true);
32979
32980 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32981 return list;
32982
32983 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32984 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32985 OMP_CLAUSE_CHAIN (c) = list;
32986 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32987
32988 return c;
32989 }
32990
32991 /* OpenMP 3.1:
32992 final ( expression ) */
32993
32994 static tree
32995 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32996 {
32997 tree t, c;
32998
32999 matching_parens parens;
33000 if (!parens.require_open (parser))
33001 return list;
33002
33003 t = cp_parser_assignment_expression (parser);
33004
33005 if (t == error_mark_node
33006 || !parens.require_close (parser))
33007 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33008 /*or_comma=*/false,
33009 /*consume_paren=*/true);
33010
33011 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
33012
33013 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
33014 OMP_CLAUSE_FINAL_EXPR (c) = t;
33015 OMP_CLAUSE_CHAIN (c) = list;
33016
33017 return c;
33018 }
33019
33020 /* OpenMP 2.5:
33021 if ( expression )
33022
33023 OpenMP 4.5:
33024 if ( directive-name-modifier : expression )
33025
33026 directive-name-modifier:
33027 parallel | task | taskloop | target data | target | target update
33028 | target enter data | target exit data
33029
33030 OpenMP 5.0:
33031 directive-name-modifier:
33032 ... | simd | cancel */
33033
33034 static tree
33035 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
33036 bool is_omp)
33037 {
33038 tree t, c;
33039 enum tree_code if_modifier = ERROR_MARK;
33040
33041 matching_parens parens;
33042 if (!parens.require_open (parser))
33043 return list;
33044
33045 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33046 {
33047 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33048 const char *p = IDENTIFIER_POINTER (id);
33049 int n = 2;
33050
33051 if (strcmp ("cancel", p) == 0)
33052 if_modifier = VOID_CST;
33053 else if (strcmp ("parallel", p) == 0)
33054 if_modifier = OMP_PARALLEL;
33055 else if (strcmp ("simd", p) == 0)
33056 if_modifier = OMP_SIMD;
33057 else if (strcmp ("task", p) == 0)
33058 if_modifier = OMP_TASK;
33059 else if (strcmp ("taskloop", p) == 0)
33060 if_modifier = OMP_TASKLOOP;
33061 else if (strcmp ("target", p) == 0)
33062 {
33063 if_modifier = OMP_TARGET;
33064 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
33065 {
33066 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
33067 p = IDENTIFIER_POINTER (id);
33068 if (strcmp ("data", p) == 0)
33069 if_modifier = OMP_TARGET_DATA;
33070 else if (strcmp ("update", p) == 0)
33071 if_modifier = OMP_TARGET_UPDATE;
33072 else if (strcmp ("enter", p) == 0)
33073 if_modifier = OMP_TARGET_ENTER_DATA;
33074 else if (strcmp ("exit", p) == 0)
33075 if_modifier = OMP_TARGET_EXIT_DATA;
33076 if (if_modifier != OMP_TARGET)
33077 n = 3;
33078 else
33079 {
33080 location_t loc
33081 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
33082 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
33083 "or %<exit%>");
33084 if_modifier = ERROR_MARK;
33085 }
33086 if (if_modifier == OMP_TARGET_ENTER_DATA
33087 || if_modifier == OMP_TARGET_EXIT_DATA)
33088 {
33089 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
33090 {
33091 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
33092 p = IDENTIFIER_POINTER (id);
33093 if (strcmp ("data", p) == 0)
33094 n = 4;
33095 }
33096 if (n != 4)
33097 {
33098 location_t loc
33099 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
33100 error_at (loc, "expected %<data%>");
33101 if_modifier = ERROR_MARK;
33102 }
33103 }
33104 }
33105 }
33106 if (if_modifier != ERROR_MARK)
33107 {
33108 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
33109 {
33110 while (n-- > 0)
33111 cp_lexer_consume_token (parser->lexer);
33112 }
33113 else
33114 {
33115 if (n > 2)
33116 {
33117 location_t loc
33118 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
33119 error_at (loc, "expected %<:%>");
33120 }
33121 if_modifier = ERROR_MARK;
33122 }
33123 }
33124 }
33125
33126 t = cp_parser_assignment_expression (parser);
33127
33128 if (t == error_mark_node
33129 || !parens.require_close (parser))
33130 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33131 /*or_comma=*/false,
33132 /*consume_paren=*/true);
33133
33134 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33135 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
33136 {
33137 if (if_modifier != ERROR_MARK
33138 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33139 {
33140 const char *p = NULL;
33141 switch (if_modifier)
33142 {
33143 case VOID_CST: p = "cancel"; break;
33144 case OMP_PARALLEL: p = "parallel"; break;
33145 case OMP_SIMD: p = "simd"; break;
33146 case OMP_TASK: p = "task"; break;
33147 case OMP_TASKLOOP: p = "taskloop"; break;
33148 case OMP_TARGET_DATA: p = "target data"; break;
33149 case OMP_TARGET: p = "target"; break;
33150 case OMP_TARGET_UPDATE: p = "target update"; break;
33151 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
33152 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
33153 default: gcc_unreachable ();
33154 }
33155 error_at (location, "too many %<if%> clauses with %qs modifier",
33156 p);
33157 return list;
33158 }
33159 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33160 {
33161 if (!is_omp)
33162 error_at (location, "too many %<if%> clauses");
33163 else
33164 error_at (location, "too many %<if%> clauses without modifier");
33165 return list;
33166 }
33167 else if (if_modifier == ERROR_MARK
33168 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
33169 {
33170 error_at (location, "if any %<if%> clause has modifier, then all "
33171 "%<if%> clauses have to use modifier");
33172 return list;
33173 }
33174 }
33175
33176 c = build_omp_clause (location, OMP_CLAUSE_IF);
33177 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
33178 OMP_CLAUSE_IF_EXPR (c) = t;
33179 OMP_CLAUSE_CHAIN (c) = list;
33180
33181 return c;
33182 }
33183
33184 /* OpenMP 3.1:
33185 mergeable */
33186
33187 static tree
33188 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
33189 tree list, location_t location)
33190 {
33191 tree c;
33192
33193 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
33194 location);
33195
33196 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
33197 OMP_CLAUSE_CHAIN (c) = list;
33198 return c;
33199 }
33200
33201 /* OpenMP 2.5:
33202 nowait */
33203
33204 static tree
33205 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
33206 tree list, location_t location)
33207 {
33208 tree c;
33209
33210 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
33211
33212 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
33213 OMP_CLAUSE_CHAIN (c) = list;
33214 return c;
33215 }
33216
33217 /* OpenMP 2.5:
33218 num_threads ( expression ) */
33219
33220 static tree
33221 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
33222 location_t location)
33223 {
33224 tree t, c;
33225
33226 matching_parens parens;
33227 if (!parens.require_open (parser))
33228 return list;
33229
33230 t = cp_parser_assignment_expression (parser);
33231
33232 if (t == error_mark_node
33233 || !parens.require_close (parser))
33234 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33235 /*or_comma=*/false,
33236 /*consume_paren=*/true);
33237
33238 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
33239 "num_threads", location);
33240
33241 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
33242 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
33243 OMP_CLAUSE_CHAIN (c) = list;
33244
33245 return c;
33246 }
33247
33248 /* OpenMP 4.5:
33249 num_tasks ( expression ) */
33250
33251 static tree
33252 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
33253 location_t location)
33254 {
33255 tree t, c;
33256
33257 matching_parens parens;
33258 if (!parens.require_open (parser))
33259 return list;
33260
33261 t = cp_parser_assignment_expression (parser);
33262
33263 if (t == error_mark_node
33264 || !parens.require_close (parser))
33265 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33266 /*or_comma=*/false,
33267 /*consume_paren=*/true);
33268
33269 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
33270 "num_tasks", location);
33271
33272 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
33273 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
33274 OMP_CLAUSE_CHAIN (c) = list;
33275
33276 return c;
33277 }
33278
33279 /* OpenMP 4.5:
33280 grainsize ( expression ) */
33281
33282 static tree
33283 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
33284 location_t location)
33285 {
33286 tree t, c;
33287
33288 matching_parens parens;
33289 if (!parens.require_open (parser))
33290 return list;
33291
33292 t = cp_parser_assignment_expression (parser);
33293
33294 if (t == error_mark_node
33295 || !parens.require_close (parser))
33296 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33297 /*or_comma=*/false,
33298 /*consume_paren=*/true);
33299
33300 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
33301 "grainsize", location);
33302
33303 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
33304 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
33305 OMP_CLAUSE_CHAIN (c) = list;
33306
33307 return c;
33308 }
33309
33310 /* OpenMP 4.5:
33311 priority ( expression ) */
33312
33313 static tree
33314 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
33315 location_t location)
33316 {
33317 tree t, c;
33318
33319 matching_parens parens;
33320 if (!parens.require_open (parser))
33321 return list;
33322
33323 t = cp_parser_assignment_expression (parser);
33324
33325 if (t == error_mark_node
33326 || !parens.require_close (parser))
33327 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33328 /*or_comma=*/false,
33329 /*consume_paren=*/true);
33330
33331 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
33332 "priority", location);
33333
33334 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
33335 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
33336 OMP_CLAUSE_CHAIN (c) = list;
33337
33338 return c;
33339 }
33340
33341 /* OpenMP 4.5:
33342 hint ( expression ) */
33343
33344 static tree
33345 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
33346 {
33347 tree t, c;
33348
33349 matching_parens parens;
33350 if (!parens.require_open (parser))
33351 return list;
33352
33353 t = cp_parser_assignment_expression (parser);
33354
33355 if (t == error_mark_node
33356 || !parens.require_close (parser))
33357 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33358 /*or_comma=*/false,
33359 /*consume_paren=*/true);
33360
33361 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
33362
33363 c = build_omp_clause (location, OMP_CLAUSE_HINT);
33364 OMP_CLAUSE_HINT_EXPR (c) = t;
33365 OMP_CLAUSE_CHAIN (c) = list;
33366
33367 return c;
33368 }
33369
33370 /* OpenMP 4.5:
33371 defaultmap ( tofrom : scalar )
33372
33373 OpenMP 5.0:
33374 defaultmap ( implicit-behavior [ : variable-category ] ) */
33375
33376 static tree
33377 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
33378 location_t location)
33379 {
33380 tree c, id;
33381 const char *p;
33382 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33383 enum omp_clause_defaultmap_kind category
33384 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
33385
33386 matching_parens parens;
33387 if (!parens.require_open (parser))
33388 return list;
33389
33390 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
33391 p = "default";
33392 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33393 {
33394 invalid_behavior:
33395 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
33396 "%<tofrom%>, %<firstprivate%>, %<none%> "
33397 "or %<default%>");
33398 goto out_err;
33399 }
33400 else
33401 {
33402 id = cp_lexer_peek_token (parser->lexer)->u.value;
33403 p = IDENTIFIER_POINTER (id);
33404 }
33405
33406 switch (p[0])
33407 {
33408 case 'a':
33409 if (strcmp ("alloc", p) == 0)
33410 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
33411 else
33412 goto invalid_behavior;
33413 break;
33414
33415 case 'd':
33416 if (strcmp ("default", p) == 0)
33417 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33418 else
33419 goto invalid_behavior;
33420 break;
33421
33422 case 'f':
33423 if (strcmp ("firstprivate", p) == 0)
33424 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
33425 else if (strcmp ("from", p) == 0)
33426 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
33427 else
33428 goto invalid_behavior;
33429 break;
33430
33431 case 'n':
33432 if (strcmp ("none", p) == 0)
33433 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
33434 else
33435 goto invalid_behavior;
33436 break;
33437
33438 case 't':
33439 if (strcmp ("tofrom", p) == 0)
33440 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
33441 else if (strcmp ("to", p) == 0)
33442 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
33443 else
33444 goto invalid_behavior;
33445 break;
33446
33447 default:
33448 goto invalid_behavior;
33449 }
33450 cp_lexer_consume_token (parser->lexer);
33451
33452 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33453 {
33454 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33455 goto out_err;
33456
33457 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33458 {
33459 invalid_category:
33460 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
33461 "%<pointer%>");
33462 goto out_err;
33463 }
33464 id = cp_lexer_peek_token (parser->lexer)->u.value;
33465 p = IDENTIFIER_POINTER (id);
33466
33467 switch (p[0])
33468 {
33469 case 'a':
33470 if (strcmp ("aggregate", p) == 0)
33471 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
33472 else
33473 goto invalid_category;
33474 break;
33475
33476 case 'p':
33477 if (strcmp ("pointer", p) == 0)
33478 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
33479 else
33480 goto invalid_category;
33481 break;
33482
33483 case 's':
33484 if (strcmp ("scalar", p) == 0)
33485 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
33486 else
33487 goto invalid_category;
33488 break;
33489
33490 default:
33491 goto invalid_category;
33492 }
33493
33494 cp_lexer_consume_token (parser->lexer);
33495 }
33496 if (!parens.require_close (parser))
33497 goto out_err;
33498
33499 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33500 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
33501 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33502 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
33503 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
33504 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
33505 {
33506 enum omp_clause_defaultmap_kind cat = category;
33507 location_t loc = OMP_CLAUSE_LOCATION (c);
33508 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
33509 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
33510 p = NULL;
33511 switch (cat)
33512 {
33513 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
33514 p = NULL;
33515 break;
33516 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
33517 p = "aggregate";
33518 break;
33519 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
33520 p = "pointer";
33521 break;
33522 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
33523 p = "scalar";
33524 break;
33525 default:
33526 gcc_unreachable ();
33527 }
33528 if (p)
33529 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
33530 p);
33531 else
33532 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
33533 "category");
33534 break;
33535 }
33536
33537 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33538 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33539 OMP_CLAUSE_CHAIN (c) = list;
33540 return c;
33541
33542 out_err:
33543 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33544 /*or_comma=*/false,
33545 /*consume_paren=*/true);
33546 return list;
33547 }
33548
33549 /* OpenMP 2.5:
33550 ordered
33551
33552 OpenMP 4.5:
33553 ordered ( constant-expression ) */
33554
33555 static tree
33556 cp_parser_omp_clause_ordered (cp_parser *parser,
33557 tree list, location_t location)
33558 {
33559 tree c, num = NULL_TREE;
33560 HOST_WIDE_INT n;
33561
33562 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
33563 "ordered", location);
33564
33565 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33566 {
33567 matching_parens parens;
33568 parens.consume_open (parser);
33569
33570 num = cp_parser_constant_expression (parser);
33571
33572 if (!parens.require_close (parser))
33573 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33574 /*or_comma=*/false,
33575 /*consume_paren=*/true);
33576
33577 if (num == error_mark_node)
33578 return list;
33579 num = fold_non_dependent_expr (num);
33580 if (!tree_fits_shwi_p (num)
33581 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33582 || (n = tree_to_shwi (num)) <= 0
33583 || (int) n != n)
33584 {
33585 error_at (location,
33586 "ordered argument needs positive constant integer "
33587 "expression");
33588 return list;
33589 }
33590 }
33591
33592 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
33593 OMP_CLAUSE_ORDERED_EXPR (c) = num;
33594 OMP_CLAUSE_CHAIN (c) = list;
33595 return c;
33596 }
33597
33598 /* OpenMP 2.5:
33599 reduction ( reduction-operator : variable-list )
33600
33601 reduction-operator:
33602 One of: + * - & ^ | && ||
33603
33604 OpenMP 3.1:
33605
33606 reduction-operator:
33607 One of: + * - & ^ | && || min max
33608
33609 OpenMP 4.0:
33610
33611 reduction-operator:
33612 One of: + * - & ^ | && ||
33613 id-expression
33614
33615 OpenMP 5.0:
33616 reduction ( reduction-modifier, reduction-operator : variable-list )
33617 in_reduction ( reduction-operator : variable-list )
33618 task_reduction ( reduction-operator : variable-list ) */
33619
33620 static tree
33621 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
33622 bool is_omp, tree list)
33623 {
33624 enum tree_code code = ERROR_MARK;
33625 tree nlist, c, id = NULL_TREE;
33626 bool task = false;
33627 bool inscan = false;
33628
33629 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33630 return list;
33631
33632 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
33633 {
33634 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
33635 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33636 {
33637 cp_lexer_consume_token (parser->lexer);
33638 cp_lexer_consume_token (parser->lexer);
33639 }
33640 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33641 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33642 {
33643 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33644 const char *p = IDENTIFIER_POINTER (id);
33645 if (strcmp (p, "task") == 0)
33646 task = true;
33647 else if (strcmp (p, "inscan") == 0)
33648 {
33649 inscan = true;
33650 sorry ("%<inscan%> modifier on %<reduction%> clause "
33651 "not supported yet");
33652 }
33653 if (task || inscan)
33654 {
33655 cp_lexer_consume_token (parser->lexer);
33656 cp_lexer_consume_token (parser->lexer);
33657 }
33658 }
33659 }
33660
33661 switch (cp_lexer_peek_token (parser->lexer)->type)
33662 {
33663 case CPP_PLUS: code = PLUS_EXPR; break;
33664 case CPP_MULT: code = MULT_EXPR; break;
33665 case CPP_MINUS: code = MINUS_EXPR; break;
33666 case CPP_AND: code = BIT_AND_EXPR; break;
33667 case CPP_XOR: code = BIT_XOR_EXPR; break;
33668 case CPP_OR: code = BIT_IOR_EXPR; break;
33669 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
33670 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
33671 default: break;
33672 }
33673
33674 if (code != ERROR_MARK)
33675 cp_lexer_consume_token (parser->lexer);
33676 else
33677 {
33678 bool saved_colon_corrects_to_scope_p;
33679 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33680 parser->colon_corrects_to_scope_p = false;
33681 id = cp_parser_id_expression (parser, /*template_p=*/false,
33682 /*check_dependency_p=*/true,
33683 /*template_p=*/NULL,
33684 /*declarator_p=*/false,
33685 /*optional_p=*/false);
33686 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33687 if (identifier_p (id))
33688 {
33689 const char *p = IDENTIFIER_POINTER (id);
33690
33691 if (strcmp (p, "min") == 0)
33692 code = MIN_EXPR;
33693 else if (strcmp (p, "max") == 0)
33694 code = MAX_EXPR;
33695 else if (id == ovl_op_identifier (false, PLUS_EXPR))
33696 code = PLUS_EXPR;
33697 else if (id == ovl_op_identifier (false, MULT_EXPR))
33698 code = MULT_EXPR;
33699 else if (id == ovl_op_identifier (false, MINUS_EXPR))
33700 code = MINUS_EXPR;
33701 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
33702 code = BIT_AND_EXPR;
33703 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
33704 code = BIT_IOR_EXPR;
33705 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
33706 code = BIT_XOR_EXPR;
33707 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
33708 code = TRUTH_ANDIF_EXPR;
33709 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
33710 code = TRUTH_ORIF_EXPR;
33711 id = omp_reduction_id (code, id, NULL_TREE);
33712 tree scope = parser->scope;
33713 if (scope)
33714 id = build_qualified_name (NULL_TREE, scope, id, false);
33715 parser->scope = NULL_TREE;
33716 parser->qualifying_scope = NULL_TREE;
33717 parser->object_scope = NULL_TREE;
33718 }
33719 else
33720 {
33721 error ("invalid reduction-identifier");
33722 resync_fail:
33723 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33724 /*or_comma=*/false,
33725 /*consume_paren=*/true);
33726 return list;
33727 }
33728 }
33729
33730 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33731 goto resync_fail;
33732
33733 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
33734 NULL);
33735 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33736 {
33737 OMP_CLAUSE_REDUCTION_CODE (c) = code;
33738 if (task)
33739 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
33740 else if (inscan)
33741 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
33742 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
33743 }
33744
33745 return nlist;
33746 }
33747
33748 /* OpenMP 2.5:
33749 schedule ( schedule-kind )
33750 schedule ( schedule-kind , expression )
33751
33752 schedule-kind:
33753 static | dynamic | guided | runtime | auto
33754
33755 OpenMP 4.5:
33756 schedule ( schedule-modifier : schedule-kind )
33757 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33758
33759 schedule-modifier:
33760 simd
33761 monotonic
33762 nonmonotonic */
33763
33764 static tree
33765 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
33766 {
33767 tree c, t;
33768 int modifiers = 0, nmodifiers = 0;
33769
33770 matching_parens parens;
33771 if (!parens.require_open (parser))
33772 return list;
33773
33774 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
33775
33776 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33777 {
33778 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33779 const char *p = IDENTIFIER_POINTER (id);
33780 if (strcmp ("simd", p) == 0)
33781 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
33782 else if (strcmp ("monotonic", p) == 0)
33783 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
33784 else if (strcmp ("nonmonotonic", p) == 0)
33785 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
33786 else
33787 break;
33788 cp_lexer_consume_token (parser->lexer);
33789 if (nmodifiers++ == 0
33790 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33791 cp_lexer_consume_token (parser->lexer);
33792 else
33793 {
33794 cp_parser_require (parser, CPP_COLON, RT_COLON);
33795 break;
33796 }
33797 }
33798
33799 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33800 {
33801 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33802 const char *p = IDENTIFIER_POINTER (id);
33803
33804 switch (p[0])
33805 {
33806 case 'd':
33807 if (strcmp ("dynamic", p) != 0)
33808 goto invalid_kind;
33809 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
33810 break;
33811
33812 case 'g':
33813 if (strcmp ("guided", p) != 0)
33814 goto invalid_kind;
33815 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
33816 break;
33817
33818 case 'r':
33819 if (strcmp ("runtime", p) != 0)
33820 goto invalid_kind;
33821 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
33822 break;
33823
33824 default:
33825 goto invalid_kind;
33826 }
33827 }
33828 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33829 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
33830 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33831 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
33832 else
33833 goto invalid_kind;
33834 cp_lexer_consume_token (parser->lexer);
33835
33836 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
33837 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33838 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33839 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33840 {
33841 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33842 "specified");
33843 modifiers = 0;
33844 }
33845
33846 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33847 {
33848 cp_token *token;
33849 cp_lexer_consume_token (parser->lexer);
33850
33851 token = cp_lexer_peek_token (parser->lexer);
33852 t = cp_parser_assignment_expression (parser);
33853
33854 if (t == error_mark_node)
33855 goto resync_fail;
33856 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
33857 error_at (token->location, "schedule %<runtime%> does not take "
33858 "a %<chunk_size%> parameter");
33859 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
33860 error_at (token->location, "schedule %<auto%> does not take "
33861 "a %<chunk_size%> parameter");
33862 else
33863 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
33864
33865 if (!parens.require_close (parser))
33866 goto resync_fail;
33867 }
33868 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33869 goto resync_fail;
33870
33871 OMP_CLAUSE_SCHEDULE_KIND (c)
33872 = (enum omp_clause_schedule_kind)
33873 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
33874
33875 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
33876 OMP_CLAUSE_CHAIN (c) = list;
33877 return c;
33878
33879 invalid_kind:
33880 cp_parser_error (parser, "invalid schedule kind");
33881 resync_fail:
33882 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33883 /*or_comma=*/false,
33884 /*consume_paren=*/true);
33885 return list;
33886 }
33887
33888 /* OpenMP 3.0:
33889 untied */
33890
33891 static tree
33892 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
33893 tree list, location_t location)
33894 {
33895 tree c;
33896
33897 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
33898
33899 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
33900 OMP_CLAUSE_CHAIN (c) = list;
33901 return c;
33902 }
33903
33904 /* OpenMP 4.0:
33905 inbranch
33906 notinbranch */
33907
33908 static tree
33909 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33910 tree list, location_t location)
33911 {
33912 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33913 tree c = build_omp_clause (location, code);
33914 OMP_CLAUSE_CHAIN (c) = list;
33915 return c;
33916 }
33917
33918 /* OpenMP 4.0:
33919 parallel
33920 for
33921 sections
33922 taskgroup */
33923
33924 static tree
33925 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33926 enum omp_clause_code code,
33927 tree list, location_t location)
33928 {
33929 tree c = build_omp_clause (location, code);
33930 OMP_CLAUSE_CHAIN (c) = list;
33931 return c;
33932 }
33933
33934 /* OpenMP 4.5:
33935 nogroup */
33936
33937 static tree
33938 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33939 tree list, location_t location)
33940 {
33941 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33942 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33943 OMP_CLAUSE_CHAIN (c) = list;
33944 return c;
33945 }
33946
33947 /* OpenMP 4.5:
33948 simd
33949 threads */
33950
33951 static tree
33952 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33953 enum omp_clause_code code,
33954 tree list, location_t location)
33955 {
33956 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33957 tree c = build_omp_clause (location, code);
33958 OMP_CLAUSE_CHAIN (c) = list;
33959 return c;
33960 }
33961
33962 /* OpenMP 4.0:
33963 num_teams ( expression ) */
33964
33965 static tree
33966 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33967 location_t location)
33968 {
33969 tree t, c;
33970
33971 matching_parens parens;
33972 if (!parens.require_open (parser))
33973 return list;
33974
33975 t = cp_parser_assignment_expression (parser);
33976
33977 if (t == error_mark_node
33978 || !parens.require_close (parser))
33979 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33980 /*or_comma=*/false,
33981 /*consume_paren=*/true);
33982
33983 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33984 "num_teams", location);
33985
33986 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33987 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33988 OMP_CLAUSE_CHAIN (c) = list;
33989
33990 return c;
33991 }
33992
33993 /* OpenMP 4.0:
33994 thread_limit ( expression ) */
33995
33996 static tree
33997 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33998 location_t location)
33999 {
34000 tree t, c;
34001
34002 matching_parens parens;
34003 if (!parens.require_open (parser))
34004 return list;
34005
34006 t = cp_parser_assignment_expression (parser);
34007
34008 if (t == error_mark_node
34009 || !parens.require_close (parser))
34010 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34011 /*or_comma=*/false,
34012 /*consume_paren=*/true);
34013
34014 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
34015 "thread_limit", location);
34016
34017 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
34018 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
34019 OMP_CLAUSE_CHAIN (c) = list;
34020
34021 return c;
34022 }
34023
34024 /* OpenMP 4.0:
34025 aligned ( variable-list )
34026 aligned ( variable-list : constant-expression ) */
34027
34028 static tree
34029 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
34030 {
34031 tree nlist, c, alignment = NULL_TREE;
34032 bool colon;
34033
34034 matching_parens parens;
34035 if (!parens.require_open (parser))
34036 return list;
34037
34038 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
34039 &colon);
34040
34041 if (colon)
34042 {
34043 alignment = cp_parser_constant_expression (parser);
34044
34045 if (!parens.require_close (parser))
34046 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34047 /*or_comma=*/false,
34048 /*consume_paren=*/true);
34049
34050 if (alignment == error_mark_node)
34051 alignment = NULL_TREE;
34052 }
34053
34054 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34055 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
34056
34057 return nlist;
34058 }
34059
34060 /* OpenMP 2.5:
34061 lastprivate ( variable-list )
34062
34063 OpenMP 5.0:
34064 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
34065
34066 static tree
34067 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
34068 {
34069 bool conditional = false;
34070
34071 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34072 return list;
34073
34074 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34075 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
34076 {
34077 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34078 const char *p = IDENTIFIER_POINTER (id);
34079
34080 if (strcmp ("conditional", p) == 0)
34081 {
34082 conditional = true;
34083 cp_lexer_consume_token (parser->lexer);
34084 cp_lexer_consume_token (parser->lexer);
34085 }
34086 }
34087
34088 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
34089 list, NULL);
34090
34091 if (conditional)
34092 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34093 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
34094 return nlist;
34095 }
34096
34097 /* OpenMP 4.0:
34098 linear ( variable-list )
34099 linear ( variable-list : expression )
34100
34101 OpenMP 4.5:
34102 linear ( modifier ( variable-list ) )
34103 linear ( modifier ( variable-list ) : expression ) */
34104
34105 static tree
34106 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
34107 bool declare_simd)
34108 {
34109 tree nlist, c, step = integer_one_node;
34110 bool colon;
34111 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
34112
34113 matching_parens parens;
34114 if (!parens.require_open (parser))
34115 return list;
34116
34117 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34118 {
34119 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34120 const char *p = IDENTIFIER_POINTER (id);
34121
34122 if (strcmp ("ref", p) == 0)
34123 kind = OMP_CLAUSE_LINEAR_REF;
34124 else if (strcmp ("val", p) == 0)
34125 kind = OMP_CLAUSE_LINEAR_VAL;
34126 else if (strcmp ("uval", p) == 0)
34127 kind = OMP_CLAUSE_LINEAR_UVAL;
34128 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
34129 cp_lexer_consume_token (parser->lexer);
34130 else
34131 kind = OMP_CLAUSE_LINEAR_DEFAULT;
34132 }
34133
34134 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
34135 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
34136 &colon);
34137 else
34138 {
34139 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
34140 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
34141 if (colon)
34142 cp_parser_require (parser, CPP_COLON, RT_COLON);
34143 else if (!parens.require_close (parser))
34144 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34145 /*or_comma=*/false,
34146 /*consume_paren=*/true);
34147 }
34148
34149 if (colon)
34150 {
34151 step = NULL_TREE;
34152 if (declare_simd
34153 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34154 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
34155 {
34156 cp_token *token = cp_lexer_peek_token (parser->lexer);
34157 cp_parser_parse_tentatively (parser);
34158 step = cp_parser_id_expression (parser, /*template_p=*/false,
34159 /*check_dependency_p=*/true,
34160 /*template_p=*/NULL,
34161 /*declarator_p=*/false,
34162 /*optional_p=*/false);
34163 if (step != error_mark_node)
34164 step = cp_parser_lookup_name_simple (parser, step, token->location);
34165 if (step == error_mark_node)
34166 {
34167 step = NULL_TREE;
34168 cp_parser_abort_tentative_parse (parser);
34169 }
34170 else if (!cp_parser_parse_definitely (parser))
34171 step = NULL_TREE;
34172 }
34173 if (!step)
34174 step = cp_parser_assignment_expression (parser);
34175
34176 if (!parens.require_close (parser))
34177 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34178 /*or_comma=*/false,
34179 /*consume_paren=*/true);
34180
34181 if (step == error_mark_node)
34182 return list;
34183 }
34184
34185 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34186 {
34187 OMP_CLAUSE_LINEAR_STEP (c) = step;
34188 OMP_CLAUSE_LINEAR_KIND (c) = kind;
34189 }
34190
34191 return nlist;
34192 }
34193
34194 /* OpenMP 4.0:
34195 safelen ( constant-expression ) */
34196
34197 static tree
34198 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
34199 location_t location)
34200 {
34201 tree t, c;
34202
34203 matching_parens parens;
34204 if (!parens.require_open (parser))
34205 return list;
34206
34207 t = cp_parser_constant_expression (parser);
34208
34209 if (t == error_mark_node
34210 || !parens.require_close (parser))
34211 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34212 /*or_comma=*/false,
34213 /*consume_paren=*/true);
34214
34215 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
34216
34217 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
34218 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
34219 OMP_CLAUSE_CHAIN (c) = list;
34220
34221 return c;
34222 }
34223
34224 /* OpenMP 4.0:
34225 simdlen ( constant-expression ) */
34226
34227 static tree
34228 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
34229 location_t location)
34230 {
34231 tree t, c;
34232
34233 matching_parens parens;
34234 if (!parens.require_open (parser))
34235 return list;
34236
34237 t = cp_parser_constant_expression (parser);
34238
34239 if (t == error_mark_node
34240 || !parens.require_close (parser))
34241 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34242 /*or_comma=*/false,
34243 /*consume_paren=*/true);
34244
34245 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
34246
34247 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
34248 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
34249 OMP_CLAUSE_CHAIN (c) = list;
34250
34251 return c;
34252 }
34253
34254 /* OpenMP 4.5:
34255 vec:
34256 identifier [+/- integer]
34257 vec , identifier [+/- integer]
34258 */
34259
34260 static tree
34261 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
34262 tree list)
34263 {
34264 tree vec = NULL;
34265
34266 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34267 {
34268 cp_parser_error (parser, "expected identifier");
34269 return list;
34270 }
34271
34272 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34273 {
34274 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
34275 tree t, identifier = cp_parser_identifier (parser);
34276 tree addend = NULL;
34277
34278 if (identifier == error_mark_node)
34279 t = error_mark_node;
34280 else
34281 {
34282 t = cp_parser_lookup_name_simple
34283 (parser, identifier,
34284 cp_lexer_peek_token (parser->lexer)->location);
34285 if (t == error_mark_node)
34286 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
34287 id_loc);
34288 }
34289
34290 bool neg = false;
34291 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
34292 neg = true;
34293 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
34294 {
34295 addend = integer_zero_node;
34296 goto add_to_vector;
34297 }
34298 cp_lexer_consume_token (parser->lexer);
34299
34300 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
34301 {
34302 cp_parser_error (parser, "expected integer");
34303 return list;
34304 }
34305
34306 addend = cp_lexer_peek_token (parser->lexer)->u.value;
34307 if (TREE_CODE (addend) != INTEGER_CST)
34308 {
34309 cp_parser_error (parser, "expected integer");
34310 return list;
34311 }
34312 cp_lexer_consume_token (parser->lexer);
34313
34314 add_to_vector:
34315 if (t != error_mark_node)
34316 {
34317 vec = tree_cons (addend, t, vec);
34318 if (neg)
34319 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
34320 }
34321
34322 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
34323 break;
34324
34325 cp_lexer_consume_token (parser->lexer);
34326 }
34327
34328 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
34329 {
34330 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
34331 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
34332 OMP_CLAUSE_DECL (u) = nreverse (vec);
34333 OMP_CLAUSE_CHAIN (u) = list;
34334 return u;
34335 }
34336 return list;
34337 }
34338
34339 /* OpenMP 5.0:
34340 iterators ( iterators-definition )
34341
34342 iterators-definition:
34343 iterator-specifier
34344 iterator-specifier , iterators-definition
34345
34346 iterator-specifier:
34347 identifier = range-specification
34348 iterator-type identifier = range-specification
34349
34350 range-specification:
34351 begin : end
34352 begin : end : step */
34353
34354 static tree
34355 cp_parser_omp_iterators (cp_parser *parser)
34356 {
34357 tree ret = NULL_TREE, *last = &ret;
34358 cp_lexer_consume_token (parser->lexer);
34359
34360 matching_parens parens;
34361 if (!parens.require_open (parser))
34362 return error_mark_node;
34363
34364 bool saved_colon_corrects_to_scope_p
34365 = parser->colon_corrects_to_scope_p;
34366 bool saved_colon_doesnt_start_class_def_p
34367 = parser->colon_doesnt_start_class_def_p;
34368
34369 do
34370 {
34371 tree iter_type;
34372 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34373 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
34374 iter_type = integer_type_node;
34375 else
34376 {
34377 const char *saved_message
34378 = parser->type_definition_forbidden_message;
34379 parser->type_definition_forbidden_message
34380 = G_("types may not be defined in iterator type");
34381
34382 iter_type = cp_parser_type_id (parser);
34383
34384 parser->type_definition_forbidden_message = saved_message;
34385 }
34386
34387 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34388 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34389 {
34390 cp_parser_error (parser, "expected identifier");
34391 break;
34392 }
34393
34394 tree id = cp_parser_identifier (parser);
34395 if (id == error_mark_node)
34396 break;
34397
34398 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34399 break;
34400
34401 parser->colon_corrects_to_scope_p = false;
34402 parser->colon_doesnt_start_class_def_p = true;
34403 tree begin = cp_parser_assignment_expression (parser);
34404
34405 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34406 break;
34407
34408 tree end = cp_parser_assignment_expression (parser);
34409
34410 tree step = integer_one_node;
34411 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34412 {
34413 cp_lexer_consume_token (parser->lexer);
34414 step = cp_parser_assignment_expression (parser);
34415 }
34416
34417 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
34418 DECL_ARTIFICIAL (iter_var) = 1;
34419 DECL_CONTEXT (iter_var) = current_function_decl;
34420 pushdecl (iter_var);
34421
34422 *last = make_tree_vec (6);
34423 TREE_VEC_ELT (*last, 0) = iter_var;
34424 TREE_VEC_ELT (*last, 1) = begin;
34425 TREE_VEC_ELT (*last, 2) = end;
34426 TREE_VEC_ELT (*last, 3) = step;
34427 last = &TREE_CHAIN (*last);
34428
34429 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34430 {
34431 cp_lexer_consume_token (parser->lexer);
34432 continue;
34433 }
34434 break;
34435 }
34436 while (1);
34437
34438 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34439 parser->colon_doesnt_start_class_def_p
34440 = saved_colon_doesnt_start_class_def_p;
34441
34442 if (!parens.require_close (parser))
34443 cp_parser_skip_to_closing_parenthesis (parser,
34444 /*recovering=*/true,
34445 /*or_comma=*/false,
34446 /*consume_paren=*/true);
34447
34448 return ret ? ret : error_mark_node;
34449 }
34450
34451 /* OpenMP 4.0:
34452 depend ( depend-kind : variable-list )
34453
34454 depend-kind:
34455 in | out | inout
34456
34457 OpenMP 4.5:
34458 depend ( source )
34459
34460 depend ( sink : vec )
34461
34462 OpenMP 5.0:
34463 depend ( depend-modifier , depend-kind: variable-list )
34464
34465 depend-kind:
34466 in | out | inout | mutexinoutset | depobj
34467
34468 depend-modifier:
34469 iterator ( iterators-definition ) */
34470
34471 static tree
34472 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
34473 {
34474 tree nlist, c, iterators = NULL_TREE;
34475 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
34476
34477 matching_parens parens;
34478 if (!parens.require_open (parser))
34479 return list;
34480
34481 do
34482 {
34483 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34484 goto invalid_kind;
34485
34486 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34487 const char *p = IDENTIFIER_POINTER (id);
34488
34489 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
34490 {
34491 begin_scope (sk_omp, NULL);
34492 iterators = cp_parser_omp_iterators (parser);
34493 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
34494 continue;
34495 }
34496 if (strcmp ("in", p) == 0)
34497 kind = OMP_CLAUSE_DEPEND_IN;
34498 else if (strcmp ("inout", p) == 0)
34499 kind = OMP_CLAUSE_DEPEND_INOUT;
34500 else if (strcmp ("mutexinoutset", p) == 0)
34501 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
34502 else if (strcmp ("out", p) == 0)
34503 kind = OMP_CLAUSE_DEPEND_OUT;
34504 else if (strcmp ("depobj", p) == 0)
34505 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
34506 else if (strcmp ("sink", p) == 0)
34507 kind = OMP_CLAUSE_DEPEND_SINK;
34508 else if (strcmp ("source", p) == 0)
34509 kind = OMP_CLAUSE_DEPEND_SOURCE;
34510 else
34511 goto invalid_kind;
34512 break;
34513 }
34514 while (1);
34515
34516 cp_lexer_consume_token (parser->lexer);
34517
34518 if (iterators
34519 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
34520 {
34521 poplevel (0, 1, 0);
34522 error_at (loc, "%<iterator%> modifier incompatible with %qs",
34523 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
34524 iterators = NULL_TREE;
34525 }
34526
34527 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
34528 {
34529 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
34530 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34531 OMP_CLAUSE_DECL (c) = NULL_TREE;
34532 OMP_CLAUSE_CHAIN (c) = list;
34533 if (!parens.require_close (parser))
34534 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34535 /*or_comma=*/false,
34536 /*consume_paren=*/true);
34537 return c;
34538 }
34539
34540 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34541 goto resync_fail;
34542
34543 if (kind == OMP_CLAUSE_DEPEND_SINK)
34544 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
34545 else
34546 {
34547 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
34548 list, NULL);
34549
34550 if (iterators)
34551 {
34552 tree block = poplevel (1, 1, 0);
34553 if (iterators == error_mark_node)
34554 iterators = NULL_TREE;
34555 else
34556 TREE_VEC_ELT (iterators, 5) = block;
34557 }
34558
34559 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34560 {
34561 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34562 if (iterators)
34563 OMP_CLAUSE_DECL (c)
34564 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
34565 }
34566 }
34567 return nlist;
34568
34569 invalid_kind:
34570 cp_parser_error (parser, "invalid depend kind");
34571 resync_fail:
34572 if (iterators)
34573 poplevel (0, 1, 0);
34574 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34575 /*or_comma=*/false,
34576 /*consume_paren=*/true);
34577 return list;
34578 }
34579
34580 /* OpenMP 4.0:
34581 map ( map-kind : variable-list )
34582 map ( variable-list )
34583
34584 map-kind:
34585 alloc | to | from | tofrom
34586
34587 OpenMP 4.5:
34588 map-kind:
34589 alloc | to | from | tofrom | release | delete
34590
34591 map ( always [,] map-kind: variable-list ) */
34592
34593 static tree
34594 cp_parser_omp_clause_map (cp_parser *parser, tree list)
34595 {
34596 tree nlist, c;
34597 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
34598 bool always = false;
34599
34600 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34601 return list;
34602
34603 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34604 {
34605 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34606 const char *p = IDENTIFIER_POINTER (id);
34607
34608 if (strcmp ("always", p) == 0)
34609 {
34610 int nth = 2;
34611 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
34612 nth++;
34613 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
34614 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
34615 == RID_DELETE))
34616 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
34617 == CPP_COLON))
34618 {
34619 always = true;
34620 cp_lexer_consume_token (parser->lexer);
34621 if (nth == 3)
34622 cp_lexer_consume_token (parser->lexer);
34623 }
34624 }
34625 }
34626
34627 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34628 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34629 {
34630 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34631 const char *p = IDENTIFIER_POINTER (id);
34632
34633 if (strcmp ("alloc", p) == 0)
34634 kind = GOMP_MAP_ALLOC;
34635 else if (strcmp ("to", p) == 0)
34636 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
34637 else if (strcmp ("from", p) == 0)
34638 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
34639 else if (strcmp ("tofrom", p) == 0)
34640 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
34641 else if (strcmp ("release", p) == 0)
34642 kind = GOMP_MAP_RELEASE;
34643 else
34644 {
34645 cp_parser_error (parser, "invalid map kind");
34646 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34647 /*or_comma=*/false,
34648 /*consume_paren=*/true);
34649 return list;
34650 }
34651 cp_lexer_consume_token (parser->lexer);
34652 cp_lexer_consume_token (parser->lexer);
34653 }
34654 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
34655 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34656 {
34657 kind = GOMP_MAP_DELETE;
34658 cp_lexer_consume_token (parser->lexer);
34659 cp_lexer_consume_token (parser->lexer);
34660 }
34661
34662 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
34663 NULL);
34664
34665 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34666 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34667
34668 return nlist;
34669 }
34670
34671 /* OpenMP 4.0:
34672 device ( expression ) */
34673
34674 static tree
34675 cp_parser_omp_clause_device (cp_parser *parser, tree list,
34676 location_t location)
34677 {
34678 tree t, c;
34679
34680 matching_parens parens;
34681 if (!parens.require_open (parser))
34682 return list;
34683
34684 t = cp_parser_assignment_expression (parser);
34685
34686 if (t == error_mark_node
34687 || !parens.require_close (parser))
34688 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34689 /*or_comma=*/false,
34690 /*consume_paren=*/true);
34691
34692 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
34693 "device", location);
34694
34695 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
34696 OMP_CLAUSE_DEVICE_ID (c) = t;
34697 OMP_CLAUSE_CHAIN (c) = list;
34698
34699 return c;
34700 }
34701
34702 /* OpenMP 4.0:
34703 dist_schedule ( static )
34704 dist_schedule ( static , expression ) */
34705
34706 static tree
34707 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
34708 location_t location)
34709 {
34710 tree c, t;
34711
34712 matching_parens parens;
34713 if (!parens.require_open (parser))
34714 return list;
34715
34716 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
34717
34718 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34719 goto invalid_kind;
34720 cp_lexer_consume_token (parser->lexer);
34721
34722 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34723 {
34724 cp_lexer_consume_token (parser->lexer);
34725
34726 t = cp_parser_assignment_expression (parser);
34727
34728 if (t == error_mark_node)
34729 goto resync_fail;
34730 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
34731
34732 if (!parens.require_close (parser))
34733 goto resync_fail;
34734 }
34735 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34736 goto resync_fail;
34737
34738 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
34739 location);
34740 OMP_CLAUSE_CHAIN (c) = list;
34741 return c;
34742
34743 invalid_kind:
34744 cp_parser_error (parser, "invalid dist_schedule kind");
34745 resync_fail:
34746 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34747 /*or_comma=*/false,
34748 /*consume_paren=*/true);
34749 return list;
34750 }
34751
34752 /* OpenMP 4.0:
34753 proc_bind ( proc-bind-kind )
34754
34755 proc-bind-kind:
34756 master | close | spread */
34757
34758 static tree
34759 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
34760 location_t location)
34761 {
34762 tree c;
34763 enum omp_clause_proc_bind_kind kind;
34764
34765 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34766 return list;
34767
34768 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34769 {
34770 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34771 const char *p = IDENTIFIER_POINTER (id);
34772
34773 if (strcmp ("master", p) == 0)
34774 kind = OMP_CLAUSE_PROC_BIND_MASTER;
34775 else if (strcmp ("close", p) == 0)
34776 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
34777 else if (strcmp ("spread", p) == 0)
34778 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
34779 else
34780 goto invalid_kind;
34781 }
34782 else
34783 goto invalid_kind;
34784
34785 cp_lexer_consume_token (parser->lexer);
34786 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34787 goto resync_fail;
34788
34789 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
34790 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
34791 location);
34792 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
34793 OMP_CLAUSE_CHAIN (c) = list;
34794 return c;
34795
34796 invalid_kind:
34797 cp_parser_error (parser, "invalid depend kind");
34798 resync_fail:
34799 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34800 /*or_comma=*/false,
34801 /*consume_paren=*/true);
34802 return list;
34803 }
34804
34805 /* OpenACC:
34806 async [( int-expr )] */
34807
34808 static tree
34809 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
34810 {
34811 tree c, t;
34812 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34813
34814 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34815
34816 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34817 {
34818 matching_parens parens;
34819 parens.consume_open (parser);
34820
34821 t = cp_parser_expression (parser);
34822 if (t == error_mark_node
34823 || !parens.require_close (parser))
34824 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34825 /*or_comma=*/false,
34826 /*consume_paren=*/true);
34827 }
34828
34829 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
34830
34831 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
34832 OMP_CLAUSE_ASYNC_EXPR (c) = t;
34833 OMP_CLAUSE_CHAIN (c) = list;
34834 list = c;
34835
34836 return list;
34837 }
34838
34839 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34840 is a bitmask in MASK. Return the list of clauses found. */
34841
34842 static tree
34843 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
34844 const char *where, cp_token *pragma_tok,
34845 bool finish_p = true)
34846 {
34847 tree clauses = NULL;
34848 bool first = true;
34849
34850 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34851 {
34852 location_t here;
34853 pragma_omp_clause c_kind;
34854 omp_clause_code code;
34855 const char *c_name;
34856 tree prev = clauses;
34857
34858 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34859 cp_lexer_consume_token (parser->lexer);
34860
34861 here = cp_lexer_peek_token (parser->lexer)->location;
34862 c_kind = cp_parser_omp_clause_name (parser);
34863
34864 switch (c_kind)
34865 {
34866 case PRAGMA_OACC_CLAUSE_ASYNC:
34867 clauses = cp_parser_oacc_clause_async (parser, clauses);
34868 c_name = "async";
34869 break;
34870 case PRAGMA_OACC_CLAUSE_AUTO:
34871 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
34872 clauses, here);
34873 c_name = "auto";
34874 break;
34875 case PRAGMA_OACC_CLAUSE_COLLAPSE:
34876 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
34877 c_name = "collapse";
34878 break;
34879 case PRAGMA_OACC_CLAUSE_COPY:
34880 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34881 c_name = "copy";
34882 break;
34883 case PRAGMA_OACC_CLAUSE_COPYIN:
34884 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34885 c_name = "copyin";
34886 break;
34887 case PRAGMA_OACC_CLAUSE_COPYOUT:
34888 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34889 c_name = "copyout";
34890 break;
34891 case PRAGMA_OACC_CLAUSE_CREATE:
34892 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34893 c_name = "create";
34894 break;
34895 case PRAGMA_OACC_CLAUSE_DELETE:
34896 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34897 c_name = "delete";
34898 break;
34899 case PRAGMA_OMP_CLAUSE_DEFAULT:
34900 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
34901 c_name = "default";
34902 break;
34903 case PRAGMA_OACC_CLAUSE_DEVICE:
34904 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34905 c_name = "device";
34906 break;
34907 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
34908 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
34909 c_name = "deviceptr";
34910 break;
34911 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
34912 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34913 c_name = "device_resident";
34914 break;
34915 case PRAGMA_OACC_CLAUSE_FINALIZE:
34916 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
34917 clauses, here);
34918 c_name = "finalize";
34919 break;
34920 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
34921 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34922 clauses);
34923 c_name = "firstprivate";
34924 break;
34925 case PRAGMA_OACC_CLAUSE_GANG:
34926 c_name = "gang";
34927 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
34928 c_name, clauses);
34929 break;
34930 case PRAGMA_OACC_CLAUSE_HOST:
34931 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34932 c_name = "host";
34933 break;
34934 case PRAGMA_OACC_CLAUSE_IF:
34935 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
34936 c_name = "if";
34937 break;
34938 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
34939 clauses = cp_parser_oacc_simple_clause (parser,
34940 OMP_CLAUSE_IF_PRESENT,
34941 clauses, here);
34942 c_name = "if_present";
34943 break;
34944 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
34945 clauses = cp_parser_oacc_simple_clause (parser,
34946 OMP_CLAUSE_INDEPENDENT,
34947 clauses, here);
34948 c_name = "independent";
34949 break;
34950 case PRAGMA_OACC_CLAUSE_LINK:
34951 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34952 c_name = "link";
34953 break;
34954 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
34955 code = OMP_CLAUSE_NUM_GANGS;
34956 c_name = "num_gangs";
34957 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34958 clauses);
34959 break;
34960 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
34961 c_name = "num_workers";
34962 code = OMP_CLAUSE_NUM_WORKERS;
34963 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34964 clauses);
34965 break;
34966 case PRAGMA_OACC_CLAUSE_PRESENT:
34967 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34968 c_name = "present";
34969 break;
34970 case PRAGMA_OACC_CLAUSE_PRIVATE:
34971 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34972 clauses);
34973 c_name = "private";
34974 break;
34975 case PRAGMA_OACC_CLAUSE_REDUCTION:
34976 clauses
34977 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34978 false, clauses);
34979 c_name = "reduction";
34980 break;
34981 case PRAGMA_OACC_CLAUSE_SEQ:
34982 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
34983 clauses, here);
34984 c_name = "seq";
34985 break;
34986 case PRAGMA_OACC_CLAUSE_TILE:
34987 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
34988 c_name = "tile";
34989 break;
34990 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
34991 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34992 clauses);
34993 c_name = "use_device";
34994 break;
34995 case PRAGMA_OACC_CLAUSE_VECTOR:
34996 c_name = "vector";
34997 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
34998 c_name, clauses);
34999 break;
35000 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
35001 c_name = "vector_length";
35002 code = OMP_CLAUSE_VECTOR_LENGTH;
35003 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35004 clauses);
35005 break;
35006 case PRAGMA_OACC_CLAUSE_WAIT:
35007 clauses = cp_parser_oacc_clause_wait (parser, clauses);
35008 c_name = "wait";
35009 break;
35010 case PRAGMA_OACC_CLAUSE_WORKER:
35011 c_name = "worker";
35012 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
35013 c_name, clauses);
35014 break;
35015 default:
35016 cp_parser_error (parser, "expected %<#pragma acc%> clause");
35017 goto saw_error;
35018 }
35019
35020 first = false;
35021
35022 if (((mask >> c_kind) & 1) == 0)
35023 {
35024 /* Remove the invalid clause(s) from the list to avoid
35025 confusing the rest of the compiler. */
35026 clauses = prev;
35027 error_at (here, "%qs is not valid for %qs", c_name, where);
35028 }
35029 }
35030
35031 saw_error:
35032 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35033
35034 if (finish_p)
35035 return finish_omp_clauses (clauses, C_ORT_ACC);
35036
35037 return clauses;
35038 }
35039
35040 /* Parse all OpenMP clauses. The set clauses allowed by the directive
35041 is a bitmask in MASK. Return the list of clauses found; the result
35042 of clause default goes in *pdefault. */
35043
35044 static tree
35045 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
35046 const char *where, cp_token *pragma_tok,
35047 bool finish_p = true)
35048 {
35049 tree clauses = NULL;
35050 bool first = true;
35051 cp_token *token = NULL;
35052
35053 /* Don't create location wrapper nodes within OpenMP clauses. */
35054 auto_suppress_location_wrappers sentinel;
35055
35056 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35057 {
35058 pragma_omp_clause c_kind;
35059 const char *c_name;
35060 tree prev = clauses;
35061
35062 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35063 cp_lexer_consume_token (parser->lexer);
35064
35065 token = cp_lexer_peek_token (parser->lexer);
35066 c_kind = cp_parser_omp_clause_name (parser);
35067
35068 switch (c_kind)
35069 {
35070 case PRAGMA_OMP_CLAUSE_COLLAPSE:
35071 clauses = cp_parser_omp_clause_collapse (parser, clauses,
35072 token->location);
35073 c_name = "collapse";
35074 break;
35075 case PRAGMA_OMP_CLAUSE_COPYIN:
35076 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
35077 c_name = "copyin";
35078 break;
35079 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
35080 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
35081 clauses);
35082 c_name = "copyprivate";
35083 break;
35084 case PRAGMA_OMP_CLAUSE_DEFAULT:
35085 clauses = cp_parser_omp_clause_default (parser, clauses,
35086 token->location, false);
35087 c_name = "default";
35088 break;
35089 case PRAGMA_OMP_CLAUSE_FINAL:
35090 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
35091 c_name = "final";
35092 break;
35093 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
35094 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
35095 clauses);
35096 c_name = "firstprivate";
35097 break;
35098 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
35099 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
35100 token->location);
35101 c_name = "grainsize";
35102 break;
35103 case PRAGMA_OMP_CLAUSE_HINT:
35104 clauses = cp_parser_omp_clause_hint (parser, clauses,
35105 token->location);
35106 c_name = "hint";
35107 break;
35108 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
35109 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
35110 token->location);
35111 c_name = "defaultmap";
35112 break;
35113 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
35114 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35115 clauses);
35116 c_name = "use_device_ptr";
35117 break;
35118 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
35119 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
35120 clauses);
35121 c_name = "is_device_ptr";
35122 break;
35123 case PRAGMA_OMP_CLAUSE_IF:
35124 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
35125 true);
35126 c_name = "if";
35127 break;
35128 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
35129 clauses
35130 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
35131 true, clauses);
35132 c_name = "in_reduction";
35133 break;
35134 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
35135 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
35136 c_name = "lastprivate";
35137 break;
35138 case PRAGMA_OMP_CLAUSE_MERGEABLE:
35139 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
35140 token->location);
35141 c_name = "mergeable";
35142 break;
35143 case PRAGMA_OMP_CLAUSE_NOWAIT:
35144 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
35145 c_name = "nowait";
35146 break;
35147 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
35148 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
35149 token->location);
35150 c_name = "num_tasks";
35151 break;
35152 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
35153 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
35154 token->location);
35155 c_name = "num_threads";
35156 break;
35157 case PRAGMA_OMP_CLAUSE_ORDERED:
35158 clauses = cp_parser_omp_clause_ordered (parser, clauses,
35159 token->location);
35160 c_name = "ordered";
35161 break;
35162 case PRAGMA_OMP_CLAUSE_PRIORITY:
35163 clauses = cp_parser_omp_clause_priority (parser, clauses,
35164 token->location);
35165 c_name = "priority";
35166 break;
35167 case PRAGMA_OMP_CLAUSE_PRIVATE:
35168 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35169 clauses);
35170 c_name = "private";
35171 break;
35172 case PRAGMA_OMP_CLAUSE_REDUCTION:
35173 clauses
35174 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35175 true, clauses);
35176 c_name = "reduction";
35177 break;
35178 case PRAGMA_OMP_CLAUSE_SCHEDULE:
35179 clauses = cp_parser_omp_clause_schedule (parser, clauses,
35180 token->location);
35181 c_name = "schedule";
35182 break;
35183 case PRAGMA_OMP_CLAUSE_SHARED:
35184 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
35185 clauses);
35186 c_name = "shared";
35187 break;
35188 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
35189 clauses
35190 = cp_parser_omp_clause_reduction (parser,
35191 OMP_CLAUSE_TASK_REDUCTION,
35192 true, clauses);
35193 c_name = "task_reduction";
35194 break;
35195 case PRAGMA_OMP_CLAUSE_UNTIED:
35196 clauses = cp_parser_omp_clause_untied (parser, clauses,
35197 token->location);
35198 c_name = "untied";
35199 break;
35200 case PRAGMA_OMP_CLAUSE_INBRANCH:
35201 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
35202 clauses, token->location);
35203 c_name = "inbranch";
35204 break;
35205 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
35206 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
35207 clauses);
35208 c_name = "nontemporal";
35209 break;
35210 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
35211 clauses = cp_parser_omp_clause_branch (parser,
35212 OMP_CLAUSE_NOTINBRANCH,
35213 clauses, token->location);
35214 c_name = "notinbranch";
35215 break;
35216 case PRAGMA_OMP_CLAUSE_PARALLEL:
35217 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
35218 clauses, token->location);
35219 c_name = "parallel";
35220 if (!first)
35221 {
35222 clause_not_first:
35223 error_at (token->location, "%qs must be the first clause of %qs",
35224 c_name, where);
35225 clauses = prev;
35226 }
35227 break;
35228 case PRAGMA_OMP_CLAUSE_FOR:
35229 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
35230 clauses, token->location);
35231 c_name = "for";
35232 if (!first)
35233 goto clause_not_first;
35234 break;
35235 case PRAGMA_OMP_CLAUSE_SECTIONS:
35236 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
35237 clauses, token->location);
35238 c_name = "sections";
35239 if (!first)
35240 goto clause_not_first;
35241 break;
35242 case PRAGMA_OMP_CLAUSE_TASKGROUP:
35243 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
35244 clauses, token->location);
35245 c_name = "taskgroup";
35246 if (!first)
35247 goto clause_not_first;
35248 break;
35249 case PRAGMA_OMP_CLAUSE_LINK:
35250 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
35251 c_name = "to";
35252 break;
35253 case PRAGMA_OMP_CLAUSE_TO:
35254 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
35255 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35256 clauses);
35257 else
35258 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
35259 c_name = "to";
35260 break;
35261 case PRAGMA_OMP_CLAUSE_FROM:
35262 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
35263 c_name = "from";
35264 break;
35265 case PRAGMA_OMP_CLAUSE_UNIFORM:
35266 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
35267 clauses);
35268 c_name = "uniform";
35269 break;
35270 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
35271 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
35272 token->location);
35273 c_name = "num_teams";
35274 break;
35275 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
35276 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
35277 token->location);
35278 c_name = "thread_limit";
35279 break;
35280 case PRAGMA_OMP_CLAUSE_ALIGNED:
35281 clauses = cp_parser_omp_clause_aligned (parser, clauses);
35282 c_name = "aligned";
35283 break;
35284 case PRAGMA_OMP_CLAUSE_LINEAR:
35285 {
35286 bool declare_simd = false;
35287 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
35288 declare_simd = true;
35289 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
35290 }
35291 c_name = "linear";
35292 break;
35293 case PRAGMA_OMP_CLAUSE_DEPEND:
35294 clauses = cp_parser_omp_clause_depend (parser, clauses,
35295 token->location);
35296 c_name = "depend";
35297 break;
35298 case PRAGMA_OMP_CLAUSE_MAP:
35299 clauses = cp_parser_omp_clause_map (parser, clauses);
35300 c_name = "map";
35301 break;
35302 case PRAGMA_OMP_CLAUSE_DEVICE:
35303 clauses = cp_parser_omp_clause_device (parser, clauses,
35304 token->location);
35305 c_name = "device";
35306 break;
35307 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
35308 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
35309 token->location);
35310 c_name = "dist_schedule";
35311 break;
35312 case PRAGMA_OMP_CLAUSE_PROC_BIND:
35313 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
35314 token->location);
35315 c_name = "proc_bind";
35316 break;
35317 case PRAGMA_OMP_CLAUSE_SAFELEN:
35318 clauses = cp_parser_omp_clause_safelen (parser, clauses,
35319 token->location);
35320 c_name = "safelen";
35321 break;
35322 case PRAGMA_OMP_CLAUSE_SIMDLEN:
35323 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
35324 token->location);
35325 c_name = "simdlen";
35326 break;
35327 case PRAGMA_OMP_CLAUSE_NOGROUP:
35328 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
35329 token->location);
35330 c_name = "nogroup";
35331 break;
35332 case PRAGMA_OMP_CLAUSE_THREADS:
35333 clauses
35334 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
35335 clauses, token->location);
35336 c_name = "threads";
35337 break;
35338 case PRAGMA_OMP_CLAUSE_SIMD:
35339 clauses
35340 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
35341 clauses, token->location);
35342 c_name = "simd";
35343 break;
35344 default:
35345 cp_parser_error (parser, "expected %<#pragma omp%> clause");
35346 goto saw_error;
35347 }
35348
35349 first = false;
35350
35351 if (((mask >> c_kind) & 1) == 0)
35352 {
35353 /* Remove the invalid clause(s) from the list to avoid
35354 confusing the rest of the compiler. */
35355 clauses = prev;
35356 error_at (token->location, "%qs is not valid for %qs", c_name, where);
35357 }
35358 }
35359 saw_error:
35360 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35361 if (finish_p)
35362 {
35363 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
35364 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
35365 else
35366 return finish_omp_clauses (clauses, C_ORT_OMP);
35367 }
35368 return clauses;
35369 }
35370
35371 /* OpenMP 2.5:
35372 structured-block:
35373 statement
35374
35375 In practice, we're also interested in adding the statement to an
35376 outer node. So it is convenient if we work around the fact that
35377 cp_parser_statement calls add_stmt. */
35378
35379 static unsigned
35380 cp_parser_begin_omp_structured_block (cp_parser *parser)
35381 {
35382 unsigned save = parser->in_statement;
35383
35384 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35385 This preserves the "not within loop or switch" style error messages
35386 for nonsense cases like
35387 void foo() {
35388 #pragma omp single
35389 break;
35390 }
35391 */
35392 if (parser->in_statement)
35393 parser->in_statement = IN_OMP_BLOCK;
35394
35395 return save;
35396 }
35397
35398 static void
35399 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
35400 {
35401 parser->in_statement = save;
35402 }
35403
35404 static tree
35405 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
35406 {
35407 tree stmt = begin_omp_structured_block ();
35408 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35409
35410 cp_parser_statement (parser, NULL_TREE, false, if_p);
35411
35412 cp_parser_end_omp_structured_block (parser, save);
35413 return finish_omp_structured_block (stmt);
35414 }
35415
35416 /* OpenMP 2.5:
35417 # pragma omp atomic new-line
35418 expression-stmt
35419
35420 expression-stmt:
35421 x binop= expr | x++ | ++x | x-- | --x
35422 binop:
35423 +, *, -, /, &, ^, |, <<, >>
35424
35425 where x is an lvalue expression with scalar type.
35426
35427 OpenMP 3.1:
35428 # pragma omp atomic new-line
35429 update-stmt
35430
35431 # pragma omp atomic read new-line
35432 read-stmt
35433
35434 # pragma omp atomic write new-line
35435 write-stmt
35436
35437 # pragma omp atomic update new-line
35438 update-stmt
35439
35440 # pragma omp atomic capture new-line
35441 capture-stmt
35442
35443 # pragma omp atomic capture new-line
35444 capture-block
35445
35446 read-stmt:
35447 v = x
35448 write-stmt:
35449 x = expr
35450 update-stmt:
35451 expression-stmt | x = x binop expr
35452 capture-stmt:
35453 v = expression-stmt
35454 capture-block:
35455 { v = x; update-stmt; } | { update-stmt; v = x; }
35456
35457 OpenMP 4.0:
35458 update-stmt:
35459 expression-stmt | x = x binop expr | x = expr binop x
35460 capture-stmt:
35461 v = update-stmt
35462 capture-block:
35463 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35464
35465 where x and v are lvalue expressions with scalar type. */
35466
35467 static void
35468 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
35469 {
35470 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
35471 tree rhs1 = NULL_TREE, orig_lhs;
35472 location_t loc = pragma_tok->location;
35473 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
35474 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
35475 bool structured_block = false;
35476 bool first = true;
35477 tree clauses = NULL_TREE;
35478
35479 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35480 {
35481 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35482 cp_lexer_consume_token (parser->lexer);
35483
35484 first = false;
35485
35486 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35487 {
35488 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35489 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
35490 const char *p = IDENTIFIER_POINTER (id);
35491 enum tree_code new_code = ERROR_MARK;
35492 enum omp_memory_order new_memory_order
35493 = OMP_MEMORY_ORDER_UNSPECIFIED;
35494
35495 if (!strcmp (p, "read"))
35496 new_code = OMP_ATOMIC_READ;
35497 else if (!strcmp (p, "write"))
35498 new_code = NOP_EXPR;
35499 else if (!strcmp (p, "update"))
35500 new_code = OMP_ATOMIC;
35501 else if (!strcmp (p, "capture"))
35502 new_code = OMP_ATOMIC_CAPTURE_NEW;
35503 else if (!strcmp (p, "seq_cst"))
35504 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35505 else if (!strcmp (p, "acq_rel"))
35506 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35507 else if (!strcmp (p, "release"))
35508 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
35509 else if (!strcmp (p, "acquire"))
35510 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35511 else if (!strcmp (p, "relaxed"))
35512 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
35513 else if (!strcmp (p, "hint"))
35514 {
35515 cp_lexer_consume_token (parser->lexer);
35516 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
35517 continue;
35518 }
35519 else
35520 {
35521 p = NULL;
35522 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
35523 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35524 "%<release%>, %<relaxed%> or %<hint%> clause");
35525 }
35526 if (p)
35527 {
35528 if (new_code != ERROR_MARK)
35529 {
35530 if (code != ERROR_MARK)
35531 error_at (cloc, "too many atomic clauses");
35532 else
35533 code = new_code;
35534 }
35535 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35536 {
35537 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35538 error_at (cloc, "too many memory order clauses");
35539 else
35540 memory_order = new_memory_order;
35541 }
35542 cp_lexer_consume_token (parser->lexer);
35543 continue;
35544 }
35545 }
35546 break;
35547 }
35548 cp_parser_require_pragma_eol (parser, pragma_tok);
35549
35550 if (code == ERROR_MARK)
35551 code = OMP_ATOMIC;
35552 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
35553 {
35554 omp_requires_mask
35555 = (enum omp_requires) (omp_requires_mask
35556 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
35557 switch ((enum omp_memory_order)
35558 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
35559 {
35560 case OMP_MEMORY_ORDER_UNSPECIFIED:
35561 case OMP_MEMORY_ORDER_RELAXED:
35562 memory_order = OMP_MEMORY_ORDER_RELAXED;
35563 break;
35564 case OMP_MEMORY_ORDER_SEQ_CST:
35565 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35566 break;
35567 case OMP_MEMORY_ORDER_ACQ_REL:
35568 switch (code)
35569 {
35570 case OMP_ATOMIC_READ:
35571 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35572 break;
35573 case NOP_EXPR: /* atomic write */
35574 case OMP_ATOMIC:
35575 memory_order = OMP_MEMORY_ORDER_RELEASE;
35576 break;
35577 default:
35578 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35579 break;
35580 }
35581 break;
35582 default:
35583 gcc_unreachable ();
35584 }
35585 }
35586 else
35587 switch (code)
35588 {
35589 case OMP_ATOMIC_READ:
35590 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35591 || memory_order == OMP_MEMORY_ORDER_RELEASE)
35592 {
35593 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
35594 "%<acq_rel%> or %<release%> clauses");
35595 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35596 }
35597 break;
35598 case NOP_EXPR: /* atomic write */
35599 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35600 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35601 {
35602 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
35603 "%<acq_rel%> or %<acquire%> clauses");
35604 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35605 }
35606 break;
35607 case OMP_ATOMIC:
35608 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35609 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35610 {
35611 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
35612 "%<acq_rel%> or %<acquire%> clauses");
35613 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35614 }
35615 break;
35616 default:
35617 break;
35618 }
35619
35620 switch (code)
35621 {
35622 case OMP_ATOMIC_READ:
35623 case NOP_EXPR: /* atomic write */
35624 v = cp_parser_unary_expression (parser);
35625 if (v == error_mark_node)
35626 goto saw_error;
35627 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35628 goto saw_error;
35629 if (code == NOP_EXPR)
35630 lhs = cp_parser_expression (parser);
35631 else
35632 lhs = cp_parser_unary_expression (parser);
35633 if (lhs == error_mark_node)
35634 goto saw_error;
35635 if (code == NOP_EXPR)
35636 {
35637 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35638 opcode. */
35639 code = OMP_ATOMIC;
35640 rhs = lhs;
35641 lhs = v;
35642 v = NULL_TREE;
35643 }
35644 goto done;
35645 case OMP_ATOMIC_CAPTURE_NEW:
35646 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35647 {
35648 cp_lexer_consume_token (parser->lexer);
35649 structured_block = true;
35650 }
35651 else
35652 {
35653 v = cp_parser_unary_expression (parser);
35654 if (v == error_mark_node)
35655 goto saw_error;
35656 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35657 goto saw_error;
35658 }
35659 default:
35660 break;
35661 }
35662
35663 restart:
35664 lhs = cp_parser_unary_expression (parser);
35665 orig_lhs = lhs;
35666 switch (TREE_CODE (lhs))
35667 {
35668 case ERROR_MARK:
35669 goto saw_error;
35670
35671 case POSTINCREMENT_EXPR:
35672 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35673 code = OMP_ATOMIC_CAPTURE_OLD;
35674 /* FALLTHROUGH */
35675 case PREINCREMENT_EXPR:
35676 lhs = TREE_OPERAND (lhs, 0);
35677 opcode = PLUS_EXPR;
35678 rhs = integer_one_node;
35679 break;
35680
35681 case POSTDECREMENT_EXPR:
35682 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35683 code = OMP_ATOMIC_CAPTURE_OLD;
35684 /* FALLTHROUGH */
35685 case PREDECREMENT_EXPR:
35686 lhs = TREE_OPERAND (lhs, 0);
35687 opcode = MINUS_EXPR;
35688 rhs = integer_one_node;
35689 break;
35690
35691 case COMPOUND_EXPR:
35692 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
35693 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
35694 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
35695 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
35696 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35697 (TREE_OPERAND (lhs, 1), 0), 0)))
35698 == BOOLEAN_TYPE)
35699 /* Undo effects of boolean_increment for post {in,de}crement. */
35700 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
35701 /* FALLTHRU */
35702 case MODIFY_EXPR:
35703 if (TREE_CODE (lhs) == MODIFY_EXPR
35704 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
35705 {
35706 /* Undo effects of boolean_increment. */
35707 if (integer_onep (TREE_OPERAND (lhs, 1)))
35708 {
35709 /* This is pre or post increment. */
35710 rhs = TREE_OPERAND (lhs, 1);
35711 lhs = TREE_OPERAND (lhs, 0);
35712 opcode = NOP_EXPR;
35713 if (code == OMP_ATOMIC_CAPTURE_NEW
35714 && !structured_block
35715 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
35716 code = OMP_ATOMIC_CAPTURE_OLD;
35717 break;
35718 }
35719 }
35720 /* FALLTHRU */
35721 default:
35722 switch (cp_lexer_peek_token (parser->lexer)->type)
35723 {
35724 case CPP_MULT_EQ:
35725 opcode = MULT_EXPR;
35726 break;
35727 case CPP_DIV_EQ:
35728 opcode = TRUNC_DIV_EXPR;
35729 break;
35730 case CPP_PLUS_EQ:
35731 opcode = PLUS_EXPR;
35732 break;
35733 case CPP_MINUS_EQ:
35734 opcode = MINUS_EXPR;
35735 break;
35736 case CPP_LSHIFT_EQ:
35737 opcode = LSHIFT_EXPR;
35738 break;
35739 case CPP_RSHIFT_EQ:
35740 opcode = RSHIFT_EXPR;
35741 break;
35742 case CPP_AND_EQ:
35743 opcode = BIT_AND_EXPR;
35744 break;
35745 case CPP_OR_EQ:
35746 opcode = BIT_IOR_EXPR;
35747 break;
35748 case CPP_XOR_EQ:
35749 opcode = BIT_XOR_EXPR;
35750 break;
35751 case CPP_EQ:
35752 enum cp_parser_prec oprec;
35753 cp_token *token;
35754 cp_lexer_consume_token (parser->lexer);
35755 cp_parser_parse_tentatively (parser);
35756 rhs1 = cp_parser_simple_cast_expression (parser);
35757 if (rhs1 == error_mark_node)
35758 {
35759 cp_parser_abort_tentative_parse (parser);
35760 cp_parser_simple_cast_expression (parser);
35761 goto saw_error;
35762 }
35763 token = cp_lexer_peek_token (parser->lexer);
35764 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
35765 {
35766 cp_parser_abort_tentative_parse (parser);
35767 cp_parser_parse_tentatively (parser);
35768 rhs = cp_parser_binary_expression (parser, false, true,
35769 PREC_NOT_OPERATOR, NULL);
35770 if (rhs == error_mark_node)
35771 {
35772 cp_parser_abort_tentative_parse (parser);
35773 cp_parser_binary_expression (parser, false, true,
35774 PREC_NOT_OPERATOR, NULL);
35775 goto saw_error;
35776 }
35777 switch (TREE_CODE (rhs))
35778 {
35779 case MULT_EXPR:
35780 case TRUNC_DIV_EXPR:
35781 case RDIV_EXPR:
35782 case PLUS_EXPR:
35783 case MINUS_EXPR:
35784 case LSHIFT_EXPR:
35785 case RSHIFT_EXPR:
35786 case BIT_AND_EXPR:
35787 case BIT_IOR_EXPR:
35788 case BIT_XOR_EXPR:
35789 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
35790 {
35791 if (cp_parser_parse_definitely (parser))
35792 {
35793 opcode = TREE_CODE (rhs);
35794 rhs1 = TREE_OPERAND (rhs, 0);
35795 rhs = TREE_OPERAND (rhs, 1);
35796 goto stmt_done;
35797 }
35798 else
35799 goto saw_error;
35800 }
35801 break;
35802 default:
35803 break;
35804 }
35805 cp_parser_abort_tentative_parse (parser);
35806 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
35807 {
35808 rhs = cp_parser_expression (parser);
35809 if (rhs == error_mark_node)
35810 goto saw_error;
35811 opcode = NOP_EXPR;
35812 rhs1 = NULL_TREE;
35813 goto stmt_done;
35814 }
35815 cp_parser_error (parser,
35816 "invalid form of %<#pragma omp atomic%>");
35817 goto saw_error;
35818 }
35819 if (!cp_parser_parse_definitely (parser))
35820 goto saw_error;
35821 switch (token->type)
35822 {
35823 case CPP_SEMICOLON:
35824 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35825 {
35826 code = OMP_ATOMIC_CAPTURE_OLD;
35827 v = lhs;
35828 lhs = NULL_TREE;
35829 lhs1 = rhs1;
35830 rhs1 = NULL_TREE;
35831 cp_lexer_consume_token (parser->lexer);
35832 goto restart;
35833 }
35834 else if (structured_block)
35835 {
35836 opcode = NOP_EXPR;
35837 rhs = rhs1;
35838 rhs1 = NULL_TREE;
35839 goto stmt_done;
35840 }
35841 cp_parser_error (parser,
35842 "invalid form of %<#pragma omp atomic%>");
35843 goto saw_error;
35844 case CPP_MULT:
35845 opcode = MULT_EXPR;
35846 break;
35847 case CPP_DIV:
35848 opcode = TRUNC_DIV_EXPR;
35849 break;
35850 case CPP_PLUS:
35851 opcode = PLUS_EXPR;
35852 break;
35853 case CPP_MINUS:
35854 opcode = MINUS_EXPR;
35855 break;
35856 case CPP_LSHIFT:
35857 opcode = LSHIFT_EXPR;
35858 break;
35859 case CPP_RSHIFT:
35860 opcode = RSHIFT_EXPR;
35861 break;
35862 case CPP_AND:
35863 opcode = BIT_AND_EXPR;
35864 break;
35865 case CPP_OR:
35866 opcode = BIT_IOR_EXPR;
35867 break;
35868 case CPP_XOR:
35869 opcode = BIT_XOR_EXPR;
35870 break;
35871 default:
35872 cp_parser_error (parser,
35873 "invalid operator for %<#pragma omp atomic%>");
35874 goto saw_error;
35875 }
35876 oprec = TOKEN_PRECEDENCE (token);
35877 gcc_assert (oprec != PREC_NOT_OPERATOR);
35878 if (commutative_tree_code (opcode))
35879 oprec = (enum cp_parser_prec) (oprec - 1);
35880 cp_lexer_consume_token (parser->lexer);
35881 rhs = cp_parser_binary_expression (parser, false, false,
35882 oprec, NULL);
35883 if (rhs == error_mark_node)
35884 goto saw_error;
35885 goto stmt_done;
35886 /* FALLTHROUGH */
35887 default:
35888 cp_parser_error (parser,
35889 "invalid operator for %<#pragma omp atomic%>");
35890 goto saw_error;
35891 }
35892 cp_lexer_consume_token (parser->lexer);
35893
35894 rhs = cp_parser_expression (parser);
35895 if (rhs == error_mark_node)
35896 goto saw_error;
35897 break;
35898 }
35899 stmt_done:
35900 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35901 {
35902 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
35903 goto saw_error;
35904 v = cp_parser_unary_expression (parser);
35905 if (v == error_mark_node)
35906 goto saw_error;
35907 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35908 goto saw_error;
35909 lhs1 = cp_parser_unary_expression (parser);
35910 if (lhs1 == error_mark_node)
35911 goto saw_error;
35912 }
35913 if (structured_block)
35914 {
35915 cp_parser_consume_semicolon_at_end_of_statement (parser);
35916 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35917 }
35918 done:
35919 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
35920 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
35921 rhs1, clauses, memory_order);
35922 if (!structured_block)
35923 cp_parser_consume_semicolon_at_end_of_statement (parser);
35924 return;
35925
35926 saw_error:
35927 cp_parser_skip_to_end_of_block_or_statement (parser);
35928 if (structured_block)
35929 {
35930 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35931 cp_lexer_consume_token (parser->lexer);
35932 else if (code == OMP_ATOMIC_CAPTURE_NEW)
35933 {
35934 cp_parser_skip_to_end_of_block_or_statement (parser);
35935 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35936 cp_lexer_consume_token (parser->lexer);
35937 }
35938 }
35939 }
35940
35941
35942 /* OpenMP 2.5:
35943 # pragma omp barrier new-line */
35944
35945 static void
35946 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
35947 {
35948 cp_parser_require_pragma_eol (parser, pragma_tok);
35949 finish_omp_barrier ();
35950 }
35951
35952 /* OpenMP 2.5:
35953 # pragma omp critical [(name)] new-line
35954 structured-block
35955
35956 OpenMP 4.5:
35957 # pragma omp critical [(name) [hint(expression)]] new-line
35958 structured-block */
35959
35960 #define OMP_CRITICAL_CLAUSE_MASK \
35961 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35962
35963 static tree
35964 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35965 {
35966 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
35967
35968 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35969 {
35970 matching_parens parens;
35971 parens.consume_open (parser);
35972
35973 name = cp_parser_identifier (parser);
35974
35975 if (name == error_mark_node
35976 || !parens.require_close (parser))
35977 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35978 /*or_comma=*/false,
35979 /*consume_paren=*/true);
35980 if (name == error_mark_node)
35981 name = NULL;
35982
35983 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
35984 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
35985 cp_lexer_consume_token (parser->lexer);
35986
35987 clauses = cp_parser_omp_all_clauses (parser,
35988 OMP_CRITICAL_CLAUSE_MASK,
35989 "#pragma omp critical", pragma_tok);
35990 }
35991 else
35992 cp_parser_require_pragma_eol (parser, pragma_tok);
35993
35994 stmt = cp_parser_omp_structured_block (parser, if_p);
35995 return c_finish_omp_critical (input_location, stmt, name, clauses);
35996 }
35997
35998 /* OpenMP 5.0:
35999 # pragma omp depobj ( depobj ) depobj-clause new-line
36000
36001 depobj-clause:
36002 depend (dependence-type : locator)
36003 destroy
36004 update (dependence-type)
36005
36006 dependence-type:
36007 in
36008 out
36009 inout
36010 mutexinout */
36011
36012 static void
36013 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
36014 {
36015 location_t loc = pragma_tok->location;
36016 matching_parens parens;
36017 if (!parens.require_open (parser))
36018 {
36019 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36020 return;
36021 }
36022
36023 tree depobj = cp_parser_assignment_expression (parser);
36024
36025 if (!parens.require_close (parser))
36026 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36027 /*or_comma=*/false,
36028 /*consume_paren=*/true);
36029
36030 tree clause = NULL_TREE;
36031 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
36032 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
36033 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36034 {
36035 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36036 const char *p = IDENTIFIER_POINTER (id);
36037
36038 cp_lexer_consume_token (parser->lexer);
36039 if (!strcmp ("depend", p))
36040 {
36041 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
36042 if (clause)
36043 clause = finish_omp_clauses (clause, C_ORT_OMP);
36044 if (!clause)
36045 clause = error_mark_node;
36046 }
36047 else if (!strcmp ("destroy", p))
36048 kind = OMP_CLAUSE_DEPEND_LAST;
36049 else if (!strcmp ("update", p))
36050 {
36051 matching_parens c_parens;
36052 if (c_parens.require_open (parser))
36053 {
36054 location_t c2_loc
36055 = cp_lexer_peek_token (parser->lexer)->location;
36056 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36057 {
36058 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
36059 const char *p2 = IDENTIFIER_POINTER (id2);
36060
36061 cp_lexer_consume_token (parser->lexer);
36062 if (!strcmp ("in", p2))
36063 kind = OMP_CLAUSE_DEPEND_IN;
36064 else if (!strcmp ("out", p2))
36065 kind = OMP_CLAUSE_DEPEND_OUT;
36066 else if (!strcmp ("inout", p2))
36067 kind = OMP_CLAUSE_DEPEND_INOUT;
36068 else if (!strcmp ("mutexinoutset", p2))
36069 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
36070 }
36071 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
36072 {
36073 clause = error_mark_node;
36074 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
36075 "%<mutexinoutset%>");
36076 }
36077 if (!c_parens.require_close (parser))
36078 cp_parser_skip_to_closing_parenthesis (parser,
36079 /*recovering=*/true,
36080 /*or_comma=*/false,
36081 /*consume_paren=*/true);
36082 }
36083 else
36084 clause = error_mark_node;
36085 }
36086 }
36087 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
36088 {
36089 clause = error_mark_node;
36090 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
36091 }
36092 cp_parser_require_pragma_eol (parser, pragma_tok);
36093
36094 finish_omp_depobj (loc, depobj, kind, clause);
36095 }
36096
36097
36098 /* OpenMP 2.5:
36099 # pragma omp flush flush-vars[opt] new-line
36100
36101 flush-vars:
36102 ( variable-list )
36103
36104 OpenMP 5.0:
36105 # pragma omp flush memory-order-clause new-line */
36106
36107 static void
36108 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
36109 {
36110 enum memmodel mo = MEMMODEL_LAST;
36111 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36112 {
36113 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36114 const char *p = IDENTIFIER_POINTER (id);
36115 if (!strcmp (p, "acq_rel"))
36116 mo = MEMMODEL_ACQ_REL;
36117 else if (!strcmp (p, "release"))
36118 mo = MEMMODEL_RELEASE;
36119 else if (!strcmp (p, "acquire"))
36120 mo = MEMMODEL_ACQUIRE;
36121 else
36122 error_at (cp_lexer_peek_token (parser->lexer)->location,
36123 "expected %<acq_rel%>, %<release%> or %<acquire%>");
36124 cp_lexer_consume_token (parser->lexer);
36125 }
36126 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36127 {
36128 if (mo != MEMMODEL_LAST)
36129 error_at (cp_lexer_peek_token (parser->lexer)->location,
36130 "%<flush%> list specified together with memory order "
36131 "clause");
36132 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36133 }
36134 cp_parser_require_pragma_eol (parser, pragma_tok);
36135
36136 finish_omp_flush (mo);
36137 }
36138
36139 /* Helper function, to parse omp for increment expression. */
36140
36141 static tree
36142 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
36143 {
36144 tree cond = cp_parser_binary_expression (parser, false, true,
36145 PREC_NOT_OPERATOR, NULL);
36146 if (cond == error_mark_node
36147 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36148 {
36149 cp_parser_skip_to_end_of_statement (parser);
36150 return error_mark_node;
36151 }
36152
36153 switch (TREE_CODE (cond))
36154 {
36155 case GT_EXPR:
36156 case GE_EXPR:
36157 case LT_EXPR:
36158 case LE_EXPR:
36159 break;
36160 case NE_EXPR:
36161 if (code != OACC_LOOP)
36162 break;
36163 gcc_fallthrough ();
36164 default:
36165 return error_mark_node;
36166 }
36167
36168 /* If decl is an iterator, preserve LHS and RHS of the relational
36169 expr until finish_omp_for. */
36170 if (decl
36171 && (type_dependent_expression_p (decl)
36172 || CLASS_TYPE_P (TREE_TYPE (decl))))
36173 return cond;
36174
36175 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
36176 TREE_CODE (cond),
36177 TREE_OPERAND (cond, 0), ERROR_MARK,
36178 TREE_OPERAND (cond, 1), ERROR_MARK,
36179 /*overload=*/NULL, tf_warning_or_error);
36180 }
36181
36182 /* Helper function, to parse omp for increment expression. */
36183
36184 static tree
36185 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
36186 {
36187 cp_token *token = cp_lexer_peek_token (parser->lexer);
36188 enum tree_code op;
36189 tree lhs, rhs;
36190 cp_id_kind idk;
36191 bool decl_first;
36192
36193 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36194 {
36195 op = (token->type == CPP_PLUS_PLUS
36196 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
36197 cp_lexer_consume_token (parser->lexer);
36198 lhs = cp_parser_simple_cast_expression (parser);
36199 if (lhs != decl
36200 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36201 return error_mark_node;
36202 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36203 }
36204
36205 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
36206 if (lhs != decl
36207 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36208 return error_mark_node;
36209
36210 token = cp_lexer_peek_token (parser->lexer);
36211 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36212 {
36213 op = (token->type == CPP_PLUS_PLUS
36214 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
36215 cp_lexer_consume_token (parser->lexer);
36216 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36217 }
36218
36219 op = cp_parser_assignment_operator_opt (parser);
36220 if (op == ERROR_MARK)
36221 return error_mark_node;
36222
36223 if (op != NOP_EXPR)
36224 {
36225 rhs = cp_parser_assignment_expression (parser);
36226 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
36227 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36228 }
36229
36230 lhs = cp_parser_binary_expression (parser, false, false,
36231 PREC_ADDITIVE_EXPRESSION, NULL);
36232 token = cp_lexer_peek_token (parser->lexer);
36233 decl_first = (lhs == decl
36234 || (processing_template_decl && cp_tree_equal (lhs, decl)));
36235 if (decl_first)
36236 lhs = NULL_TREE;
36237 if (token->type != CPP_PLUS
36238 && token->type != CPP_MINUS)
36239 return error_mark_node;
36240
36241 do
36242 {
36243 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
36244 cp_lexer_consume_token (parser->lexer);
36245 rhs = cp_parser_binary_expression (parser, false, false,
36246 PREC_ADDITIVE_EXPRESSION, NULL);
36247 token = cp_lexer_peek_token (parser->lexer);
36248 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
36249 {
36250 if (lhs == NULL_TREE)
36251 {
36252 if (op == PLUS_EXPR)
36253 lhs = rhs;
36254 else
36255 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
36256 tf_warning_or_error);
36257 }
36258 else
36259 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
36260 ERROR_MARK, NULL, tf_warning_or_error);
36261 }
36262 }
36263 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
36264
36265 if (!decl_first)
36266 {
36267 if ((rhs != decl
36268 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
36269 || op == MINUS_EXPR)
36270 return error_mark_node;
36271 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
36272 }
36273 else
36274 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
36275
36276 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36277 }
36278
36279 /* Parse the initialization statement of an OpenMP for loop.
36280
36281 Return true if the resulting construct should have an
36282 OMP_CLAUSE_PRIVATE added to it. */
36283
36284 static tree
36285 cp_parser_omp_for_loop_init (cp_parser *parser,
36286 tree &this_pre_body,
36287 vec<tree, va_gc> *&for_block,
36288 tree &init,
36289 tree &orig_init,
36290 tree &decl,
36291 tree &real_decl)
36292 {
36293 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36294 return NULL_TREE;
36295
36296 tree add_private_clause = NULL_TREE;
36297
36298 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
36299
36300 init-expr:
36301 var = lb
36302 integer-type var = lb
36303 random-access-iterator-type var = lb
36304 pointer-type var = lb
36305 */
36306 cp_decl_specifier_seq type_specifiers;
36307
36308 /* First, try to parse as an initialized declaration. See
36309 cp_parser_condition, from whence the bulk of this is copied. */
36310
36311 cp_parser_parse_tentatively (parser);
36312 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
36313 /*is_declaration=*/true,
36314 /*is_trailing_return=*/false,
36315 &type_specifiers);
36316 if (cp_parser_parse_definitely (parser))
36317 {
36318 /* If parsing a type specifier seq succeeded, then this
36319 MUST be a initialized declaration. */
36320 tree asm_specification, attributes;
36321 cp_declarator *declarator;
36322
36323 declarator = cp_parser_declarator (parser,
36324 CP_PARSER_DECLARATOR_NAMED,
36325 CP_PARSER_FLAGS_NONE,
36326 /*ctor_dtor_or_conv_p=*/NULL,
36327 /*parenthesized_p=*/NULL,
36328 /*member_p=*/false,
36329 /*friend_p=*/false,
36330 /*static_p=*/false);
36331 attributes = cp_parser_attributes_opt (parser);
36332 asm_specification = cp_parser_asm_specification_opt (parser);
36333
36334 if (declarator == cp_error_declarator)
36335 cp_parser_skip_to_end_of_statement (parser);
36336
36337 else
36338 {
36339 tree pushed_scope, auto_node;
36340
36341 decl = start_decl (declarator, &type_specifiers,
36342 SD_INITIALIZED, attributes,
36343 /*prefix_attributes=*/NULL_TREE,
36344 &pushed_scope);
36345
36346 auto_node = type_uses_auto (TREE_TYPE (decl));
36347 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36348 {
36349 if (cp_lexer_next_token_is (parser->lexer,
36350 CPP_OPEN_PAREN))
36351 error ("parenthesized initialization is not allowed in "
36352 "OpenMP %<for%> loop");
36353 else
36354 /* Trigger an error. */
36355 cp_parser_require (parser, CPP_EQ, RT_EQ);
36356
36357 init = error_mark_node;
36358 cp_parser_skip_to_end_of_statement (parser);
36359 }
36360 else if (CLASS_TYPE_P (TREE_TYPE (decl))
36361 || type_dependent_expression_p (decl)
36362 || auto_node)
36363 {
36364 bool is_direct_init, is_non_constant_init;
36365
36366 init = cp_parser_initializer (parser,
36367 &is_direct_init,
36368 &is_non_constant_init);
36369
36370 if (auto_node)
36371 {
36372 TREE_TYPE (decl)
36373 = do_auto_deduction (TREE_TYPE (decl), init,
36374 auto_node);
36375
36376 if (!CLASS_TYPE_P (TREE_TYPE (decl))
36377 && !type_dependent_expression_p (decl))
36378 goto non_class;
36379 }
36380
36381 cp_finish_decl (decl, init, !is_non_constant_init,
36382 asm_specification,
36383 LOOKUP_ONLYCONVERTING);
36384 orig_init = init;
36385 if (CLASS_TYPE_P (TREE_TYPE (decl)))
36386 {
36387 vec_safe_push (for_block, this_pre_body);
36388 init = NULL_TREE;
36389 }
36390 else
36391 {
36392 init = pop_stmt_list (this_pre_body);
36393 if (init && TREE_CODE (init) == STATEMENT_LIST)
36394 {
36395 tree_stmt_iterator i = tsi_start (init);
36396 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36397 while (!tsi_end_p (i))
36398 {
36399 tree t = tsi_stmt (i);
36400 if (TREE_CODE (t) == DECL_EXPR
36401 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
36402 {
36403 tsi_delink (&i);
36404 vec_safe_push (for_block, t);
36405 continue;
36406 }
36407 break;
36408 }
36409 if (tsi_one_before_end_p (i))
36410 {
36411 tree t = tsi_stmt (i);
36412 tsi_delink (&i);
36413 free_stmt_list (init);
36414 init = t;
36415 }
36416 }
36417 }
36418 this_pre_body = NULL_TREE;
36419 }
36420 else
36421 {
36422 /* Consume '='. */
36423 cp_lexer_consume_token (parser->lexer);
36424 init = cp_parser_assignment_expression (parser);
36425
36426 non_class:
36427 if (TYPE_REF_P (TREE_TYPE (decl)))
36428 init = error_mark_node;
36429 else
36430 cp_finish_decl (decl, NULL_TREE,
36431 /*init_const_expr_p=*/false,
36432 asm_specification,
36433 LOOKUP_ONLYCONVERTING);
36434 }
36435
36436 if (pushed_scope)
36437 pop_scope (pushed_scope);
36438 }
36439 }
36440 else
36441 {
36442 cp_id_kind idk;
36443 /* If parsing a type specifier sequence failed, then
36444 this MUST be a simple expression. */
36445 cp_parser_parse_tentatively (parser);
36446 decl = cp_parser_primary_expression (parser, false, false,
36447 false, &idk);
36448 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
36449 if (!cp_parser_error_occurred (parser)
36450 && decl
36451 && (TREE_CODE (decl) == COMPONENT_REF
36452 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
36453 {
36454 cp_parser_abort_tentative_parse (parser);
36455 cp_parser_parse_tentatively (parser);
36456 cp_token *token = cp_lexer_peek_token (parser->lexer);
36457 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
36458 /*check_dependency_p=*/true,
36459 /*template_p=*/NULL,
36460 /*declarator_p=*/false,
36461 /*optional_p=*/false);
36462 if (name != error_mark_node
36463 && last_tok == cp_lexer_peek_token (parser->lexer))
36464 {
36465 decl = cp_parser_lookup_name_simple (parser, name,
36466 token->location);
36467 if (TREE_CODE (decl) == FIELD_DECL)
36468 add_private_clause = omp_privatize_field (decl, false);
36469 }
36470 cp_parser_abort_tentative_parse (parser);
36471 cp_parser_parse_tentatively (parser);
36472 decl = cp_parser_primary_expression (parser, false, false,
36473 false, &idk);
36474 }
36475 if (!cp_parser_error_occurred (parser)
36476 && decl
36477 && DECL_P (decl)
36478 && CLASS_TYPE_P (TREE_TYPE (decl)))
36479 {
36480 tree rhs;
36481
36482 cp_parser_parse_definitely (parser);
36483 cp_parser_require (parser, CPP_EQ, RT_EQ);
36484 rhs = cp_parser_assignment_expression (parser);
36485 orig_init = rhs;
36486 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
36487 decl, NOP_EXPR,
36488 rhs,
36489 tf_warning_or_error));
36490 if (!add_private_clause)
36491 add_private_clause = decl;
36492 }
36493 else
36494 {
36495 decl = NULL;
36496 cp_parser_abort_tentative_parse (parser);
36497 init = cp_parser_expression (parser);
36498 if (init)
36499 {
36500 if (TREE_CODE (init) == MODIFY_EXPR
36501 || TREE_CODE (init) == MODOP_EXPR)
36502 real_decl = TREE_OPERAND (init, 0);
36503 }
36504 }
36505 }
36506 return add_private_clause;
36507 }
36508
36509 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36510
36511 void
36512 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
36513 tree &decl, tree &orig_decl, tree &init,
36514 tree &orig_init, tree &cond, tree &incr)
36515 {
36516 tree begin, end, range_temp_decl = NULL_TREE;
36517 tree iter_type, begin_expr, end_expr;
36518
36519 if (processing_template_decl)
36520 {
36521 if (check_for_bare_parameter_packs (init))
36522 init = error_mark_node;
36523 if (!type_dependent_expression_p (init)
36524 /* do_auto_deduction doesn't mess with template init-lists. */
36525 && !BRACE_ENCLOSED_INITIALIZER_P (init))
36526 {
36527 tree d = decl;
36528 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
36529 {
36530 tree v = DECL_VALUE_EXPR (decl);
36531 if (TREE_CODE (v) == ARRAY_REF
36532 && VAR_P (TREE_OPERAND (v, 0))
36533 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36534 d = TREE_OPERAND (v, 0);
36535 }
36536 do_range_for_auto_deduction (d, init);
36537 }
36538 cond = global_namespace;
36539 incr = NULL_TREE;
36540 orig_init = init;
36541 if (this_pre_body)
36542 this_pre_body = pop_stmt_list (this_pre_body);
36543 return;
36544 }
36545
36546 init = mark_lvalue_use (init);
36547
36548 if (decl == error_mark_node || init == error_mark_node)
36549 /* If an error happened previously do nothing or else a lot of
36550 unhelpful errors would be issued. */
36551 begin_expr = end_expr = iter_type = error_mark_node;
36552 else
36553 {
36554 tree range_temp;
36555
36556 if (VAR_P (init)
36557 && array_of_runtime_bound_p (TREE_TYPE (init)))
36558 /* Can't bind a reference to an array of runtime bound. */
36559 range_temp = init;
36560 else
36561 {
36562 range_temp = build_range_temp (init);
36563 DECL_NAME (range_temp) = NULL_TREE;
36564 pushdecl (range_temp);
36565 cp_finish_decl (range_temp, init,
36566 /*is_constant_init*/false, NULL_TREE,
36567 LOOKUP_ONLYCONVERTING);
36568 range_temp_decl = range_temp;
36569 range_temp = convert_from_reference (range_temp);
36570 }
36571 iter_type = cp_parser_perform_range_for_lookup (range_temp,
36572 &begin_expr, &end_expr);
36573 }
36574
36575 tree end_iter_type = iter_type;
36576 if (cxx_dialect >= cxx17)
36577 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
36578 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
36579 TREE_USED (end) = 1;
36580 DECL_ARTIFICIAL (end) = 1;
36581 pushdecl (end);
36582 cp_finish_decl (end, end_expr,
36583 /*is_constant_init*/false, NULL_TREE,
36584 LOOKUP_ONLYCONVERTING);
36585
36586 /* The new for initialization statement. */
36587 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
36588 TREE_USED (begin) = 1;
36589 DECL_ARTIFICIAL (begin) = 1;
36590 pushdecl (begin);
36591 orig_init = init;
36592 if (CLASS_TYPE_P (iter_type))
36593 init = NULL_TREE;
36594 else
36595 {
36596 init = begin_expr;
36597 begin_expr = NULL_TREE;
36598 }
36599 cp_finish_decl (begin, begin_expr,
36600 /*is_constant_init*/false, NULL_TREE,
36601 LOOKUP_ONLYCONVERTING);
36602
36603 /* The new for condition. */
36604 if (CLASS_TYPE_P (iter_type))
36605 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
36606 else
36607 cond = build_x_binary_op (input_location, NE_EXPR,
36608 begin, ERROR_MARK,
36609 end, ERROR_MARK,
36610 NULL, tf_warning_or_error);
36611
36612 /* The new increment expression. */
36613 if (CLASS_TYPE_P (iter_type))
36614 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
36615 else
36616 incr = finish_unary_op_expr (input_location,
36617 PREINCREMENT_EXPR, begin,
36618 tf_warning_or_error);
36619
36620 orig_decl = decl;
36621 decl = begin;
36622 if (for_block)
36623 {
36624 vec_safe_push (for_block, this_pre_body);
36625 this_pre_body = NULL_TREE;
36626 }
36627
36628 tree decomp_first_name = NULL_TREE;
36629 unsigned decomp_cnt = 0;
36630 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
36631 {
36632 tree v = DECL_VALUE_EXPR (orig_decl);
36633 if (TREE_CODE (v) == ARRAY_REF
36634 && VAR_P (TREE_OPERAND (v, 0))
36635 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36636 {
36637 tree d = orig_decl;
36638 orig_decl = TREE_OPERAND (v, 0);
36639 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
36640 decomp_first_name = d;
36641 }
36642 }
36643
36644 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
36645 if (auto_node)
36646 {
36647 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36648 tf_none);
36649 if (!error_operand_p (t))
36650 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
36651 t, auto_node);
36652 }
36653
36654 tree v = make_tree_vec (decomp_cnt + 3);
36655 TREE_VEC_ELT (v, 0) = range_temp_decl;
36656 TREE_VEC_ELT (v, 1) = end;
36657 TREE_VEC_ELT (v, 2) = orig_decl;
36658 for (unsigned i = 0; i < decomp_cnt; i++)
36659 {
36660 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
36661 decomp_first_name = DECL_CHAIN (decomp_first_name);
36662 }
36663 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
36664 }
36665
36666 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36667 inside of the collapsed body. */
36668
36669 void
36670 cp_finish_omp_range_for (tree orig, tree begin)
36671 {
36672 gcc_assert (TREE_CODE (orig) == TREE_LIST
36673 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
36674 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
36675 tree decomp_first_name = NULL_TREE;
36676 unsigned int decomp_cnt = 0;
36677
36678 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36679 {
36680 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
36681 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
36682 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
36683 }
36684
36685 /* The declaration is initialized with *__begin inside the loop body. */
36686 cp_finish_decl (decl,
36687 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36688 tf_warning_or_error),
36689 /*is_constant_init*/false, NULL_TREE,
36690 LOOKUP_ONLYCONVERTING);
36691 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36692 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
36693 }
36694
36695 /* Parse the restricted form of the for statement allowed by OpenMP. */
36696
36697 static tree
36698 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
36699 tree *cclauses, bool *if_p)
36700 {
36701 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
36702 tree orig_decl;
36703 tree real_decl, initv, condv, incrv, declv, orig_declv;
36704 tree this_pre_body, cl, ordered_cl = NULL_TREE;
36705 location_t loc_first;
36706 bool collapse_err = false;
36707 int i, collapse = 1, ordered = 0, count, nbraces = 0;
36708 vec<tree, va_gc> *for_block = make_tree_vector ();
36709 auto_vec<tree, 4> orig_inits;
36710 bool tiling = false;
36711
36712 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
36713 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
36714 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
36715 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
36716 {
36717 tiling = true;
36718 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
36719 }
36720 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
36721 && OMP_CLAUSE_ORDERED_EXPR (cl))
36722 {
36723 ordered_cl = cl;
36724 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
36725 }
36726
36727 if (ordered && ordered < collapse)
36728 {
36729 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36730 "%<ordered%> clause parameter is less than %<collapse%>");
36731 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
36732 = build_int_cst (NULL_TREE, collapse);
36733 ordered = collapse;
36734 }
36735 if (ordered)
36736 {
36737 for (tree *pc = &clauses; *pc; )
36738 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
36739 {
36740 error_at (OMP_CLAUSE_LOCATION (*pc),
36741 "%<linear%> clause may not be specified together "
36742 "with %<ordered%> clause with a parameter");
36743 *pc = OMP_CLAUSE_CHAIN (*pc);
36744 }
36745 else
36746 pc = &OMP_CLAUSE_CHAIN (*pc);
36747 }
36748
36749 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
36750 count = ordered ? ordered : collapse;
36751
36752 declv = make_tree_vec (count);
36753 initv = make_tree_vec (count);
36754 condv = make_tree_vec (count);
36755 incrv = make_tree_vec (count);
36756 orig_declv = NULL_TREE;
36757
36758 loc_first = cp_lexer_peek_token (parser->lexer)->location;
36759
36760 for (i = 0; i < count; i++)
36761 {
36762 int bracecount = 0;
36763 tree add_private_clause = NULL_TREE;
36764 location_t loc;
36765
36766 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36767 {
36768 if (!collapse_err)
36769 cp_parser_error (parser, "for statement expected");
36770 return NULL;
36771 }
36772 loc = cp_lexer_consume_token (parser->lexer)->location;
36773
36774 /* Don't create location wrapper nodes within an OpenMP "for"
36775 statement. */
36776 auto_suppress_location_wrappers sentinel;
36777
36778 matching_parens parens;
36779 if (!parens.require_open (parser))
36780 return NULL;
36781
36782 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
36783 this_pre_body = push_stmt_list ();
36784
36785 if (code != OACC_LOOP && cxx_dialect >= cxx11)
36786 {
36787 /* Save tokens so that we can put them back. */
36788 cp_lexer_save_tokens (parser->lexer);
36789
36790 /* Look for ':' that is not nested in () or {}. */
36791 bool is_range_for
36792 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
36793 /*recovering=*/false,
36794 CPP_COLON,
36795 /*consume_paren=*/
36796 false) == -1);
36797
36798 /* Roll back the tokens we skipped. */
36799 cp_lexer_rollback_tokens (parser->lexer);
36800
36801 if (is_range_for)
36802 {
36803 bool saved_colon_corrects_to_scope_p
36804 = parser->colon_corrects_to_scope_p;
36805
36806 /* A colon is used in range-based for. */
36807 parser->colon_corrects_to_scope_p = false;
36808
36809 /* Parse the declaration. */
36810 cp_parser_simple_declaration (parser,
36811 /*function_definition_allowed_p=*/
36812 false, &decl);
36813 parser->colon_corrects_to_scope_p
36814 = saved_colon_corrects_to_scope_p;
36815
36816 cp_parser_require (parser, CPP_COLON, RT_COLON);
36817
36818 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
36819 false, 0, true);
36820
36821 cp_convert_omp_range_for (this_pre_body, for_block, decl,
36822 orig_decl, init, orig_init,
36823 cond, incr);
36824 if (this_pre_body)
36825 {
36826 if (pre_body)
36827 {
36828 tree t = pre_body;
36829 pre_body = push_stmt_list ();
36830 add_stmt (t);
36831 add_stmt (this_pre_body);
36832 pre_body = pop_stmt_list (pre_body);
36833 }
36834 else
36835 pre_body = this_pre_body;
36836 }
36837
36838 if (ordered_cl)
36839 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36840 "%<ordered%> clause with parameter on "
36841 "range-based %<for%> loop");
36842
36843 goto parse_close_paren;
36844 }
36845 }
36846
36847 add_private_clause
36848 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
36849 init, orig_init, decl, real_decl);
36850
36851 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36852 if (this_pre_body)
36853 {
36854 this_pre_body = pop_stmt_list (this_pre_body);
36855 if (pre_body)
36856 {
36857 tree t = pre_body;
36858 pre_body = push_stmt_list ();
36859 add_stmt (t);
36860 add_stmt (this_pre_body);
36861 pre_body = pop_stmt_list (pre_body);
36862 }
36863 else
36864 pre_body = this_pre_body;
36865 }
36866
36867 if (decl)
36868 real_decl = decl;
36869 if (cclauses != NULL
36870 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
36871 && real_decl != NULL_TREE)
36872 {
36873 tree *c;
36874 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
36875 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
36876 && OMP_CLAUSE_DECL (*c) == real_decl)
36877 {
36878 error_at (loc, "iteration variable %qD"
36879 " should not be firstprivate", real_decl);
36880 *c = OMP_CLAUSE_CHAIN (*c);
36881 }
36882 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
36883 && OMP_CLAUSE_DECL (*c) == real_decl)
36884 {
36885 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36886 tree l = *c;
36887 *c = OMP_CLAUSE_CHAIN (*c);
36888 if (code == OMP_SIMD)
36889 {
36890 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36891 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
36892 }
36893 else
36894 {
36895 OMP_CLAUSE_CHAIN (l) = clauses;
36896 clauses = l;
36897 }
36898 add_private_clause = NULL_TREE;
36899 }
36900 else
36901 {
36902 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
36903 && OMP_CLAUSE_DECL (*c) == real_decl)
36904 add_private_clause = NULL_TREE;
36905 c = &OMP_CLAUSE_CHAIN (*c);
36906 }
36907 }
36908
36909 if (add_private_clause)
36910 {
36911 tree c;
36912 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
36913 {
36914 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
36915 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
36916 && OMP_CLAUSE_DECL (c) == decl)
36917 break;
36918 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
36919 && OMP_CLAUSE_DECL (c) == decl)
36920 error_at (loc, "iteration variable %qD "
36921 "should not be firstprivate",
36922 decl);
36923 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
36924 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
36925 && OMP_CLAUSE_DECL (c) == decl)
36926 error_at (loc, "iteration variable %qD should not be reduction",
36927 decl);
36928 }
36929 if (c == NULL)
36930 {
36931 if (code != OMP_SIMD)
36932 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
36933 else if (collapse == 1)
36934 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
36935 else
36936 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
36937 OMP_CLAUSE_DECL (c) = add_private_clause;
36938 c = finish_omp_clauses (c, C_ORT_OMP);
36939 if (c)
36940 {
36941 OMP_CLAUSE_CHAIN (c) = clauses;
36942 clauses = c;
36943 /* For linear, signal that we need to fill up
36944 the so far unknown linear step. */
36945 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
36946 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
36947 }
36948 }
36949 }
36950
36951 cond = NULL;
36952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36953 cond = cp_parser_omp_for_cond (parser, decl, code);
36954 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36955
36956 incr = NULL;
36957 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
36958 {
36959 /* If decl is an iterator, preserve the operator on decl
36960 until finish_omp_for. */
36961 if (real_decl
36962 && ((processing_template_decl
36963 && (TREE_TYPE (real_decl) == NULL_TREE
36964 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
36965 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
36966 incr = cp_parser_omp_for_incr (parser, real_decl);
36967 else
36968 incr = cp_parser_expression (parser);
36969 if (!EXPR_HAS_LOCATION (incr))
36970 protected_set_expr_location (incr, input_location);
36971 }
36972
36973 parse_close_paren:
36974 if (!parens.require_close (parser))
36975 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36976 /*or_comma=*/false,
36977 /*consume_paren=*/true);
36978
36979 TREE_VEC_ELT (declv, i) = decl;
36980 TREE_VEC_ELT (initv, i) = init;
36981 TREE_VEC_ELT (condv, i) = cond;
36982 TREE_VEC_ELT (incrv, i) = incr;
36983 if (orig_init)
36984 {
36985 orig_inits.safe_grow_cleared (i + 1);
36986 orig_inits[i] = orig_init;
36987 }
36988 if (orig_decl)
36989 {
36990 if (!orig_declv)
36991 orig_declv = copy_node (declv);
36992 TREE_VEC_ELT (orig_declv, i) = orig_decl;
36993 }
36994 else if (orig_declv)
36995 TREE_VEC_ELT (orig_declv, i) = decl;
36996
36997 if (i == count - 1)
36998 break;
36999
37000 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
37001 in between the collapsed for loops to be still considered perfectly
37002 nested. Hopefully the final version clarifies this.
37003 For now handle (multiple) {'s and empty statements. */
37004 cp_parser_parse_tentatively (parser);
37005 for (;;)
37006 {
37007 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37008 break;
37009 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
37010 {
37011 cp_lexer_consume_token (parser->lexer);
37012 bracecount++;
37013 }
37014 else if (bracecount
37015 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37016 cp_lexer_consume_token (parser->lexer);
37017 else
37018 {
37019 loc = cp_lexer_peek_token (parser->lexer)->location;
37020 error_at (loc, "not enough for loops to collapse");
37021 collapse_err = true;
37022 cp_parser_abort_tentative_parse (parser);
37023 declv = NULL_TREE;
37024 break;
37025 }
37026 }
37027
37028 if (declv)
37029 {
37030 cp_parser_parse_definitely (parser);
37031 nbraces += bracecount;
37032 }
37033 }
37034
37035 if (nbraces)
37036 if_p = NULL;
37037
37038 /* Note that we saved the original contents of this flag when we entered
37039 the structured block, and so we don't need to re-save it here. */
37040 parser->in_statement = IN_OMP_FOR;
37041
37042 /* Note that the grammar doesn't call for a structured block here,
37043 though the loop as a whole is a structured block. */
37044 if (orig_declv)
37045 {
37046 body = begin_omp_structured_block ();
37047 for (i = 0; i < count; i++)
37048 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
37049 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
37050 TREE_VEC_ELT (declv, i));
37051 }
37052 else
37053 body = push_stmt_list ();
37054 cp_parser_statement (parser, NULL_TREE, false, if_p);
37055 if (orig_declv)
37056 body = finish_omp_structured_block (body);
37057 else
37058 body = pop_stmt_list (body);
37059
37060 if (declv == NULL_TREE)
37061 ret = NULL_TREE;
37062 else
37063 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
37064 incrv, body, pre_body, &orig_inits, clauses);
37065
37066 while (nbraces)
37067 {
37068 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
37069 {
37070 cp_lexer_consume_token (parser->lexer);
37071 nbraces--;
37072 }
37073 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37074 cp_lexer_consume_token (parser->lexer);
37075 else
37076 {
37077 if (!collapse_err)
37078 {
37079 error_at (cp_lexer_peek_token (parser->lexer)->location,
37080 "collapsed loops not perfectly nested");
37081 }
37082 collapse_err = true;
37083 cp_parser_statement_seq_opt (parser, NULL);
37084 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
37085 break;
37086 }
37087 }
37088
37089 while (!for_block->is_empty ())
37090 {
37091 tree t = for_block->pop ();
37092 if (TREE_CODE (t) == STATEMENT_LIST)
37093 add_stmt (pop_stmt_list (t));
37094 else
37095 add_stmt (t);
37096 }
37097 release_tree_vector (for_block);
37098
37099 return ret;
37100 }
37101
37102 /* Helper function for OpenMP parsing, split clauses and call
37103 finish_omp_clauses on each of the set of clauses afterwards. */
37104
37105 static void
37106 cp_omp_split_clauses (location_t loc, enum tree_code code,
37107 omp_clause_mask mask, tree clauses, tree *cclauses)
37108 {
37109 int i;
37110 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
37111 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
37112 if (cclauses[i])
37113 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
37114 }
37115
37116 /* OpenMP 4.0:
37117 #pragma omp simd simd-clause[optseq] new-line
37118 for-loop */
37119
37120 #define OMP_SIMD_CLAUSE_MASK \
37121 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
37122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
37131
37132 static tree
37133 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
37134 char *p_name, omp_clause_mask mask, tree *cclauses,
37135 bool *if_p)
37136 {
37137 tree clauses, sb, ret;
37138 unsigned int save;
37139 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37140
37141 strcat (p_name, " simd");
37142 mask |= OMP_SIMD_CLAUSE_MASK;
37143
37144 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37145 cclauses == NULL);
37146 if (cclauses)
37147 {
37148 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
37149 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
37150 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
37151 OMP_CLAUSE_ORDERED);
37152 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
37153 {
37154 error_at (OMP_CLAUSE_LOCATION (c),
37155 "%<ordered%> clause with parameter may not be specified "
37156 "on %qs construct", p_name);
37157 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
37158 }
37159 }
37160
37161 keep_next_level (true);
37162 sb = begin_omp_structured_block ();
37163 save = cp_parser_begin_omp_structured_block (parser);
37164
37165 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
37166
37167 cp_parser_end_omp_structured_block (parser, save);
37168 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37169
37170 return ret;
37171 }
37172
37173 /* OpenMP 2.5:
37174 #pragma omp for for-clause[optseq] new-line
37175 for-loop
37176
37177 OpenMP 4.0:
37178 #pragma omp for simd for-simd-clause[optseq] new-line
37179 for-loop */
37180
37181 #define OMP_FOR_CLAUSE_MASK \
37182 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
37188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
37189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37191
37192 static tree
37193 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
37194 char *p_name, omp_clause_mask mask, tree *cclauses,
37195 bool *if_p)
37196 {
37197 tree clauses, sb, ret;
37198 unsigned int save;
37199 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37200
37201 strcat (p_name, " for");
37202 mask |= OMP_FOR_CLAUSE_MASK;
37203 /* parallel for{, simd} disallows nowait clause, but for
37204 target {teams distribute ,}parallel for{, simd} it should be accepted. */
37205 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
37206 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37207 /* Composite distribute parallel for{, simd} disallows ordered clause. */
37208 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37209 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
37210
37211 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37212 {
37213 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37214 const char *p = IDENTIFIER_POINTER (id);
37215
37216 if (strcmp (p, "simd") == 0)
37217 {
37218 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37219 if (cclauses == NULL)
37220 cclauses = cclauses_buf;
37221
37222 cp_lexer_consume_token (parser->lexer);
37223 if (!flag_openmp) /* flag_openmp_simd */
37224 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37225 cclauses, if_p);
37226 sb = begin_omp_structured_block ();
37227 save = cp_parser_begin_omp_structured_block (parser);
37228 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37229 cclauses, if_p);
37230 cp_parser_end_omp_structured_block (parser, save);
37231 tree body = finish_omp_structured_block (sb);
37232 if (ret == NULL)
37233 return ret;
37234 ret = make_node (OMP_FOR);
37235 TREE_TYPE (ret) = void_type_node;
37236 OMP_FOR_BODY (ret) = body;
37237 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37238 SET_EXPR_LOCATION (ret, loc);
37239 add_stmt (ret);
37240 return ret;
37241 }
37242 }
37243 if (!flag_openmp) /* flag_openmp_simd */
37244 {
37245 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37246 return NULL_TREE;
37247 }
37248
37249 /* Composite distribute parallel for disallows linear clause. */
37250 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37251 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
37252
37253 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37254 cclauses == NULL);
37255 if (cclauses)
37256 {
37257 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
37258 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37259 }
37260
37261 keep_next_level (true);
37262 sb = begin_omp_structured_block ();
37263 save = cp_parser_begin_omp_structured_block (parser);
37264
37265 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
37266
37267 cp_parser_end_omp_structured_block (parser, save);
37268 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37269
37270 return ret;
37271 }
37272
37273 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
37274 omp_clause_mask, tree *, bool *);
37275
37276 /* OpenMP 2.5:
37277 # pragma omp master new-line
37278 structured-block */
37279
37280 static tree
37281 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
37282 char *p_name, omp_clause_mask mask, tree *cclauses,
37283 bool *if_p)
37284 {
37285 tree clauses, sb, ret;
37286 unsigned int save;
37287 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37288
37289 strcat (p_name, " master");
37290
37291 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37292 {
37293 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37294 const char *p = IDENTIFIER_POINTER (id);
37295
37296 if (strcmp (p, "taskloop") == 0)
37297 {
37298 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37299 if (cclauses == NULL)
37300 cclauses = cclauses_buf;
37301
37302 cp_lexer_consume_token (parser->lexer);
37303 if (!flag_openmp) /* flag_openmp_simd */
37304 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37305 cclauses, if_p);
37306 sb = begin_omp_structured_block ();
37307 save = cp_parser_begin_omp_structured_block (parser);
37308 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37309 cclauses, if_p);
37310 cp_parser_end_omp_structured_block (parser, save);
37311 tree body = finish_omp_structured_block (sb);
37312 if (ret == NULL)
37313 return ret;
37314 return c_finish_omp_master (loc, body);
37315 }
37316 }
37317 if (!flag_openmp) /* flag_openmp_simd */
37318 {
37319 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37320 return NULL_TREE;
37321 }
37322
37323 if (cclauses)
37324 {
37325 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37326 false);
37327 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
37328 }
37329 else
37330 cp_parser_require_pragma_eol (parser, pragma_tok);
37331
37332 return c_finish_omp_master (loc,
37333 cp_parser_omp_structured_block (parser, if_p));
37334 }
37335
37336 /* OpenMP 2.5:
37337 # pragma omp ordered new-line
37338 structured-block
37339
37340 OpenMP 4.5:
37341 # pragma omp ordered ordered-clauses new-line
37342 structured-block */
37343
37344 #define OMP_ORDERED_CLAUSE_MASK \
37345 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
37346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37347
37348 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
37349 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37350
37351 static bool
37352 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
37353 enum pragma_context context, bool *if_p)
37354 {
37355 location_t loc = pragma_tok->location;
37356
37357 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37358 {
37359 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37360 const char *p = IDENTIFIER_POINTER (id);
37361
37362 if (strcmp (p, "depend") == 0)
37363 {
37364 if (!flag_openmp) /* flag_openmp_simd */
37365 {
37366 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37367 return false;
37368 }
37369 if (context == pragma_stmt)
37370 {
37371 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
37372 "%<depend%> clause may only be used in compound "
37373 "statements");
37374 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37375 return false;
37376 }
37377 tree clauses
37378 = cp_parser_omp_all_clauses (parser,
37379 OMP_ORDERED_DEPEND_CLAUSE_MASK,
37380 "#pragma omp ordered", pragma_tok);
37381 c_finish_omp_ordered (loc, clauses, NULL_TREE);
37382 return false;
37383 }
37384 }
37385
37386 tree clauses
37387 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
37388 "#pragma omp ordered", pragma_tok);
37389
37390 if (!flag_openmp /* flag_openmp_simd */
37391 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
37392 return false;
37393
37394 c_finish_omp_ordered (loc, clauses,
37395 cp_parser_omp_structured_block (parser, if_p));
37396 return true;
37397 }
37398
37399 /* OpenMP 2.5:
37400
37401 section-scope:
37402 { section-sequence }
37403
37404 section-sequence:
37405 section-directive[opt] structured-block
37406 section-sequence section-directive structured-block */
37407
37408 static tree
37409 cp_parser_omp_sections_scope (cp_parser *parser)
37410 {
37411 tree stmt, substmt;
37412 bool error_suppress = false;
37413 cp_token *tok;
37414
37415 matching_braces braces;
37416 if (!braces.require_open (parser))
37417 return NULL_TREE;
37418
37419 stmt = push_stmt_list ();
37420
37421 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
37422 != PRAGMA_OMP_SECTION)
37423 {
37424 substmt = cp_parser_omp_structured_block (parser, NULL);
37425 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37426 add_stmt (substmt);
37427 }
37428
37429 while (1)
37430 {
37431 tok = cp_lexer_peek_token (parser->lexer);
37432 if (tok->type == CPP_CLOSE_BRACE)
37433 break;
37434 if (tok->type == CPP_EOF)
37435 break;
37436
37437 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
37438 {
37439 cp_lexer_consume_token (parser->lexer);
37440 cp_parser_require_pragma_eol (parser, tok);
37441 error_suppress = false;
37442 }
37443 else if (!error_suppress)
37444 {
37445 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
37446 error_suppress = true;
37447 }
37448
37449 substmt = cp_parser_omp_structured_block (parser, NULL);
37450 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37451 add_stmt (substmt);
37452 }
37453 braces.require_close (parser);
37454
37455 substmt = pop_stmt_list (stmt);
37456
37457 stmt = make_node (OMP_SECTIONS);
37458 TREE_TYPE (stmt) = void_type_node;
37459 OMP_SECTIONS_BODY (stmt) = substmt;
37460
37461 add_stmt (stmt);
37462 return stmt;
37463 }
37464
37465 /* OpenMP 2.5:
37466 # pragma omp sections sections-clause[optseq] newline
37467 sections-scope */
37468
37469 #define OMP_SECTIONS_CLAUSE_MASK \
37470 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37475
37476 static tree
37477 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
37478 char *p_name, omp_clause_mask mask, tree *cclauses)
37479 {
37480 tree clauses, ret;
37481 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37482
37483 strcat (p_name, " sections");
37484 mask |= OMP_SECTIONS_CLAUSE_MASK;
37485 if (cclauses)
37486 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37487
37488 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37489 cclauses == NULL);
37490 if (cclauses)
37491 {
37492 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
37493 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
37494 }
37495
37496 ret = cp_parser_omp_sections_scope (parser);
37497 if (ret)
37498 OMP_SECTIONS_CLAUSES (ret) = clauses;
37499
37500 return ret;
37501 }
37502
37503 /* OpenMP 2.5:
37504 # pragma omp parallel parallel-clause[optseq] new-line
37505 structured-block
37506 # pragma omp parallel for parallel-for-clause[optseq] new-line
37507 structured-block
37508 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37509 structured-block
37510
37511 OpenMP 4.0:
37512 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37513 structured-block */
37514
37515 #define OMP_PARALLEL_CLAUSE_MASK \
37516 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37525
37526 static tree
37527 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
37528 char *p_name, omp_clause_mask mask, tree *cclauses,
37529 bool *if_p)
37530 {
37531 tree stmt, clauses, block;
37532 unsigned int save;
37533 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37534
37535 strcat (p_name, " parallel");
37536 mask |= OMP_PARALLEL_CLAUSE_MASK;
37537 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37538 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
37539 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
37540 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
37541
37542 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37543 {
37544 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37545 if (cclauses == NULL)
37546 cclauses = cclauses_buf;
37547
37548 cp_lexer_consume_token (parser->lexer);
37549 if (!flag_openmp) /* flag_openmp_simd */
37550 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37551 if_p);
37552 block = begin_omp_parallel ();
37553 save = cp_parser_begin_omp_structured_block (parser);
37554 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37555 if_p);
37556 cp_parser_end_omp_structured_block (parser, save);
37557 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37558 block);
37559 if (ret == NULL_TREE)
37560 return ret;
37561 OMP_PARALLEL_COMBINED (stmt) = 1;
37562 return stmt;
37563 }
37564 /* When combined with distribute, parallel has to be followed by for.
37565 #pragma omp target parallel is allowed though. */
37566 else if (cclauses
37567 && (mask & (OMP_CLAUSE_MASK_1
37568 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37569 {
37570 error_at (loc, "expected %<for%> after %qs", p_name);
37571 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37572 return NULL_TREE;
37573 }
37574 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37575 {
37576 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37577 const char *p = IDENTIFIER_POINTER (id);
37578 if (strcmp (p, "master") == 0)
37579 {
37580 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37581 cclauses = cclauses_buf;
37582
37583 cp_lexer_consume_token (parser->lexer);
37584 block = begin_omp_parallel ();
37585 save = cp_parser_begin_omp_structured_block (parser);
37586 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
37587 cclauses, if_p);
37588 cp_parser_end_omp_structured_block (parser, save);
37589 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37590 block);
37591 OMP_PARALLEL_COMBINED (stmt) = 1;
37592 if (ret == NULL_TREE)
37593 return ret;
37594 return stmt;
37595 }
37596 else if (!flag_openmp) /* flag_openmp_simd */
37597 {
37598 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37599 return NULL_TREE;
37600 }
37601 else if (strcmp (p, "sections") == 0)
37602 {
37603 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37604 cclauses = cclauses_buf;
37605
37606 cp_lexer_consume_token (parser->lexer);
37607 block = begin_omp_parallel ();
37608 save = cp_parser_begin_omp_structured_block (parser);
37609 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
37610 cp_parser_end_omp_structured_block (parser, save);
37611 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37612 block);
37613 OMP_PARALLEL_COMBINED (stmt) = 1;
37614 return stmt;
37615 }
37616 }
37617 else if (!flag_openmp) /* flag_openmp_simd */
37618 {
37619 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37620 return NULL_TREE;
37621 }
37622
37623 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37624 cclauses == NULL);
37625 if (cclauses)
37626 {
37627 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
37628 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
37629 }
37630
37631 block = begin_omp_parallel ();
37632 save = cp_parser_begin_omp_structured_block (parser);
37633 cp_parser_statement (parser, NULL_TREE, false, if_p);
37634 cp_parser_end_omp_structured_block (parser, save);
37635 stmt = finish_omp_parallel (clauses, block);
37636 return stmt;
37637 }
37638
37639 /* OpenMP 2.5:
37640 # pragma omp single single-clause[optseq] new-line
37641 structured-block */
37642
37643 #define OMP_SINGLE_CLAUSE_MASK \
37644 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37648
37649 static tree
37650 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37651 {
37652 tree stmt = make_node (OMP_SINGLE);
37653 TREE_TYPE (stmt) = void_type_node;
37654 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37655
37656 OMP_SINGLE_CLAUSES (stmt)
37657 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
37658 "#pragma omp single", pragma_tok);
37659 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37660
37661 return add_stmt (stmt);
37662 }
37663
37664 /* OpenMP 3.0:
37665 # pragma omp task task-clause[optseq] new-line
37666 structured-block */
37667
37668 #define OMP_TASK_CLAUSE_MASK \
37669 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37680
37681 static tree
37682 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37683 {
37684 tree clauses, block;
37685 unsigned int save;
37686
37687 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
37688 "#pragma omp task", pragma_tok);
37689 block = begin_omp_task ();
37690 save = cp_parser_begin_omp_structured_block (parser);
37691 cp_parser_statement (parser, NULL_TREE, false, if_p);
37692 cp_parser_end_omp_structured_block (parser, save);
37693 return finish_omp_task (clauses, block);
37694 }
37695
37696 /* OpenMP 3.0:
37697 # pragma omp taskwait new-line
37698
37699 OpenMP 5.0:
37700 # pragma omp taskwait taskwait-clause[opt] new-line */
37701
37702 #define OMP_TASKWAIT_CLAUSE_MASK \
37703 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37704
37705 static void
37706 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
37707 {
37708 tree clauses
37709 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
37710 "#pragma omp taskwait", pragma_tok);
37711
37712 if (clauses)
37713 {
37714 tree stmt = make_node (OMP_TASK);
37715 TREE_TYPE (stmt) = void_node;
37716 OMP_TASK_CLAUSES (stmt) = clauses;
37717 OMP_TASK_BODY (stmt) = NULL_TREE;
37718 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37719 add_stmt (stmt);
37720 }
37721 else
37722 finish_omp_taskwait ();
37723 }
37724
37725 /* OpenMP 3.1:
37726 # pragma omp taskyield new-line */
37727
37728 static void
37729 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
37730 {
37731 cp_parser_require_pragma_eol (parser, pragma_tok);
37732 finish_omp_taskyield ();
37733 }
37734
37735 /* OpenMP 4.0:
37736 # pragma omp taskgroup new-line
37737 structured-block
37738
37739 OpenMP 5.0:
37740 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37741
37742 #define OMP_TASKGROUP_CLAUSE_MASK \
37743 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37744
37745 static tree
37746 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37747 {
37748 tree clauses
37749 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
37750 "#pragma omp taskgroup", pragma_tok);
37751 return c_finish_omp_taskgroup (input_location,
37752 cp_parser_omp_structured_block (parser,
37753 if_p),
37754 clauses);
37755 }
37756
37757
37758 /* OpenMP 2.5:
37759 # pragma omp threadprivate (variable-list) */
37760
37761 static void
37762 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
37763 {
37764 tree vars;
37765
37766 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37767 cp_parser_require_pragma_eol (parser, pragma_tok);
37768
37769 finish_omp_threadprivate (vars);
37770 }
37771
37772 /* OpenMP 4.0:
37773 # pragma omp cancel cancel-clause[optseq] new-line */
37774
37775 #define OMP_CANCEL_CLAUSE_MASK \
37776 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37781
37782 static void
37783 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
37784 {
37785 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
37786 "#pragma omp cancel", pragma_tok);
37787 finish_omp_cancel (clauses);
37788 }
37789
37790 /* OpenMP 4.0:
37791 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37792
37793 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37794 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37798
37799 static void
37800 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
37801 enum pragma_context context)
37802 {
37803 tree clauses;
37804 bool point_seen = false;
37805
37806 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37807 {
37808 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37809 const char *p = IDENTIFIER_POINTER (id);
37810
37811 if (strcmp (p, "point") == 0)
37812 {
37813 cp_lexer_consume_token (parser->lexer);
37814 point_seen = true;
37815 }
37816 }
37817 if (!point_seen)
37818 {
37819 cp_parser_error (parser, "expected %<point%>");
37820 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37821 return;
37822 }
37823
37824 if (context != pragma_compound)
37825 {
37826 if (context == pragma_stmt)
37827 error_at (pragma_tok->location,
37828 "%<#pragma %s%> may only be used in compound statements",
37829 "omp cancellation point");
37830 else
37831 cp_parser_error (parser, "expected declaration specifiers");
37832 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37833 return;
37834 }
37835
37836 clauses = cp_parser_omp_all_clauses (parser,
37837 OMP_CANCELLATION_POINT_CLAUSE_MASK,
37838 "#pragma omp cancellation point",
37839 pragma_tok);
37840 finish_omp_cancellation_point (clauses);
37841 }
37842
37843 /* OpenMP 4.0:
37844 #pragma omp distribute distribute-clause[optseq] new-line
37845 for-loop */
37846
37847 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37848 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37853
37854 static tree
37855 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
37856 char *p_name, omp_clause_mask mask, tree *cclauses,
37857 bool *if_p)
37858 {
37859 tree clauses, sb, ret;
37860 unsigned int save;
37861 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37862
37863 strcat (p_name, " distribute");
37864 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
37865
37866 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37867 {
37868 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37869 const char *p = IDENTIFIER_POINTER (id);
37870 bool simd = false;
37871 bool parallel = false;
37872
37873 if (strcmp (p, "simd") == 0)
37874 simd = true;
37875 else
37876 parallel = strcmp (p, "parallel") == 0;
37877 if (parallel || simd)
37878 {
37879 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37880 if (cclauses == NULL)
37881 cclauses = cclauses_buf;
37882 cp_lexer_consume_token (parser->lexer);
37883 if (!flag_openmp) /* flag_openmp_simd */
37884 {
37885 if (simd)
37886 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37887 cclauses, if_p);
37888 else
37889 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37890 cclauses, if_p);
37891 }
37892 sb = begin_omp_structured_block ();
37893 save = cp_parser_begin_omp_structured_block (parser);
37894 if (simd)
37895 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37896 cclauses, if_p);
37897 else
37898 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37899 cclauses, if_p);
37900 cp_parser_end_omp_structured_block (parser, save);
37901 tree body = finish_omp_structured_block (sb);
37902 if (ret == NULL)
37903 return ret;
37904 ret = make_node (OMP_DISTRIBUTE);
37905 TREE_TYPE (ret) = void_type_node;
37906 OMP_FOR_BODY (ret) = body;
37907 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37908 SET_EXPR_LOCATION (ret, loc);
37909 add_stmt (ret);
37910 return ret;
37911 }
37912 }
37913 if (!flag_openmp) /* flag_openmp_simd */
37914 {
37915 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37916 return NULL_TREE;
37917 }
37918
37919 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37920 cclauses == NULL);
37921 if (cclauses)
37922 {
37923 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
37924 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37925 }
37926
37927 keep_next_level (true);
37928 sb = begin_omp_structured_block ();
37929 save = cp_parser_begin_omp_structured_block (parser);
37930
37931 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
37932
37933 cp_parser_end_omp_structured_block (parser, save);
37934 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37935
37936 return ret;
37937 }
37938
37939 /* OpenMP 4.0:
37940 # pragma omp teams teams-clause[optseq] new-line
37941 structured-block */
37942
37943 #define OMP_TEAMS_CLAUSE_MASK \
37944 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37951
37952 static tree
37953 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
37954 char *p_name, omp_clause_mask mask, tree *cclauses,
37955 bool *if_p)
37956 {
37957 tree clauses, sb, ret;
37958 unsigned int save;
37959 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37960
37961 strcat (p_name, " teams");
37962 mask |= OMP_TEAMS_CLAUSE_MASK;
37963
37964 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37965 {
37966 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37967 const char *p = IDENTIFIER_POINTER (id);
37968 if (strcmp (p, "distribute") == 0)
37969 {
37970 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37971 if (cclauses == NULL)
37972 cclauses = cclauses_buf;
37973
37974 cp_lexer_consume_token (parser->lexer);
37975 if (!flag_openmp) /* flag_openmp_simd */
37976 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37977 cclauses, if_p);
37978 keep_next_level (true);
37979 sb = begin_omp_structured_block ();
37980 save = cp_parser_begin_omp_structured_block (parser);
37981 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37982 cclauses, if_p);
37983 cp_parser_end_omp_structured_block (parser, save);
37984 tree body = finish_omp_structured_block (sb);
37985 if (ret == NULL)
37986 return ret;
37987 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37988 ret = make_node (OMP_TEAMS);
37989 TREE_TYPE (ret) = void_type_node;
37990 OMP_TEAMS_CLAUSES (ret) = clauses;
37991 OMP_TEAMS_BODY (ret) = body;
37992 OMP_TEAMS_COMBINED (ret) = 1;
37993 SET_EXPR_LOCATION (ret, loc);
37994 return add_stmt (ret);
37995 }
37996 }
37997 if (!flag_openmp) /* flag_openmp_simd */
37998 {
37999 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38000 return NULL_TREE;
38001 }
38002
38003 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38004 cclauses == NULL);
38005 if (cclauses)
38006 {
38007 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
38008 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38009 }
38010
38011 tree stmt = make_node (OMP_TEAMS);
38012 TREE_TYPE (stmt) = void_type_node;
38013 OMP_TEAMS_CLAUSES (stmt) = clauses;
38014 keep_next_level (true);
38015 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38016 SET_EXPR_LOCATION (stmt, loc);
38017
38018 return add_stmt (stmt);
38019 }
38020
38021 /* OpenMP 4.0:
38022 # pragma omp target data target-data-clause[optseq] new-line
38023 structured-block */
38024
38025 #define OMP_TARGET_DATA_CLAUSE_MASK \
38026 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
38030
38031 static tree
38032 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38033 {
38034 tree clauses
38035 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
38036 "#pragma omp target data", pragma_tok);
38037 int map_seen = 0;
38038 for (tree *pc = &clauses; *pc;)
38039 {
38040 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38041 switch (OMP_CLAUSE_MAP_KIND (*pc))
38042 {
38043 case GOMP_MAP_TO:
38044 case GOMP_MAP_ALWAYS_TO:
38045 case GOMP_MAP_FROM:
38046 case GOMP_MAP_ALWAYS_FROM:
38047 case GOMP_MAP_TOFROM:
38048 case GOMP_MAP_ALWAYS_TOFROM:
38049 case GOMP_MAP_ALLOC:
38050 map_seen = 3;
38051 break;
38052 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38053 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38054 case GOMP_MAP_ALWAYS_POINTER:
38055 break;
38056 default:
38057 map_seen |= 1;
38058 error_at (OMP_CLAUSE_LOCATION (*pc),
38059 "%<#pragma omp target data%> with map-type other "
38060 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38061 "on %<map%> clause");
38062 *pc = OMP_CLAUSE_CHAIN (*pc);
38063 continue;
38064 }
38065 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
38066 map_seen = 3;
38067 pc = &OMP_CLAUSE_CHAIN (*pc);
38068 }
38069
38070 if (map_seen != 3)
38071 {
38072 if (map_seen == 0)
38073 error_at (pragma_tok->location,
38074 "%<#pragma omp target data%> must contain at least "
38075 "one %<map%> or %<use_device_ptr%> clause");
38076 return NULL_TREE;
38077 }
38078
38079 tree stmt = make_node (OMP_TARGET_DATA);
38080 TREE_TYPE (stmt) = void_type_node;
38081 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
38082
38083 keep_next_level (true);
38084 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38085
38086 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38087 return add_stmt (stmt);
38088 }
38089
38090 /* OpenMP 4.5:
38091 # pragma omp target enter data target-enter-data-clause[optseq] new-line
38092 structured-block */
38093
38094 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
38095 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38100
38101 static tree
38102 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
38103 enum pragma_context context)
38104 {
38105 bool data_seen = false;
38106 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38107 {
38108 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38109 const char *p = IDENTIFIER_POINTER (id);
38110
38111 if (strcmp (p, "data") == 0)
38112 {
38113 cp_lexer_consume_token (parser->lexer);
38114 data_seen = true;
38115 }
38116 }
38117 if (!data_seen)
38118 {
38119 cp_parser_error (parser, "expected %<data%>");
38120 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38121 return NULL_TREE;
38122 }
38123
38124 if (context == pragma_stmt)
38125 {
38126 error_at (pragma_tok->location,
38127 "%<#pragma %s%> may only be used in compound statements",
38128 "omp target enter data");
38129 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38130 return NULL_TREE;
38131 }
38132
38133 tree clauses
38134 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
38135 "#pragma omp target enter data", pragma_tok);
38136 int map_seen = 0;
38137 for (tree *pc = &clauses; *pc;)
38138 {
38139 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38140 switch (OMP_CLAUSE_MAP_KIND (*pc))
38141 {
38142 case GOMP_MAP_TO:
38143 case GOMP_MAP_ALWAYS_TO:
38144 case GOMP_MAP_ALLOC:
38145 map_seen = 3;
38146 break;
38147 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38148 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38149 case GOMP_MAP_ALWAYS_POINTER:
38150 break;
38151 default:
38152 map_seen |= 1;
38153 error_at (OMP_CLAUSE_LOCATION (*pc),
38154 "%<#pragma omp target enter data%> with map-type other "
38155 "than %<to%> or %<alloc%> on %<map%> clause");
38156 *pc = OMP_CLAUSE_CHAIN (*pc);
38157 continue;
38158 }
38159 pc = &OMP_CLAUSE_CHAIN (*pc);
38160 }
38161
38162 if (map_seen != 3)
38163 {
38164 if (map_seen == 0)
38165 error_at (pragma_tok->location,
38166 "%<#pragma omp target enter data%> must contain at least "
38167 "one %<map%> clause");
38168 return NULL_TREE;
38169 }
38170
38171 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
38172 TREE_TYPE (stmt) = void_type_node;
38173 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
38174 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38175 return add_stmt (stmt);
38176 }
38177
38178 /* OpenMP 4.5:
38179 # pragma omp target exit data target-enter-data-clause[optseq] new-line
38180 structured-block */
38181
38182 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
38183 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38188
38189 static tree
38190 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
38191 enum pragma_context context)
38192 {
38193 bool data_seen = false;
38194 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38195 {
38196 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38197 const char *p = IDENTIFIER_POINTER (id);
38198
38199 if (strcmp (p, "data") == 0)
38200 {
38201 cp_lexer_consume_token (parser->lexer);
38202 data_seen = true;
38203 }
38204 }
38205 if (!data_seen)
38206 {
38207 cp_parser_error (parser, "expected %<data%>");
38208 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38209 return NULL_TREE;
38210 }
38211
38212 if (context == pragma_stmt)
38213 {
38214 error_at (pragma_tok->location,
38215 "%<#pragma %s%> may only be used in compound statements",
38216 "omp target exit data");
38217 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38218 return NULL_TREE;
38219 }
38220
38221 tree clauses
38222 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
38223 "#pragma omp target exit data", pragma_tok);
38224 int map_seen = 0;
38225 for (tree *pc = &clauses; *pc;)
38226 {
38227 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38228 switch (OMP_CLAUSE_MAP_KIND (*pc))
38229 {
38230 case GOMP_MAP_FROM:
38231 case GOMP_MAP_ALWAYS_FROM:
38232 case GOMP_MAP_RELEASE:
38233 case GOMP_MAP_DELETE:
38234 map_seen = 3;
38235 break;
38236 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38237 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38238 case GOMP_MAP_ALWAYS_POINTER:
38239 break;
38240 default:
38241 map_seen |= 1;
38242 error_at (OMP_CLAUSE_LOCATION (*pc),
38243 "%<#pragma omp target exit data%> with map-type other "
38244 "than %<from%>, %<release%> or %<delete%> on %<map%>"
38245 " clause");
38246 *pc = OMP_CLAUSE_CHAIN (*pc);
38247 continue;
38248 }
38249 pc = &OMP_CLAUSE_CHAIN (*pc);
38250 }
38251
38252 if (map_seen != 3)
38253 {
38254 if (map_seen == 0)
38255 error_at (pragma_tok->location,
38256 "%<#pragma omp target exit data%> must contain at least "
38257 "one %<map%> clause");
38258 return NULL_TREE;
38259 }
38260
38261 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
38262 TREE_TYPE (stmt) = void_type_node;
38263 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
38264 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38265 return add_stmt (stmt);
38266 }
38267
38268 /* OpenMP 4.0:
38269 # pragma omp target update target-update-clause[optseq] new-line */
38270
38271 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
38272 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
38273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38278
38279 static bool
38280 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
38281 enum pragma_context context)
38282 {
38283 if (context == pragma_stmt)
38284 {
38285 error_at (pragma_tok->location,
38286 "%<#pragma %s%> may only be used in compound statements",
38287 "omp target update");
38288 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38289 return false;
38290 }
38291
38292 tree clauses
38293 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
38294 "#pragma omp target update", pragma_tok);
38295 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
38296 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
38297 {
38298 error_at (pragma_tok->location,
38299 "%<#pragma omp target update%> must contain at least one "
38300 "%<from%> or %<to%> clauses");
38301 return false;
38302 }
38303
38304 tree stmt = make_node (OMP_TARGET_UPDATE);
38305 TREE_TYPE (stmt) = void_type_node;
38306 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
38307 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38308 add_stmt (stmt);
38309 return false;
38310 }
38311
38312 /* OpenMP 4.0:
38313 # pragma omp target target-clause[optseq] new-line
38314 structured-block */
38315
38316 #define OMP_TARGET_CLAUSE_MASK \
38317 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
38325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
38326
38327 static bool
38328 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
38329 enum pragma_context context, bool *if_p)
38330 {
38331 tree *pc = NULL, stmt;
38332
38333 if (flag_openmp)
38334 omp_requires_mask
38335 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
38336
38337 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38338 {
38339 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38340 const char *p = IDENTIFIER_POINTER (id);
38341 enum tree_code ccode = ERROR_MARK;
38342
38343 if (strcmp (p, "teams") == 0)
38344 ccode = OMP_TEAMS;
38345 else if (strcmp (p, "parallel") == 0)
38346 ccode = OMP_PARALLEL;
38347 else if (strcmp (p, "simd") == 0)
38348 ccode = OMP_SIMD;
38349 if (ccode != ERROR_MARK)
38350 {
38351 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
38352 char p_name[sizeof ("#pragma omp target teams distribute "
38353 "parallel for simd")];
38354
38355 cp_lexer_consume_token (parser->lexer);
38356 strcpy (p_name, "#pragma omp target");
38357 if (!flag_openmp) /* flag_openmp_simd */
38358 {
38359 tree stmt;
38360 switch (ccode)
38361 {
38362 case OMP_TEAMS:
38363 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
38364 OMP_TARGET_CLAUSE_MASK,
38365 cclauses, if_p);
38366 break;
38367 case OMP_PARALLEL:
38368 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38369 OMP_TARGET_CLAUSE_MASK,
38370 cclauses, if_p);
38371 break;
38372 case OMP_SIMD:
38373 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
38374 OMP_TARGET_CLAUSE_MASK,
38375 cclauses, if_p);
38376 break;
38377 default:
38378 gcc_unreachable ();
38379 }
38380 return stmt != NULL_TREE;
38381 }
38382 keep_next_level (true);
38383 tree sb = begin_omp_structured_block (), ret;
38384 unsigned save = cp_parser_begin_omp_structured_block (parser);
38385 switch (ccode)
38386 {
38387 case OMP_TEAMS:
38388 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
38389 OMP_TARGET_CLAUSE_MASK, cclauses,
38390 if_p);
38391 break;
38392 case OMP_PARALLEL:
38393 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38394 OMP_TARGET_CLAUSE_MASK, cclauses,
38395 if_p);
38396 break;
38397 case OMP_SIMD:
38398 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
38399 OMP_TARGET_CLAUSE_MASK, cclauses,
38400 if_p);
38401 break;
38402 default:
38403 gcc_unreachable ();
38404 }
38405 cp_parser_end_omp_structured_block (parser, save);
38406 tree body = finish_omp_structured_block (sb);
38407 if (ret == NULL_TREE)
38408 return false;
38409 if (ccode == OMP_TEAMS && !processing_template_decl)
38410 {
38411 /* For combined target teams, ensure the num_teams and
38412 thread_limit clause expressions are evaluated on the host,
38413 before entering the target construct. */
38414 tree c;
38415 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38416 c; c = OMP_CLAUSE_CHAIN (c))
38417 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
38418 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
38419 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
38420 {
38421 tree expr = OMP_CLAUSE_OPERAND (c, 0);
38422 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
38423 if (expr == error_mark_node)
38424 continue;
38425 tree tmp = TARGET_EXPR_SLOT (expr);
38426 add_stmt (expr);
38427 OMP_CLAUSE_OPERAND (c, 0) = expr;
38428 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
38429 OMP_CLAUSE_FIRSTPRIVATE);
38430 OMP_CLAUSE_DECL (tc) = tmp;
38431 OMP_CLAUSE_CHAIN (tc)
38432 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38433 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
38434 }
38435 }
38436 tree stmt = make_node (OMP_TARGET);
38437 TREE_TYPE (stmt) = void_type_node;
38438 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38439 OMP_TARGET_BODY (stmt) = body;
38440 OMP_TARGET_COMBINED (stmt) = 1;
38441 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38442 add_stmt (stmt);
38443 pc = &OMP_TARGET_CLAUSES (stmt);
38444 goto check_clauses;
38445 }
38446 else if (!flag_openmp) /* flag_openmp_simd */
38447 {
38448 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38449 return false;
38450 }
38451 else if (strcmp (p, "data") == 0)
38452 {
38453 cp_lexer_consume_token (parser->lexer);
38454 cp_parser_omp_target_data (parser, pragma_tok, if_p);
38455 return true;
38456 }
38457 else if (strcmp (p, "enter") == 0)
38458 {
38459 cp_lexer_consume_token (parser->lexer);
38460 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
38461 return false;
38462 }
38463 else if (strcmp (p, "exit") == 0)
38464 {
38465 cp_lexer_consume_token (parser->lexer);
38466 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
38467 return false;
38468 }
38469 else if (strcmp (p, "update") == 0)
38470 {
38471 cp_lexer_consume_token (parser->lexer);
38472 return cp_parser_omp_target_update (parser, pragma_tok, context);
38473 }
38474 }
38475 if (!flag_openmp) /* flag_openmp_simd */
38476 {
38477 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38478 return false;
38479 }
38480
38481 stmt = make_node (OMP_TARGET);
38482 TREE_TYPE (stmt) = void_type_node;
38483
38484 OMP_TARGET_CLAUSES (stmt)
38485 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
38486 "#pragma omp target", pragma_tok);
38487 pc = &OMP_TARGET_CLAUSES (stmt);
38488 keep_next_level (true);
38489 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38490
38491 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38492 add_stmt (stmt);
38493
38494 check_clauses:
38495 while (*pc)
38496 {
38497 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38498 switch (OMP_CLAUSE_MAP_KIND (*pc))
38499 {
38500 case GOMP_MAP_TO:
38501 case GOMP_MAP_ALWAYS_TO:
38502 case GOMP_MAP_FROM:
38503 case GOMP_MAP_ALWAYS_FROM:
38504 case GOMP_MAP_TOFROM:
38505 case GOMP_MAP_ALWAYS_TOFROM:
38506 case GOMP_MAP_ALLOC:
38507 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38508 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38509 case GOMP_MAP_ALWAYS_POINTER:
38510 break;
38511 default:
38512 error_at (OMP_CLAUSE_LOCATION (*pc),
38513 "%<#pragma omp target%> with map-type other "
38514 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38515 "on %<map%> clause");
38516 *pc = OMP_CLAUSE_CHAIN (*pc);
38517 continue;
38518 }
38519 pc = &OMP_CLAUSE_CHAIN (*pc);
38520 }
38521 return true;
38522 }
38523
38524 /* OpenACC 2.0:
38525 # pragma acc cache (variable-list) new-line
38526 */
38527
38528 static tree
38529 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
38530 {
38531 tree stmt, clauses;
38532
38533 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
38534 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38535
38536 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
38537
38538 stmt = make_node (OACC_CACHE);
38539 TREE_TYPE (stmt) = void_type_node;
38540 OACC_CACHE_CLAUSES (stmt) = clauses;
38541 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38542 add_stmt (stmt);
38543
38544 return stmt;
38545 }
38546
38547 /* OpenACC 2.0:
38548 # pragma acc data oacc-data-clause[optseq] new-line
38549 structured-block */
38550
38551 #define OACC_DATA_CLAUSE_MASK \
38552 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38559
38560 static tree
38561 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38562 {
38563 tree stmt, clauses, block;
38564 unsigned int save;
38565
38566 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
38567 "#pragma acc data", pragma_tok);
38568
38569 block = begin_omp_parallel ();
38570 save = cp_parser_begin_omp_structured_block (parser);
38571 cp_parser_statement (parser, NULL_TREE, false, if_p);
38572 cp_parser_end_omp_structured_block (parser, save);
38573 stmt = finish_oacc_data (clauses, block);
38574 return stmt;
38575 }
38576
38577 /* OpenACC 2.0:
38578 # pragma acc host_data <clauses> new-line
38579 structured-block */
38580
38581 #define OACC_HOST_DATA_CLAUSE_MASK \
38582 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38583
38584 static tree
38585 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38586 {
38587 tree stmt, clauses, block;
38588 unsigned int save;
38589
38590 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
38591 "#pragma acc host_data", pragma_tok);
38592
38593 block = begin_omp_parallel ();
38594 save = cp_parser_begin_omp_structured_block (parser);
38595 cp_parser_statement (parser, NULL_TREE, false, if_p);
38596 cp_parser_end_omp_structured_block (parser, save);
38597 stmt = finish_oacc_host_data (clauses, block);
38598 return stmt;
38599 }
38600
38601 /* OpenACC 2.0:
38602 # pragma acc declare oacc-data-clause[optseq] new-line
38603 */
38604
38605 #define OACC_DECLARE_CLAUSE_MASK \
38606 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38614
38615 static tree
38616 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
38617 {
38618 tree clauses, stmt;
38619 bool error = false;
38620
38621 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
38622 "#pragma acc declare", pragma_tok, true);
38623
38624
38625 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38626 {
38627 error_at (pragma_tok->location,
38628 "no valid clauses specified in %<#pragma acc declare%>");
38629 return NULL_TREE;
38630 }
38631
38632 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
38633 {
38634 location_t loc = OMP_CLAUSE_LOCATION (t);
38635 tree decl = OMP_CLAUSE_DECL (t);
38636 if (!DECL_P (decl))
38637 {
38638 error_at (loc, "array section in %<#pragma acc declare%>");
38639 error = true;
38640 continue;
38641 }
38642 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
38643 switch (OMP_CLAUSE_MAP_KIND (t))
38644 {
38645 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38646 case GOMP_MAP_ALLOC:
38647 case GOMP_MAP_TO:
38648 case GOMP_MAP_FORCE_DEVICEPTR:
38649 case GOMP_MAP_DEVICE_RESIDENT:
38650 break;
38651
38652 case GOMP_MAP_LINK:
38653 if (!global_bindings_p ()
38654 && (TREE_STATIC (decl)
38655 || !DECL_EXTERNAL (decl)))
38656 {
38657 error_at (loc,
38658 "%qD must be a global variable in "
38659 "%<#pragma acc declare link%>",
38660 decl);
38661 error = true;
38662 continue;
38663 }
38664 break;
38665
38666 default:
38667 if (global_bindings_p ())
38668 {
38669 error_at (loc, "invalid OpenACC clause at file scope");
38670 error = true;
38671 continue;
38672 }
38673 if (DECL_EXTERNAL (decl))
38674 {
38675 error_at (loc,
38676 "invalid use of %<extern%> variable %qD "
38677 "in %<#pragma acc declare%>", decl);
38678 error = true;
38679 continue;
38680 }
38681 else if (TREE_PUBLIC (decl))
38682 {
38683 error_at (loc,
38684 "invalid use of %<global%> variable %qD "
38685 "in %<#pragma acc declare%>", decl);
38686 error = true;
38687 continue;
38688 }
38689 break;
38690 }
38691
38692 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
38693 || lookup_attribute ("omp declare target link",
38694 DECL_ATTRIBUTES (decl)))
38695 {
38696 error_at (loc, "variable %qD used more than once with "
38697 "%<#pragma acc declare%>", decl);
38698 error = true;
38699 continue;
38700 }
38701
38702 if (!error)
38703 {
38704 tree id;
38705
38706 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
38707 id = get_identifier ("omp declare target link");
38708 else
38709 id = get_identifier ("omp declare target");
38710
38711 DECL_ATTRIBUTES (decl)
38712 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
38713 if (global_bindings_p ())
38714 {
38715 symtab_node *node = symtab_node::get (decl);
38716 if (node != NULL)
38717 {
38718 node->offloadable = 1;
38719 if (ENABLE_OFFLOADING)
38720 {
38721 g->have_offload = true;
38722 if (is_a <varpool_node *> (node))
38723 vec_safe_push (offload_vars, decl);
38724 }
38725 }
38726 }
38727 }
38728 }
38729
38730 if (error || global_bindings_p ())
38731 return NULL_TREE;
38732
38733 stmt = make_node (OACC_DECLARE);
38734 TREE_TYPE (stmt) = void_type_node;
38735 OACC_DECLARE_CLAUSES (stmt) = clauses;
38736 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38737
38738 add_stmt (stmt);
38739
38740 return NULL_TREE;
38741 }
38742
38743 /* OpenACC 2.0:
38744 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38745
38746 or
38747
38748 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38749
38750 LOC is the location of the #pragma token.
38751 */
38752
38753 #define OACC_ENTER_DATA_CLAUSE_MASK \
38754 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38759
38760 #define OACC_EXIT_DATA_CLAUSE_MASK \
38761 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38767
38768 static tree
38769 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
38770 bool enter)
38771 {
38772 location_t loc = pragma_tok->location;
38773 tree stmt, clauses;
38774 const char *p = "";
38775
38776 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38777 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38778
38779 if (strcmp (p, "data") != 0)
38780 {
38781 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
38782 enter ? "enter" : "exit");
38783 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38784 return NULL_TREE;
38785 }
38786
38787 cp_lexer_consume_token (parser->lexer);
38788
38789 if (enter)
38790 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
38791 "#pragma acc enter data", pragma_tok);
38792 else
38793 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
38794 "#pragma acc exit data", pragma_tok);
38795
38796 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38797 {
38798 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
38799 enter ? "enter" : "exit");
38800 return NULL_TREE;
38801 }
38802
38803 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
38804 TREE_TYPE (stmt) = void_type_node;
38805 OMP_STANDALONE_CLAUSES (stmt) = clauses;
38806 SET_EXPR_LOCATION (stmt, loc);
38807 add_stmt (stmt);
38808 return stmt;
38809 }
38810
38811 /* OpenACC 2.0:
38812 # pragma acc loop oacc-loop-clause[optseq] new-line
38813 structured-block */
38814
38815 #define OACC_LOOP_CLAUSE_MASK \
38816 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38826
38827 static tree
38828 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
38829 omp_clause_mask mask, tree *cclauses, bool *if_p)
38830 {
38831 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
38832
38833 strcat (p_name, " loop");
38834 mask |= OACC_LOOP_CLAUSE_MASK;
38835
38836 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
38837 cclauses == NULL);
38838 if (cclauses)
38839 {
38840 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
38841 if (*cclauses)
38842 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
38843 if (clauses)
38844 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38845 }
38846
38847 tree block = begin_omp_structured_block ();
38848 int save = cp_parser_begin_omp_structured_block (parser);
38849 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
38850 cp_parser_end_omp_structured_block (parser, save);
38851 add_stmt (finish_omp_structured_block (block));
38852
38853 return stmt;
38854 }
38855
38856 /* OpenACC 2.0:
38857 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38858 structured-block
38859
38860 or
38861
38862 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38863 structured-block
38864 */
38865
38866 #define OACC_KERNELS_CLAUSE_MASK \
38867 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38880
38881 #define OACC_PARALLEL_CLAUSE_MASK \
38882 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38898
38899 static tree
38900 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
38901 char *p_name, bool *if_p)
38902 {
38903 omp_clause_mask mask;
38904 enum tree_code code;
38905 switch (cp_parser_pragma_kind (pragma_tok))
38906 {
38907 case PRAGMA_OACC_KERNELS:
38908 strcat (p_name, " kernels");
38909 mask = OACC_KERNELS_CLAUSE_MASK;
38910 code = OACC_KERNELS;
38911 break;
38912 case PRAGMA_OACC_PARALLEL:
38913 strcat (p_name, " parallel");
38914 mask = OACC_PARALLEL_CLAUSE_MASK;
38915 code = OACC_PARALLEL;
38916 break;
38917 default:
38918 gcc_unreachable ();
38919 }
38920
38921 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38922 {
38923 const char *p
38924 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38925 if (strcmp (p, "loop") == 0)
38926 {
38927 cp_lexer_consume_token (parser->lexer);
38928 tree block = begin_omp_parallel ();
38929 tree clauses;
38930 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
38931 &clauses, if_p);
38932 protected_set_expr_location (stmt, pragma_tok->location);
38933 return finish_omp_construct (code, block, clauses);
38934 }
38935 }
38936
38937 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
38938
38939 tree block = begin_omp_parallel ();
38940 unsigned int save = cp_parser_begin_omp_structured_block (parser);
38941 cp_parser_statement (parser, NULL_TREE, false, if_p);
38942 cp_parser_end_omp_structured_block (parser, save);
38943 return finish_omp_construct (code, block, clauses);
38944 }
38945
38946 /* OpenACC 2.0:
38947 # pragma acc update oacc-update-clause[optseq] new-line
38948 */
38949
38950 #define OACC_UPDATE_CLAUSE_MASK \
38951 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38957
38958 static tree
38959 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
38960 {
38961 tree stmt, clauses;
38962
38963 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
38964 "#pragma acc update", pragma_tok);
38965
38966 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38967 {
38968 error_at (pragma_tok->location,
38969 "%<#pragma acc update%> must contain at least one "
38970 "%<device%> or %<host%> or %<self%> clause");
38971 return NULL_TREE;
38972 }
38973
38974 stmt = make_node (OACC_UPDATE);
38975 TREE_TYPE (stmt) = void_type_node;
38976 OACC_UPDATE_CLAUSES (stmt) = clauses;
38977 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38978 add_stmt (stmt);
38979 return stmt;
38980 }
38981
38982 /* OpenACC 2.0:
38983 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
38984
38985 LOC is the location of the #pragma token.
38986 */
38987
38988 #define OACC_WAIT_CLAUSE_MASK \
38989 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
38990
38991 static tree
38992 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
38993 {
38994 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
38995 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38996
38997 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38998 list = cp_parser_oacc_wait_list (parser, loc, list);
38999
39000 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
39001 "#pragma acc wait", pragma_tok);
39002
39003 stmt = c_finish_oacc_wait (loc, list, clauses);
39004 stmt = finish_expr_stmt (stmt);
39005
39006 return stmt;
39007 }
39008
39009 /* OpenMP 4.0:
39010 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
39011
39012 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
39013 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
39014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
39016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
39017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
39018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
39019
39020 static void
39021 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
39022 enum pragma_context context)
39023 {
39024 bool first_p = parser->omp_declare_simd == NULL;
39025 cp_omp_declare_simd_data data;
39026 if (first_p)
39027 {
39028 data.error_seen = false;
39029 data.fndecl_seen = false;
39030 data.tokens = vNULL;
39031 data.clauses = NULL_TREE;
39032 /* It is safe to take the address of a local variable; it will only be
39033 used while this scope is live. */
39034 parser->omp_declare_simd = &data;
39035 }
39036
39037 /* Store away all pragma tokens. */
39038 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39039 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39040 cp_lexer_consume_token (parser->lexer);
39041 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39042 parser->omp_declare_simd->error_seen = true;
39043 cp_parser_require_pragma_eol (parser, pragma_tok);
39044 struct cp_token_cache *cp
39045 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39046 parser->omp_declare_simd->tokens.safe_push (cp);
39047
39048 if (first_p)
39049 {
39050 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39051 cp_parser_pragma (parser, context, NULL);
39052 switch (context)
39053 {
39054 case pragma_external:
39055 cp_parser_declaration (parser);
39056 break;
39057 case pragma_member:
39058 cp_parser_member_declaration (parser);
39059 break;
39060 case pragma_objc_icode:
39061 cp_parser_block_declaration (parser, /*statement_p=*/false);
39062 break;
39063 default:
39064 cp_parser_declaration_statement (parser);
39065 break;
39066 }
39067 if (parser->omp_declare_simd
39068 && !parser->omp_declare_simd->error_seen
39069 && !parser->omp_declare_simd->fndecl_seen)
39070 error_at (pragma_tok->location,
39071 "%<#pragma omp declare simd%> not immediately followed by "
39072 "function declaration or definition");
39073 data.tokens.release ();
39074 parser->omp_declare_simd = NULL;
39075 }
39076 }
39077
39078 /* Finalize #pragma omp declare simd clauses after direct declarator has
39079 been parsed, and put that into "omp declare simd" attribute. */
39080
39081 static tree
39082 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
39083 {
39084 struct cp_token_cache *ce;
39085 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
39086 int i;
39087
39088 if (!data->error_seen && data->fndecl_seen)
39089 {
39090 error ("%<#pragma omp declare simd%> not immediately followed by "
39091 "a single function declaration or definition");
39092 data->error_seen = true;
39093 }
39094 if (data->error_seen)
39095 return attrs;
39096
39097 FOR_EACH_VEC_ELT (data->tokens, i, ce)
39098 {
39099 tree c, cl;
39100
39101 cp_parser_push_lexer_for_tokens (parser, ce);
39102 parser->lexer->in_pragma = true;
39103 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39104 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39105 cp_lexer_consume_token (parser->lexer);
39106 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
39107 "#pragma omp declare simd", pragma_tok);
39108 cp_parser_pop_lexer (parser);
39109 if (cl)
39110 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
39111 c = build_tree_list (get_identifier ("omp declare simd"), cl);
39112 TREE_CHAIN (c) = attrs;
39113 if (processing_template_decl)
39114 ATTR_IS_DEPENDENT (c) = 1;
39115 attrs = c;
39116 }
39117
39118 data->fndecl_seen = true;
39119 return attrs;
39120 }
39121
39122
39123 /* OpenMP 4.0:
39124 # pragma omp declare target new-line
39125 declarations and definitions
39126 # pragma omp end declare target new-line
39127
39128 OpenMP 4.5:
39129 # pragma omp declare target ( extended-list ) new-line
39130
39131 # pragma omp declare target declare-target-clauses[seq] new-line */
39132
39133 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
39134 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
39135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
39136
39137 static void
39138 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
39139 {
39140 tree clauses = NULL_TREE;
39141 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39142 clauses
39143 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
39144 "#pragma omp declare target", pragma_tok);
39145 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39146 {
39147 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
39148 clauses);
39149 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
39150 cp_parser_require_pragma_eol (parser, pragma_tok);
39151 }
39152 else
39153 {
39154 cp_parser_require_pragma_eol (parser, pragma_tok);
39155 scope_chain->omp_declare_target_attribute++;
39156 return;
39157 }
39158 if (scope_chain->omp_declare_target_attribute)
39159 error_at (pragma_tok->location,
39160 "%<#pragma omp declare target%> with clauses in between "
39161 "%<#pragma omp declare target%> without clauses and "
39162 "%<#pragma omp end declare target%>");
39163 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
39164 {
39165 tree t = OMP_CLAUSE_DECL (c), id;
39166 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
39167 tree at2 = lookup_attribute ("omp declare target link",
39168 DECL_ATTRIBUTES (t));
39169 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
39170 {
39171 id = get_identifier ("omp declare target link");
39172 std::swap (at1, at2);
39173 }
39174 else
39175 id = get_identifier ("omp declare target");
39176 if (at2)
39177 {
39178 error_at (OMP_CLAUSE_LOCATION (c),
39179 "%qD specified both in declare target %<link%> and %<to%>"
39180 " clauses", t);
39181 continue;
39182 }
39183 if (!at1)
39184 {
39185 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
39186 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
39187 continue;
39188
39189 symtab_node *node = symtab_node::get (t);
39190 if (node != NULL)
39191 {
39192 node->offloadable = 1;
39193 if (ENABLE_OFFLOADING)
39194 {
39195 g->have_offload = true;
39196 if (is_a <varpool_node *> (node))
39197 vec_safe_push (offload_vars, t);
39198 }
39199 }
39200 }
39201 }
39202 }
39203
39204 static void
39205 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
39206 {
39207 const char *p = "";
39208 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39209 {
39210 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39211 p = IDENTIFIER_POINTER (id);
39212 }
39213 if (strcmp (p, "declare") == 0)
39214 {
39215 cp_lexer_consume_token (parser->lexer);
39216 p = "";
39217 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39218 {
39219 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39220 p = IDENTIFIER_POINTER (id);
39221 }
39222 if (strcmp (p, "target") == 0)
39223 cp_lexer_consume_token (parser->lexer);
39224 else
39225 {
39226 cp_parser_error (parser, "expected %<target%>");
39227 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39228 return;
39229 }
39230 }
39231 else
39232 {
39233 cp_parser_error (parser, "expected %<declare%>");
39234 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39235 return;
39236 }
39237 cp_parser_require_pragma_eol (parser, pragma_tok);
39238 if (!scope_chain->omp_declare_target_attribute)
39239 error_at (pragma_tok->location,
39240 "%<#pragma omp end declare target%> without corresponding "
39241 "%<#pragma omp declare target%>");
39242 else
39243 scope_chain->omp_declare_target_attribute--;
39244 }
39245
39246 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
39247 expression and optional initializer clause of
39248 #pragma omp declare reduction. We store the expression(s) as
39249 either 3, 6 or 7 special statements inside of the artificial function's
39250 body. The first two statements are DECL_EXPRs for the artificial
39251 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
39252 expression that uses those variables.
39253 If there was any INITIALIZER clause, this is followed by further statements,
39254 the fourth and fifth statements are DECL_EXPRs for the artificial
39255 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
39256 constructor variant (first token after open paren is not omp_priv),
39257 then the sixth statement is a statement with the function call expression
39258 that uses the OMP_PRIV and optionally OMP_ORIG variable.
39259 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
39260 to initialize the OMP_PRIV artificial variable and there is seventh
39261 statement, a DECL_EXPR of the OMP_PRIV statement again. */
39262
39263 static bool
39264 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
39265 {
39266 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
39267 gcc_assert (TYPE_REF_P (type));
39268 type = TREE_TYPE (type);
39269 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
39270 DECL_ARTIFICIAL (omp_out) = 1;
39271 pushdecl (omp_out);
39272 add_decl_expr (omp_out);
39273 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
39274 DECL_ARTIFICIAL (omp_in) = 1;
39275 pushdecl (omp_in);
39276 add_decl_expr (omp_in);
39277 tree combiner;
39278 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
39279
39280 keep_next_level (true);
39281 tree block = begin_omp_structured_block ();
39282 combiner = cp_parser_expression (parser);
39283 finish_expr_stmt (combiner);
39284 block = finish_omp_structured_block (block);
39285 add_stmt (block);
39286
39287 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
39288 return false;
39289
39290 const char *p = "";
39291 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39292 {
39293 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39294 p = IDENTIFIER_POINTER (id);
39295 }
39296
39297 if (strcmp (p, "initializer") == 0)
39298 {
39299 cp_lexer_consume_token (parser->lexer);
39300 matching_parens parens;
39301 if (!parens.require_open (parser))
39302 return false;
39303
39304 p = "";
39305 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39306 {
39307 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39308 p = IDENTIFIER_POINTER (id);
39309 }
39310
39311 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
39312 DECL_ARTIFICIAL (omp_priv) = 1;
39313 pushdecl (omp_priv);
39314 add_decl_expr (omp_priv);
39315 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
39316 DECL_ARTIFICIAL (omp_orig) = 1;
39317 pushdecl (omp_orig);
39318 add_decl_expr (omp_orig);
39319
39320 keep_next_level (true);
39321 block = begin_omp_structured_block ();
39322
39323 bool ctor = false;
39324 if (strcmp (p, "omp_priv") == 0)
39325 {
39326 bool is_direct_init, is_non_constant_init;
39327 ctor = true;
39328 cp_lexer_consume_token (parser->lexer);
39329 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
39330 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
39331 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39332 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
39333 == CPP_CLOSE_PAREN
39334 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
39335 == CPP_CLOSE_PAREN))
39336 {
39337 finish_omp_structured_block (block);
39338 error ("invalid initializer clause");
39339 return false;
39340 }
39341 initializer = cp_parser_initializer (parser, &is_direct_init,
39342 &is_non_constant_init);
39343 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
39344 NULL_TREE, LOOKUP_ONLYCONVERTING);
39345 }
39346 else
39347 {
39348 cp_parser_parse_tentatively (parser);
39349 /* Don't create location wrapper nodes here. */
39350 auto_suppress_location_wrappers sentinel;
39351 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
39352 /*check_dependency_p=*/true,
39353 /*template_p=*/NULL,
39354 /*declarator_p=*/false,
39355 /*optional_p=*/false);
39356 vec<tree, va_gc> *args;
39357 if (fn_name == error_mark_node
39358 || cp_parser_error_occurred (parser)
39359 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39360 || ((args = cp_parser_parenthesized_expression_list
39361 (parser, non_attr, /*cast_p=*/false,
39362 /*allow_expansion_p=*/true,
39363 /*non_constant_p=*/NULL)),
39364 cp_parser_error_occurred (parser)))
39365 {
39366 finish_omp_structured_block (block);
39367 cp_parser_abort_tentative_parse (parser);
39368 cp_parser_error (parser, "expected id-expression (arguments)");
39369 return false;
39370 }
39371 unsigned int i;
39372 tree arg;
39373 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
39374 if (arg == omp_priv
39375 || (TREE_CODE (arg) == ADDR_EXPR
39376 && TREE_OPERAND (arg, 0) == omp_priv))
39377 break;
39378 cp_parser_abort_tentative_parse (parser);
39379 if (arg == NULL_TREE)
39380 error ("one of the initializer call arguments should be %<omp_priv%>"
39381 " or %<&omp_priv%>");
39382 initializer = cp_parser_postfix_expression (parser, false, false, false,
39383 false, NULL);
39384 finish_expr_stmt (initializer);
39385 }
39386
39387 block = finish_omp_structured_block (block);
39388 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
39389 add_stmt (block);
39390
39391 if (ctor)
39392 add_decl_expr (omp_orig);
39393
39394 if (!parens.require_close (parser))
39395 return false;
39396 }
39397
39398 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
39399 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
39400 UNKNOWN_LOCATION);
39401
39402 return true;
39403 }
39404
39405 /* OpenMP 4.0
39406 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39407 initializer-clause[opt] new-line
39408
39409 initializer-clause:
39410 initializer (omp_priv initializer)
39411 initializer (function-name (argument-list)) */
39412
39413 static void
39414 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
39415 enum pragma_context)
39416 {
39417 auto_vec<tree> types;
39418 enum tree_code reduc_code = ERROR_MARK;
39419 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
39420 unsigned int i;
39421 cp_token *first_token;
39422 cp_token_cache *cp;
39423 int errs;
39424 void *p;
39425
39426 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39427 p = obstack_alloc (&declarator_obstack, 0);
39428
39429 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39430 goto fail;
39431
39432 switch (cp_lexer_peek_token (parser->lexer)->type)
39433 {
39434 case CPP_PLUS:
39435 reduc_code = PLUS_EXPR;
39436 break;
39437 case CPP_MULT:
39438 reduc_code = MULT_EXPR;
39439 break;
39440 case CPP_MINUS:
39441 reduc_code = MINUS_EXPR;
39442 break;
39443 case CPP_AND:
39444 reduc_code = BIT_AND_EXPR;
39445 break;
39446 case CPP_XOR:
39447 reduc_code = BIT_XOR_EXPR;
39448 break;
39449 case CPP_OR:
39450 reduc_code = BIT_IOR_EXPR;
39451 break;
39452 case CPP_AND_AND:
39453 reduc_code = TRUTH_ANDIF_EXPR;
39454 break;
39455 case CPP_OR_OR:
39456 reduc_code = TRUTH_ORIF_EXPR;
39457 break;
39458 case CPP_NAME:
39459 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
39460 break;
39461 default:
39462 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39463 "%<|%>, %<&&%>, %<||%> or identifier");
39464 goto fail;
39465 }
39466
39467 if (reduc_code != ERROR_MARK)
39468 cp_lexer_consume_token (parser->lexer);
39469
39470 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
39471 if (reduc_id == error_mark_node)
39472 goto fail;
39473
39474 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39475 goto fail;
39476
39477 /* Types may not be defined in declare reduction type list. */
39478 const char *saved_message;
39479 saved_message = parser->type_definition_forbidden_message;
39480 parser->type_definition_forbidden_message
39481 = G_("types may not be defined in declare reduction type list");
39482 bool saved_colon_corrects_to_scope_p;
39483 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39484 parser->colon_corrects_to_scope_p = false;
39485 bool saved_colon_doesnt_start_class_def_p;
39486 saved_colon_doesnt_start_class_def_p
39487 = parser->colon_doesnt_start_class_def_p;
39488 parser->colon_doesnt_start_class_def_p = true;
39489
39490 while (true)
39491 {
39492 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39493 type = cp_parser_type_id (parser);
39494 if (type == error_mark_node)
39495 ;
39496 else if (ARITHMETIC_TYPE_P (type)
39497 && (orig_reduc_id == NULL_TREE
39498 || (TREE_CODE (type) != COMPLEX_TYPE
39499 && (id_equal (orig_reduc_id, "min")
39500 || id_equal (orig_reduc_id, "max")))))
39501 error_at (loc, "predeclared arithmetic type %qT in "
39502 "%<#pragma omp declare reduction%>", type);
39503 else if (TREE_CODE (type) == FUNCTION_TYPE
39504 || TREE_CODE (type) == METHOD_TYPE
39505 || TREE_CODE (type) == ARRAY_TYPE)
39506 error_at (loc, "function or array type %qT in "
39507 "%<#pragma omp declare reduction%>", type);
39508 else if (TYPE_REF_P (type))
39509 error_at (loc, "reference type %qT in "
39510 "%<#pragma omp declare reduction%>", type);
39511 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
39512 error_at (loc, "const, volatile or __restrict qualified type %qT in "
39513 "%<#pragma omp declare reduction%>", type);
39514 else
39515 types.safe_push (type);
39516
39517 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39518 cp_lexer_consume_token (parser->lexer);
39519 else
39520 break;
39521 }
39522
39523 /* Restore the saved message. */
39524 parser->type_definition_forbidden_message = saved_message;
39525 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39526 parser->colon_doesnt_start_class_def_p
39527 = saved_colon_doesnt_start_class_def_p;
39528
39529 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
39530 || types.is_empty ())
39531 {
39532 fail:
39533 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39534 goto done;
39535 }
39536
39537 first_token = cp_lexer_peek_token (parser->lexer);
39538 cp = NULL;
39539 errs = errorcount;
39540 FOR_EACH_VEC_ELT (types, i, type)
39541 {
39542 tree fntype
39543 = build_function_type_list (void_type_node,
39544 cp_build_reference_type (type, false),
39545 NULL_TREE);
39546 tree this_reduc_id = reduc_id;
39547 if (!dependent_type_p (type))
39548 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
39549 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
39550 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
39551 DECL_ARTIFICIAL (fndecl) = 1;
39552 DECL_EXTERNAL (fndecl) = 1;
39553 DECL_DECLARED_INLINE_P (fndecl) = 1;
39554 DECL_IGNORED_P (fndecl) = 1;
39555 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
39556 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
39557 DECL_ATTRIBUTES (fndecl)
39558 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
39559 DECL_ATTRIBUTES (fndecl));
39560 if (processing_template_decl)
39561 fndecl = push_template_decl (fndecl);
39562 bool block_scope = false;
39563 tree block = NULL_TREE;
39564 if (current_function_decl)
39565 {
39566 block_scope = true;
39567 DECL_CONTEXT (fndecl) = global_namespace;
39568 if (!processing_template_decl)
39569 pushdecl (fndecl);
39570 }
39571 else if (current_class_type)
39572 {
39573 if (cp == NULL)
39574 {
39575 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39576 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39577 cp_lexer_consume_token (parser->lexer);
39578 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39579 goto fail;
39580 cp = cp_token_cache_new (first_token,
39581 cp_lexer_peek_nth_token (parser->lexer,
39582 2));
39583 }
39584 DECL_STATIC_FUNCTION_P (fndecl) = 1;
39585 finish_member_declaration (fndecl);
39586 DECL_PENDING_INLINE_INFO (fndecl) = cp;
39587 DECL_PENDING_INLINE_P (fndecl) = 1;
39588 vec_safe_push (unparsed_funs_with_definitions, fndecl);
39589 continue;
39590 }
39591 else
39592 {
39593 DECL_CONTEXT (fndecl) = current_namespace;
39594 pushdecl (fndecl);
39595 }
39596 if (!block_scope)
39597 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
39598 else
39599 block = begin_omp_structured_block ();
39600 if (cp)
39601 {
39602 cp_parser_push_lexer_for_tokens (parser, cp);
39603 parser->lexer->in_pragma = true;
39604 }
39605 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
39606 {
39607 if (!block_scope)
39608 finish_function (/*inline_p=*/false);
39609 else
39610 DECL_CONTEXT (fndecl) = current_function_decl;
39611 if (cp)
39612 cp_parser_pop_lexer (parser);
39613 goto fail;
39614 }
39615 if (cp)
39616 cp_parser_pop_lexer (parser);
39617 if (!block_scope)
39618 finish_function (/*inline_p=*/false);
39619 else
39620 {
39621 DECL_CONTEXT (fndecl) = current_function_decl;
39622 block = finish_omp_structured_block (block);
39623 if (TREE_CODE (block) == BIND_EXPR)
39624 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
39625 else if (TREE_CODE (block) == STATEMENT_LIST)
39626 DECL_SAVED_TREE (fndecl) = block;
39627 if (processing_template_decl)
39628 add_decl_expr (fndecl);
39629 }
39630 cp_check_omp_declare_reduction (fndecl);
39631 if (cp == NULL && types.length () > 1)
39632 cp = cp_token_cache_new (first_token,
39633 cp_lexer_peek_nth_token (parser->lexer, 2));
39634 if (errs != errorcount)
39635 break;
39636 }
39637
39638 cp_parser_require_pragma_eol (parser, pragma_tok);
39639
39640 done:
39641 /* Free any declarators allocated. */
39642 obstack_free (&declarator_obstack, p);
39643 }
39644
39645 /* OpenMP 4.0
39646 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39647 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39648 initializer-clause[opt] new-line
39649 #pragma omp declare target new-line */
39650
39651 static bool
39652 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
39653 enum pragma_context context)
39654 {
39655 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39656 {
39657 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39658 const char *p = IDENTIFIER_POINTER (id);
39659
39660 if (strcmp (p, "simd") == 0)
39661 {
39662 cp_lexer_consume_token (parser->lexer);
39663 cp_parser_omp_declare_simd (parser, pragma_tok,
39664 context);
39665 return true;
39666 }
39667 cp_ensure_no_omp_declare_simd (parser);
39668 if (strcmp (p, "reduction") == 0)
39669 {
39670 cp_lexer_consume_token (parser->lexer);
39671 cp_parser_omp_declare_reduction (parser, pragma_tok,
39672 context);
39673 return false;
39674 }
39675 if (!flag_openmp) /* flag_openmp_simd */
39676 {
39677 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39678 return false;
39679 }
39680 if (strcmp (p, "target") == 0)
39681 {
39682 cp_lexer_consume_token (parser->lexer);
39683 cp_parser_omp_declare_target (parser, pragma_tok);
39684 return false;
39685 }
39686 }
39687 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
39688 "or %<target%>");
39689 cp_parser_require_pragma_eol (parser, pragma_tok);
39690 return false;
39691 }
39692
39693 /* OpenMP 5.0
39694 #pragma omp requires clauses[optseq] new-line */
39695
39696 static bool
39697 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
39698 {
39699 bool first = true;
39700 enum omp_requires new_req = (enum omp_requires) 0;
39701
39702 location_t loc = pragma_tok->location;
39703 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39704 {
39705 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39706 cp_lexer_consume_token (parser->lexer);
39707
39708 first = false;
39709
39710 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39711 {
39712 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39713 const char *p = IDENTIFIER_POINTER (id);
39714 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
39715 enum omp_requires this_req = (enum omp_requires) 0;
39716
39717 if (!strcmp (p, "unified_address"))
39718 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
39719 else if (!strcmp (p, "unified_shared_memory"))
39720 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
39721 else if (!strcmp (p, "dynamic_allocators"))
39722 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
39723 else if (!strcmp (p, "reverse_offload"))
39724 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
39725 else if (!strcmp (p, "atomic_default_mem_order"))
39726 {
39727 cp_lexer_consume_token (parser->lexer);
39728
39729 matching_parens parens;
39730 if (parens.require_open (parser))
39731 {
39732 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39733 {
39734 id = cp_lexer_peek_token (parser->lexer)->u.value;
39735 p = IDENTIFIER_POINTER (id);
39736
39737 if (!strcmp (p, "seq_cst"))
39738 this_req
39739 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
39740 else if (!strcmp (p, "relaxed"))
39741 this_req
39742 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
39743 else if (!strcmp (p, "acq_rel"))
39744 this_req
39745 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
39746 }
39747 if (this_req == 0)
39748 {
39749 error_at (cp_lexer_peek_token (parser->lexer)->location,
39750 "expected %<seq_cst%>, %<relaxed%> or "
39751 "%<acq_rel%>");
39752 if (cp_lexer_nth_token_is (parser->lexer, 2,
39753 CPP_CLOSE_PAREN))
39754 cp_lexer_consume_token (parser->lexer);
39755 }
39756 else
39757 cp_lexer_consume_token (parser->lexer);
39758
39759 if (!parens.require_close (parser))
39760 cp_parser_skip_to_closing_parenthesis (parser,
39761 /*recovering=*/true,
39762 /*or_comma=*/false,
39763 /*consume_paren=*/
39764 true);
39765
39766 if (this_req == 0)
39767 {
39768 cp_parser_require_pragma_eol (parser, pragma_tok);
39769 return false;
39770 }
39771 }
39772 p = NULL;
39773 }
39774 else
39775 {
39776 error_at (cloc, "expected %<unified_address%>, "
39777 "%<unified_shared_memory%>, "
39778 "%<dynamic_allocators%>, "
39779 "%<reverse_offload%> "
39780 "or %<atomic_default_mem_order%> clause");
39781 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39782 return false;
39783 }
39784 if (p)
39785 sorry_at (cloc, "%qs clause on %<requires%> directive not "
39786 "supported yet", p);
39787 if (p)
39788 cp_lexer_consume_token (parser->lexer);
39789 if (this_req)
39790 {
39791 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39792 {
39793 if ((this_req & new_req) != 0)
39794 error_at (cloc, "too many %qs clauses", p);
39795 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
39796 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
39797 error_at (cloc, "%qs clause used lexically after first "
39798 "target construct or offloading API", p);
39799 }
39800 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39801 {
39802 error_at (cloc, "too many %qs clauses",
39803 "atomic_default_mem_order");
39804 this_req = (enum omp_requires) 0;
39805 }
39806 else if ((omp_requires_mask
39807 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39808 {
39809 error_at (cloc, "more than one %<atomic_default_mem_order%>"
39810 " clause in a single compilation unit");
39811 this_req
39812 = (enum omp_requires)
39813 (omp_requires_mask
39814 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
39815 }
39816 else if ((omp_requires_mask
39817 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
39818 error_at (cloc, "%<atomic_default_mem_order%> clause used "
39819 "lexically after first %<atomic%> construct "
39820 "without memory order clause");
39821 new_req = (enum omp_requires) (new_req | this_req);
39822 omp_requires_mask
39823 = (enum omp_requires) (omp_requires_mask | this_req);
39824 continue;
39825 }
39826 }
39827 break;
39828 }
39829 cp_parser_require_pragma_eol (parser, pragma_tok);
39830
39831 if (new_req == 0)
39832 error_at (loc, "%<pragma omp requires%> requires at least one clause");
39833 return false;
39834 }
39835
39836
39837 /* OpenMP 4.5:
39838 #pragma omp taskloop taskloop-clause[optseq] new-line
39839 for-loop
39840
39841 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39842 for-loop */
39843
39844 #define OMP_TASKLOOP_CLAUSE_MASK \
39845 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39861
39862 static tree
39863 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
39864 char *p_name, omp_clause_mask mask, tree *cclauses,
39865 bool *if_p)
39866 {
39867 tree clauses, sb, ret;
39868 unsigned int save;
39869 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39870
39871 strcat (p_name, " taskloop");
39872 mask |= OMP_TASKLOOP_CLAUSE_MASK;
39873 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39874 clause. */
39875 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
39876 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
39877
39878 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39879 {
39880 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39881 const char *p = IDENTIFIER_POINTER (id);
39882
39883 if (strcmp (p, "simd") == 0)
39884 {
39885 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39886 if (cclauses == NULL)
39887 cclauses = cclauses_buf;
39888
39889 cp_lexer_consume_token (parser->lexer);
39890 if (!flag_openmp) /* flag_openmp_simd */
39891 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39892 cclauses, if_p);
39893 sb = begin_omp_structured_block ();
39894 save = cp_parser_begin_omp_structured_block (parser);
39895 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39896 cclauses, if_p);
39897 cp_parser_end_omp_structured_block (parser, save);
39898 tree body = finish_omp_structured_block (sb);
39899 if (ret == NULL)
39900 return ret;
39901 ret = make_node (OMP_TASKLOOP);
39902 TREE_TYPE (ret) = void_type_node;
39903 OMP_FOR_BODY (ret) = body;
39904 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39905 SET_EXPR_LOCATION (ret, loc);
39906 add_stmt (ret);
39907 return ret;
39908 }
39909 }
39910 if (!flag_openmp) /* flag_openmp_simd */
39911 {
39912 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39913 return NULL_TREE;
39914 }
39915
39916 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39917 cclauses == NULL);
39918 if (cclauses)
39919 {
39920 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
39921 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39922 }
39923
39924 keep_next_level (true);
39925 sb = begin_omp_structured_block ();
39926 save = cp_parser_begin_omp_structured_block (parser);
39927
39928 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
39929 if_p);
39930
39931 cp_parser_end_omp_structured_block (parser, save);
39932 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39933
39934 return ret;
39935 }
39936
39937
39938 /* OpenACC 2.0:
39939 # pragma acc routine oacc-routine-clause[optseq] new-line
39940 function-definition
39941
39942 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39943 */
39944
39945 #define OACC_ROUTINE_CLAUSE_MASK \
39946 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39950
39951
39952 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39953 component, which must resolve to a declared namespace-scope
39954 function. The clauses are either processed directly (for a named
39955 function), or defered until the immediatley following declaration
39956 is parsed. */
39957
39958 static void
39959 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
39960 enum pragma_context context)
39961 {
39962 gcc_checking_assert (context == pragma_external);
39963 /* The checking for "another pragma following this one" in the "no optional
39964 '( name )'" case makes sure that we dont re-enter. */
39965 gcc_checking_assert (parser->oacc_routine == NULL);
39966
39967 cp_oacc_routine_data data;
39968 data.error_seen = false;
39969 data.fndecl_seen = false;
39970 data.tokens = vNULL;
39971 data.clauses = NULL_TREE;
39972 data.loc = pragma_tok->location;
39973 /* It is safe to take the address of a local variable; it will only be
39974 used while this scope is live. */
39975 parser->oacc_routine = &data;
39976
39977 /* Look for optional '( name )'. */
39978 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39979 {
39980 matching_parens parens;
39981 parens.consume_open (parser); /* '(' */
39982
39983 /* We parse the name as an id-expression. If it resolves to
39984 anything other than a non-overloaded function at namespace
39985 scope, it's an error. */
39986 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
39987 tree name = cp_parser_id_expression (parser,
39988 /*template_keyword_p=*/false,
39989 /*check_dependency_p=*/false,
39990 /*template_p=*/NULL,
39991 /*declarator_p=*/false,
39992 /*optional_p=*/false);
39993 tree decl = (identifier_p (name)
39994 ? cp_parser_lookup_name_simple (parser, name, name_loc)
39995 : name);
39996 if (name != error_mark_node && decl == error_mark_node)
39997 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
39998
39999 if (decl == error_mark_node
40000 || !parens.require_close (parser))
40001 {
40002 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40003 parser->oacc_routine = NULL;
40004 return;
40005 }
40006
40007 data.clauses
40008 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40009 "#pragma acc routine",
40010 cp_lexer_peek_token (parser->lexer));
40011
40012 if (decl && is_overloaded_fn (decl)
40013 && (TREE_CODE (decl) != FUNCTION_DECL
40014 || DECL_FUNCTION_TEMPLATE_P (decl)))
40015 {
40016 error_at (name_loc,
40017 "%<#pragma acc routine%> names a set of overloads");
40018 parser->oacc_routine = NULL;
40019 return;
40020 }
40021
40022 /* Perhaps we should use the same rule as declarations in different
40023 namespaces? */
40024 if (!DECL_NAMESPACE_SCOPE_P (decl))
40025 {
40026 error_at (name_loc,
40027 "%qD does not refer to a namespace scope function", decl);
40028 parser->oacc_routine = NULL;
40029 return;
40030 }
40031
40032 if (TREE_CODE (decl) != FUNCTION_DECL)
40033 {
40034 error_at (name_loc, "%qD does not refer to a function", decl);
40035 parser->oacc_routine = NULL;
40036 return;
40037 }
40038
40039 cp_finalize_oacc_routine (parser, decl, false);
40040 parser->oacc_routine = NULL;
40041 }
40042 else /* No optional '( name )'. */
40043 {
40044 /* Store away all pragma tokens. */
40045 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
40046 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
40047 cp_lexer_consume_token (parser->lexer);
40048 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40049 parser->oacc_routine->error_seen = true;
40050 cp_parser_require_pragma_eol (parser, pragma_tok);
40051 struct cp_token_cache *cp
40052 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
40053 parser->oacc_routine->tokens.safe_push (cp);
40054
40055 /* Emit a helpful diagnostic if there's another pragma following this
40056 one. */
40057 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
40058 {
40059 cp_ensure_no_oacc_routine (parser);
40060 data.tokens.release ();
40061 /* ..., and then just keep going. */
40062 return;
40063 }
40064
40065 /* We only have to consider the pragma_external case here. */
40066 cp_parser_declaration (parser);
40067 if (parser->oacc_routine
40068 && !parser->oacc_routine->fndecl_seen)
40069 cp_ensure_no_oacc_routine (parser);
40070 else
40071 parser->oacc_routine = NULL;
40072 data.tokens.release ();
40073 }
40074 }
40075
40076 /* Finalize #pragma acc routine clauses after direct declarator has
40077 been parsed. */
40078
40079 static tree
40080 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
40081 {
40082 struct cp_token_cache *ce;
40083 cp_oacc_routine_data *data = parser->oacc_routine;
40084
40085 if (!data->error_seen && data->fndecl_seen)
40086 {
40087 error_at (data->loc,
40088 "%<#pragma acc routine%> not immediately followed by "
40089 "a single function declaration or definition");
40090 data->error_seen = true;
40091 }
40092 if (data->error_seen)
40093 return attrs;
40094
40095 gcc_checking_assert (data->tokens.length () == 1);
40096 ce = data->tokens[0];
40097
40098 cp_parser_push_lexer_for_tokens (parser, ce);
40099 parser->lexer->in_pragma = true;
40100 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
40101
40102 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
40103 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
40104 parser->oacc_routine->clauses
40105 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40106 "#pragma acc routine", pragma_tok);
40107 cp_parser_pop_lexer (parser);
40108 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
40109 fndecl_seen. */
40110
40111 return attrs;
40112 }
40113
40114 /* Apply any saved OpenACC routine clauses to a just-parsed
40115 declaration. */
40116
40117 static void
40118 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
40119 {
40120 if (__builtin_expect (parser->oacc_routine != NULL, 0))
40121 {
40122 /* Keep going if we're in error reporting mode. */
40123 if (parser->oacc_routine->error_seen
40124 || fndecl == error_mark_node)
40125 return;
40126
40127 if (parser->oacc_routine->fndecl_seen)
40128 {
40129 error_at (parser->oacc_routine->loc,
40130 "%<#pragma acc routine%> not immediately followed by"
40131 " a single function declaration or definition");
40132 parser->oacc_routine = NULL;
40133 return;
40134 }
40135 if (TREE_CODE (fndecl) != FUNCTION_DECL)
40136 {
40137 cp_ensure_no_oacc_routine (parser);
40138 return;
40139 }
40140
40141 if (oacc_get_fn_attrib (fndecl))
40142 {
40143 error_at (parser->oacc_routine->loc,
40144 "%<#pragma acc routine%> already applied to %qD", fndecl);
40145 parser->oacc_routine = NULL;
40146 return;
40147 }
40148
40149 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
40150 {
40151 error_at (parser->oacc_routine->loc,
40152 TREE_USED (fndecl)
40153 ? G_("%<#pragma acc routine%> must be applied before use")
40154 : G_("%<#pragma acc routine%> must be applied before "
40155 "definition"));
40156 parser->oacc_routine = NULL;
40157 return;
40158 }
40159
40160 /* Process the routine's dimension clauses. */
40161 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
40162 oacc_replace_fn_attrib (fndecl, dims);
40163
40164 /* Add an "omp declare target" attribute. */
40165 DECL_ATTRIBUTES (fndecl)
40166 = tree_cons (get_identifier ("omp declare target"),
40167 NULL_TREE, DECL_ATTRIBUTES (fndecl));
40168
40169 /* Don't unset parser->oacc_routine here: we may still need it to
40170 diagnose wrong usage. But, remember that we've used this "#pragma acc
40171 routine". */
40172 parser->oacc_routine->fndecl_seen = true;
40173 }
40174 }
40175
40176 /* Main entry point to OpenMP statement pragmas. */
40177
40178 static void
40179 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40180 {
40181 tree stmt;
40182 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
40183 omp_clause_mask mask (0);
40184
40185 switch (cp_parser_pragma_kind (pragma_tok))
40186 {
40187 case PRAGMA_OACC_ATOMIC:
40188 cp_parser_omp_atomic (parser, pragma_tok);
40189 return;
40190 case PRAGMA_OACC_CACHE:
40191 stmt = cp_parser_oacc_cache (parser, pragma_tok);
40192 break;
40193 case PRAGMA_OACC_DATA:
40194 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
40195 break;
40196 case PRAGMA_OACC_ENTER_DATA:
40197 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
40198 break;
40199 case PRAGMA_OACC_EXIT_DATA:
40200 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
40201 break;
40202 case PRAGMA_OACC_HOST_DATA:
40203 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
40204 break;
40205 case PRAGMA_OACC_KERNELS:
40206 case PRAGMA_OACC_PARALLEL:
40207 strcpy (p_name, "#pragma acc");
40208 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
40209 if_p);
40210 break;
40211 case PRAGMA_OACC_LOOP:
40212 strcpy (p_name, "#pragma acc");
40213 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
40214 if_p);
40215 break;
40216 case PRAGMA_OACC_UPDATE:
40217 stmt = cp_parser_oacc_update (parser, pragma_tok);
40218 break;
40219 case PRAGMA_OACC_WAIT:
40220 stmt = cp_parser_oacc_wait (parser, pragma_tok);
40221 break;
40222 case PRAGMA_OMP_ATOMIC:
40223 cp_parser_omp_atomic (parser, pragma_tok);
40224 return;
40225 case PRAGMA_OMP_CRITICAL:
40226 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
40227 break;
40228 case PRAGMA_OMP_DISTRIBUTE:
40229 strcpy (p_name, "#pragma omp");
40230 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
40231 if_p);
40232 break;
40233 case PRAGMA_OMP_FOR:
40234 strcpy (p_name, "#pragma omp");
40235 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
40236 if_p);
40237 break;
40238 case PRAGMA_OMP_MASTER:
40239 strcpy (p_name, "#pragma omp");
40240 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
40241 if_p);
40242 break;
40243 case PRAGMA_OMP_PARALLEL:
40244 strcpy (p_name, "#pragma omp");
40245 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
40246 if_p);
40247 break;
40248 case PRAGMA_OMP_SECTIONS:
40249 strcpy (p_name, "#pragma omp");
40250 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
40251 break;
40252 case PRAGMA_OMP_SIMD:
40253 strcpy (p_name, "#pragma omp");
40254 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
40255 if_p);
40256 break;
40257 case PRAGMA_OMP_SINGLE:
40258 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
40259 break;
40260 case PRAGMA_OMP_TASK:
40261 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
40262 break;
40263 case PRAGMA_OMP_TASKGROUP:
40264 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
40265 break;
40266 case PRAGMA_OMP_TASKLOOP:
40267 strcpy (p_name, "#pragma omp");
40268 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
40269 if_p);
40270 break;
40271 case PRAGMA_OMP_TEAMS:
40272 strcpy (p_name, "#pragma omp");
40273 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
40274 if_p);
40275 break;
40276 default:
40277 gcc_unreachable ();
40278 }
40279
40280 protected_set_expr_location (stmt, pragma_tok->location);
40281 }
40282 \f
40283 /* Transactional Memory parsing routines. */
40284
40285 /* Parse a transaction attribute.
40286
40287 txn-attribute:
40288 attribute
40289 [ [ identifier ] ]
40290
40291 We use this instead of cp_parser_attributes_opt for transactions to avoid
40292 the pedwarn in C++98 mode. */
40293
40294 static tree
40295 cp_parser_txn_attribute_opt (cp_parser *parser)
40296 {
40297 cp_token *token;
40298 tree attr_name, attr = NULL;
40299
40300 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
40301 return cp_parser_attributes_opt (parser);
40302
40303 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
40304 return NULL_TREE;
40305 cp_lexer_consume_token (parser->lexer);
40306 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
40307 goto error1;
40308
40309 token = cp_lexer_peek_token (parser->lexer);
40310 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
40311 {
40312 token = cp_lexer_consume_token (parser->lexer);
40313
40314 attr_name = (token->type == CPP_KEYWORD
40315 /* For keywords, use the canonical spelling,
40316 not the parsed identifier. */
40317 ? ridpointers[(int) token->keyword]
40318 : token->u.value);
40319 attr = build_tree_list (attr_name, NULL_TREE);
40320 }
40321 else
40322 cp_parser_error (parser, "expected identifier");
40323
40324 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40325 error1:
40326 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40327 return attr;
40328 }
40329
40330 /* Parse a __transaction_atomic or __transaction_relaxed statement.
40331
40332 transaction-statement:
40333 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40334 compound-statement
40335 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40336 */
40337
40338 static tree
40339 cp_parser_transaction (cp_parser *parser, cp_token *token)
40340 {
40341 unsigned char old_in = parser->in_transaction;
40342 unsigned char this_in = 1, new_in;
40343 enum rid keyword = token->keyword;
40344 tree stmt, attrs, noex;
40345
40346 cp_lexer_consume_token (parser->lexer);
40347
40348 if (keyword == RID_TRANSACTION_RELAXED
40349 || keyword == RID_SYNCHRONIZED)
40350 this_in |= TM_STMT_ATTR_RELAXED;
40351 else
40352 {
40353 attrs = cp_parser_txn_attribute_opt (parser);
40354 if (attrs)
40355 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40356 }
40357
40358 /* Parse a noexcept specification. */
40359 if (keyword == RID_ATOMIC_NOEXCEPT)
40360 noex = boolean_true_node;
40361 else if (keyword == RID_ATOMIC_CANCEL)
40362 {
40363 /* cancel-and-throw is unimplemented. */
40364 sorry ("atomic_cancel");
40365 noex = NULL_TREE;
40366 }
40367 else
40368 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
40369
40370 /* Keep track if we're in the lexical scope of an outer transaction. */
40371 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
40372
40373 stmt = begin_transaction_stmt (token->location, NULL, this_in);
40374
40375 parser->in_transaction = new_in;
40376 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
40377 parser->in_transaction = old_in;
40378
40379 finish_transaction_stmt (stmt, NULL, this_in, noex);
40380
40381 return stmt;
40382 }
40383
40384 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40385
40386 transaction-expression:
40387 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40388 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40389 */
40390
40391 static tree
40392 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
40393 {
40394 unsigned char old_in = parser->in_transaction;
40395 unsigned char this_in = 1;
40396 cp_token *token;
40397 tree expr, noex;
40398 bool noex_expr;
40399 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40400
40401 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40402 || keyword == RID_TRANSACTION_RELAXED);
40403
40404 if (!flag_tm)
40405 error_at (loc,
40406 keyword == RID_TRANSACTION_RELAXED
40407 ? G_("%<__transaction_relaxed%> without transactional memory "
40408 "support enabled")
40409 : G_("%<__transaction_atomic%> without transactional memory "
40410 "support enabled"));
40411
40412 token = cp_parser_require_keyword (parser, keyword,
40413 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40414 : RT_TRANSACTION_RELAXED));
40415 gcc_assert (token != NULL);
40416
40417 if (keyword == RID_TRANSACTION_RELAXED)
40418 this_in |= TM_STMT_ATTR_RELAXED;
40419
40420 /* Set this early. This might mean that we allow transaction_cancel in
40421 an expression that we find out later actually has to be a constexpr.
40422 However, we expect that cxx_constant_value will be able to deal with
40423 this; also, if the noexcept has no constexpr, then what we parse next
40424 really is a transaction's body. */
40425 parser->in_transaction = this_in;
40426
40427 /* Parse a noexcept specification. */
40428 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
40429 true);
40430
40431 if (!noex || !noex_expr
40432 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
40433 {
40434 matching_parens parens;
40435 parens.require_open (parser);
40436
40437 expr = cp_parser_expression (parser);
40438 expr = finish_parenthesized_expr (expr);
40439
40440 parens.require_close (parser);
40441 }
40442 else
40443 {
40444 /* The only expression that is available got parsed for the noexcept
40445 already. noexcept is true then. */
40446 expr = noex;
40447 noex = boolean_true_node;
40448 }
40449
40450 expr = build_transaction_expr (token->location, expr, this_in, noex);
40451 parser->in_transaction = old_in;
40452
40453 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
40454 return error_mark_node;
40455
40456 return (flag_tm ? expr : error_mark_node);
40457 }
40458
40459 /* Parse a function-transaction-block.
40460
40461 function-transaction-block:
40462 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40463 function-body
40464 __transaction_atomic txn-attribute[opt] function-try-block
40465 __transaction_relaxed ctor-initializer[opt] function-body
40466 __transaction_relaxed function-try-block
40467 */
40468
40469 static void
40470 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
40471 {
40472 unsigned char old_in = parser->in_transaction;
40473 unsigned char new_in = 1;
40474 tree compound_stmt, stmt, attrs;
40475 cp_token *token;
40476
40477 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40478 || keyword == RID_TRANSACTION_RELAXED);
40479 token = cp_parser_require_keyword (parser, keyword,
40480 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40481 : RT_TRANSACTION_RELAXED));
40482 gcc_assert (token != NULL);
40483
40484 if (keyword == RID_TRANSACTION_RELAXED)
40485 new_in |= TM_STMT_ATTR_RELAXED;
40486 else
40487 {
40488 attrs = cp_parser_txn_attribute_opt (parser);
40489 if (attrs)
40490 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40491 }
40492
40493 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
40494
40495 parser->in_transaction = new_in;
40496
40497 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
40498 cp_parser_function_try_block (parser);
40499 else
40500 cp_parser_ctor_initializer_opt_and_function_body
40501 (parser, /*in_function_try_block=*/false);
40502
40503 parser->in_transaction = old_in;
40504
40505 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
40506 }
40507
40508 /* Parse a __transaction_cancel statement.
40509
40510 cancel-statement:
40511 __transaction_cancel txn-attribute[opt] ;
40512 __transaction_cancel txn-attribute[opt] throw-expression ;
40513
40514 ??? Cancel and throw is not yet implemented. */
40515
40516 static tree
40517 cp_parser_transaction_cancel (cp_parser *parser)
40518 {
40519 cp_token *token;
40520 bool is_outer = false;
40521 tree stmt, attrs;
40522
40523 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
40524 RT_TRANSACTION_CANCEL);
40525 gcc_assert (token != NULL);
40526
40527 attrs = cp_parser_txn_attribute_opt (parser);
40528 if (attrs)
40529 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
40530
40531 /* ??? Parse cancel-and-throw here. */
40532
40533 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40534
40535 if (!flag_tm)
40536 {
40537 error_at (token->location, "%<__transaction_cancel%> without "
40538 "transactional memory support enabled");
40539 return error_mark_node;
40540 }
40541 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
40542 {
40543 error_at (token->location, "%<__transaction_cancel%> within a "
40544 "%<__transaction_relaxed%>");
40545 return error_mark_node;
40546 }
40547 else if (is_outer)
40548 {
40549 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
40550 && !is_tm_may_cancel_outer (current_function_decl))
40551 {
40552 error_at (token->location, "outer %<__transaction_cancel%> not "
40553 "within outer %<__transaction_atomic%>");
40554 error_at (token->location,
40555 " or a %<transaction_may_cancel_outer%> function");
40556 return error_mark_node;
40557 }
40558 }
40559 else if (parser->in_transaction == 0)
40560 {
40561 error_at (token->location, "%<__transaction_cancel%> not within "
40562 "%<__transaction_atomic%>");
40563 return error_mark_node;
40564 }
40565
40566 stmt = build_tm_abort_call (token->location, is_outer);
40567 add_stmt (stmt);
40568
40569 return stmt;
40570 }
40571 \f
40572 /* The parser. */
40573
40574 static GTY (()) cp_parser *the_parser;
40575
40576 \f
40577 /* Special handling for the first token or line in the file. The first
40578 thing in the file might be #pragma GCC pch_preprocess, which loads a
40579 PCH file, which is a GC collection point. So we need to handle this
40580 first pragma without benefit of an existing lexer structure.
40581
40582 Always returns one token to the caller in *FIRST_TOKEN. This is
40583 either the true first token of the file, or the first token after
40584 the initial pragma. */
40585
40586 static void
40587 cp_parser_initial_pragma (cp_token *first_token)
40588 {
40589 tree name = NULL;
40590
40591 cp_lexer_get_preprocessor_token (NULL, first_token);
40592 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
40593 return;
40594
40595 cp_lexer_get_preprocessor_token (NULL, first_token);
40596 if (first_token->type == CPP_STRING)
40597 {
40598 name = first_token->u.value;
40599
40600 cp_lexer_get_preprocessor_token (NULL, first_token);
40601 if (first_token->type != CPP_PRAGMA_EOL)
40602 error_at (first_token->location,
40603 "junk at end of %<#pragma GCC pch_preprocess%>");
40604 }
40605 else
40606 error_at (first_token->location, "expected string literal");
40607
40608 /* Skip to the end of the pragma. */
40609 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
40610 cp_lexer_get_preprocessor_token (NULL, first_token);
40611
40612 /* Now actually load the PCH file. */
40613 if (name)
40614 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
40615
40616 /* Read one more token to return to our caller. We have to do this
40617 after reading the PCH file in, since its pointers have to be
40618 live. */
40619 cp_lexer_get_preprocessor_token (NULL, first_token);
40620 }
40621
40622 /* Parse a pragma GCC ivdep. */
40623
40624 static bool
40625 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
40626 {
40627 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40628 return true;
40629 }
40630
40631 /* Parse a pragma GCC unroll. */
40632
40633 static unsigned short
40634 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
40635 {
40636 location_t location = cp_lexer_peek_token (parser->lexer)->location;
40637 tree expr = cp_parser_constant_expression (parser);
40638 unsigned short unroll;
40639 expr = maybe_constant_value (expr);
40640 HOST_WIDE_INT lunroll = 0;
40641 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
40642 || TREE_CODE (expr) != INTEGER_CST
40643 || (lunroll = tree_to_shwi (expr)) < 0
40644 || lunroll >= USHRT_MAX)
40645 {
40646 error_at (location, "%<#pragma GCC unroll%> requires an"
40647 " assignment-expression that evaluates to a non-negative"
40648 " integral constant less than %u", USHRT_MAX);
40649 unroll = 0;
40650 }
40651 else
40652 {
40653 unroll = (unsigned short)lunroll;
40654 if (unroll == 0)
40655 unroll = 1;
40656 }
40657 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40658 return unroll;
40659 }
40660
40661 /* Normal parsing of a pragma token. Here we can (and must) use the
40662 regular lexer. */
40663
40664 static bool
40665 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
40666 {
40667 cp_token *pragma_tok;
40668 unsigned int id;
40669 tree stmt;
40670 bool ret;
40671
40672 pragma_tok = cp_lexer_consume_token (parser->lexer);
40673 gcc_assert (pragma_tok->type == CPP_PRAGMA);
40674 parser->lexer->in_pragma = true;
40675
40676 id = cp_parser_pragma_kind (pragma_tok);
40677 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
40678 cp_ensure_no_omp_declare_simd (parser);
40679 switch (id)
40680 {
40681 case PRAGMA_GCC_PCH_PREPROCESS:
40682 error_at (pragma_tok->location,
40683 "%<#pragma GCC pch_preprocess%> must be first");
40684 break;
40685
40686 case PRAGMA_OMP_BARRIER:
40687 switch (context)
40688 {
40689 case pragma_compound:
40690 cp_parser_omp_barrier (parser, pragma_tok);
40691 return false;
40692 case pragma_stmt:
40693 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40694 "used in compound statements", "omp barrier");
40695 break;
40696 default:
40697 goto bad_stmt;
40698 }
40699 break;
40700
40701 case PRAGMA_OMP_DEPOBJ:
40702 switch (context)
40703 {
40704 case pragma_compound:
40705 cp_parser_omp_depobj (parser, pragma_tok);
40706 return false;
40707 case pragma_stmt:
40708 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40709 "used in compound statements", "omp depobj");
40710 break;
40711 default:
40712 goto bad_stmt;
40713 }
40714 break;
40715
40716 case PRAGMA_OMP_FLUSH:
40717 switch (context)
40718 {
40719 case pragma_compound:
40720 cp_parser_omp_flush (parser, pragma_tok);
40721 return false;
40722 case pragma_stmt:
40723 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40724 "used in compound statements", "omp flush");
40725 break;
40726 default:
40727 goto bad_stmt;
40728 }
40729 break;
40730
40731 case PRAGMA_OMP_TASKWAIT:
40732 switch (context)
40733 {
40734 case pragma_compound:
40735 cp_parser_omp_taskwait (parser, pragma_tok);
40736 return false;
40737 case pragma_stmt:
40738 error_at (pragma_tok->location,
40739 "%<#pragma %s%> may only be used in compound statements",
40740 "omp taskwait");
40741 break;
40742 default:
40743 goto bad_stmt;
40744 }
40745 break;
40746
40747 case PRAGMA_OMP_TASKYIELD:
40748 switch (context)
40749 {
40750 case pragma_compound:
40751 cp_parser_omp_taskyield (parser, pragma_tok);
40752 return false;
40753 case pragma_stmt:
40754 error_at (pragma_tok->location,
40755 "%<#pragma %s%> may only be used in compound statements",
40756 "omp taskyield");
40757 break;
40758 default:
40759 goto bad_stmt;
40760 }
40761 break;
40762
40763 case PRAGMA_OMP_CANCEL:
40764 switch (context)
40765 {
40766 case pragma_compound:
40767 cp_parser_omp_cancel (parser, pragma_tok);
40768 return false;
40769 case pragma_stmt:
40770 error_at (pragma_tok->location,
40771 "%<#pragma %s%> may only be used in compound statements",
40772 "omp cancel");
40773 break;
40774 default:
40775 goto bad_stmt;
40776 }
40777 break;
40778
40779 case PRAGMA_OMP_CANCELLATION_POINT:
40780 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
40781 return false;
40782
40783 case PRAGMA_OMP_THREADPRIVATE:
40784 cp_parser_omp_threadprivate (parser, pragma_tok);
40785 return false;
40786
40787 case PRAGMA_OMP_DECLARE:
40788 return cp_parser_omp_declare (parser, pragma_tok, context);
40789
40790 case PRAGMA_OACC_DECLARE:
40791 cp_parser_oacc_declare (parser, pragma_tok);
40792 return false;
40793
40794 case PRAGMA_OACC_ENTER_DATA:
40795 if (context == pragma_stmt)
40796 {
40797 error_at (pragma_tok->location,
40798 "%<#pragma %s%> may only be used in compound statements",
40799 "acc enter data");
40800 break;
40801 }
40802 else if (context != pragma_compound)
40803 goto bad_stmt;
40804 cp_parser_omp_construct (parser, pragma_tok, if_p);
40805 return true;
40806
40807 case PRAGMA_OACC_EXIT_DATA:
40808 if (context == pragma_stmt)
40809 {
40810 error_at (pragma_tok->location,
40811 "%<#pragma %s%> may only be used in compound statements",
40812 "acc exit data");
40813 break;
40814 }
40815 else if (context != pragma_compound)
40816 goto bad_stmt;
40817 cp_parser_omp_construct (parser, pragma_tok, if_p);
40818 return true;
40819
40820 case PRAGMA_OACC_ROUTINE:
40821 if (context != pragma_external)
40822 {
40823 error_at (pragma_tok->location,
40824 "%<#pragma acc routine%> must be at file scope");
40825 break;
40826 }
40827 cp_parser_oacc_routine (parser, pragma_tok, context);
40828 return false;
40829
40830 case PRAGMA_OACC_UPDATE:
40831 if (context == pragma_stmt)
40832 {
40833 error_at (pragma_tok->location,
40834 "%<#pragma %s%> may only be used in compound statements",
40835 "acc update");
40836 break;
40837 }
40838 else if (context != pragma_compound)
40839 goto bad_stmt;
40840 cp_parser_omp_construct (parser, pragma_tok, if_p);
40841 return true;
40842
40843 case PRAGMA_OACC_WAIT:
40844 if (context == pragma_stmt)
40845 {
40846 error_at (pragma_tok->location,
40847 "%<#pragma %s%> may only be used in compound statements",
40848 "acc wait");
40849 break;
40850 }
40851 else if (context != pragma_compound)
40852 goto bad_stmt;
40853 cp_parser_omp_construct (parser, pragma_tok, if_p);
40854 return true;
40855
40856 case PRAGMA_OACC_ATOMIC:
40857 case PRAGMA_OACC_CACHE:
40858 case PRAGMA_OACC_DATA:
40859 case PRAGMA_OACC_HOST_DATA:
40860 case PRAGMA_OACC_KERNELS:
40861 case PRAGMA_OACC_PARALLEL:
40862 case PRAGMA_OACC_LOOP:
40863 case PRAGMA_OMP_ATOMIC:
40864 case PRAGMA_OMP_CRITICAL:
40865 case PRAGMA_OMP_DISTRIBUTE:
40866 case PRAGMA_OMP_FOR:
40867 case PRAGMA_OMP_MASTER:
40868 case PRAGMA_OMP_PARALLEL:
40869 case PRAGMA_OMP_SECTIONS:
40870 case PRAGMA_OMP_SIMD:
40871 case PRAGMA_OMP_SINGLE:
40872 case PRAGMA_OMP_TASK:
40873 case PRAGMA_OMP_TASKGROUP:
40874 case PRAGMA_OMP_TASKLOOP:
40875 case PRAGMA_OMP_TEAMS:
40876 if (context != pragma_stmt && context != pragma_compound)
40877 goto bad_stmt;
40878 stmt = push_omp_privatization_clauses (false);
40879 cp_parser_omp_construct (parser, pragma_tok, if_p);
40880 pop_omp_privatization_clauses (stmt);
40881 return true;
40882
40883 case PRAGMA_OMP_REQUIRES:
40884 return cp_parser_omp_requires (parser, pragma_tok);
40885
40886 case PRAGMA_OMP_ORDERED:
40887 if (context != pragma_stmt && context != pragma_compound)
40888 goto bad_stmt;
40889 stmt = push_omp_privatization_clauses (false);
40890 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
40891 pop_omp_privatization_clauses (stmt);
40892 return ret;
40893
40894 case PRAGMA_OMP_TARGET:
40895 if (context != pragma_stmt && context != pragma_compound)
40896 goto bad_stmt;
40897 stmt = push_omp_privatization_clauses (false);
40898 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
40899 pop_omp_privatization_clauses (stmt);
40900 return ret;
40901
40902 case PRAGMA_OMP_END_DECLARE_TARGET:
40903 cp_parser_omp_end_declare_target (parser, pragma_tok);
40904 return false;
40905
40906 case PRAGMA_OMP_SECTION:
40907 error_at (pragma_tok->location,
40908 "%<#pragma omp section%> may only be used in "
40909 "%<#pragma omp sections%> construct");
40910 break;
40911
40912 case PRAGMA_IVDEP:
40913 {
40914 if (context == pragma_external)
40915 {
40916 error_at (pragma_tok->location,
40917 "%<#pragma GCC ivdep%> must be inside a function");
40918 break;
40919 }
40920 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
40921 unsigned short unroll;
40922 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40923 if (tok->type == CPP_PRAGMA
40924 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
40925 {
40926 tok = cp_lexer_consume_token (parser->lexer);
40927 unroll = cp_parser_pragma_unroll (parser, tok);
40928 tok = cp_lexer_peek_token (the_parser->lexer);
40929 }
40930 else
40931 unroll = 0;
40932 if (tok->type != CPP_KEYWORD
40933 || (tok->keyword != RID_FOR
40934 && tok->keyword != RID_WHILE
40935 && tok->keyword != RID_DO))
40936 {
40937 cp_parser_error (parser, "for, while or do statement expected");
40938 return false;
40939 }
40940 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40941 return true;
40942 }
40943
40944 case PRAGMA_UNROLL:
40945 {
40946 if (context == pragma_external)
40947 {
40948 error_at (pragma_tok->location,
40949 "%<#pragma GCC unroll%> must be inside a function");
40950 break;
40951 }
40952 const unsigned short unroll
40953 = cp_parser_pragma_unroll (parser, pragma_tok);
40954 bool ivdep;
40955 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40956 if (tok->type == CPP_PRAGMA
40957 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
40958 {
40959 tok = cp_lexer_consume_token (parser->lexer);
40960 ivdep = cp_parser_pragma_ivdep (parser, tok);
40961 tok = cp_lexer_peek_token (the_parser->lexer);
40962 }
40963 else
40964 ivdep = false;
40965 if (tok->type != CPP_KEYWORD
40966 || (tok->keyword != RID_FOR
40967 && tok->keyword != RID_WHILE
40968 && tok->keyword != RID_DO))
40969 {
40970 cp_parser_error (parser, "for, while or do statement expected");
40971 return false;
40972 }
40973 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40974 return true;
40975 }
40976
40977 default:
40978 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
40979 c_invoke_pragma_handler (id);
40980 break;
40981
40982 bad_stmt:
40983 cp_parser_error (parser, "expected declaration specifiers");
40984 break;
40985 }
40986
40987 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40988 return false;
40989 }
40990
40991 /* The interface the pragma parsers have to the lexer. */
40992
40993 enum cpp_ttype
40994 pragma_lex (tree *value, location_t *loc)
40995 {
40996 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40997 enum cpp_ttype ret = tok->type;
40998
40999 *value = tok->u.value;
41000 if (loc)
41001 *loc = tok->location;
41002
41003 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
41004 ret = CPP_EOF;
41005 else if (ret == CPP_STRING)
41006 *value = cp_parser_string_literal (the_parser, false, false);
41007 else
41008 {
41009 if (ret == CPP_KEYWORD)
41010 ret = CPP_NAME;
41011 cp_lexer_consume_token (the_parser->lexer);
41012 }
41013
41014 return ret;
41015 }
41016
41017 \f
41018 /* External interface. */
41019
41020 /* Parse one entire translation unit. */
41021
41022 void
41023 c_parse_file (void)
41024 {
41025 static bool already_called = false;
41026
41027 if (already_called)
41028 fatal_error (input_location,
41029 "inter-module optimizations not implemented for C++");
41030 already_called = true;
41031
41032 the_parser = cp_parser_new ();
41033 push_deferring_access_checks (flag_access_control
41034 ? dk_no_deferred : dk_no_check);
41035 cp_parser_translation_unit (the_parser);
41036 the_parser = NULL;
41037
41038 finish_translation_unit ();
41039 }
41040
41041 /* Create an identifier for a generic parameter type (a synthesized
41042 template parameter implied by `auto' or a concept identifier). */
41043
41044 static GTY(()) int generic_parm_count;
41045 static tree
41046 make_generic_type_name ()
41047 {
41048 char buf[32];
41049 sprintf (buf, "auto:%d", ++generic_parm_count);
41050 return get_identifier (buf);
41051 }
41052
41053 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
41054 (creating a new template parameter list if necessary). Returns the newly
41055 created template type parm. */
41056
41057 static tree
41058 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
41059 {
41060 gcc_assert (current_binding_level->kind == sk_function_parms);
41061
41062 /* Before committing to modifying any scope, if we're in an
41063 implicit template scope, and we're trying to synthesize a
41064 constrained parameter, try to find a previous parameter with
41065 the same name. This is the same-type rule for abbreviated
41066 function templates.
41067
41068 NOTE: We can generate implicit parameters when tentatively
41069 parsing a nested name specifier, only to reject that parse
41070 later. However, matching the same template-id as part of a
41071 direct-declarator should generate an identical template
41072 parameter, so this rule will merge them. */
41073 if (parser->implicit_template_scope && constr)
41074 {
41075 tree t = parser->implicit_template_parms;
41076 while (t)
41077 {
41078 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
41079 {
41080 tree d = TREE_VALUE (t);
41081 if (TREE_CODE (d) == PARM_DECL)
41082 /* Return the TEMPLATE_PARM_INDEX. */
41083 d = DECL_INITIAL (d);
41084 return d;
41085 }
41086 t = TREE_CHAIN (t);
41087 }
41088 }
41089
41090 /* We are either continuing a function template that already contains implicit
41091 template parameters, creating a new fully-implicit function template, or
41092 extending an existing explicit function template with implicit template
41093 parameters. */
41094
41095 cp_binding_level *const entry_scope = current_binding_level;
41096
41097 bool become_template = false;
41098 cp_binding_level *parent_scope = 0;
41099
41100 if (parser->implicit_template_scope)
41101 {
41102 gcc_assert (parser->implicit_template_parms);
41103
41104 current_binding_level = parser->implicit_template_scope;
41105 }
41106 else
41107 {
41108 /* Roll back to the existing template parameter scope (in the case of
41109 extending an explicit function template) or introduce a new template
41110 parameter scope ahead of the function parameter scope (or class scope
41111 in the case of out-of-line member definitions). The function scope is
41112 added back after template parameter synthesis below. */
41113
41114 cp_binding_level *scope = entry_scope;
41115
41116 while (scope->kind == sk_function_parms)
41117 {
41118 parent_scope = scope;
41119 scope = scope->level_chain;
41120 }
41121 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
41122 {
41123 /* If not defining a class, then any class scope is a scope level in
41124 an out-of-line member definition. In this case simply wind back
41125 beyond the first such scope to inject the template parameter list.
41126 Otherwise wind back to the class being defined. The latter can
41127 occur in class member friend declarations such as:
41128
41129 class A {
41130 void foo (auto);
41131 };
41132 class B {
41133 friend void A::foo (auto);
41134 };
41135
41136 The template parameter list synthesized for the friend declaration
41137 must be injected in the scope of 'B'. This can also occur in
41138 erroneous cases such as:
41139
41140 struct A {
41141 struct B {
41142 void foo (auto);
41143 };
41144 void B::foo (auto) {}
41145 };
41146
41147 Here the attempted definition of 'B::foo' within 'A' is ill-formed
41148 but, nevertheless, the template parameter list synthesized for the
41149 declarator should be injected into the scope of 'A' as if the
41150 ill-formed template was specified explicitly. */
41151
41152 while (scope->kind == sk_class && !scope->defining_class_p)
41153 {
41154 parent_scope = scope;
41155 scope = scope->level_chain;
41156 }
41157 }
41158
41159 current_binding_level = scope;
41160
41161 if (scope->kind != sk_template_parms
41162 || !function_being_declared_is_template_p (parser))
41163 {
41164 /* Introduce a new template parameter list for implicit template
41165 parameters. */
41166
41167 become_template = true;
41168
41169 parser->implicit_template_scope
41170 = begin_scope (sk_template_parms, NULL);
41171
41172 ++processing_template_decl;
41173
41174 parser->fully_implicit_function_template_p = true;
41175 ++parser->num_template_parameter_lists;
41176 }
41177 else
41178 {
41179 /* Synthesize implicit template parameters at the end of the explicit
41180 template parameter list. */
41181
41182 gcc_assert (current_template_parms);
41183
41184 parser->implicit_template_scope = scope;
41185
41186 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41187 parser->implicit_template_parms
41188 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
41189 }
41190 }
41191
41192 /* Synthesize a new template parameter and track the current template
41193 parameter chain with implicit_template_parms. */
41194
41195 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
41196 tree synth_id = make_generic_type_name ();
41197 tree synth_tmpl_parm;
41198 bool non_type = false;
41199
41200 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
41201 synth_tmpl_parm
41202 = finish_template_type_parm (class_type_node, synth_id);
41203 else if (TREE_CODE (proto) == TEMPLATE_DECL)
41204 synth_tmpl_parm
41205 = finish_constrained_template_template_parm (proto, synth_id);
41206 else
41207 {
41208 synth_tmpl_parm = copy_decl (proto);
41209 DECL_NAME (synth_tmpl_parm) = synth_id;
41210 non_type = true;
41211 }
41212
41213 // Attach the constraint to the parm before processing.
41214 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
41215 TREE_TYPE (node) = constr;
41216 tree new_parm
41217 = process_template_parm (parser->implicit_template_parms,
41218 input_location,
41219 node,
41220 /*non_type=*/non_type,
41221 /*param_pack=*/false);
41222
41223 // Chain the new parameter to the list of implicit parameters.
41224 if (parser->implicit_template_parms)
41225 parser->implicit_template_parms
41226 = TREE_CHAIN (parser->implicit_template_parms);
41227 else
41228 parser->implicit_template_parms = new_parm;
41229
41230 tree new_decl = get_local_decls ();
41231 if (non_type)
41232 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
41233 new_decl = DECL_INITIAL (new_decl);
41234
41235 /* If creating a fully implicit function template, start the new implicit
41236 template parameter list with this synthesized type, otherwise grow the
41237 current template parameter list. */
41238
41239 if (become_template)
41240 {
41241 parent_scope->level_chain = current_binding_level;
41242
41243 tree new_parms = make_tree_vec (1);
41244 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
41245 current_template_parms = tree_cons (size_int (processing_template_decl),
41246 new_parms, current_template_parms);
41247 }
41248 else
41249 {
41250 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41251 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
41252 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
41253 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
41254 }
41255
41256 // If the new parameter was constrained, we need to add that to the
41257 // constraints in the template parameter list.
41258 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
41259 {
41260 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
41261 reqs = conjoin_constraints (reqs, req);
41262 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
41263 }
41264
41265 current_binding_level = entry_scope;
41266
41267 return new_decl;
41268 }
41269
41270 /* Finish the declaration of a fully implicit function template. Such a
41271 template has no explicit template parameter list so has not been through the
41272 normal template head and tail processing. synthesize_implicit_template_parm
41273 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
41274 provided if the declaration is a class member such that its template
41275 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
41276 form is returned. Otherwise NULL_TREE is returned. */
41277
41278 static tree
41279 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
41280 {
41281 gcc_assert (parser->fully_implicit_function_template_p);
41282
41283 if (member_decl_opt && member_decl_opt != error_mark_node
41284 && DECL_VIRTUAL_P (member_decl_opt))
41285 {
41286 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
41287 "implicit templates may not be %<virtual%>");
41288 DECL_VIRTUAL_P (member_decl_opt) = false;
41289 }
41290
41291 if (member_decl_opt)
41292 member_decl_opt = finish_member_template_decl (member_decl_opt);
41293 end_template_decl ();
41294
41295 parser->fully_implicit_function_template_p = false;
41296 parser->implicit_template_parms = 0;
41297 parser->implicit_template_scope = 0;
41298 --parser->num_template_parameter_lists;
41299
41300 return member_decl_opt;
41301 }
41302
41303 /* Like finish_fully_implicit_template, but to be used in error
41304 recovery, rearranging scopes so that we restore the state we had
41305 before synthesize_implicit_template_parm inserted the implement
41306 template parms scope. */
41307
41308 static void
41309 abort_fully_implicit_template (cp_parser *parser)
41310 {
41311 cp_binding_level *return_to_scope = current_binding_level;
41312
41313 if (parser->implicit_template_scope
41314 && return_to_scope != parser->implicit_template_scope)
41315 {
41316 cp_binding_level *child = return_to_scope;
41317 for (cp_binding_level *scope = child->level_chain;
41318 scope != parser->implicit_template_scope;
41319 scope = child->level_chain)
41320 child = scope;
41321 child->level_chain = parser->implicit_template_scope->level_chain;
41322 parser->implicit_template_scope->level_chain = return_to_scope;
41323 current_binding_level = parser->implicit_template_scope;
41324 }
41325 else
41326 return_to_scope = return_to_scope->level_chain;
41327
41328 finish_fully_implicit_template (parser, NULL);
41329
41330 gcc_assert (current_binding_level == return_to_scope);
41331 }
41332
41333 /* Helper function for diagnostics that have complained about things
41334 being used with 'extern "C"' linkage.
41335
41336 Attempt to issue a note showing where the 'extern "C"' linkage began. */
41337
41338 void
41339 maybe_show_extern_c_location (void)
41340 {
41341 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
41342 inform (the_parser->innermost_linkage_specification_location,
41343 "%<extern \"C\"%> linkage started here");
41344 }
41345
41346 #include "gt-cp-parser.h"