Simplify type-specifier parsing.
[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 static tree cp_parser_noexcept_specification_opt
251 (cp_parser *, bool, bool *, bool, bool);
252 static tree cp_parser_late_noexcept_specifier
253 (cp_parser *, tree);
254 static void noexcept_override_late_checks
255 (tree, tree);
256
257 static void cp_parser_initial_pragma
258 (cp_token *);
259
260 static bool cp_parser_omp_declare_reduction_exprs
261 (tree, cp_parser *);
262 static void cp_finalize_oacc_routine
263 (cp_parser *, tree, bool);
264
265 /* Manifest constants. */
266 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
267 #define CP_SAVED_TOKEN_STACK 5
268
269 /* Variables. */
270
271 /* The stream to which debugging output should be written. */
272 static FILE *cp_lexer_debug_stream;
273
274 /* Nonzero if we are parsing an unevaluated operand: an operand to
275 sizeof, typeof, or alignof. */
276 int cp_unevaluated_operand;
277
278 /* Dump up to NUM tokens in BUFFER to FILE starting with token
279 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
280 first token in BUFFER. If NUM is 0, dump all the tokens. If
281 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
282 highlighted by surrounding it in [[ ]]. */
283
284 static void
285 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
286 cp_token *start_token, unsigned num,
287 cp_token *curr_token)
288 {
289 unsigned i, nprinted;
290 cp_token *token;
291 bool do_print;
292
293 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
294
295 if (buffer == NULL)
296 return;
297
298 if (num == 0)
299 num = buffer->length ();
300
301 if (start_token == NULL)
302 start_token = buffer->address ();
303
304 if (start_token > buffer->address ())
305 {
306 cp_lexer_print_token (file, &(*buffer)[0]);
307 fprintf (file, " ... ");
308 }
309
310 do_print = false;
311 nprinted = 0;
312 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
313 {
314 if (token == start_token)
315 do_print = true;
316
317 if (!do_print)
318 continue;
319
320 nprinted++;
321 if (token == curr_token)
322 fprintf (file, "[[");
323
324 cp_lexer_print_token (file, token);
325
326 if (token == curr_token)
327 fprintf (file, "]]");
328
329 switch (token->type)
330 {
331 case CPP_SEMICOLON:
332 case CPP_OPEN_BRACE:
333 case CPP_CLOSE_BRACE:
334 case CPP_EOF:
335 fputc ('\n', file);
336 break;
337
338 default:
339 fputc (' ', file);
340 }
341 }
342
343 if (i == num && i < buffer->length ())
344 {
345 fprintf (file, " ... ");
346 cp_lexer_print_token (file, &buffer->last ());
347 }
348
349 fprintf (file, "\n");
350 }
351
352
353 /* Dump all tokens in BUFFER to stderr. */
354
355 void
356 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
357 {
358 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
359 }
360
361 DEBUG_FUNCTION void
362 debug (vec<cp_token, va_gc> &ref)
363 {
364 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
365 }
366
367 DEBUG_FUNCTION void
368 debug (vec<cp_token, va_gc> *ptr)
369 {
370 if (ptr)
371 debug (*ptr);
372 else
373 fprintf (stderr, "<nil>\n");
374 }
375
376
377 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
378 description for T. */
379
380 static void
381 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
382 {
383 if (t)
384 {
385 fprintf (file, "%s: ", desc);
386 print_node_brief (file, "", t, 0);
387 }
388 }
389
390
391 /* Dump parser context C to FILE. */
392
393 static void
394 cp_debug_print_context (FILE *file, cp_parser_context *c)
395 {
396 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
397 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
398 print_node_brief (file, "", c->object_type, 0);
399 fprintf (file, "}\n");
400 }
401
402
403 /* Print the stack of parsing contexts to FILE starting with FIRST. */
404
405 static void
406 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
407 {
408 unsigned i;
409 cp_parser_context *c;
410
411 fprintf (file, "Parsing context stack:\n");
412 for (i = 0, c = first; c; c = c->next, i++)
413 {
414 fprintf (file, "\t#%u: ", i);
415 cp_debug_print_context (file, c);
416 }
417 }
418
419
420 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
421
422 static void
423 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
424 {
425 if (flag)
426 fprintf (file, "%s: true\n", desc);
427 }
428
429
430 /* Print an unparsed function entry UF to FILE. */
431
432 static void
433 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
434 {
435 unsigned i;
436 cp_default_arg_entry *default_arg_fn;
437 tree fn;
438
439 fprintf (file, "\tFunctions with default args:\n");
440 for (i = 0;
441 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
442 i++)
443 {
444 fprintf (file, "\t\tClass type: ");
445 print_node_brief (file, "", default_arg_fn->class_type, 0);
446 fprintf (file, "\t\tDeclaration: ");
447 print_node_brief (file, "", default_arg_fn->decl, 0);
448 fprintf (file, "\n");
449 }
450
451 fprintf (file, "\n\tFunctions with definitions that require "
452 "post-processing\n\t\t");
453 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
454 {
455 print_node_brief (file, "", fn, 0);
456 fprintf (file, " ");
457 }
458 fprintf (file, "\n");
459
460 fprintf (file, "\n\tNon-static data members with initializers that require "
461 "post-processing\n\t\t");
462 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
463 {
464 print_node_brief (file, "", fn, 0);
465 fprintf (file, " ");
466 }
467 fprintf (file, "\n");
468 }
469
470
471 /* Print the stack of unparsed member functions S to FILE. */
472
473 static void
474 cp_debug_print_unparsed_queues (FILE *file,
475 vec<cp_unparsed_functions_entry, va_gc> *s)
476 {
477 unsigned i;
478 cp_unparsed_functions_entry *uf;
479
480 fprintf (file, "Unparsed functions\n");
481 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
482 {
483 fprintf (file, "#%u:\n", i);
484 cp_debug_print_unparsed_function (file, uf);
485 }
486 }
487
488
489 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
490 the given PARSER. If FILE is NULL, the output is printed on stderr. */
491
492 static void
493 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
494 {
495 cp_token *next_token, *first_token, *start_token;
496
497 if (file == NULL)
498 file = stderr;
499
500 next_token = parser->lexer->next_token;
501 first_token = parser->lexer->buffer->address ();
502 start_token = (next_token > first_token + window_size / 2)
503 ? next_token - window_size / 2
504 : first_token;
505 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
506 next_token);
507 }
508
509
510 /* Dump debugging information for the given PARSER. If FILE is NULL,
511 the output is printed on stderr. */
512
513 void
514 cp_debug_parser (FILE *file, cp_parser *parser)
515 {
516 const size_t window_size = 20;
517 cp_token *token;
518 expanded_location eloc;
519
520 if (file == NULL)
521 file = stderr;
522
523 fprintf (file, "Parser state\n\n");
524 fprintf (file, "Number of tokens: %u\n",
525 vec_safe_length (parser->lexer->buffer));
526 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
527 cp_debug_print_tree_if_set (file, "Object scope",
528 parser->object_scope);
529 cp_debug_print_tree_if_set (file, "Qualifying scope",
530 parser->qualifying_scope);
531 cp_debug_print_context_stack (file, parser->context);
532 cp_debug_print_flag (file, "Allow GNU extensions",
533 parser->allow_gnu_extensions_p);
534 cp_debug_print_flag (file, "'>' token is greater-than",
535 parser->greater_than_is_operator_p);
536 cp_debug_print_flag (file, "Default args allowed in current "
537 "parameter list", parser->default_arg_ok_p);
538 cp_debug_print_flag (file, "Parsing integral constant-expression",
539 parser->integral_constant_expression_p);
540 cp_debug_print_flag (file, "Allow non-constant expression in current "
541 "constant-expression",
542 parser->allow_non_integral_constant_expression_p);
543 cp_debug_print_flag (file, "Seen non-constant expression",
544 parser->non_integral_constant_expression_p);
545 cp_debug_print_flag (file, "Local names forbidden in current context",
546 (parser->local_variables_forbidden_p
547 & LOCAL_VARS_FORBIDDEN));
548 cp_debug_print_flag (file, "'this' forbidden in current context",
549 (parser->local_variables_forbidden_p
550 & THIS_FORBIDDEN));
551 cp_debug_print_flag (file, "In unbraced linkage specification",
552 parser->in_unbraced_linkage_specification_p);
553 cp_debug_print_flag (file, "Parsing a declarator",
554 parser->in_declarator_p);
555 cp_debug_print_flag (file, "In template argument list",
556 parser->in_template_argument_list_p);
557 cp_debug_print_flag (file, "Parsing an iteration statement",
558 parser->in_statement & IN_ITERATION_STMT);
559 cp_debug_print_flag (file, "Parsing a switch statement",
560 parser->in_statement & IN_SWITCH_STMT);
561 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
562 parser->in_statement & IN_OMP_BLOCK);
563 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
564 parser->in_statement & IN_OMP_FOR);
565 cp_debug_print_flag (file, "Parsing an if statement",
566 parser->in_statement & IN_IF_STMT);
567 cp_debug_print_flag (file, "Parsing a type-id in an expression "
568 "context", parser->in_type_id_in_expr_p);
569 cp_debug_print_flag (file, "String expressions should be translated "
570 "to execution character set",
571 parser->translate_strings_p);
572 cp_debug_print_flag (file, "Parsing function body outside of a "
573 "local class", parser->in_function_body);
574 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
575 parser->colon_corrects_to_scope_p);
576 cp_debug_print_flag (file, "Colon doesn't start a class definition",
577 parser->colon_doesnt_start_class_def_p);
578 if (parser->type_definition_forbidden_message)
579 fprintf (file, "Error message for forbidden type definitions: %s %s\n",
580 parser->type_definition_forbidden_message,
581 parser->type_definition_forbidden_message_arg
582 ? parser->type_definition_forbidden_message_arg : "<none>");
583 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
584 fprintf (file, "Number of class definitions in progress: %u\n",
585 parser->num_classes_being_defined);
586 fprintf (file, "Number of template parameter lists for the current "
587 "declaration: %u\n", parser->num_template_parameter_lists);
588 cp_debug_parser_tokens (file, parser, window_size);
589 token = parser->lexer->next_token;
590 fprintf (file, "Next token to parse:\n");
591 fprintf (file, "\tToken: ");
592 cp_lexer_print_token (file, token);
593 eloc = expand_location (token->location);
594 fprintf (file, "\n\tFile: %s\n", eloc.file);
595 fprintf (file, "\tLine: %d\n", eloc.line);
596 fprintf (file, "\tColumn: %d\n", eloc.column);
597 }
598
599 DEBUG_FUNCTION void
600 debug (cp_parser &ref)
601 {
602 cp_debug_parser (stderr, &ref);
603 }
604
605 DEBUG_FUNCTION void
606 debug (cp_parser *ptr)
607 {
608 if (ptr)
609 debug (*ptr);
610 else
611 fprintf (stderr, "<nil>\n");
612 }
613
614 /* Allocate memory for a new lexer object and return it. */
615
616 static cp_lexer *
617 cp_lexer_alloc (void)
618 {
619 cp_lexer *lexer;
620
621 c_common_no_more_pch ();
622
623 /* Allocate the memory. */
624 lexer = ggc_cleared_alloc<cp_lexer> ();
625
626 /* Initially we are not debugging. */
627 lexer->debugging_p = false;
628
629 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
630
631 /* Create the buffer. */
632 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
633
634 return lexer;
635 }
636
637
638 /* Create a new main C++ lexer, the lexer that gets tokens from the
639 preprocessor. */
640
641 static cp_lexer *
642 cp_lexer_new_main (void)
643 {
644 cp_lexer *lexer;
645 cp_token token;
646
647 /* It's possible that parsing the first pragma will load a PCH file,
648 which is a GC collection point. So we have to do that before
649 allocating any memory. */
650 cp_parser_initial_pragma (&token);
651
652 lexer = cp_lexer_alloc ();
653
654 /* Put the first token in the buffer. */
655 lexer->buffer->quick_push (token);
656
657 /* Get the remaining tokens from the preprocessor. */
658 while (token.type != CPP_EOF)
659 {
660 cp_lexer_get_preprocessor_token (lexer, &token);
661 vec_safe_push (lexer->buffer, token);
662 }
663
664 lexer->last_token = lexer->buffer->address ()
665 + lexer->buffer->length ()
666 - 1;
667 lexer->next_token = lexer->buffer->length ()
668 ? lexer->buffer->address ()
669 : &eof_token;
670
671 /* Subsequent preprocessor diagnostics should use compiler
672 diagnostic functions to get the compiler source location. */
673 done_lexing = true;
674
675 gcc_assert (!lexer->next_token->purged_p);
676 return lexer;
677 }
678
679 /* Create a new lexer whose token stream is primed with the tokens in
680 CACHE. When these tokens are exhausted, no new tokens will be read. */
681
682 static cp_lexer *
683 cp_lexer_new_from_tokens (cp_token_cache *cache)
684 {
685 cp_token *first = cache->first;
686 cp_token *last = cache->last;
687 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
688
689 /* We do not own the buffer. */
690 lexer->buffer = NULL;
691 lexer->next_token = first == last ? &eof_token : first;
692 lexer->last_token = last;
693
694 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
695
696 /* Initially we are not debugging. */
697 lexer->debugging_p = false;
698
699 gcc_assert (!lexer->next_token->purged_p);
700 return lexer;
701 }
702
703 /* Frees all resources associated with LEXER. */
704
705 static void
706 cp_lexer_destroy (cp_lexer *lexer)
707 {
708 vec_free (lexer->buffer);
709 lexer->saved_tokens.release ();
710 ggc_free (lexer);
711 }
712
713 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
714 be used. The point of this flag is to help the compiler to fold away calls
715 to cp_lexer_debugging_p within this source file at compile time, when the
716 lexer is not being debugged. */
717
718 #define LEXER_DEBUGGING_ENABLED_P false
719
720 /* Returns nonzero if debugging information should be output. */
721
722 static inline bool
723 cp_lexer_debugging_p (cp_lexer *lexer)
724 {
725 if (!LEXER_DEBUGGING_ENABLED_P)
726 return false;
727
728 return lexer->debugging_p;
729 }
730
731
732 static inline cp_token_position
733 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
734 {
735 gcc_assert (!previous_p || lexer->next_token != &eof_token);
736
737 return lexer->next_token - previous_p;
738 }
739
740 static inline cp_token *
741 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
742 {
743 return pos;
744 }
745
746 static inline void
747 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
748 {
749 lexer->next_token = cp_lexer_token_at (lexer, pos);
750 }
751
752 static inline cp_token_position
753 cp_lexer_previous_token_position (cp_lexer *lexer)
754 {
755 if (lexer->next_token == &eof_token)
756 return lexer->last_token - 1;
757 else
758 return cp_lexer_token_position (lexer, true);
759 }
760
761 static inline cp_token *
762 cp_lexer_previous_token (cp_lexer *lexer)
763 {
764 cp_token_position tp = cp_lexer_previous_token_position (lexer);
765
766 /* Skip past purged tokens. */
767 while (tp->purged_p)
768 {
769 gcc_assert (tp != vec_safe_address (lexer->buffer));
770 tp--;
771 }
772
773 return cp_lexer_token_at (lexer, tp);
774 }
775
776 /* nonzero if we are presently saving tokens. */
777
778 static inline int
779 cp_lexer_saving_tokens (const cp_lexer* lexer)
780 {
781 return lexer->saved_tokens.length () != 0;
782 }
783
784 /* Store the next token from the preprocessor in *TOKEN. Return true
785 if we reach EOF. If LEXER is NULL, assume we are handling an
786 initial #pragma pch_preprocess, and thus want the lexer to return
787 processed strings. */
788
789 static void
790 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
791 {
792 static int is_extern_c = 0;
793
794 /* Get a new token from the preprocessor. */
795 token->type
796 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
797 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
798 token->keyword = RID_MAX;
799 token->purged_p = false;
800 token->error_reported = false;
801
802 /* On some systems, some header files are surrounded by an
803 implicit extern "C" block. Set a flag in the token if it
804 comes from such a header. */
805 is_extern_c += pending_lang_change;
806 pending_lang_change = 0;
807 token->implicit_extern_c = is_extern_c > 0;
808
809 /* Check to see if this token is a keyword. */
810 if (token->type == CPP_NAME)
811 {
812 if (IDENTIFIER_KEYWORD_P (token->u.value))
813 {
814 /* Mark this token as a keyword. */
815 token->type = CPP_KEYWORD;
816 /* Record which keyword. */
817 token->keyword = C_RID_CODE (token->u.value);
818 }
819 else
820 {
821 if (warn_cxx11_compat
822 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
823 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
824 {
825 /* Warn about the C++0x keyword (but still treat it as
826 an identifier). */
827 warning (OPT_Wc__11_compat,
828 "identifier %qE is a keyword in C++11",
829 token->u.value);
830
831 /* Clear out the C_RID_CODE so we don't warn about this
832 particular identifier-turned-keyword again. */
833 C_SET_RID_CODE (token->u.value, RID_MAX);
834 }
835
836 token->keyword = RID_MAX;
837 }
838 }
839 else if (token->type == CPP_AT_NAME)
840 {
841 /* This only happens in Objective-C++; it must be a keyword. */
842 token->type = CPP_KEYWORD;
843 switch (C_RID_CODE (token->u.value))
844 {
845 /* Replace 'class' with '@class', 'private' with '@private',
846 etc. This prevents confusion with the C++ keyword
847 'class', and makes the tokens consistent with other
848 Objective-C 'AT' keywords. For example '@class' is
849 reported as RID_AT_CLASS which is consistent with
850 '@synchronized', which is reported as
851 RID_AT_SYNCHRONIZED.
852 */
853 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
854 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
855 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
856 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
857 case RID_THROW: token->keyword = RID_AT_THROW; break;
858 case RID_TRY: token->keyword = RID_AT_TRY; break;
859 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
860 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
861 default: token->keyword = C_RID_CODE (token->u.value);
862 }
863 }
864 }
865
866 /* Update the globals input_location and the input file stack from TOKEN. */
867 static inline void
868 cp_lexer_set_source_position_from_token (cp_token *token)
869 {
870 if (token->type != CPP_EOF)
871 {
872 input_location = token->location;
873 }
874 }
875
876 /* Update the globals input_location and the input file stack from LEXER. */
877 static inline void
878 cp_lexer_set_source_position (cp_lexer *lexer)
879 {
880 cp_token *token = cp_lexer_peek_token (lexer);
881 cp_lexer_set_source_position_from_token (token);
882 }
883
884 /* Return a pointer to the next token in the token stream, but do not
885 consume it. */
886
887 static inline cp_token *
888 cp_lexer_peek_token (cp_lexer *lexer)
889 {
890 if (cp_lexer_debugging_p (lexer))
891 {
892 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
893 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
894 putc ('\n', cp_lexer_debug_stream);
895 }
896 return lexer->next_token;
897 }
898
899 /* Return true if the next token has the indicated TYPE. */
900
901 static inline bool
902 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
903 {
904 return cp_lexer_peek_token (lexer)->type == type;
905 }
906
907 /* Return true if the next token does not have the indicated TYPE. */
908
909 static inline bool
910 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
911 {
912 return !cp_lexer_next_token_is (lexer, type);
913 }
914
915 /* Return true if the next token is the indicated KEYWORD. */
916
917 static inline bool
918 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
919 {
920 return cp_lexer_peek_token (lexer)->keyword == keyword;
921 }
922
923 static inline bool
924 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
925 {
926 return cp_lexer_peek_nth_token (lexer, n)->type == type;
927 }
928
929 static inline bool
930 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
931 {
932 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
933 }
934
935 /* Return true if KEYWORD can start a decl-specifier. */
936
937 bool
938 cp_keyword_starts_decl_specifier_p (enum rid keyword)
939 {
940 switch (keyword)
941 {
942 /* auto specifier: storage-class-specifier in C++,
943 simple-type-specifier in C++0x. */
944 case RID_AUTO:
945 /* Storage classes. */
946 case RID_REGISTER:
947 case RID_STATIC:
948 case RID_EXTERN:
949 case RID_MUTABLE:
950 case RID_THREAD:
951 /* Elaborated type specifiers. */
952 case RID_ENUM:
953 case RID_CLASS:
954 case RID_STRUCT:
955 case RID_UNION:
956 case RID_TYPENAME:
957 /* Simple type specifiers. */
958 case RID_CHAR:
959 case RID_CHAR8:
960 case RID_CHAR16:
961 case RID_CHAR32:
962 case RID_WCHAR:
963 case RID_BOOL:
964 case RID_SHORT:
965 case RID_INT:
966 case RID_LONG:
967 case RID_SIGNED:
968 case RID_UNSIGNED:
969 case RID_FLOAT:
970 case RID_DOUBLE:
971 case RID_VOID:
972 /* GNU extensions. */
973 case RID_ATTRIBUTE:
974 case RID_TYPEOF:
975 /* C++0x extensions. */
976 case RID_DECLTYPE:
977 case RID_UNDERLYING_TYPE:
978 case RID_CONSTEXPR:
979 return true;
980
981 default:
982 if (keyword >= RID_FIRST_INT_N
983 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
984 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
985 return true;
986 return false;
987 }
988 }
989
990 /* Return true if the next token is a keyword for a decl-specifier. */
991
992 static bool
993 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
994 {
995 cp_token *token;
996
997 token = cp_lexer_peek_token (lexer);
998 return cp_keyword_starts_decl_specifier_p (token->keyword);
999 }
1000
1001 /* Returns TRUE iff the token T begins a decltype type. */
1002
1003 static bool
1004 token_is_decltype (cp_token *t)
1005 {
1006 return (t->keyword == RID_DECLTYPE
1007 || t->type == CPP_DECLTYPE);
1008 }
1009
1010 /* Returns TRUE iff the next token begins a decltype type. */
1011
1012 static bool
1013 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1014 {
1015 cp_token *t = cp_lexer_peek_token (lexer);
1016 return token_is_decltype (t);
1017 }
1018
1019 /* Called when processing a token with tree_check_value; perform or defer the
1020 associated checks and return the value. */
1021
1022 static tree
1023 saved_checks_value (struct tree_check *check_value)
1024 {
1025 /* Perform any access checks that were deferred. */
1026 vec<deferred_access_check, va_gc> *checks;
1027 deferred_access_check *chk;
1028 checks = check_value->checks;
1029 if (checks)
1030 {
1031 int i;
1032 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1033 perform_or_defer_access_check (chk->binfo,
1034 chk->decl,
1035 chk->diag_decl, tf_warning_or_error);
1036 }
1037 /* Return the stored value. */
1038 return check_value->value;
1039 }
1040
1041 /* Return a pointer to the Nth token in the token stream. If N is 1,
1042 then this is precisely equivalent to cp_lexer_peek_token (except
1043 that it is not inline). One would like to disallow that case, but
1044 there is one case (cp_parser_nth_token_starts_template_id) where
1045 the caller passes a variable for N and it might be 1. */
1046
1047 static cp_token *
1048 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1049 {
1050 cp_token *token;
1051
1052 /* N is 1-based, not zero-based. */
1053 gcc_assert (n > 0);
1054
1055 if (cp_lexer_debugging_p (lexer))
1056 fprintf (cp_lexer_debug_stream,
1057 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1058
1059 --n;
1060 token = lexer->next_token;
1061 gcc_assert (!n || token != &eof_token);
1062 while (n != 0)
1063 {
1064 ++token;
1065 if (token == lexer->last_token)
1066 {
1067 token = &eof_token;
1068 break;
1069 }
1070
1071 if (!token->purged_p)
1072 --n;
1073 }
1074
1075 if (cp_lexer_debugging_p (lexer))
1076 {
1077 cp_lexer_print_token (cp_lexer_debug_stream, token);
1078 putc ('\n', cp_lexer_debug_stream);
1079 }
1080
1081 return token;
1082 }
1083
1084 /* Return the next token, and advance the lexer's next_token pointer
1085 to point to the next non-purged token. */
1086
1087 static cp_token *
1088 cp_lexer_consume_token (cp_lexer* lexer)
1089 {
1090 cp_token *token = lexer->next_token;
1091
1092 gcc_assert (token != &eof_token);
1093 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1094
1095 do
1096 {
1097 lexer->next_token++;
1098 if (lexer->next_token == lexer->last_token)
1099 {
1100 lexer->next_token = &eof_token;
1101 break;
1102 }
1103
1104 }
1105 while (lexer->next_token->purged_p);
1106
1107 cp_lexer_set_source_position_from_token (token);
1108
1109 /* Provide debugging output. */
1110 if (cp_lexer_debugging_p (lexer))
1111 {
1112 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1113 cp_lexer_print_token (cp_lexer_debug_stream, token);
1114 putc ('\n', cp_lexer_debug_stream);
1115 }
1116
1117 return token;
1118 }
1119
1120 /* Permanently remove the next token from the token stream, and
1121 advance the next_token pointer to refer to the next non-purged
1122 token. */
1123
1124 static void
1125 cp_lexer_purge_token (cp_lexer *lexer)
1126 {
1127 cp_token *tok = lexer->next_token;
1128
1129 gcc_assert (tok != &eof_token);
1130 tok->purged_p = true;
1131 tok->location = UNKNOWN_LOCATION;
1132 tok->u.value = NULL_TREE;
1133 tok->keyword = RID_MAX;
1134
1135 do
1136 {
1137 tok++;
1138 if (tok == lexer->last_token)
1139 {
1140 tok = &eof_token;
1141 break;
1142 }
1143 }
1144 while (tok->purged_p);
1145 lexer->next_token = tok;
1146 }
1147
1148 /* Permanently remove all tokens after TOK, up to, but not
1149 including, the token that will be returned next by
1150 cp_lexer_peek_token. */
1151
1152 static void
1153 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1154 {
1155 cp_token *peek = lexer->next_token;
1156
1157 if (peek == &eof_token)
1158 peek = lexer->last_token;
1159
1160 gcc_assert (tok < peek);
1161
1162 for ( tok += 1; tok != peek; tok += 1)
1163 {
1164 tok->purged_p = true;
1165 tok->location = UNKNOWN_LOCATION;
1166 tok->u.value = NULL_TREE;
1167 tok->keyword = RID_MAX;
1168 }
1169 }
1170
1171 /* Begin saving tokens. All tokens consumed after this point will be
1172 preserved. */
1173
1174 static void
1175 cp_lexer_save_tokens (cp_lexer* lexer)
1176 {
1177 /* Provide debugging output. */
1178 if (cp_lexer_debugging_p (lexer))
1179 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1180
1181 lexer->saved_tokens.safe_push (lexer->next_token);
1182 }
1183
1184 /* Commit to the portion of the token stream most recently saved. */
1185
1186 static void
1187 cp_lexer_commit_tokens (cp_lexer* lexer)
1188 {
1189 /* Provide debugging output. */
1190 if (cp_lexer_debugging_p (lexer))
1191 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1192
1193 lexer->saved_tokens.pop ();
1194 }
1195
1196 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1197 to the token stream. Stop saving tokens. */
1198
1199 static void
1200 cp_lexer_rollback_tokens (cp_lexer* lexer)
1201 {
1202 /* Provide debugging output. */
1203 if (cp_lexer_debugging_p (lexer))
1204 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1205
1206 lexer->next_token = lexer->saved_tokens.pop ();
1207 }
1208
1209 /* RAII wrapper around the above functions, with sanity checking. Creating
1210 a variable saves tokens, which are committed when the variable is
1211 destroyed unless they are explicitly rolled back by calling the rollback
1212 member function. */
1213
1214 struct saved_token_sentinel
1215 {
1216 cp_lexer *lexer;
1217 unsigned len;
1218 bool commit;
1219 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1220 {
1221 len = lexer->saved_tokens.length ();
1222 cp_lexer_save_tokens (lexer);
1223 }
1224 void rollback ()
1225 {
1226 cp_lexer_rollback_tokens (lexer);
1227 commit = false;
1228 }
1229 ~saved_token_sentinel()
1230 {
1231 if (commit)
1232 cp_lexer_commit_tokens (lexer);
1233 gcc_assert (lexer->saved_tokens.length () == len);
1234 }
1235 };
1236
1237 /* Print a representation of the TOKEN on the STREAM. */
1238
1239 static void
1240 cp_lexer_print_token (FILE * stream, cp_token *token)
1241 {
1242 /* We don't use cpp_type2name here because the parser defines
1243 a few tokens of its own. */
1244 static const char *const token_names[] = {
1245 /* cpplib-defined token types */
1246 #define OP(e, s) #e,
1247 #define TK(e, s) #e,
1248 TTYPE_TABLE
1249 #undef OP
1250 #undef TK
1251 /* C++ parser token types - see "Manifest constants", above. */
1252 "KEYWORD",
1253 "TEMPLATE_ID",
1254 "NESTED_NAME_SPECIFIER",
1255 };
1256
1257 /* For some tokens, print the associated data. */
1258 switch (token->type)
1259 {
1260 case CPP_KEYWORD:
1261 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1262 For example, `struct' is mapped to an INTEGER_CST. */
1263 if (!identifier_p (token->u.value))
1264 break;
1265 /* fall through */
1266 case CPP_NAME:
1267 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1268 break;
1269
1270 case CPP_STRING:
1271 case CPP_STRING16:
1272 case CPP_STRING32:
1273 case CPP_WSTRING:
1274 case CPP_UTF8STRING:
1275 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1276 break;
1277
1278 case CPP_NUMBER:
1279 print_generic_expr (stream, token->u.value);
1280 break;
1281
1282 default:
1283 /* If we have a name for the token, print it out. Otherwise, we
1284 simply give the numeric code. */
1285 if (token->type < ARRAY_SIZE(token_names))
1286 fputs (token_names[token->type], stream);
1287 else
1288 fprintf (stream, "[%d]", token->type);
1289 break;
1290 }
1291 }
1292
1293 DEBUG_FUNCTION void
1294 debug (cp_token &ref)
1295 {
1296 cp_lexer_print_token (stderr, &ref);
1297 fprintf (stderr, "\n");
1298 }
1299
1300 DEBUG_FUNCTION void
1301 debug (cp_token *ptr)
1302 {
1303 if (ptr)
1304 debug (*ptr);
1305 else
1306 fprintf (stderr, "<nil>\n");
1307 }
1308
1309
1310 /* Start emitting debugging information. */
1311
1312 static void
1313 cp_lexer_start_debugging (cp_lexer* lexer)
1314 {
1315 if (!LEXER_DEBUGGING_ENABLED_P)
1316 fatal_error (input_location,
1317 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1318
1319 lexer->debugging_p = true;
1320 cp_lexer_debug_stream = stderr;
1321 }
1322
1323 /* Stop emitting debugging information. */
1324
1325 static void
1326 cp_lexer_stop_debugging (cp_lexer* lexer)
1327 {
1328 if (!LEXER_DEBUGGING_ENABLED_P)
1329 fatal_error (input_location,
1330 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1331
1332 lexer->debugging_p = false;
1333 cp_lexer_debug_stream = NULL;
1334 }
1335
1336 /* Create a new cp_token_cache, representing a range of tokens. */
1337
1338 static cp_token_cache *
1339 cp_token_cache_new (cp_token *first, cp_token *last)
1340 {
1341 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1342 cache->first = first;
1343 cache->last = last;
1344 return cache;
1345 }
1346
1347 /* Diagnose if #pragma omp declare simd isn't followed immediately
1348 by function declaration or definition. */
1349
1350 static inline void
1351 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1352 {
1353 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1354 {
1355 error ("%<#pragma omp declare simd%> not immediately followed by "
1356 "function declaration or definition");
1357 parser->omp_declare_simd = NULL;
1358 }
1359 }
1360
1361 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1362 and put that into "omp declare simd" attribute. */
1363
1364 static inline void
1365 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1366 {
1367 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1368 {
1369 if (fndecl == error_mark_node)
1370 {
1371 parser->omp_declare_simd = NULL;
1372 return;
1373 }
1374 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1375 {
1376 cp_ensure_no_omp_declare_simd (parser);
1377 return;
1378 }
1379 }
1380 }
1381
1382 /* Diagnose if #pragma acc routine isn't followed immediately by function
1383 declaration or definition. */
1384
1385 static inline void
1386 cp_ensure_no_oacc_routine (cp_parser *parser)
1387 {
1388 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1389 {
1390 error_at (parser->oacc_routine->loc,
1391 "%<#pragma acc routine%> not immediately followed by "
1392 "function declaration or definition");
1393 parser->oacc_routine = NULL;
1394 }
1395 }
1396 \f
1397 /* Decl-specifiers. */
1398
1399 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1400
1401 static void
1402 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1403 {
1404 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1405 }
1406
1407 /* Declarators. */
1408
1409 /* Nothing other than the parser should be creating declarators;
1410 declarators are a semi-syntactic representation of C++ entities.
1411 Other parts of the front end that need to create entities (like
1412 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1413
1414 static cp_declarator *make_call_declarator
1415 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1416 static cp_declarator *make_array_declarator
1417 (cp_declarator *, tree);
1418 static cp_declarator *make_pointer_declarator
1419 (cp_cv_quals, cp_declarator *, tree);
1420 static cp_declarator *make_reference_declarator
1421 (cp_cv_quals, cp_declarator *, bool, tree);
1422 static cp_declarator *make_ptrmem_declarator
1423 (cp_cv_quals, tree, cp_declarator *, tree);
1424
1425 /* An erroneous declarator. */
1426 static cp_declarator *cp_error_declarator;
1427
1428 /* The obstack on which declarators and related data structures are
1429 allocated. */
1430 static struct obstack declarator_obstack;
1431
1432 /* Alloc BYTES from the declarator memory pool. */
1433
1434 static inline void *
1435 alloc_declarator (size_t bytes)
1436 {
1437 return obstack_alloc (&declarator_obstack, bytes);
1438 }
1439
1440 /* Allocate a declarator of the indicated KIND. Clear fields that are
1441 common to all declarators. */
1442
1443 static cp_declarator *
1444 make_declarator (cp_declarator_kind kind)
1445 {
1446 cp_declarator *declarator;
1447
1448 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1449 declarator->kind = kind;
1450 declarator->parenthesized = UNKNOWN_LOCATION;
1451 declarator->attributes = NULL_TREE;
1452 declarator->std_attributes = NULL_TREE;
1453 declarator->declarator = NULL;
1454 declarator->parameter_pack_p = false;
1455 declarator->id_loc = UNKNOWN_LOCATION;
1456
1457 return declarator;
1458 }
1459
1460 /* Make a declarator for a generalized identifier. If
1461 QUALIFYING_SCOPE is non-NULL, the identifier is
1462 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1463 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1464 is, if any. */
1465
1466 static cp_declarator *
1467 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1468 special_function_kind sfk, location_t id_location)
1469 {
1470 cp_declarator *declarator;
1471
1472 /* It is valid to write:
1473
1474 class C { void f(); };
1475 typedef C D;
1476 void D::f();
1477
1478 The standard is not clear about whether `typedef const C D' is
1479 legal; as of 2002-09-15 the committee is considering that
1480 question. EDG 3.0 allows that syntax. Therefore, we do as
1481 well. */
1482 if (qualifying_scope && TYPE_P (qualifying_scope))
1483 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1484
1485 gcc_assert (identifier_p (unqualified_name)
1486 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1487 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1488
1489 declarator = make_declarator (cdk_id);
1490 declarator->u.id.qualifying_scope = qualifying_scope;
1491 declarator->u.id.unqualified_name = unqualified_name;
1492 declarator->u.id.sfk = sfk;
1493 declarator->id_loc = id_location;
1494
1495 return declarator;
1496 }
1497
1498 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1499 of modifiers such as const or volatile to apply to the pointer
1500 type, represented as identifiers. ATTRIBUTES represent the attributes that
1501 appertain to the pointer or reference. */
1502
1503 cp_declarator *
1504 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1505 tree attributes)
1506 {
1507 cp_declarator *declarator;
1508
1509 declarator = make_declarator (cdk_pointer);
1510 declarator->declarator = target;
1511 declarator->u.pointer.qualifiers = cv_qualifiers;
1512 declarator->u.pointer.class_type = NULL_TREE;
1513 if (target)
1514 {
1515 declarator->id_loc = target->id_loc;
1516 declarator->parameter_pack_p = target->parameter_pack_p;
1517 target->parameter_pack_p = false;
1518 }
1519 else
1520 declarator->parameter_pack_p = false;
1521
1522 declarator->std_attributes = attributes;
1523
1524 return declarator;
1525 }
1526
1527 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1528 represent the attributes that appertain to the pointer or
1529 reference. */
1530
1531 cp_declarator *
1532 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1533 bool rvalue_ref, tree attributes)
1534 {
1535 cp_declarator *declarator;
1536
1537 declarator = make_declarator (cdk_reference);
1538 declarator->declarator = target;
1539 declarator->u.reference.qualifiers = cv_qualifiers;
1540 declarator->u.reference.rvalue_ref = rvalue_ref;
1541 if (target)
1542 {
1543 declarator->id_loc = target->id_loc;
1544 declarator->parameter_pack_p = target->parameter_pack_p;
1545 target->parameter_pack_p = false;
1546 }
1547 else
1548 declarator->parameter_pack_p = false;
1549
1550 declarator->std_attributes = attributes;
1551
1552 return declarator;
1553 }
1554
1555 /* Like make_pointer_declarator -- but for a pointer to a non-static
1556 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1557 appertain to the pointer or reference. */
1558
1559 cp_declarator *
1560 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1561 cp_declarator *pointee,
1562 tree attributes)
1563 {
1564 cp_declarator *declarator;
1565
1566 declarator = make_declarator (cdk_ptrmem);
1567 declarator->declarator = pointee;
1568 declarator->u.pointer.qualifiers = cv_qualifiers;
1569 declarator->u.pointer.class_type = class_type;
1570
1571 if (pointee)
1572 {
1573 declarator->parameter_pack_p = pointee->parameter_pack_p;
1574 pointee->parameter_pack_p = false;
1575 }
1576 else
1577 declarator->parameter_pack_p = false;
1578
1579 declarator->std_attributes = attributes;
1580
1581 return declarator;
1582 }
1583
1584 /* Make a declarator for the function given by TARGET, with the
1585 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1586 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1587 indicates what exceptions can be thrown. */
1588
1589 cp_declarator *
1590 make_call_declarator (cp_declarator *target,
1591 tree parms,
1592 cp_cv_quals cv_qualifiers,
1593 cp_virt_specifiers virt_specifiers,
1594 cp_ref_qualifier ref_qualifier,
1595 tree tx_qualifier,
1596 tree exception_specification,
1597 tree late_return_type,
1598 tree requires_clause)
1599 {
1600 cp_declarator *declarator;
1601
1602 declarator = make_declarator (cdk_function);
1603 declarator->declarator = target;
1604 declarator->u.function.parameters = parms;
1605 declarator->u.function.qualifiers = cv_qualifiers;
1606 declarator->u.function.virt_specifiers = virt_specifiers;
1607 declarator->u.function.ref_qualifier = ref_qualifier;
1608 declarator->u.function.tx_qualifier = tx_qualifier;
1609 declarator->u.function.exception_specification = exception_specification;
1610 declarator->u.function.late_return_type = late_return_type;
1611 declarator->u.function.requires_clause = requires_clause;
1612 if (target)
1613 {
1614 declarator->id_loc = target->id_loc;
1615 declarator->parameter_pack_p = target->parameter_pack_p;
1616 target->parameter_pack_p = false;
1617 }
1618 else
1619 declarator->parameter_pack_p = false;
1620
1621 return declarator;
1622 }
1623
1624 /* Make a declarator for an array of BOUNDS elements, each of which is
1625 defined by ELEMENT. */
1626
1627 cp_declarator *
1628 make_array_declarator (cp_declarator *element, tree bounds)
1629 {
1630 cp_declarator *declarator;
1631
1632 declarator = make_declarator (cdk_array);
1633 declarator->declarator = element;
1634 declarator->u.array.bounds = bounds;
1635 if (element)
1636 {
1637 declarator->id_loc = element->id_loc;
1638 declarator->parameter_pack_p = element->parameter_pack_p;
1639 element->parameter_pack_p = false;
1640 }
1641 else
1642 declarator->parameter_pack_p = false;
1643
1644 return declarator;
1645 }
1646
1647 /* Determine whether the declarator we've seen so far can be a
1648 parameter pack, when followed by an ellipsis. */
1649 static bool
1650 declarator_can_be_parameter_pack (cp_declarator *declarator)
1651 {
1652 if (declarator && declarator->parameter_pack_p)
1653 /* We already saw an ellipsis. */
1654 return false;
1655
1656 /* Search for a declarator name, or any other declarator that goes
1657 after the point where the ellipsis could appear in a parameter
1658 pack. If we find any of these, then this declarator cannot be
1659 made into a parameter pack. */
1660 bool found = false;
1661 while (declarator && !found)
1662 {
1663 switch ((int)declarator->kind)
1664 {
1665 case cdk_id:
1666 case cdk_array:
1667 case cdk_decomp:
1668 found = true;
1669 break;
1670
1671 case cdk_error:
1672 return true;
1673
1674 default:
1675 declarator = declarator->declarator;
1676 break;
1677 }
1678 }
1679
1680 return !found;
1681 }
1682
1683 cp_parameter_declarator *no_parameters;
1684
1685 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1686 DECLARATOR and DEFAULT_ARGUMENT. */
1687
1688 cp_parameter_declarator *
1689 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1690 cp_declarator *declarator,
1691 tree default_argument,
1692 location_t loc,
1693 bool template_parameter_pack_p = false)
1694 {
1695 cp_parameter_declarator *parameter;
1696
1697 parameter = ((cp_parameter_declarator *)
1698 alloc_declarator (sizeof (cp_parameter_declarator)));
1699 parameter->next = NULL;
1700 if (decl_specifiers)
1701 parameter->decl_specifiers = *decl_specifiers;
1702 else
1703 clear_decl_specs (&parameter->decl_specifiers);
1704 parameter->declarator = declarator;
1705 parameter->default_argument = default_argument;
1706 parameter->template_parameter_pack_p = template_parameter_pack_p;
1707 parameter->loc = loc;
1708
1709 return parameter;
1710 }
1711
1712 /* Returns true iff DECLARATOR is a declaration for a function. */
1713
1714 static bool
1715 function_declarator_p (const cp_declarator *declarator)
1716 {
1717 while (declarator)
1718 {
1719 if (declarator->kind == cdk_function
1720 && declarator->declarator->kind == cdk_id)
1721 return true;
1722 if (declarator->kind == cdk_id
1723 || declarator->kind == cdk_decomp
1724 || declarator->kind == cdk_error)
1725 return false;
1726 declarator = declarator->declarator;
1727 }
1728 return false;
1729 }
1730
1731 /* The parser. */
1732
1733 /* Overview
1734 --------
1735
1736 A cp_parser parses the token stream as specified by the C++
1737 grammar. Its job is purely parsing, not semantic analysis. For
1738 example, the parser breaks the token stream into declarators,
1739 expressions, statements, and other similar syntactic constructs.
1740 It does not check that the types of the expressions on either side
1741 of an assignment-statement are compatible, or that a function is
1742 not declared with a parameter of type `void'.
1743
1744 The parser invokes routines elsewhere in the compiler to perform
1745 semantic analysis and to build up the abstract syntax tree for the
1746 code processed.
1747
1748 The parser (and the template instantiation code, which is, in a
1749 way, a close relative of parsing) are the only parts of the
1750 compiler that should be calling push_scope and pop_scope, or
1751 related functions. The parser (and template instantiation code)
1752 keeps track of what scope is presently active; everything else
1753 should simply honor that. (The code that generates static
1754 initializers may also need to set the scope, in order to check
1755 access control correctly when emitting the initializers.)
1756
1757 Methodology
1758 -----------
1759
1760 The parser is of the standard recursive-descent variety. Upcoming
1761 tokens in the token stream are examined in order to determine which
1762 production to use when parsing a non-terminal. Some C++ constructs
1763 require arbitrary look ahead to disambiguate. For example, it is
1764 impossible, in the general case, to tell whether a statement is an
1765 expression or declaration without scanning the entire statement.
1766 Therefore, the parser is capable of "parsing tentatively." When the
1767 parser is not sure what construct comes next, it enters this mode.
1768 Then, while we attempt to parse the construct, the parser queues up
1769 error messages, rather than issuing them immediately, and saves the
1770 tokens it consumes. If the construct is parsed successfully, the
1771 parser "commits", i.e., it issues any queued error messages and
1772 the tokens that were being preserved are permanently discarded.
1773 If, however, the construct is not parsed successfully, the parser
1774 rolls back its state completely so that it can resume parsing using
1775 a different alternative.
1776
1777 Future Improvements
1778 -------------------
1779
1780 The performance of the parser could probably be improved substantially.
1781 We could often eliminate the need to parse tentatively by looking ahead
1782 a little bit. In some places, this approach might not entirely eliminate
1783 the need to parse tentatively, but it might still speed up the average
1784 case. */
1785
1786 /* Flags that are passed to some parsing functions. These values can
1787 be bitwise-ored together. */
1788
1789 enum
1790 {
1791 /* No flags. */
1792 CP_PARSER_FLAGS_NONE = 0x0,
1793 /* The construct is optional. If it is not present, then no error
1794 should be issued. */
1795 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1796 /* When parsing a type-specifier, treat user-defined type-names
1797 as non-type identifiers. */
1798 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1799 /* When parsing a type-specifier, do not try to parse a class-specifier
1800 or enum-specifier. */
1801 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1802 /* When parsing a decl-specifier-seq, only allow type-specifier or
1803 constexpr. */
1804 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1805 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1806 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1807 /* When parsing a decl-specifier-seq, allow missing typename. */
1808 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20
1809 };
1810
1811 /* This type is used for parameters and variables which hold
1812 combinations of the above flags. */
1813 typedef int cp_parser_flags;
1814
1815 /* The different kinds of declarators we want to parse. */
1816
1817 enum cp_parser_declarator_kind
1818 {
1819 /* We want an abstract declarator. */
1820 CP_PARSER_DECLARATOR_ABSTRACT,
1821 /* We want a named declarator. */
1822 CP_PARSER_DECLARATOR_NAMED,
1823 /* We don't mind, but the name must be an unqualified-id. */
1824 CP_PARSER_DECLARATOR_EITHER
1825 };
1826
1827 /* The precedence values used to parse binary expressions. The minimum value
1828 of PREC must be 1, because zero is reserved to quickly discriminate
1829 binary operators from other tokens. */
1830
1831 enum cp_parser_prec
1832 {
1833 PREC_NOT_OPERATOR,
1834 PREC_LOGICAL_OR_EXPRESSION,
1835 PREC_LOGICAL_AND_EXPRESSION,
1836 PREC_INCLUSIVE_OR_EXPRESSION,
1837 PREC_EXCLUSIVE_OR_EXPRESSION,
1838 PREC_AND_EXPRESSION,
1839 PREC_EQUALITY_EXPRESSION,
1840 PREC_RELATIONAL_EXPRESSION,
1841 PREC_SHIFT_EXPRESSION,
1842 PREC_ADDITIVE_EXPRESSION,
1843 PREC_MULTIPLICATIVE_EXPRESSION,
1844 PREC_PM_EXPRESSION,
1845 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1846 };
1847
1848 /* A mapping from a token type to a corresponding tree node type, with a
1849 precedence value. */
1850
1851 struct cp_parser_binary_operations_map_node
1852 {
1853 /* The token type. */
1854 enum cpp_ttype token_type;
1855 /* The corresponding tree code. */
1856 enum tree_code tree_type;
1857 /* The precedence of this operator. */
1858 enum cp_parser_prec prec;
1859 };
1860
1861 struct cp_parser_expression_stack_entry
1862 {
1863 /* Left hand side of the binary operation we are currently
1864 parsing. */
1865 cp_expr lhs;
1866 /* Original tree code for left hand side, if it was a binary
1867 expression itself (used for -Wparentheses). */
1868 enum tree_code lhs_type;
1869 /* Tree code for the binary operation we are parsing. */
1870 enum tree_code tree_type;
1871 /* Precedence of the binary operation we are parsing. */
1872 enum cp_parser_prec prec;
1873 /* Location of the binary operation we are parsing. */
1874 location_t loc;
1875 };
1876
1877 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1878 entries because precedence levels on the stack are monotonically
1879 increasing. */
1880 typedef struct cp_parser_expression_stack_entry
1881 cp_parser_expression_stack[NUM_PREC_VALUES];
1882
1883 /* Prototypes. */
1884
1885 /* Constructors and destructors. */
1886
1887 static cp_parser_context *cp_parser_context_new
1888 (cp_parser_context *);
1889
1890 /* Class variables. */
1891
1892 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1893
1894 /* The operator-precedence table used by cp_parser_binary_expression.
1895 Transformed into an associative array (binops_by_token) by
1896 cp_parser_new. */
1897
1898 static const cp_parser_binary_operations_map_node binops[] = {
1899 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1900 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1901
1902 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1903 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1904 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1905
1906 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1907 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1908
1909 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1910 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1911
1912 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1913 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1914 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1915 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1916
1917 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1918 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1919
1920 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1921
1922 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1923
1924 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1925
1926 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1927
1928 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1929 };
1930
1931 /* The same as binops, but initialized by cp_parser_new so that
1932 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1933 for speed. */
1934 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1935
1936 /* Constructors and destructors. */
1937
1938 /* Construct a new context. The context below this one on the stack
1939 is given by NEXT. */
1940
1941 static cp_parser_context *
1942 cp_parser_context_new (cp_parser_context* next)
1943 {
1944 cp_parser_context *context;
1945
1946 /* Allocate the storage. */
1947 if (cp_parser_context_free_list != NULL)
1948 {
1949 /* Pull the first entry from the free list. */
1950 context = cp_parser_context_free_list;
1951 cp_parser_context_free_list = context->next;
1952 memset (context, 0, sizeof (*context));
1953 }
1954 else
1955 context = ggc_cleared_alloc<cp_parser_context> ();
1956
1957 /* No errors have occurred yet in this context. */
1958 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1959 /* If this is not the bottommost context, copy information that we
1960 need from the previous context. */
1961 if (next)
1962 {
1963 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1964 expression, then we are parsing one in this context, too. */
1965 context->object_type = next->object_type;
1966 /* Thread the stack. */
1967 context->next = next;
1968 }
1969
1970 return context;
1971 }
1972
1973 /* Managing the unparsed function queues. */
1974
1975 #define unparsed_funs_with_default_args \
1976 parser->unparsed_queues->last ().funs_with_default_args
1977 #define unparsed_funs_with_definitions \
1978 parser->unparsed_queues->last ().funs_with_definitions
1979 #define unparsed_nsdmis \
1980 parser->unparsed_queues->last ().nsdmis
1981 #define unparsed_classes \
1982 parser->unparsed_queues->last ().classes
1983 #define unparsed_noexcepts \
1984 parser->unparsed_queues->last ().noexcepts
1985
1986 static void
1987 push_unparsed_function_queues (cp_parser *parser)
1988 {
1989 cp_unparsed_functions_entry e = { NULL, make_tree_vector (), NULL, NULL,
1990 NULL };
1991 vec_safe_push (parser->unparsed_queues, e);
1992 }
1993
1994 static void
1995 pop_unparsed_function_queues (cp_parser *parser)
1996 {
1997 release_tree_vector (unparsed_funs_with_definitions);
1998 parser->unparsed_queues->pop ();
1999 }
2000
2001 /* Prototypes. */
2002
2003 /* Constructors and destructors. */
2004
2005 static cp_parser *cp_parser_new
2006 (void);
2007
2008 /* Routines to parse various constructs.
2009
2010 Those that return `tree' will return the error_mark_node (rather
2011 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2012 Sometimes, they will return an ordinary node if error-recovery was
2013 attempted, even though a parse error occurred. So, to check
2014 whether or not a parse error occurred, you should always use
2015 cp_parser_error_occurred. If the construct is optional (indicated
2016 either by an `_opt' in the name of the function that does the
2017 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2018 the construct is not present. */
2019
2020 /* Lexical conventions [gram.lex] */
2021
2022 static cp_expr cp_parser_identifier
2023 (cp_parser *);
2024 static cp_expr cp_parser_string_literal
2025 (cp_parser *, bool, bool, bool);
2026 static cp_expr cp_parser_userdef_char_literal
2027 (cp_parser *);
2028 static tree cp_parser_userdef_string_literal
2029 (tree);
2030 static cp_expr cp_parser_userdef_numeric_literal
2031 (cp_parser *);
2032
2033 /* Basic concepts [gram.basic] */
2034
2035 static void cp_parser_translation_unit (cp_parser *);
2036
2037 /* Expressions [gram.expr] */
2038
2039 static cp_expr cp_parser_primary_expression
2040 (cp_parser *, bool, bool, bool, cp_id_kind *);
2041 static cp_expr cp_parser_id_expression
2042 (cp_parser *, bool, bool, bool *, bool, bool);
2043 static cp_expr cp_parser_unqualified_id
2044 (cp_parser *, bool, bool, bool, bool);
2045 static tree cp_parser_nested_name_specifier_opt
2046 (cp_parser *, bool, bool, bool, bool, bool = false);
2047 static tree cp_parser_nested_name_specifier
2048 (cp_parser *, bool, bool, bool, bool);
2049 static tree cp_parser_qualifying_entity
2050 (cp_parser *, bool, bool, bool, bool, bool);
2051 static cp_expr cp_parser_postfix_expression
2052 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2053 static tree cp_parser_postfix_open_square_expression
2054 (cp_parser *, tree, bool, bool);
2055 static tree cp_parser_postfix_dot_deref_expression
2056 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2057 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2058 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2059 bool = false);
2060 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2061 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2062 static void cp_parser_pseudo_destructor_name
2063 (cp_parser *, tree, tree *, tree *);
2064 static cp_expr cp_parser_unary_expression
2065 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2066 static enum tree_code cp_parser_unary_operator
2067 (cp_token *);
2068 static tree cp_parser_has_attribute_expression
2069 (cp_parser *);
2070 static tree cp_parser_new_expression
2071 (cp_parser *);
2072 static vec<tree, va_gc> *cp_parser_new_placement
2073 (cp_parser *);
2074 static tree cp_parser_new_type_id
2075 (cp_parser *, tree *);
2076 static cp_declarator *cp_parser_new_declarator_opt
2077 (cp_parser *);
2078 static cp_declarator *cp_parser_direct_new_declarator
2079 (cp_parser *);
2080 static vec<tree, va_gc> *cp_parser_new_initializer
2081 (cp_parser *);
2082 static tree cp_parser_delete_expression
2083 (cp_parser *);
2084 static cp_expr cp_parser_cast_expression
2085 (cp_parser *, bool, bool, bool, cp_id_kind *);
2086 static cp_expr cp_parser_binary_expression
2087 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2088 static tree cp_parser_question_colon_clause
2089 (cp_parser *, cp_expr);
2090 static cp_expr cp_parser_assignment_expression
2091 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2092 static enum tree_code cp_parser_assignment_operator_opt
2093 (cp_parser *);
2094 static cp_expr cp_parser_expression
2095 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2096 static cp_expr cp_parser_constant_expression
2097 (cp_parser *, bool = false, bool * = NULL, bool = false);
2098 static cp_expr cp_parser_builtin_offsetof
2099 (cp_parser *);
2100 static cp_expr cp_parser_lambda_expression
2101 (cp_parser *);
2102 static void cp_parser_lambda_introducer
2103 (cp_parser *, tree);
2104 static bool cp_parser_lambda_declarator_opt
2105 (cp_parser *, tree);
2106 static void cp_parser_lambda_body
2107 (cp_parser *, tree);
2108
2109 /* Statements [gram.stmt.stmt] */
2110
2111 static void cp_parser_statement
2112 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2113 static void cp_parser_label_for_labeled_statement
2114 (cp_parser *, tree);
2115 static tree cp_parser_expression_statement
2116 (cp_parser *, tree);
2117 static tree cp_parser_compound_statement
2118 (cp_parser *, tree, int, bool);
2119 static void cp_parser_statement_seq_opt
2120 (cp_parser *, tree);
2121 static tree cp_parser_selection_statement
2122 (cp_parser *, bool *, vec<tree> *);
2123 static tree cp_parser_condition
2124 (cp_parser *);
2125 static tree cp_parser_iteration_statement
2126 (cp_parser *, bool *, bool, unsigned short);
2127 static bool cp_parser_init_statement
2128 (cp_parser *, tree *decl);
2129 static tree cp_parser_for
2130 (cp_parser *, bool, unsigned short);
2131 static tree cp_parser_c_for
2132 (cp_parser *, tree, tree, bool, unsigned short);
2133 static tree cp_parser_range_for
2134 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2135 static void do_range_for_auto_deduction
2136 (tree, tree);
2137 static tree cp_parser_perform_range_for_lookup
2138 (tree, tree *, tree *);
2139 static tree cp_parser_range_for_member_function
2140 (tree, tree);
2141 static tree cp_parser_jump_statement
2142 (cp_parser *);
2143 static void cp_parser_declaration_statement
2144 (cp_parser *);
2145
2146 static tree cp_parser_implicitly_scoped_statement
2147 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2148 static void cp_parser_already_scoped_statement
2149 (cp_parser *, bool *, const token_indent_info &);
2150
2151 /* Declarations [gram.dcl.dcl] */
2152
2153 static void cp_parser_declaration_seq_opt
2154 (cp_parser *);
2155 static void cp_parser_declaration
2156 (cp_parser *);
2157 static void cp_parser_toplevel_declaration
2158 (cp_parser *);
2159 static void cp_parser_block_declaration
2160 (cp_parser *, bool);
2161 static void cp_parser_simple_declaration
2162 (cp_parser *, bool, tree *);
2163 static void cp_parser_decl_specifier_seq
2164 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2165 static tree cp_parser_storage_class_specifier_opt
2166 (cp_parser *);
2167 static tree cp_parser_function_specifier_opt
2168 (cp_parser *, cp_decl_specifier_seq *);
2169 static tree cp_parser_type_specifier
2170 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2171 int *, bool *);
2172 static tree cp_parser_simple_type_specifier
2173 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2174 static tree cp_parser_type_name
2175 (cp_parser *, bool);
2176 static tree cp_parser_nonclass_name
2177 (cp_parser* parser);
2178 static tree cp_parser_elaborated_type_specifier
2179 (cp_parser *, bool, bool);
2180 static tree cp_parser_enum_specifier
2181 (cp_parser *);
2182 static void cp_parser_enumerator_list
2183 (cp_parser *, tree);
2184 static void cp_parser_enumerator_definition
2185 (cp_parser *, tree);
2186 static tree cp_parser_namespace_name
2187 (cp_parser *);
2188 static void cp_parser_namespace_definition
2189 (cp_parser *);
2190 static void cp_parser_namespace_body
2191 (cp_parser *);
2192 static tree cp_parser_qualified_namespace_specifier
2193 (cp_parser *);
2194 static void cp_parser_namespace_alias_definition
2195 (cp_parser *);
2196 static bool cp_parser_using_declaration
2197 (cp_parser *, bool);
2198 static void cp_parser_using_directive
2199 (cp_parser *);
2200 static tree cp_parser_alias_declaration
2201 (cp_parser *);
2202 static void cp_parser_asm_definition
2203 (cp_parser *);
2204 static void cp_parser_linkage_specification
2205 (cp_parser *);
2206 static void cp_parser_static_assert
2207 (cp_parser *, bool);
2208 static tree cp_parser_decltype
2209 (cp_parser *);
2210 static tree cp_parser_decomposition_declaration
2211 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2212
2213 /* Declarators [gram.dcl.decl] */
2214
2215 static tree cp_parser_init_declarator
2216 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2217 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2218 location_t *, tree *);
2219 static cp_declarator *cp_parser_declarator
2220 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2221 bool, bool, bool);
2222 static cp_declarator *cp_parser_direct_declarator
2223 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2224 bool);
2225 static enum tree_code cp_parser_ptr_operator
2226 (cp_parser *, tree *, cp_cv_quals *, tree *);
2227 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2228 (cp_parser *);
2229 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2230 (cp_parser *);
2231 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2232 (cp_parser *);
2233 static tree cp_parser_tx_qualifier_opt
2234 (cp_parser *);
2235 static tree cp_parser_late_return_type_opt
2236 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2237 static tree cp_parser_declarator_id
2238 (cp_parser *, bool);
2239 static tree cp_parser_type_id
2240 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2241 static tree cp_parser_template_type_arg
2242 (cp_parser *);
2243 static tree cp_parser_trailing_type_id (cp_parser *);
2244 static tree cp_parser_type_id_1
2245 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2246 static void cp_parser_type_specifier_seq
2247 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2248 static tree cp_parser_parameter_declaration_clause
2249 (cp_parser *, cp_parser_flags);
2250 static tree cp_parser_parameter_declaration_list
2251 (cp_parser *, cp_parser_flags);
2252 static cp_parameter_declarator *cp_parser_parameter_declaration
2253 (cp_parser *, cp_parser_flags, bool, bool *);
2254 static tree cp_parser_default_argument
2255 (cp_parser *, bool);
2256 static void cp_parser_function_body
2257 (cp_parser *, bool);
2258 static tree cp_parser_initializer
2259 (cp_parser *, bool *, bool *, bool = false);
2260 static cp_expr cp_parser_initializer_clause
2261 (cp_parser *, bool *);
2262 static cp_expr cp_parser_braced_list
2263 (cp_parser*, bool*);
2264 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2265 (cp_parser *, bool *, bool *);
2266
2267 static void cp_parser_ctor_initializer_opt_and_function_body
2268 (cp_parser *, bool);
2269
2270 static tree cp_parser_late_parsing_omp_declare_simd
2271 (cp_parser *, tree);
2272
2273 static tree cp_parser_late_parsing_oacc_routine
2274 (cp_parser *, tree);
2275
2276 static tree synthesize_implicit_template_parm
2277 (cp_parser *, tree);
2278 static tree finish_fully_implicit_template
2279 (cp_parser *, tree);
2280 static void abort_fully_implicit_template
2281 (cp_parser *);
2282
2283 /* Classes [gram.class] */
2284
2285 static tree cp_parser_class_name
2286 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2287 static tree cp_parser_class_specifier
2288 (cp_parser *);
2289 static tree cp_parser_class_head
2290 (cp_parser *, bool *);
2291 static enum tag_types cp_parser_class_key
2292 (cp_parser *);
2293 static void cp_parser_type_parameter_key
2294 (cp_parser* parser);
2295 static void cp_parser_member_specification_opt
2296 (cp_parser *);
2297 static void cp_parser_member_declaration
2298 (cp_parser *);
2299 static tree cp_parser_pure_specifier
2300 (cp_parser *);
2301 static tree cp_parser_constant_initializer
2302 (cp_parser *);
2303
2304 /* Derived classes [gram.class.derived] */
2305
2306 static tree cp_parser_base_clause
2307 (cp_parser *);
2308 static tree cp_parser_base_specifier
2309 (cp_parser *);
2310
2311 /* Special member functions [gram.special] */
2312
2313 static tree cp_parser_conversion_function_id
2314 (cp_parser *);
2315 static tree cp_parser_conversion_type_id
2316 (cp_parser *);
2317 static cp_declarator *cp_parser_conversion_declarator_opt
2318 (cp_parser *);
2319 static void cp_parser_ctor_initializer_opt
2320 (cp_parser *);
2321 static void cp_parser_mem_initializer_list
2322 (cp_parser *);
2323 static tree cp_parser_mem_initializer
2324 (cp_parser *);
2325 static tree cp_parser_mem_initializer_id
2326 (cp_parser *);
2327
2328 /* Overloading [gram.over] */
2329
2330 static cp_expr cp_parser_operator_function_id
2331 (cp_parser *);
2332 static cp_expr cp_parser_operator
2333 (cp_parser *, location_t);
2334
2335 /* Templates [gram.temp] */
2336
2337 static void cp_parser_template_declaration
2338 (cp_parser *, bool);
2339 static tree cp_parser_template_parameter_list
2340 (cp_parser *);
2341 static tree cp_parser_template_parameter
2342 (cp_parser *, bool *, bool *);
2343 static tree cp_parser_type_parameter
2344 (cp_parser *, bool *);
2345 static tree cp_parser_template_id
2346 (cp_parser *, bool, bool, enum tag_types, bool);
2347 static tree cp_parser_template_name
2348 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2349 static tree cp_parser_template_argument_list
2350 (cp_parser *);
2351 static tree cp_parser_template_argument
2352 (cp_parser *);
2353 static void cp_parser_explicit_instantiation
2354 (cp_parser *);
2355 static void cp_parser_explicit_specialization
2356 (cp_parser *);
2357
2358 /* Exception handling [gram.exception] */
2359
2360 static tree cp_parser_try_block
2361 (cp_parser *);
2362 static void cp_parser_function_try_block
2363 (cp_parser *);
2364 static void cp_parser_handler_seq
2365 (cp_parser *);
2366 static void cp_parser_handler
2367 (cp_parser *);
2368 static tree cp_parser_exception_declaration
2369 (cp_parser *);
2370 static tree cp_parser_throw_expression
2371 (cp_parser *);
2372 static tree cp_parser_exception_specification_opt
2373 (cp_parser *, bool = false);
2374 static tree cp_parser_type_id_list
2375 (cp_parser *);
2376
2377 /* GNU Extensions */
2378
2379 static tree cp_parser_asm_specification_opt
2380 (cp_parser *);
2381 static tree cp_parser_asm_operand_list
2382 (cp_parser *);
2383 static tree cp_parser_asm_clobber_list
2384 (cp_parser *);
2385 static tree cp_parser_asm_label_list
2386 (cp_parser *);
2387 static bool cp_next_tokens_can_be_attribute_p
2388 (cp_parser *);
2389 static bool cp_next_tokens_can_be_gnu_attribute_p
2390 (cp_parser *);
2391 static bool cp_next_tokens_can_be_std_attribute_p
2392 (cp_parser *);
2393 static bool cp_nth_tokens_can_be_std_attribute_p
2394 (cp_parser *, size_t);
2395 static bool cp_nth_tokens_can_be_gnu_attribute_p
2396 (cp_parser *, size_t);
2397 static bool cp_nth_tokens_can_be_attribute_p
2398 (cp_parser *, size_t);
2399 static tree cp_parser_attributes_opt
2400 (cp_parser *);
2401 static tree cp_parser_gnu_attributes_opt
2402 (cp_parser *);
2403 static tree cp_parser_gnu_attribute_list
2404 (cp_parser *, bool = false);
2405 static tree cp_parser_std_attribute
2406 (cp_parser *, tree);
2407 static tree cp_parser_std_attribute_spec
2408 (cp_parser *);
2409 static tree cp_parser_std_attribute_spec_seq
2410 (cp_parser *);
2411 static size_t cp_parser_skip_attributes_opt
2412 (cp_parser *, size_t);
2413 static bool cp_parser_extension_opt
2414 (cp_parser *, int *);
2415 static void cp_parser_label_declaration
2416 (cp_parser *);
2417
2418 /* Concept Extensions */
2419
2420 static tree cp_parser_requires_clause
2421 (cp_parser *);
2422 static tree cp_parser_requires_clause_opt
2423 (cp_parser *);
2424 static tree cp_parser_requires_expression
2425 (cp_parser *);
2426 static tree cp_parser_requirement_parameter_list
2427 (cp_parser *);
2428 static tree cp_parser_requirement_body
2429 (cp_parser *);
2430 static tree cp_parser_requirement_list
2431 (cp_parser *);
2432 static tree cp_parser_requirement
2433 (cp_parser *);
2434 static tree cp_parser_simple_requirement
2435 (cp_parser *);
2436 static tree cp_parser_compound_requirement
2437 (cp_parser *);
2438 static tree cp_parser_type_requirement
2439 (cp_parser *);
2440 static tree cp_parser_nested_requirement
2441 (cp_parser *);
2442
2443 /* Transactional Memory Extensions */
2444
2445 static tree cp_parser_transaction
2446 (cp_parser *, cp_token *);
2447 static tree cp_parser_transaction_expression
2448 (cp_parser *, enum rid);
2449 static void cp_parser_function_transaction
2450 (cp_parser *, enum rid);
2451 static tree cp_parser_transaction_cancel
2452 (cp_parser *);
2453
2454 enum pragma_context {
2455 pragma_external,
2456 pragma_member,
2457 pragma_objc_icode,
2458 pragma_stmt,
2459 pragma_compound
2460 };
2461 static bool cp_parser_pragma
2462 (cp_parser *, enum pragma_context, bool *);
2463
2464 /* Objective-C++ Productions */
2465
2466 static tree cp_parser_objc_message_receiver
2467 (cp_parser *);
2468 static tree cp_parser_objc_message_args
2469 (cp_parser *);
2470 static tree cp_parser_objc_message_expression
2471 (cp_parser *);
2472 static cp_expr cp_parser_objc_encode_expression
2473 (cp_parser *);
2474 static tree cp_parser_objc_defs_expression
2475 (cp_parser *);
2476 static tree cp_parser_objc_protocol_expression
2477 (cp_parser *);
2478 static tree cp_parser_objc_selector_expression
2479 (cp_parser *);
2480 static cp_expr cp_parser_objc_expression
2481 (cp_parser *);
2482 static bool cp_parser_objc_selector_p
2483 (enum cpp_ttype);
2484 static tree cp_parser_objc_selector
2485 (cp_parser *);
2486 static tree cp_parser_objc_protocol_refs_opt
2487 (cp_parser *);
2488 static void cp_parser_objc_declaration
2489 (cp_parser *, tree);
2490 static tree cp_parser_objc_statement
2491 (cp_parser *);
2492 static bool cp_parser_objc_valid_prefix_attributes
2493 (cp_parser *, tree *);
2494 static void cp_parser_objc_at_property_declaration
2495 (cp_parser *) ;
2496 static void cp_parser_objc_at_synthesize_declaration
2497 (cp_parser *) ;
2498 static void cp_parser_objc_at_dynamic_declaration
2499 (cp_parser *) ;
2500 static tree cp_parser_objc_struct_declaration
2501 (cp_parser *) ;
2502
2503 /* Utility Routines */
2504
2505 static cp_expr cp_parser_lookup_name
2506 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2507 static tree cp_parser_lookup_name_simple
2508 (cp_parser *, tree, location_t);
2509 static tree cp_parser_maybe_treat_template_as_class
2510 (tree, bool);
2511 static bool cp_parser_check_declarator_template_parameters
2512 (cp_parser *, cp_declarator *, location_t);
2513 static bool cp_parser_check_template_parameters
2514 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2515 static cp_expr cp_parser_simple_cast_expression
2516 (cp_parser *);
2517 static tree cp_parser_global_scope_opt
2518 (cp_parser *, bool);
2519 static bool cp_parser_constructor_declarator_p
2520 (cp_parser *, cp_parser_flags, bool);
2521 static tree cp_parser_function_definition_from_specifiers_and_declarator
2522 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2523 static tree cp_parser_function_definition_after_declarator
2524 (cp_parser *, bool);
2525 static bool cp_parser_template_declaration_after_export
2526 (cp_parser *, bool);
2527 static void cp_parser_perform_template_parameter_access_checks
2528 (vec<deferred_access_check, va_gc> *);
2529 static tree cp_parser_single_declaration
2530 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2531 static cp_expr cp_parser_functional_cast
2532 (cp_parser *, tree);
2533 static tree cp_parser_save_member_function_body
2534 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2535 static tree cp_parser_save_nsdmi
2536 (cp_parser *);
2537 static tree cp_parser_enclosed_template_argument_list
2538 (cp_parser *);
2539 static void cp_parser_save_default_args
2540 (cp_parser *, tree);
2541 static void cp_parser_late_parsing_for_member
2542 (cp_parser *, tree);
2543 static tree cp_parser_late_parse_one_default_arg
2544 (cp_parser *, tree, tree, tree);
2545 static void cp_parser_late_parsing_nsdmi
2546 (cp_parser *, tree);
2547 static void cp_parser_late_parsing_default_args
2548 (cp_parser *, tree);
2549 static tree cp_parser_sizeof_operand
2550 (cp_parser *, enum rid);
2551 static cp_expr cp_parser_trait_expr
2552 (cp_parser *, enum rid);
2553 static bool cp_parser_declares_only_class_p
2554 (cp_parser *);
2555 static void cp_parser_set_storage_class
2556 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2557 static void cp_parser_set_decl_spec_type
2558 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2559 static void set_and_check_decl_spec_loc
2560 (cp_decl_specifier_seq *decl_specs,
2561 cp_decl_spec ds, cp_token *);
2562 static bool cp_parser_friend_p
2563 (const cp_decl_specifier_seq *);
2564 static void cp_parser_required_error
2565 (cp_parser *, required_token, bool, location_t);
2566 static cp_token *cp_parser_require
2567 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2568 static cp_token *cp_parser_require_keyword
2569 (cp_parser *, enum rid, required_token);
2570 static bool cp_parser_token_starts_function_definition_p
2571 (cp_token *);
2572 static bool cp_parser_next_token_starts_class_definition_p
2573 (cp_parser *);
2574 static bool cp_parser_next_token_ends_template_argument_p
2575 (cp_parser *);
2576 static bool cp_parser_nth_token_starts_template_argument_list_p
2577 (cp_parser *, size_t);
2578 static enum tag_types cp_parser_token_is_class_key
2579 (cp_token *);
2580 static enum tag_types cp_parser_token_is_type_parameter_key
2581 (cp_token *);
2582 static void cp_parser_check_class_key
2583 (enum tag_types, tree type);
2584 static void cp_parser_check_access_in_redeclaration
2585 (tree type, location_t location);
2586 static bool cp_parser_optional_template_keyword
2587 (cp_parser *);
2588 static void cp_parser_pre_parsed_nested_name_specifier
2589 (cp_parser *);
2590 static bool cp_parser_cache_group
2591 (cp_parser *, enum cpp_ttype, unsigned);
2592 static tree cp_parser_cache_defarg
2593 (cp_parser *parser, bool nsdmi);
2594 static void cp_parser_parse_tentatively
2595 (cp_parser *);
2596 static void cp_parser_commit_to_tentative_parse
2597 (cp_parser *);
2598 static void cp_parser_commit_to_topmost_tentative_parse
2599 (cp_parser *);
2600 static void cp_parser_abort_tentative_parse
2601 (cp_parser *);
2602 static bool cp_parser_parse_definitely
2603 (cp_parser *);
2604 static inline bool cp_parser_parsing_tentatively
2605 (cp_parser *);
2606 static bool cp_parser_uncommitted_to_tentative_parse_p
2607 (cp_parser *);
2608 static void cp_parser_error
2609 (cp_parser *, const char *);
2610 static void cp_parser_name_lookup_error
2611 (cp_parser *, tree, tree, name_lookup_error, location_t);
2612 static bool cp_parser_simulate_error
2613 (cp_parser *);
2614 static bool cp_parser_check_type_definition
2615 (cp_parser *);
2616 static void cp_parser_check_for_definition_in_return_type
2617 (cp_declarator *, tree, location_t type_location);
2618 static void cp_parser_check_for_invalid_template_id
2619 (cp_parser *, tree, enum tag_types, location_t location);
2620 static bool cp_parser_non_integral_constant_expression
2621 (cp_parser *, non_integral_constant);
2622 static void cp_parser_diagnose_invalid_type_name
2623 (cp_parser *, tree, location_t);
2624 static bool cp_parser_parse_and_diagnose_invalid_type_name
2625 (cp_parser *);
2626 static int cp_parser_skip_to_closing_parenthesis
2627 (cp_parser *, bool, bool, bool);
2628 static void cp_parser_skip_to_end_of_statement
2629 (cp_parser *);
2630 static void cp_parser_consume_semicolon_at_end_of_statement
2631 (cp_parser *);
2632 static void cp_parser_skip_to_end_of_block_or_statement
2633 (cp_parser *);
2634 static bool cp_parser_skip_to_closing_brace
2635 (cp_parser *);
2636 static void cp_parser_skip_to_end_of_template_parameter_list
2637 (cp_parser *);
2638 static void cp_parser_skip_to_pragma_eol
2639 (cp_parser*, cp_token *);
2640 static bool cp_parser_error_occurred
2641 (cp_parser *);
2642 static bool cp_parser_allow_gnu_extensions_p
2643 (cp_parser *);
2644 static bool cp_parser_is_pure_string_literal
2645 (cp_token *);
2646 static bool cp_parser_is_string_literal
2647 (cp_token *);
2648 static bool cp_parser_is_keyword
2649 (cp_token *, enum rid);
2650 static tree cp_parser_make_typename_type
2651 (cp_parser *, tree, location_t location);
2652 static cp_declarator * cp_parser_make_indirect_declarator
2653 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2654 static bool cp_parser_compound_literal_p
2655 (cp_parser *);
2656 static bool cp_parser_array_designator_p
2657 (cp_parser *);
2658 static bool cp_parser_init_statement_p
2659 (cp_parser *);
2660 static bool cp_parser_skip_to_closing_square_bracket
2661 (cp_parser *);
2662
2663 /* Concept-related syntactic transformations */
2664
2665 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2666 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2667
2668 // -------------------------------------------------------------------------- //
2669 // Unevaluated Operand Guard
2670 //
2671 // Implementation of an RAII helper for unevaluated operand parsing.
2672 cp_unevaluated::cp_unevaluated ()
2673 {
2674 ++cp_unevaluated_operand;
2675 ++c_inhibit_evaluation_warnings;
2676 }
2677
2678 cp_unevaluated::~cp_unevaluated ()
2679 {
2680 --c_inhibit_evaluation_warnings;
2681 --cp_unevaluated_operand;
2682 }
2683
2684 // -------------------------------------------------------------------------- //
2685 // Tentative Parsing
2686
2687 /* Returns nonzero if we are parsing tentatively. */
2688
2689 static inline bool
2690 cp_parser_parsing_tentatively (cp_parser* parser)
2691 {
2692 return parser->context->next != NULL;
2693 }
2694
2695 /* Returns nonzero if TOKEN is a string literal. */
2696
2697 static bool
2698 cp_parser_is_pure_string_literal (cp_token* token)
2699 {
2700 return (token->type == CPP_STRING ||
2701 token->type == CPP_STRING16 ||
2702 token->type == CPP_STRING32 ||
2703 token->type == CPP_WSTRING ||
2704 token->type == CPP_UTF8STRING);
2705 }
2706
2707 /* Returns nonzero if TOKEN is a string literal
2708 of a user-defined string literal. */
2709
2710 static bool
2711 cp_parser_is_string_literal (cp_token* token)
2712 {
2713 return (cp_parser_is_pure_string_literal (token) ||
2714 token->type == CPP_STRING_USERDEF ||
2715 token->type == CPP_STRING16_USERDEF ||
2716 token->type == CPP_STRING32_USERDEF ||
2717 token->type == CPP_WSTRING_USERDEF ||
2718 token->type == CPP_UTF8STRING_USERDEF);
2719 }
2720
2721 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2722
2723 static bool
2724 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2725 {
2726 return token->keyword == keyword;
2727 }
2728
2729 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2730 PRAGMA_NONE. */
2731
2732 static enum pragma_kind
2733 cp_parser_pragma_kind (cp_token *token)
2734 {
2735 if (token->type != CPP_PRAGMA)
2736 return PRAGMA_NONE;
2737 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2738 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2739 }
2740
2741 /* Helper function for cp_parser_error.
2742 Having peeked a token of kind TOK1_KIND that might signify
2743 a conflict marker, peek successor tokens to determine
2744 if we actually do have a conflict marker.
2745 Specifically, we consider a run of 7 '<', '=' or '>' characters
2746 at the start of a line as a conflict marker.
2747 These come through the lexer as three pairs and a single,
2748 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2749 If it returns true, *OUT_LOC is written to with the location/range
2750 of the marker. */
2751
2752 static bool
2753 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2754 location_t *out_loc)
2755 {
2756 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2757 if (token2->type != tok1_kind)
2758 return false;
2759 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2760 if (token3->type != tok1_kind)
2761 return false;
2762 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2763 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2764 return false;
2765
2766 /* It must be at the start of the line. */
2767 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2768 if (LOCATION_COLUMN (start_loc) != 1)
2769 return false;
2770
2771 /* We have a conflict marker. Construct a location of the form:
2772 <<<<<<<
2773 ^~~~~~~
2774 with start == caret, finishing at the end of the marker. */
2775 location_t finish_loc = get_finish (token4->location);
2776 *out_loc = make_location (start_loc, start_loc, finish_loc);
2777
2778 return true;
2779 }
2780
2781 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2782 RT_CLOSE_PAREN. */
2783
2784 static const char *
2785 get_matching_symbol (required_token token_desc)
2786 {
2787 switch (token_desc)
2788 {
2789 default:
2790 gcc_unreachable ();
2791 return "";
2792 case RT_CLOSE_BRACE:
2793 return "{";
2794 case RT_CLOSE_PAREN:
2795 return "(";
2796 }
2797 }
2798
2799 /* Attempt to convert TOKEN_DESC from a required_token to an
2800 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2801
2802 static enum cpp_ttype
2803 get_required_cpp_ttype (required_token token_desc)
2804 {
2805 switch (token_desc)
2806 {
2807 case RT_SEMICOLON:
2808 return CPP_SEMICOLON;
2809 case RT_OPEN_PAREN:
2810 return CPP_OPEN_PAREN;
2811 case RT_CLOSE_BRACE:
2812 return CPP_CLOSE_BRACE;
2813 case RT_OPEN_BRACE:
2814 return CPP_OPEN_BRACE;
2815 case RT_CLOSE_SQUARE:
2816 return CPP_CLOSE_SQUARE;
2817 case RT_OPEN_SQUARE:
2818 return CPP_OPEN_SQUARE;
2819 case RT_COMMA:
2820 return CPP_COMMA;
2821 case RT_COLON:
2822 return CPP_COLON;
2823 case RT_CLOSE_PAREN:
2824 return CPP_CLOSE_PAREN;
2825
2826 default:
2827 /* Use CPP_EOF as a "no completions possible" code. */
2828 return CPP_EOF;
2829 }
2830 }
2831
2832
2833 /* Subroutine of cp_parser_error and cp_parser_required_error.
2834
2835 Issue a diagnostic of the form
2836 FILE:LINE: MESSAGE before TOKEN
2837 where TOKEN is the next token in the input stream. MESSAGE
2838 (specified by the caller) is usually of the form "expected
2839 OTHER-TOKEN".
2840
2841 This bypasses the check for tentative passing, and potentially
2842 adds material needed by cp_parser_required_error.
2843
2844 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2845 suggesting insertion of the missing token.
2846
2847 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2848 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2849 location. */
2850
2851 static void
2852 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2853 required_token missing_token_desc,
2854 location_t matching_location)
2855 {
2856 cp_token *token = cp_lexer_peek_token (parser->lexer);
2857 /* This diagnostic makes more sense if it is tagged to the line
2858 of the token we just peeked at. */
2859 cp_lexer_set_source_position_from_token (token);
2860
2861 if (token->type == CPP_PRAGMA)
2862 {
2863 error_at (token->location,
2864 "%<#pragma%> is not allowed here");
2865 cp_parser_skip_to_pragma_eol (parser, token);
2866 return;
2867 }
2868
2869 /* If this is actually a conflict marker, report it as such. */
2870 if (token->type == CPP_LSHIFT
2871 || token->type == CPP_RSHIFT
2872 || token->type == CPP_EQ_EQ)
2873 {
2874 location_t loc;
2875 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2876 {
2877 error_at (loc, "version control conflict marker in file");
2878 expanded_location token_exploc = expand_location (token->location);
2879 /* Consume tokens until the end of the source line. */
2880 while (1)
2881 {
2882 cp_lexer_consume_token (parser->lexer);
2883 cp_token *next = cp_lexer_peek_token (parser->lexer);
2884 if (next == NULL)
2885 break;
2886 expanded_location next_exploc = expand_location (next->location);
2887 if (next_exploc.file != token_exploc.file)
2888 break;
2889 if (next_exploc.line != token_exploc.line)
2890 break;
2891 }
2892 return;
2893 }
2894 }
2895
2896 gcc_rich_location richloc (input_location);
2897
2898 bool added_matching_location = false;
2899
2900 if (missing_token_desc != RT_NONE)
2901 {
2902 /* Potentially supply a fix-it hint, suggesting to add the
2903 missing token immediately after the *previous* token.
2904 This may move the primary location within richloc. */
2905 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2906 location_t prev_token_loc
2907 = cp_lexer_previous_token (parser->lexer)->location;
2908 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2909
2910 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2911 Attempt to consolidate diagnostics by printing it as a
2912 secondary range within the main diagnostic. */
2913 if (matching_location != UNKNOWN_LOCATION)
2914 added_matching_location
2915 = richloc.add_location_if_nearby (matching_location);
2916 }
2917
2918 /* Actually emit the error. */
2919 c_parse_error (gmsgid,
2920 /* Because c_parser_error does not understand
2921 CPP_KEYWORD, keywords are treated like
2922 identifiers. */
2923 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2924 token->u.value, token->flags, &richloc);
2925
2926 if (missing_token_desc != RT_NONE)
2927 {
2928 /* If we weren't able to consolidate matching_location, then
2929 print it as a secondary diagnostic. */
2930 if (matching_location != UNKNOWN_LOCATION
2931 && !added_matching_location)
2932 inform (matching_location, "to match this %qs",
2933 get_matching_symbol (missing_token_desc));
2934 }
2935 }
2936
2937 /* If not parsing tentatively, issue a diagnostic of the form
2938 FILE:LINE: MESSAGE before TOKEN
2939 where TOKEN is the next token in the input stream. MESSAGE
2940 (specified by the caller) is usually of the form "expected
2941 OTHER-TOKEN". */
2942
2943 static void
2944 cp_parser_error (cp_parser* parser, const char* gmsgid)
2945 {
2946 if (!cp_parser_simulate_error (parser))
2947 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2948 }
2949
2950 /* Issue an error about name-lookup failing. NAME is the
2951 IDENTIFIER_NODE DECL is the result of
2952 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2953 the thing that we hoped to find. */
2954
2955 static void
2956 cp_parser_name_lookup_error (cp_parser* parser,
2957 tree name,
2958 tree decl,
2959 name_lookup_error desired,
2960 location_t location)
2961 {
2962 /* If name lookup completely failed, tell the user that NAME was not
2963 declared. */
2964 if (decl == error_mark_node)
2965 {
2966 if (parser->scope && parser->scope != global_namespace)
2967 error_at (location, "%<%E::%E%> has not been declared",
2968 parser->scope, name);
2969 else if (parser->scope == global_namespace)
2970 error_at (location, "%<::%E%> has not been declared", name);
2971 else if (parser->object_scope
2972 && !CLASS_TYPE_P (parser->object_scope))
2973 error_at (location, "request for member %qE in non-class type %qT",
2974 name, parser->object_scope);
2975 else if (parser->object_scope)
2976 error_at (location, "%<%T::%E%> has not been declared",
2977 parser->object_scope, name);
2978 else
2979 error_at (location, "%qE has not been declared", name);
2980 }
2981 else if (parser->scope && parser->scope != global_namespace)
2982 {
2983 switch (desired)
2984 {
2985 case NLE_TYPE:
2986 error_at (location, "%<%E::%E%> is not a type",
2987 parser->scope, name);
2988 break;
2989 case NLE_CXX98:
2990 error_at (location, "%<%E::%E%> is not a class or namespace",
2991 parser->scope, name);
2992 break;
2993 case NLE_NOT_CXX98:
2994 error_at (location,
2995 "%<%E::%E%> is not a class, namespace, or enumeration",
2996 parser->scope, name);
2997 break;
2998 default:
2999 gcc_unreachable ();
3000
3001 }
3002 }
3003 else if (parser->scope == global_namespace)
3004 {
3005 switch (desired)
3006 {
3007 case NLE_TYPE:
3008 error_at (location, "%<::%E%> is not a type", name);
3009 break;
3010 case NLE_CXX98:
3011 error_at (location, "%<::%E%> is not a class or namespace", name);
3012 break;
3013 case NLE_NOT_CXX98:
3014 error_at (location,
3015 "%<::%E%> is not a class, namespace, or enumeration",
3016 name);
3017 break;
3018 default:
3019 gcc_unreachable ();
3020 }
3021 }
3022 else
3023 {
3024 switch (desired)
3025 {
3026 case NLE_TYPE:
3027 error_at (location, "%qE is not a type", name);
3028 break;
3029 case NLE_CXX98:
3030 error_at (location, "%qE is not a class or namespace", name);
3031 break;
3032 case NLE_NOT_CXX98:
3033 error_at (location,
3034 "%qE is not a class, namespace, or enumeration", name);
3035 break;
3036 default:
3037 gcc_unreachable ();
3038 }
3039 }
3040 }
3041
3042 /* If we are parsing tentatively, remember that an error has occurred
3043 during this tentative parse. Returns true if the error was
3044 simulated; false if a message should be issued by the caller. */
3045
3046 static bool
3047 cp_parser_simulate_error (cp_parser* parser)
3048 {
3049 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3050 {
3051 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3052 return true;
3053 }
3054 return false;
3055 }
3056
3057 /* This function is called when a type is defined. If type
3058 definitions are forbidden at this point, an error message is
3059 issued. */
3060
3061 static bool
3062 cp_parser_check_type_definition (cp_parser* parser)
3063 {
3064 /* If types are forbidden here, issue a message. */
3065 if (parser->type_definition_forbidden_message)
3066 {
3067 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3068 or %qs in the message need to be interpreted. */
3069 error (parser->type_definition_forbidden_message,
3070 parser->type_definition_forbidden_message_arg);
3071 return false;
3072 }
3073 return true;
3074 }
3075
3076 /* This function is called when the DECLARATOR is processed. The TYPE
3077 was a type defined in the decl-specifiers. If it is invalid to
3078 define a type in the decl-specifiers for DECLARATOR, an error is
3079 issued. TYPE_LOCATION is the location of TYPE and is used
3080 for error reporting. */
3081
3082 static void
3083 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3084 tree type, location_t type_location)
3085 {
3086 /* [dcl.fct] forbids type definitions in return types.
3087 Unfortunately, it's not easy to know whether or not we are
3088 processing a return type until after the fact. */
3089 while (declarator
3090 && (declarator->kind == cdk_pointer
3091 || declarator->kind == cdk_reference
3092 || declarator->kind == cdk_ptrmem))
3093 declarator = declarator->declarator;
3094 if (declarator
3095 && declarator->kind == cdk_function)
3096 {
3097 error_at (type_location,
3098 "new types may not be defined in a return type");
3099 inform (type_location,
3100 "(perhaps a semicolon is missing after the definition of %qT)",
3101 type);
3102 }
3103 }
3104
3105 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3106 "<" in any valid C++ program. If the next token is indeed "<",
3107 issue a message warning the user about what appears to be an
3108 invalid attempt to form a template-id. LOCATION is the location
3109 of the type-specifier (TYPE) */
3110
3111 static void
3112 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3113 tree type,
3114 enum tag_types tag_type,
3115 location_t location)
3116 {
3117 cp_token_position start = 0;
3118
3119 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3120 {
3121 if (TREE_CODE (type) == TYPE_DECL)
3122 type = TREE_TYPE (type);
3123 if (TYPE_P (type) && !template_placeholder_p (type))
3124 error_at (location, "%qT is not a template", type);
3125 else if (identifier_p (type))
3126 {
3127 if (tag_type != none_type)
3128 error_at (location, "%qE is not a class template", type);
3129 else
3130 error_at (location, "%qE is not a template", type);
3131 }
3132 else
3133 error_at (location, "invalid template-id");
3134 /* Remember the location of the invalid "<". */
3135 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3136 start = cp_lexer_token_position (parser->lexer, true);
3137 /* Consume the "<". */
3138 cp_lexer_consume_token (parser->lexer);
3139 /* Parse the template arguments. */
3140 cp_parser_enclosed_template_argument_list (parser);
3141 /* Permanently remove the invalid template arguments so that
3142 this error message is not issued again. */
3143 if (start)
3144 cp_lexer_purge_tokens_after (parser->lexer, start);
3145 }
3146 }
3147
3148 /* If parsing an integral constant-expression, issue an error message
3149 about the fact that THING appeared and return true. Otherwise,
3150 return false. In either case, set
3151 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3152
3153 static bool
3154 cp_parser_non_integral_constant_expression (cp_parser *parser,
3155 non_integral_constant thing)
3156 {
3157 parser->non_integral_constant_expression_p = true;
3158 if (parser->integral_constant_expression_p)
3159 {
3160 if (!parser->allow_non_integral_constant_expression_p)
3161 {
3162 const char *msg = NULL;
3163 switch (thing)
3164 {
3165 case NIC_FLOAT:
3166 pedwarn (input_location, OPT_Wpedantic,
3167 "ISO C++ forbids using a floating-point literal "
3168 "in a constant-expression");
3169 return true;
3170 case NIC_CAST:
3171 error ("a cast to a type other than an integral or "
3172 "enumeration type cannot appear in a "
3173 "constant-expression");
3174 return true;
3175 case NIC_TYPEID:
3176 error ("%<typeid%> operator "
3177 "cannot appear in a constant-expression");
3178 return true;
3179 case NIC_NCC:
3180 error ("non-constant compound literals "
3181 "cannot appear in a constant-expression");
3182 return true;
3183 case NIC_FUNC_CALL:
3184 error ("a function call "
3185 "cannot appear in a constant-expression");
3186 return true;
3187 case NIC_INC:
3188 error ("an increment "
3189 "cannot appear in a constant-expression");
3190 return true;
3191 case NIC_DEC:
3192 error ("an decrement "
3193 "cannot appear in a constant-expression");
3194 return true;
3195 case NIC_ARRAY_REF:
3196 error ("an array reference "
3197 "cannot appear in a constant-expression");
3198 return true;
3199 case NIC_ADDR_LABEL:
3200 error ("the address of a label "
3201 "cannot appear in a constant-expression");
3202 return true;
3203 case NIC_OVERLOADED:
3204 error ("calls to overloaded operators "
3205 "cannot appear in a constant-expression");
3206 return true;
3207 case NIC_ASSIGNMENT:
3208 error ("an assignment cannot appear in a constant-expression");
3209 return true;
3210 case NIC_COMMA:
3211 error ("a comma operator "
3212 "cannot appear in a constant-expression");
3213 return true;
3214 case NIC_CONSTRUCTOR:
3215 error ("a call to a constructor "
3216 "cannot appear in a constant-expression");
3217 return true;
3218 case NIC_TRANSACTION:
3219 error ("a transaction expression "
3220 "cannot appear in a constant-expression");
3221 return true;
3222 case NIC_THIS:
3223 msg = "this";
3224 break;
3225 case NIC_FUNC_NAME:
3226 msg = "__FUNCTION__";
3227 break;
3228 case NIC_PRETTY_FUNC:
3229 msg = "__PRETTY_FUNCTION__";
3230 break;
3231 case NIC_C99_FUNC:
3232 msg = "__func__";
3233 break;
3234 case NIC_VA_ARG:
3235 msg = "va_arg";
3236 break;
3237 case NIC_ARROW:
3238 msg = "->";
3239 break;
3240 case NIC_POINT:
3241 msg = ".";
3242 break;
3243 case NIC_STAR:
3244 msg = "*";
3245 break;
3246 case NIC_ADDR:
3247 msg = "&";
3248 break;
3249 case NIC_PREINCREMENT:
3250 msg = "++";
3251 break;
3252 case NIC_PREDECREMENT:
3253 msg = "--";
3254 break;
3255 case NIC_NEW:
3256 msg = "new";
3257 break;
3258 case NIC_DEL:
3259 msg = "delete";
3260 break;
3261 default:
3262 gcc_unreachable ();
3263 }
3264 if (msg)
3265 error ("%qs cannot appear in a constant-expression", msg);
3266 return true;
3267 }
3268 }
3269 return false;
3270 }
3271
3272 /* Emit a diagnostic for an invalid type name. This function commits
3273 to the current active tentative parse, if any. (Otherwise, the
3274 problematic construct might be encountered again later, resulting
3275 in duplicate error messages.) LOCATION is the location of ID. */
3276
3277 static void
3278 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3279 location_t location)
3280 {
3281 tree decl, ambiguous_decls;
3282 cp_parser_commit_to_tentative_parse (parser);
3283 /* Try to lookup the identifier. */
3284 decl = cp_parser_lookup_name (parser, id, none_type,
3285 /*is_template=*/false,
3286 /*is_namespace=*/false,
3287 /*check_dependency=*/true,
3288 &ambiguous_decls, location);
3289 if (ambiguous_decls)
3290 /* If the lookup was ambiguous, an error will already have
3291 been issued. */
3292 return;
3293 /* If the lookup found a template-name, it means that the user forgot
3294 to specify an argument list. Emit a useful error message. */
3295 if (DECL_TYPE_TEMPLATE_P (decl))
3296 {
3297 auto_diagnostic_group d;
3298 error_at (location,
3299 "invalid use of template-name %qE without an argument list",
3300 decl);
3301 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3302 inform (location, "class template argument deduction is only available "
3303 "with %<-std=c++17%> or %<-std=gnu++17%>");
3304 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3305 }
3306 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3307 error_at (location, "invalid use of destructor %qD as a type", id);
3308 else if (TREE_CODE (decl) == TYPE_DECL)
3309 /* Something like 'unsigned A a;' */
3310 error_at (location, "invalid combination of multiple type-specifiers");
3311 else if (!parser->scope)
3312 {
3313 /* Issue an error message. */
3314 auto_diagnostic_group d;
3315 name_hint hint;
3316 if (TREE_CODE (id) == IDENTIFIER_NODE)
3317 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3318 if (const char *suggestion = hint.suggestion ())
3319 {
3320 gcc_rich_location richloc (location);
3321 richloc.add_fixit_replace (suggestion);
3322 error_at (&richloc,
3323 "%qE does not name a type; did you mean %qs?",
3324 id, suggestion);
3325 }
3326 else
3327 error_at (location, "%qE does not name a type", id);
3328 /* If we're in a template class, it's possible that the user was
3329 referring to a type from a base class. For example:
3330
3331 template <typename T> struct A { typedef T X; };
3332 template <typename T> struct B : public A<T> { X x; };
3333
3334 The user should have said "typename A<T>::X". */
3335 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3336 inform (location, "C++11 %<constexpr%> only available with "
3337 "%<-std=c++11%> or %<-std=gnu++11%>");
3338 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3339 inform (location, "C++11 %<noexcept%> only available with "
3340 "%<-std=c++11%> or %<-std=gnu++11%>");
3341 else if (cxx_dialect < cxx11
3342 && TREE_CODE (id) == IDENTIFIER_NODE
3343 && id_equal (id, "thread_local"))
3344 inform (location, "C++11 %<thread_local%> only available with "
3345 "%<-std=c++11%> or %<-std=gnu++11%>");
3346 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3347 inform (location, "%<concept%> only available with %<-fconcepts%>");
3348 else if (processing_template_decl && current_class_type
3349 && TYPE_BINFO (current_class_type))
3350 {
3351 tree b;
3352
3353 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3354 b;
3355 b = TREE_CHAIN (b))
3356 {
3357 tree base_type = BINFO_TYPE (b);
3358 if (CLASS_TYPE_P (base_type)
3359 && dependent_type_p (base_type))
3360 {
3361 tree field;
3362 /* Go from a particular instantiation of the
3363 template (which will have an empty TYPE_FIELDs),
3364 to the main version. */
3365 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3366 for (field = TYPE_FIELDS (base_type);
3367 field;
3368 field = DECL_CHAIN (field))
3369 if (TREE_CODE (field) == TYPE_DECL
3370 && DECL_NAME (field) == id)
3371 {
3372 inform (location,
3373 "(perhaps %<typename %T::%E%> was intended)",
3374 BINFO_TYPE (b), id);
3375 break;
3376 }
3377 if (field)
3378 break;
3379 }
3380 }
3381 }
3382 }
3383 /* Here we diagnose qualified-ids where the scope is actually correct,
3384 but the identifier does not resolve to a valid type name. */
3385 else if (parser->scope != error_mark_node)
3386 {
3387 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3388 {
3389 auto_diagnostic_group d;
3390 name_hint hint;
3391 if (decl == error_mark_node)
3392 hint = suggest_alternative_in_explicit_scope (location, id,
3393 parser->scope);
3394 const char *suggestion = hint.suggestion ();
3395 gcc_rich_location richloc (location_of (id));
3396 if (suggestion)
3397 richloc.add_fixit_replace (suggestion);
3398 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
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 id, parser->scope, suggestion);
3405 else
3406 error_at (&richloc,
3407 "%qE in namespace %qE does not name a template type",
3408 id, parser->scope);
3409 }
3410 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3411 {
3412 if (suggestion)
3413 error_at (&richloc,
3414 "%qE in namespace %qE does not name a template"
3415 " type; did you mean %qs?",
3416 TREE_OPERAND (id, 0), parser->scope, suggestion);
3417 else
3418 error_at (&richloc,
3419 "%qE in namespace %qE does not name a template"
3420 " type",
3421 TREE_OPERAND (id, 0), parser->scope);
3422 }
3423 else
3424 {
3425 if (suggestion)
3426 error_at (&richloc,
3427 "%qE in namespace %qE does not name a type"
3428 "; did you mean %qs?",
3429 id, parser->scope, suggestion);
3430 else
3431 error_at (&richloc,
3432 "%qE in namespace %qE does not name a type",
3433 id, parser->scope);
3434 }
3435 if (DECL_P (decl))
3436 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3437 }
3438 else if (CLASS_TYPE_P (parser->scope)
3439 && constructor_name_p (id, parser->scope))
3440 {
3441 /* A<T>::A<T>() */
3442 auto_diagnostic_group d;
3443 error_at (location, "%<%T::%E%> names the constructor, not"
3444 " the type", parser->scope, id);
3445 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3446 error_at (location, "and %qT has no template constructors",
3447 parser->scope);
3448 }
3449 else if (TYPE_P (parser->scope)
3450 && dependent_scope_p (parser->scope))
3451 {
3452 gcc_rich_location richloc (location);
3453 richloc.add_fixit_insert_before ("typename ");
3454 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3455 error_at (&richloc,
3456 "need %<typename%> before %<%T::%D::%E%> because "
3457 "%<%T::%D%> is a dependent scope",
3458 TYPE_CONTEXT (parser->scope),
3459 TYPENAME_TYPE_FULLNAME (parser->scope),
3460 id,
3461 TYPE_CONTEXT (parser->scope),
3462 TYPENAME_TYPE_FULLNAME (parser->scope));
3463 else
3464 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3465 "%qT is a dependent scope",
3466 parser->scope, id, parser->scope);
3467 }
3468 else if (TYPE_P (parser->scope))
3469 {
3470 auto_diagnostic_group d;
3471 if (!COMPLETE_TYPE_P (parser->scope))
3472 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3473 parser->scope);
3474 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3475 error_at (location_of (id),
3476 "%qE in %q#T does not name a template type",
3477 id, parser->scope);
3478 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3479 error_at (location_of (id),
3480 "%qE in %q#T does not name a template type",
3481 TREE_OPERAND (id, 0), parser->scope);
3482 else
3483 error_at (location_of (id),
3484 "%qE in %q#T does not name a type",
3485 id, parser->scope);
3486 if (DECL_P (decl))
3487 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3488 }
3489 else
3490 gcc_unreachable ();
3491 }
3492 }
3493
3494 /* Check for a common situation where a type-name should be present,
3495 but is not, and issue a sensible error message. Returns true if an
3496 invalid type-name was detected.
3497
3498 The situation handled by this function are variable declarations of the
3499 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3500 Usually, `ID' should name a type, but if we got here it means that it
3501 does not. We try to emit the best possible error message depending on
3502 how exactly the id-expression looks like. */
3503
3504 static bool
3505 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3506 {
3507 tree id;
3508 cp_token *token = cp_lexer_peek_token (parser->lexer);
3509
3510 /* Avoid duplicate error about ambiguous lookup. */
3511 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3512 {
3513 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3514 if (next->type == CPP_NAME && next->error_reported)
3515 goto out;
3516 }
3517
3518 cp_parser_parse_tentatively (parser);
3519 id = cp_parser_id_expression (parser,
3520 /*template_keyword_p=*/false,
3521 /*check_dependency_p=*/true,
3522 /*template_p=*/NULL,
3523 /*declarator_p=*/false,
3524 /*optional_p=*/false);
3525 /* If the next token is a (, this is a function with no explicit return
3526 type, i.e. constructor, destructor or conversion op. */
3527 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3528 || TREE_CODE (id) == TYPE_DECL)
3529 {
3530 cp_parser_abort_tentative_parse (parser);
3531 return false;
3532 }
3533 if (!cp_parser_parse_definitely (parser))
3534 return false;
3535
3536 /* Emit a diagnostic for the invalid type. */
3537 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3538 out:
3539 /* If we aren't in the middle of a declarator (i.e. in a
3540 parameter-declaration-clause), skip to the end of the declaration;
3541 there's no point in trying to process it. */
3542 if (!parser->in_declarator_p)
3543 cp_parser_skip_to_end_of_block_or_statement (parser);
3544 return true;
3545 }
3546
3547 /* Consume tokens up to, and including, the next non-nested closing `)'.
3548 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3549 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3550 found an unnested token of that type. */
3551
3552 static int
3553 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3554 bool recovering,
3555 cpp_ttype or_ttype,
3556 bool consume_paren)
3557 {
3558 unsigned paren_depth = 0;
3559 unsigned brace_depth = 0;
3560 unsigned square_depth = 0;
3561 unsigned condop_depth = 0;
3562
3563 if (recovering && or_ttype == CPP_EOF
3564 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3565 return 0;
3566
3567 while (true)
3568 {
3569 cp_token * token = cp_lexer_peek_token (parser->lexer);
3570
3571 /* Have we found what we're looking for before the closing paren? */
3572 if (token->type == or_ttype && or_ttype != CPP_EOF
3573 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3574 return -1;
3575
3576 switch (token->type)
3577 {
3578 case CPP_PRAGMA_EOL:
3579 if (!parser->lexer->in_pragma)
3580 break;
3581 /* FALLTHRU */
3582 case CPP_EOF:
3583 /* If we've run out of tokens, then there is no closing `)'. */
3584 return 0;
3585
3586 /* This is good for lambda expression capture-lists. */
3587 case CPP_OPEN_SQUARE:
3588 ++square_depth;
3589 break;
3590 case CPP_CLOSE_SQUARE:
3591 if (!square_depth--)
3592 return 0;
3593 break;
3594
3595 case CPP_SEMICOLON:
3596 /* This matches the processing in skip_to_end_of_statement. */
3597 if (!brace_depth)
3598 return 0;
3599 break;
3600
3601 case CPP_OPEN_BRACE:
3602 ++brace_depth;
3603 break;
3604 case CPP_CLOSE_BRACE:
3605 if (!brace_depth--)
3606 return 0;
3607 break;
3608
3609 case CPP_OPEN_PAREN:
3610 if (!brace_depth)
3611 ++paren_depth;
3612 break;
3613
3614 case CPP_CLOSE_PAREN:
3615 if (!brace_depth && !paren_depth--)
3616 {
3617 if (consume_paren)
3618 cp_lexer_consume_token (parser->lexer);
3619 return 1;
3620 }
3621 break;
3622
3623 case CPP_QUERY:
3624 if (!brace_depth && !paren_depth && !square_depth)
3625 ++condop_depth;
3626 break;
3627
3628 case CPP_COLON:
3629 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3630 condop_depth--;
3631 break;
3632
3633 default:
3634 break;
3635 }
3636
3637 /* Consume the token. */
3638 cp_lexer_consume_token (parser->lexer);
3639 }
3640 }
3641
3642 /* Consume tokens up to, and including, the next non-nested closing `)'.
3643 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3644 are doing error recovery. Returns -1 if OR_COMMA is true and we
3645 found an unnested token of that type. */
3646
3647 static int
3648 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3649 bool recovering,
3650 bool or_comma,
3651 bool consume_paren)
3652 {
3653 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3654 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3655 ttype, consume_paren);
3656 }
3657
3658 /* Consume tokens until we reach the end of the current statement.
3659 Normally, that will be just before consuming a `;'. However, if a
3660 non-nested `}' comes first, then we stop before consuming that. */
3661
3662 static void
3663 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3664 {
3665 unsigned nesting_depth = 0;
3666
3667 /* Unwind generic function template scope if necessary. */
3668 if (parser->fully_implicit_function_template_p)
3669 abort_fully_implicit_template (parser);
3670
3671 while (true)
3672 {
3673 cp_token *token = cp_lexer_peek_token (parser->lexer);
3674
3675 switch (token->type)
3676 {
3677 case CPP_PRAGMA_EOL:
3678 if (!parser->lexer->in_pragma)
3679 break;
3680 /* FALLTHRU */
3681 case CPP_EOF:
3682 /* If we've run out of tokens, stop. */
3683 return;
3684
3685 case CPP_SEMICOLON:
3686 /* If the next token is a `;', we have reached the end of the
3687 statement. */
3688 if (!nesting_depth)
3689 return;
3690 break;
3691
3692 case CPP_CLOSE_BRACE:
3693 /* If this is a non-nested '}', stop before consuming it.
3694 That way, when confronted with something like:
3695
3696 { 3 + }
3697
3698 we stop before consuming the closing '}', even though we
3699 have not yet reached a `;'. */
3700 if (nesting_depth == 0)
3701 return;
3702
3703 /* If it is the closing '}' for a block that we have
3704 scanned, stop -- but only after consuming the token.
3705 That way given:
3706
3707 void f g () { ... }
3708 typedef int I;
3709
3710 we will stop after the body of the erroneously declared
3711 function, but before consuming the following `typedef'
3712 declaration. */
3713 if (--nesting_depth == 0)
3714 {
3715 cp_lexer_consume_token (parser->lexer);
3716 return;
3717 }
3718 break;
3719
3720 case CPP_OPEN_BRACE:
3721 ++nesting_depth;
3722 break;
3723
3724 default:
3725 break;
3726 }
3727
3728 /* Consume the token. */
3729 cp_lexer_consume_token (parser->lexer);
3730 }
3731 }
3732
3733 /* This function is called at the end of a statement or declaration.
3734 If the next token is a semicolon, it is consumed; otherwise, error
3735 recovery is attempted. */
3736
3737 static void
3738 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3739 {
3740 /* Look for the trailing `;'. */
3741 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3742 {
3743 /* If there is additional (erroneous) input, skip to the end of
3744 the statement. */
3745 cp_parser_skip_to_end_of_statement (parser);
3746 /* If the next token is now a `;', consume it. */
3747 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3748 cp_lexer_consume_token (parser->lexer);
3749 }
3750 }
3751
3752 /* Skip tokens until we have consumed an entire block, or until we
3753 have consumed a non-nested `;'. */
3754
3755 static void
3756 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3757 {
3758 int nesting_depth = 0;
3759
3760 /* Unwind generic function template scope if necessary. */
3761 if (parser->fully_implicit_function_template_p)
3762 abort_fully_implicit_template (parser);
3763
3764 while (nesting_depth >= 0)
3765 {
3766 cp_token *token = cp_lexer_peek_token (parser->lexer);
3767
3768 switch (token->type)
3769 {
3770 case CPP_PRAGMA_EOL:
3771 if (!parser->lexer->in_pragma)
3772 break;
3773 /* FALLTHRU */
3774 case CPP_EOF:
3775 /* If we've run out of tokens, stop. */
3776 return;
3777
3778 case CPP_SEMICOLON:
3779 /* Stop if this is an unnested ';'. */
3780 if (!nesting_depth)
3781 nesting_depth = -1;
3782 break;
3783
3784 case CPP_CLOSE_BRACE:
3785 /* Stop if this is an unnested '}', or closes the outermost
3786 nesting level. */
3787 nesting_depth--;
3788 if (nesting_depth < 0)
3789 return;
3790 if (!nesting_depth)
3791 nesting_depth = -1;
3792 break;
3793
3794 case CPP_OPEN_BRACE:
3795 /* Nest. */
3796 nesting_depth++;
3797 break;
3798
3799 default:
3800 break;
3801 }
3802
3803 /* Consume the token. */
3804 cp_lexer_consume_token (parser->lexer);
3805 }
3806 }
3807
3808 /* Skip tokens until a non-nested closing curly brace is the next
3809 token, or there are no more tokens. Return true in the first case,
3810 false otherwise. */
3811
3812 static bool
3813 cp_parser_skip_to_closing_brace (cp_parser *parser)
3814 {
3815 unsigned nesting_depth = 0;
3816
3817 while (true)
3818 {
3819 cp_token *token = cp_lexer_peek_token (parser->lexer);
3820
3821 switch (token->type)
3822 {
3823 case CPP_PRAGMA_EOL:
3824 if (!parser->lexer->in_pragma)
3825 break;
3826 /* FALLTHRU */
3827 case CPP_EOF:
3828 /* If we've run out of tokens, stop. */
3829 return false;
3830
3831 case CPP_CLOSE_BRACE:
3832 /* If the next token is a non-nested `}', then we have reached
3833 the end of the current block. */
3834 if (nesting_depth-- == 0)
3835 return true;
3836 break;
3837
3838 case CPP_OPEN_BRACE:
3839 /* If it the next token is a `{', then we are entering a new
3840 block. Consume the entire block. */
3841 ++nesting_depth;
3842 break;
3843
3844 default:
3845 break;
3846 }
3847
3848 /* Consume the token. */
3849 cp_lexer_consume_token (parser->lexer);
3850 }
3851 }
3852
3853 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3854 parameter is the PRAGMA token, allowing us to purge the entire pragma
3855 sequence. */
3856
3857 static void
3858 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3859 {
3860 cp_token *token;
3861
3862 parser->lexer->in_pragma = false;
3863
3864 do
3865 token = cp_lexer_consume_token (parser->lexer);
3866 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3867
3868 /* Ensure that the pragma is not parsed again. */
3869 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3870 }
3871
3872 /* Require pragma end of line, resyncing with it as necessary. The
3873 arguments are as for cp_parser_skip_to_pragma_eol. */
3874
3875 static void
3876 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3877 {
3878 parser->lexer->in_pragma = false;
3879 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3880 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3881 }
3882
3883 /* This is a simple wrapper around make_typename_type. When the id is
3884 an unresolved identifier node, we can provide a superior diagnostic
3885 using cp_parser_diagnose_invalid_type_name. */
3886
3887 static tree
3888 cp_parser_make_typename_type (cp_parser *parser, tree id,
3889 location_t id_location)
3890 {
3891 tree result;
3892 if (identifier_p (id))
3893 {
3894 result = make_typename_type (parser->scope, id, typename_type,
3895 /*complain=*/tf_none);
3896 if (result == error_mark_node)
3897 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3898 return result;
3899 }
3900 return make_typename_type (parser->scope, id, typename_type, tf_error);
3901 }
3902
3903 /* This is a wrapper around the
3904 make_{pointer,ptrmem,reference}_declarator functions that decides
3905 which one to call based on the CODE and CLASS_TYPE arguments. The
3906 CODE argument should be one of the values returned by
3907 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3908 appertain to the pointer or reference. */
3909
3910 static cp_declarator *
3911 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3912 cp_cv_quals cv_qualifiers,
3913 cp_declarator *target,
3914 tree attributes)
3915 {
3916 if (code == ERROR_MARK || target == cp_error_declarator)
3917 return cp_error_declarator;
3918
3919 if (code == INDIRECT_REF)
3920 if (class_type == NULL_TREE)
3921 return make_pointer_declarator (cv_qualifiers, target, attributes);
3922 else
3923 return make_ptrmem_declarator (cv_qualifiers, class_type,
3924 target, attributes);
3925 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3926 return make_reference_declarator (cv_qualifiers, target,
3927 false, attributes);
3928 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3929 return make_reference_declarator (cv_qualifiers, target,
3930 true, attributes);
3931 gcc_unreachable ();
3932 }
3933
3934 /* Create a new C++ parser. */
3935
3936 static cp_parser *
3937 cp_parser_new (void)
3938 {
3939 cp_parser *parser;
3940 cp_lexer *lexer;
3941 unsigned i;
3942
3943 /* cp_lexer_new_main is called before doing GC allocation because
3944 cp_lexer_new_main might load a PCH file. */
3945 lexer = cp_lexer_new_main ();
3946
3947 /* Initialize the binops_by_token so that we can get the tree
3948 directly from the token. */
3949 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3950 binops_by_token[binops[i].token_type] = binops[i];
3951
3952 parser = ggc_cleared_alloc<cp_parser> ();
3953 parser->lexer = lexer;
3954 parser->context = cp_parser_context_new (NULL);
3955
3956 /* For now, we always accept GNU extensions. */
3957 parser->allow_gnu_extensions_p = 1;
3958
3959 /* The `>' token is a greater-than operator, not the end of a
3960 template-id. */
3961 parser->greater_than_is_operator_p = true;
3962
3963 parser->default_arg_ok_p = true;
3964
3965 /* We are not parsing a constant-expression. */
3966 parser->integral_constant_expression_p = false;
3967 parser->allow_non_integral_constant_expression_p = false;
3968 parser->non_integral_constant_expression_p = false;
3969
3970 /* Local variable names are not forbidden. */
3971 parser->local_variables_forbidden_p = 0;
3972
3973 /* We are not processing an `extern "C"' declaration. */
3974 parser->in_unbraced_linkage_specification_p = false;
3975
3976 /* We are not processing a declarator. */
3977 parser->in_declarator_p = false;
3978
3979 /* We are not processing a template-argument-list. */
3980 parser->in_template_argument_list_p = false;
3981
3982 /* We are not in an iteration statement. */
3983 parser->in_statement = 0;
3984
3985 /* We are not in a switch statement. */
3986 parser->in_switch_statement_p = false;
3987
3988 /* We are not parsing a type-id inside an expression. */
3989 parser->in_type_id_in_expr_p = false;
3990
3991 /* String literals should be translated to the execution character set. */
3992 parser->translate_strings_p = true;
3993
3994 /* We are not parsing a function body. */
3995 parser->in_function_body = false;
3996
3997 /* We can correct until told otherwise. */
3998 parser->colon_corrects_to_scope_p = true;
3999
4000 /* The unparsed function queue is empty. */
4001 push_unparsed_function_queues (parser);
4002
4003 /* There are no classes being defined. */
4004 parser->num_classes_being_defined = 0;
4005
4006 /* No template parameters apply. */
4007 parser->num_template_parameter_lists = 0;
4008
4009 /* Special parsing data structures. */
4010 parser->omp_declare_simd = NULL;
4011 parser->oacc_routine = NULL;
4012
4013 /* Not declaring an implicit function template. */
4014 parser->auto_is_implicit_function_template_parm_p = false;
4015 parser->fully_implicit_function_template_p = false;
4016 parser->implicit_template_parms = 0;
4017 parser->implicit_template_scope = 0;
4018
4019 /* Allow constrained-type-specifiers. */
4020 parser->prevent_constrained_type_specifiers = 0;
4021
4022 /* We haven't yet seen an 'extern "C"'. */
4023 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4024
4025 return parser;
4026 }
4027
4028 /* Create a cp_lexer structure which will emit the tokens in CACHE
4029 and push it onto the parser's lexer stack. This is used for delayed
4030 parsing of in-class method bodies and default arguments, and should
4031 not be confused with tentative parsing. */
4032 static void
4033 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4034 {
4035 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4036 lexer->next = parser->lexer;
4037 parser->lexer = lexer;
4038
4039 /* Move the current source position to that of the first token in the
4040 new lexer. */
4041 cp_lexer_set_source_position_from_token (lexer->next_token);
4042 }
4043
4044 /* Pop the top lexer off the parser stack. This is never used for the
4045 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4046 static void
4047 cp_parser_pop_lexer (cp_parser *parser)
4048 {
4049 cp_lexer *lexer = parser->lexer;
4050 parser->lexer = lexer->next;
4051 cp_lexer_destroy (lexer);
4052
4053 /* Put the current source position back where it was before this
4054 lexer was pushed. */
4055 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4056 }
4057
4058 /* Lexical conventions [gram.lex] */
4059
4060 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4061 identifier. */
4062
4063 static cp_expr
4064 cp_parser_identifier (cp_parser* parser)
4065 {
4066 cp_token *token;
4067
4068 /* Look for the identifier. */
4069 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4070 /* Return the value. */
4071 if (token)
4072 return cp_expr (token->u.value, token->location);
4073 else
4074 return error_mark_node;
4075 }
4076
4077 /* Parse a sequence of adjacent string constants. Returns a
4078 TREE_STRING representing the combined, nul-terminated string
4079 constant. If TRANSLATE is true, translate the string to the
4080 execution character set. If WIDE_OK is true, a wide string is
4081 invalid here.
4082
4083 C++98 [lex.string] says that if a narrow string literal token is
4084 adjacent to a wide string literal token, the behavior is undefined.
4085 However, C99 6.4.5p4 says that this results in a wide string literal.
4086 We follow C99 here, for consistency with the C front end.
4087
4088 This code is largely lifted from lex_string() in c-lex.c.
4089
4090 FUTURE: ObjC++ will need to handle @-strings here. */
4091 static cp_expr
4092 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4093 bool lookup_udlit = true)
4094 {
4095 tree value;
4096 size_t count;
4097 struct obstack str_ob;
4098 struct obstack loc_ob;
4099 cpp_string str, istr, *strs;
4100 cp_token *tok;
4101 enum cpp_ttype type, curr_type;
4102 int have_suffix_p = 0;
4103 tree string_tree;
4104 tree suffix_id = NULL_TREE;
4105 bool curr_tok_is_userdef_p = false;
4106
4107 tok = cp_lexer_peek_token (parser->lexer);
4108 if (!cp_parser_is_string_literal (tok))
4109 {
4110 cp_parser_error (parser, "expected string-literal");
4111 return error_mark_node;
4112 }
4113
4114 location_t loc = tok->location;
4115
4116 if (cpp_userdef_string_p (tok->type))
4117 {
4118 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4119 curr_type = cpp_userdef_string_remove_type (tok->type);
4120 curr_tok_is_userdef_p = true;
4121 }
4122 else
4123 {
4124 string_tree = tok->u.value;
4125 curr_type = tok->type;
4126 }
4127 type = curr_type;
4128
4129 /* Try to avoid the overhead of creating and destroying an obstack
4130 for the common case of just one string. */
4131 if (!cp_parser_is_string_literal
4132 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4133 {
4134 cp_lexer_consume_token (parser->lexer);
4135
4136 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4137 str.len = TREE_STRING_LENGTH (string_tree);
4138 count = 1;
4139
4140 if (curr_tok_is_userdef_p)
4141 {
4142 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4143 have_suffix_p = 1;
4144 curr_type = cpp_userdef_string_remove_type (tok->type);
4145 }
4146 else
4147 curr_type = tok->type;
4148
4149 strs = &str;
4150 }
4151 else
4152 {
4153 location_t last_tok_loc = tok->location;
4154 gcc_obstack_init (&str_ob);
4155 gcc_obstack_init (&loc_ob);
4156 count = 0;
4157
4158 do
4159 {
4160 cp_lexer_consume_token (parser->lexer);
4161 count++;
4162 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4163 str.len = TREE_STRING_LENGTH (string_tree);
4164
4165 if (curr_tok_is_userdef_p)
4166 {
4167 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4168 if (have_suffix_p == 0)
4169 {
4170 suffix_id = curr_suffix_id;
4171 have_suffix_p = 1;
4172 }
4173 else if (have_suffix_p == 1
4174 && curr_suffix_id != suffix_id)
4175 {
4176 error ("inconsistent user-defined literal suffixes"
4177 " %qD and %qD in string literal",
4178 suffix_id, curr_suffix_id);
4179 have_suffix_p = -1;
4180 }
4181 curr_type = cpp_userdef_string_remove_type (tok->type);
4182 }
4183 else
4184 curr_type = tok->type;
4185
4186 if (type != curr_type)
4187 {
4188 if (type == CPP_STRING)
4189 type = curr_type;
4190 else if (curr_type != CPP_STRING)
4191 {
4192 rich_location rich_loc (line_table, tok->location);
4193 rich_loc.add_range (last_tok_loc);
4194 error_at (&rich_loc,
4195 "unsupported non-standard concatenation "
4196 "of string literals");
4197 }
4198 }
4199
4200 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4201 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4202
4203 last_tok_loc = tok->location;
4204
4205 tok = cp_lexer_peek_token (parser->lexer);
4206 if (cpp_userdef_string_p (tok->type))
4207 {
4208 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4209 curr_type = cpp_userdef_string_remove_type (tok->type);
4210 curr_tok_is_userdef_p = true;
4211 }
4212 else
4213 {
4214 string_tree = tok->u.value;
4215 curr_type = tok->type;
4216 curr_tok_is_userdef_p = false;
4217 }
4218 }
4219 while (cp_parser_is_string_literal (tok));
4220
4221 /* A string literal built by concatenation has its caret=start at
4222 the start of the initial string, and its finish at the finish of
4223 the final string literal. */
4224 loc = make_location (loc, loc, get_finish (last_tok_loc));
4225
4226 strs = (cpp_string *) obstack_finish (&str_ob);
4227 }
4228
4229 if (type != CPP_STRING && !wide_ok)
4230 {
4231 cp_parser_error (parser, "a wide string is invalid in this context");
4232 type = CPP_STRING;
4233 }
4234
4235 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4236 (parse_in, strs, count, &istr, type))
4237 {
4238 value = build_string (istr.len, (const char *)istr.text);
4239 free (CONST_CAST (unsigned char *, istr.text));
4240 if (count > 1)
4241 {
4242 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4243 gcc_assert (g_string_concat_db);
4244 g_string_concat_db->record_string_concatenation (count, locs);
4245 }
4246
4247 switch (type)
4248 {
4249 default:
4250 case CPP_STRING:
4251 TREE_TYPE (value) = char_array_type_node;
4252 break;
4253 case CPP_UTF8STRING:
4254 if (flag_char8_t)
4255 TREE_TYPE (value) = char8_array_type_node;
4256 else
4257 TREE_TYPE (value) = char_array_type_node;
4258 break;
4259 case CPP_STRING16:
4260 TREE_TYPE (value) = char16_array_type_node;
4261 break;
4262 case CPP_STRING32:
4263 TREE_TYPE (value) = char32_array_type_node;
4264 break;
4265 case CPP_WSTRING:
4266 TREE_TYPE (value) = wchar_array_type_node;
4267 break;
4268 }
4269
4270 value = fix_string_type (value);
4271
4272 if (have_suffix_p)
4273 {
4274 tree literal = build_userdef_literal (suffix_id, value,
4275 OT_NONE, NULL_TREE);
4276 if (lookup_udlit)
4277 value = cp_parser_userdef_string_literal (literal);
4278 else
4279 value = literal;
4280 }
4281 }
4282 else
4283 /* cpp_interpret_string has issued an error. */
4284 value = error_mark_node;
4285
4286 if (count > 1)
4287 {
4288 obstack_free (&str_ob, 0);
4289 obstack_free (&loc_ob, 0);
4290 }
4291
4292 return cp_expr (value, loc);
4293 }
4294
4295 /* Look up a literal operator with the name and the exact arguments. */
4296
4297 static tree
4298 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4299 {
4300 tree decl = lookup_name (name);
4301 if (!decl || !is_overloaded_fn (decl))
4302 return error_mark_node;
4303
4304 for (lkp_iterator iter (decl); iter; ++iter)
4305 {
4306 tree fn = *iter;
4307
4308 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4309 {
4310 unsigned int ix;
4311 bool found = true;
4312
4313 for (ix = 0;
4314 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4315 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4316 {
4317 tree tparm = TREE_VALUE (parmtypes);
4318 tree targ = TREE_TYPE ((*args)[ix]);
4319 bool ptr = TYPE_PTR_P (tparm);
4320 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4321 if ((ptr || arr || !same_type_p (tparm, targ))
4322 && (!ptr || !arr
4323 || !same_type_p (TREE_TYPE (tparm),
4324 TREE_TYPE (targ))))
4325 found = false;
4326 }
4327
4328 if (found
4329 && ix == vec_safe_length (args)
4330 /* May be this should be sufficient_parms_p instead,
4331 depending on how exactly should user-defined literals
4332 work in presence of default arguments on the literal
4333 operator parameters. */
4334 && parmtypes == void_list_node)
4335 return decl;
4336 }
4337 }
4338
4339 return error_mark_node;
4340 }
4341
4342 /* Parse a user-defined char constant. Returns a call to a user-defined
4343 literal operator taking the character as an argument. */
4344
4345 static cp_expr
4346 cp_parser_userdef_char_literal (cp_parser *parser)
4347 {
4348 cp_token *token = cp_lexer_consume_token (parser->lexer);
4349 tree literal = token->u.value;
4350 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4351 tree value = USERDEF_LITERAL_VALUE (literal);
4352 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4353 tree decl, result;
4354
4355 /* Build up a call to the user-defined operator */
4356 /* Lookup the name we got back from the id-expression. */
4357 releasing_vec args;
4358 vec_safe_push (args, value);
4359 decl = lookup_literal_operator (name, args);
4360 if (!decl || decl == error_mark_node)
4361 {
4362 error ("unable to find character literal operator %qD with %qT argument",
4363 name, TREE_TYPE (value));
4364 return error_mark_node;
4365 }
4366 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4367 return result;
4368 }
4369
4370 /* A subroutine of cp_parser_userdef_numeric_literal to
4371 create a char... template parameter pack from a string node. */
4372
4373 static tree
4374 make_char_string_pack (tree value)
4375 {
4376 tree charvec;
4377 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4378 const char *str = TREE_STRING_POINTER (value);
4379 int i, len = TREE_STRING_LENGTH (value) - 1;
4380 tree argvec = make_tree_vec (1);
4381
4382 /* Fill in CHARVEC with all of the parameters. */
4383 charvec = make_tree_vec (len);
4384 for (i = 0; i < len; ++i)
4385 {
4386 unsigned char s[3] = { '\'', str[i], '\'' };
4387 cpp_string in = { 3, s };
4388 cpp_string out = { 0, 0 };
4389 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4390 return NULL_TREE;
4391 gcc_assert (out.len == 2);
4392 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4393 out.text[0]);
4394 }
4395
4396 /* Build the argument packs. */
4397 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4398
4399 TREE_VEC_ELT (argvec, 0) = argpack;
4400
4401 return argvec;
4402 }
4403
4404 /* A subroutine of cp_parser_userdef_numeric_literal to
4405 create a char... template parameter pack from a string node. */
4406
4407 static tree
4408 make_string_pack (tree value)
4409 {
4410 tree charvec;
4411 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4412 const unsigned char *str
4413 = (const unsigned char *) TREE_STRING_POINTER (value);
4414 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4415 int len = TREE_STRING_LENGTH (value) / sz - 1;
4416 tree argvec = make_tree_vec (2);
4417
4418 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4419 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4420
4421 /* First template parm is character type. */
4422 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4423
4424 /* Fill in CHARVEC with all of the parameters. */
4425 charvec = make_tree_vec (len);
4426 for (int i = 0; i < len; ++i)
4427 TREE_VEC_ELT (charvec, i)
4428 = double_int_to_tree (str_char_type_node,
4429 double_int::from_buffer (str + i * sz, sz));
4430
4431 /* Build the argument packs. */
4432 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4433
4434 TREE_VEC_ELT (argvec, 1) = argpack;
4435
4436 return argvec;
4437 }
4438
4439 /* Parse a user-defined numeric constant. returns a call to a user-defined
4440 literal operator. */
4441
4442 static cp_expr
4443 cp_parser_userdef_numeric_literal (cp_parser *parser)
4444 {
4445 cp_token *token = cp_lexer_consume_token (parser->lexer);
4446 tree literal = token->u.value;
4447 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4448 tree value = USERDEF_LITERAL_VALUE (literal);
4449 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4450 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4451 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4452 tree decl, result;
4453
4454 /* Look for a literal operator taking the exact type of numeric argument
4455 as the literal value. */
4456 releasing_vec args;
4457 vec_safe_push (args, value);
4458 decl = lookup_literal_operator (name, args);
4459 if (decl && decl != error_mark_node)
4460 {
4461 result = finish_call_expr (decl, &args, false, true,
4462 tf_warning_or_error);
4463
4464 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4465 {
4466 warning_at (token->location, OPT_Woverflow,
4467 "integer literal exceeds range of %qT type",
4468 long_long_unsigned_type_node);
4469 }
4470 else
4471 {
4472 if (overflow > 0)
4473 warning_at (token->location, OPT_Woverflow,
4474 "floating literal exceeds range of %qT type",
4475 long_double_type_node);
4476 else if (overflow < 0)
4477 warning_at (token->location, OPT_Woverflow,
4478 "floating literal truncated to zero");
4479 }
4480
4481 return result;
4482 }
4483
4484 /* If the numeric argument didn't work, look for a raw literal
4485 operator taking a const char* argument consisting of the number
4486 in string format. */
4487 args->truncate (0);
4488 vec_safe_push (args, num_string);
4489 decl = lookup_literal_operator (name, args);
4490 if (decl && decl != error_mark_node)
4491 {
4492 result = finish_call_expr (decl, &args, false, true,
4493 tf_warning_or_error);
4494 return result;
4495 }
4496
4497 /* If the raw literal didn't work, look for a non-type template
4498 function with parameter pack char.... Call the function with
4499 template parameter characters representing the number. */
4500 args->truncate (0);
4501 decl = lookup_literal_operator (name, args);
4502 if (decl && decl != error_mark_node)
4503 {
4504 tree tmpl_args = make_char_string_pack (num_string);
4505 if (tmpl_args == NULL_TREE)
4506 {
4507 error ("failed to translate literal to execution character set %qT",
4508 num_string);
4509 return error_mark_node;
4510 }
4511 decl = lookup_template_function (decl, tmpl_args);
4512 result = finish_call_expr (decl, &args, false, true,
4513 tf_warning_or_error);
4514 return result;
4515 }
4516
4517 /* In C++14 the standard library defines complex number suffixes that
4518 conflict with GNU extensions. Prefer them if <complex> is #included. */
4519 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4520 bool i14 = (cxx_dialect > cxx11
4521 && (id_equal (suffix_id, "i")
4522 || id_equal (suffix_id, "if")
4523 || id_equal (suffix_id, "il")));
4524 diagnostic_t kind = DK_ERROR;
4525 int opt = 0;
4526
4527 if (i14 && ext)
4528 {
4529 tree cxlit = lookup_qualified_name (std_node,
4530 get_identifier ("complex_literals"),
4531 0, false, false);
4532 if (cxlit == error_mark_node)
4533 {
4534 /* No <complex>, so pedwarn and use GNU semantics. */
4535 kind = DK_PEDWARN;
4536 opt = OPT_Wpedantic;
4537 }
4538 }
4539
4540 bool complained
4541 = emit_diagnostic (kind, input_location, opt,
4542 "unable to find numeric literal operator %qD", name);
4543
4544 if (!complained)
4545 /* Don't inform either. */;
4546 else if (i14)
4547 {
4548 inform (token->location, "add %<using namespace std::complex_literals%> "
4549 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4550 "suffixes");
4551 if (ext)
4552 inform (token->location, "or use %<j%> instead of %<i%> for the "
4553 "GNU built-in suffix");
4554 }
4555 else if (!ext)
4556 inform (token->location, "use %<-fext-numeric-literals%> "
4557 "to enable more built-in suffixes");
4558
4559 if (kind == DK_ERROR)
4560 value = error_mark_node;
4561 else
4562 {
4563 /* Use the built-in semantics. */
4564 tree type;
4565 if (id_equal (suffix_id, "i"))
4566 {
4567 if (TREE_CODE (value) == INTEGER_CST)
4568 type = integer_type_node;
4569 else
4570 type = double_type_node;
4571 }
4572 else if (id_equal (suffix_id, "if"))
4573 type = float_type_node;
4574 else /* if (id_equal (suffix_id, "il")) */
4575 type = long_double_type_node;
4576
4577 value = build_complex (build_complex_type (type),
4578 fold_convert (type, integer_zero_node),
4579 fold_convert (type, value));
4580 }
4581
4582 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4583 /* Avoid repeated diagnostics. */
4584 token->u.value = value;
4585 return value;
4586 }
4587
4588 /* Parse a user-defined string constant. Returns a call to a user-defined
4589 literal operator taking a character pointer and the length of the string
4590 as arguments. */
4591
4592 static tree
4593 cp_parser_userdef_string_literal (tree literal)
4594 {
4595 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4596 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4597 tree value = USERDEF_LITERAL_VALUE (literal);
4598 int len = TREE_STRING_LENGTH (value)
4599 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4600 tree decl;
4601
4602 /* Build up a call to the user-defined operator. */
4603 /* Lookup the name we got back from the id-expression. */
4604 releasing_vec args;
4605 vec_safe_push (args, value);
4606 vec_safe_push (args, build_int_cst (size_type_node, len));
4607 decl = lookup_literal_operator (name, args);
4608
4609 if (decl && decl != error_mark_node)
4610 return finish_call_expr (decl, &args, false, true,
4611 tf_warning_or_error);
4612
4613 /* Look for a suitable template function, either (C++20) with a single
4614 parameter of class type, or (N3599) with typename parameter CharT and
4615 parameter pack CharT... */
4616 args->truncate (0);
4617 decl = lookup_literal_operator (name, args);
4618 if (decl && decl != error_mark_node)
4619 {
4620 /* Use resolve_nondeduced_context to try to choose one form of template
4621 or the other. */
4622 tree tmpl_args = make_tree_vec (1);
4623 TREE_VEC_ELT (tmpl_args, 0) = value;
4624 decl = lookup_template_function (decl, tmpl_args);
4625 tree res = resolve_nondeduced_context (decl, tf_none);
4626 if (DECL_P (res))
4627 decl = res;
4628 else
4629 {
4630 TREE_OPERAND (decl, 1) = make_string_pack (value);
4631 res = resolve_nondeduced_context (decl, tf_none);
4632 if (DECL_P (res))
4633 decl = res;
4634 }
4635 if (!DECL_P (decl) && cxx_dialect > cxx17)
4636 TREE_OPERAND (decl, 1) = tmpl_args;
4637 return finish_call_expr (decl, &args, false, true,
4638 tf_warning_or_error);
4639 }
4640
4641 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4642 name, TREE_TYPE (value), size_type_node);
4643 return error_mark_node;
4644 }
4645
4646
4647 /* Basic concepts [gram.basic] */
4648
4649 /* Parse a translation-unit.
4650
4651 translation-unit:
4652 declaration-seq [opt] */
4653
4654 static void
4655 cp_parser_translation_unit (cp_parser* parser)
4656 {
4657 gcc_checking_assert (!cp_error_declarator);
4658
4659 /* Create the declarator obstack. */
4660 gcc_obstack_init (&declarator_obstack);
4661 /* Create the error declarator. */
4662 cp_error_declarator = make_declarator (cdk_error);
4663 /* Create the empty parameter list. */
4664 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4665 UNKNOWN_LOCATION);
4666 /* Remember where the base of the declarator obstack lies. */
4667 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4668
4669 bool implicit_extern_c = false;
4670
4671 for (;;)
4672 {
4673 cp_token *token = cp_lexer_peek_token (parser->lexer);
4674
4675 /* If we're entering or exiting a region that's implicitly
4676 extern "C", modify the lang context appropriately. */
4677 if (implicit_extern_c
4678 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4679 {
4680 implicit_extern_c = !implicit_extern_c;
4681 if (implicit_extern_c)
4682 push_lang_context (lang_name_c);
4683 else
4684 pop_lang_context ();
4685 }
4686
4687 if (token->type == CPP_EOF)
4688 break;
4689
4690 if (token->type == CPP_CLOSE_BRACE)
4691 {
4692 cp_parser_error (parser, "expected declaration");
4693 cp_lexer_consume_token (parser->lexer);
4694 /* If the next token is now a `;', consume it. */
4695 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4696 cp_lexer_consume_token (parser->lexer);
4697 }
4698 else
4699 cp_parser_toplevel_declaration (parser);
4700 }
4701
4702 /* Get rid of the token array; we don't need it any more. */
4703 cp_lexer_destroy (parser->lexer);
4704 parser->lexer = NULL;
4705
4706 /* The EOF should have reset this. */
4707 gcc_checking_assert (!implicit_extern_c);
4708
4709 /* Make sure the declarator obstack was fully cleaned up. */
4710 gcc_assert (obstack_next_free (&declarator_obstack)
4711 == declarator_obstack_base);
4712 }
4713
4714 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4715 decltype context. */
4716
4717 static inline tsubst_flags_t
4718 complain_flags (bool decltype_p)
4719 {
4720 tsubst_flags_t complain = tf_warning_or_error;
4721 if (decltype_p)
4722 complain |= tf_decltype;
4723 return complain;
4724 }
4725
4726 /* We're about to parse a collection of statements. If we're currently
4727 parsing tentatively, set up a firewall so that any nested
4728 cp_parser_commit_to_tentative_parse won't affect the current context. */
4729
4730 static cp_token_position
4731 cp_parser_start_tentative_firewall (cp_parser *parser)
4732 {
4733 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4734 return 0;
4735
4736 cp_parser_parse_tentatively (parser);
4737 cp_parser_commit_to_topmost_tentative_parse (parser);
4738 return cp_lexer_token_position (parser->lexer, false);
4739 }
4740
4741 /* We've finished parsing the collection of statements. Wrap up the
4742 firewall and replace the relevant tokens with the parsed form. */
4743
4744 static void
4745 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4746 tree expr)
4747 {
4748 if (!start)
4749 return;
4750
4751 /* Finish the firewall level. */
4752 cp_parser_parse_definitely (parser);
4753 /* And remember the result of the parse for when we try again. */
4754 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4755 token->type = CPP_PREPARSED_EXPR;
4756 token->u.value = expr;
4757 token->keyword = RID_MAX;
4758 cp_lexer_purge_tokens_after (parser->lexer, start);
4759 }
4760
4761 /* Like the above functions, but let the user modify the tokens. Used by
4762 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4763 later parses, so it makes sense to localize the effects of
4764 cp_parser_commit_to_tentative_parse. */
4765
4766 struct tentative_firewall
4767 {
4768 cp_parser *parser;
4769 bool set;
4770
4771 tentative_firewall (cp_parser *p): parser(p)
4772 {
4773 /* If we're currently parsing tentatively, start a committed level as a
4774 firewall and then an inner tentative parse. */
4775 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4776 {
4777 cp_parser_parse_tentatively (parser);
4778 cp_parser_commit_to_topmost_tentative_parse (parser);
4779 cp_parser_parse_tentatively (parser);
4780 }
4781 }
4782
4783 ~tentative_firewall()
4784 {
4785 if (set)
4786 {
4787 /* Finish the inner tentative parse and the firewall, propagating any
4788 uncommitted error state to the outer tentative parse. */
4789 bool err = cp_parser_error_occurred (parser);
4790 cp_parser_parse_definitely (parser);
4791 cp_parser_parse_definitely (parser);
4792 if (err)
4793 cp_parser_simulate_error (parser);
4794 }
4795 }
4796 };
4797
4798 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4799 This class is for tracking such a matching pair of symbols.
4800 In particular, it tracks the location of the first token,
4801 so that if the second token is missing, we can highlight the
4802 location of the first token when notifying the user about the
4803 problem. */
4804
4805 template <typename traits_t>
4806 class token_pair
4807 {
4808 public:
4809 /* token_pair's ctor. */
4810 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4811
4812 /* If the next token is the opening symbol for this pair, consume it and
4813 return true.
4814 Otherwise, issue an error and return false.
4815 In either case, record the location of the opening token. */
4816
4817 bool require_open (cp_parser *parser)
4818 {
4819 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4820 return cp_parser_require (parser, traits_t::open_token_type,
4821 traits_t::required_token_open);
4822 }
4823
4824 /* Consume the next token from PARSER, recording its location as
4825 that of the opening token within the pair. */
4826
4827 cp_token * consume_open (cp_parser *parser)
4828 {
4829 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4830 gcc_assert (tok->type == traits_t::open_token_type);
4831 m_open_loc = tok->location;
4832 return tok;
4833 }
4834
4835 /* If the next token is the closing symbol for this pair, consume it
4836 and return it.
4837 Otherwise, issue an error, highlighting the location of the
4838 corresponding opening token, and return NULL. */
4839
4840 cp_token *require_close (cp_parser *parser) const
4841 {
4842 return cp_parser_require (parser, traits_t::close_token_type,
4843 traits_t::required_token_close,
4844 m_open_loc);
4845 }
4846
4847 private:
4848 location_t m_open_loc;
4849 };
4850
4851 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4852
4853 struct matching_paren_traits
4854 {
4855 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4856 static const enum required_token required_token_open = RT_OPEN_PAREN;
4857 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4858 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4859 };
4860
4861 /* "matching_parens" is a token_pair<T> class for tracking matching
4862 pairs of parentheses. */
4863
4864 typedef token_pair<matching_paren_traits> matching_parens;
4865
4866 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4867
4868 struct matching_brace_traits
4869 {
4870 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4871 static const enum required_token required_token_open = RT_OPEN_BRACE;
4872 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4873 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4874 };
4875
4876 /* "matching_braces" is a token_pair<T> class for tracking matching
4877 pairs of braces. */
4878
4879 typedef token_pair<matching_brace_traits> matching_braces;
4880
4881
4882 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4883 enclosing parentheses. */
4884
4885 static cp_expr
4886 cp_parser_statement_expr (cp_parser *parser)
4887 {
4888 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4889
4890 /* Consume the '('. */
4891 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4892 matching_parens parens;
4893 parens.consume_open (parser);
4894 /* Start the statement-expression. */
4895 tree expr = begin_stmt_expr ();
4896 /* Parse the compound-statement. */
4897 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4898 /* Finish up. */
4899 expr = finish_stmt_expr (expr, false);
4900 /* Consume the ')'. */
4901 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4902 if (!parens.require_close (parser))
4903 cp_parser_skip_to_end_of_statement (parser);
4904
4905 cp_parser_end_tentative_firewall (parser, start, expr);
4906 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4907 return cp_expr (expr, combined_loc);
4908 }
4909
4910 /* Expressions [gram.expr] */
4911
4912 /* Parse a fold-operator.
4913
4914 fold-operator:
4915 - * / % ^ & | = < > << >>
4916 = -= *= /= %= ^= &= |= <<= >>=
4917 == != <= >= && || , .* ->*
4918
4919 This returns the tree code corresponding to the matched operator
4920 as an int. When the current token matches a compound assignment
4921 opertor, the resulting tree code is the negative value of the
4922 non-assignment operator. */
4923
4924 static int
4925 cp_parser_fold_operator (cp_token *token)
4926 {
4927 switch (token->type)
4928 {
4929 case CPP_PLUS: return PLUS_EXPR;
4930 case CPP_MINUS: return MINUS_EXPR;
4931 case CPP_MULT: return MULT_EXPR;
4932 case CPP_DIV: return TRUNC_DIV_EXPR;
4933 case CPP_MOD: return TRUNC_MOD_EXPR;
4934 case CPP_XOR: return BIT_XOR_EXPR;
4935 case CPP_AND: return BIT_AND_EXPR;
4936 case CPP_OR: return BIT_IOR_EXPR;
4937 case CPP_LSHIFT: return LSHIFT_EXPR;
4938 case CPP_RSHIFT: return RSHIFT_EXPR;
4939
4940 case CPP_EQ: return -NOP_EXPR;
4941 case CPP_PLUS_EQ: return -PLUS_EXPR;
4942 case CPP_MINUS_EQ: return -MINUS_EXPR;
4943 case CPP_MULT_EQ: return -MULT_EXPR;
4944 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4945 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4946 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4947 case CPP_AND_EQ: return -BIT_AND_EXPR;
4948 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4949 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4950 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4951
4952 case CPP_EQ_EQ: return EQ_EXPR;
4953 case CPP_NOT_EQ: return NE_EXPR;
4954 case CPP_LESS: return LT_EXPR;
4955 case CPP_GREATER: return GT_EXPR;
4956 case CPP_LESS_EQ: return LE_EXPR;
4957 case CPP_GREATER_EQ: return GE_EXPR;
4958
4959 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4960 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4961
4962 case CPP_COMMA: return COMPOUND_EXPR;
4963
4964 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4965 case CPP_DEREF_STAR: return MEMBER_REF;
4966
4967 default: return ERROR_MARK;
4968 }
4969 }
4970
4971 /* Returns true if CODE indicates a binary expression, which is not allowed in
4972 the LHS of a fold-expression. More codes will need to be added to use this
4973 function in other contexts. */
4974
4975 static bool
4976 is_binary_op (tree_code code)
4977 {
4978 switch (code)
4979 {
4980 case PLUS_EXPR:
4981 case POINTER_PLUS_EXPR:
4982 case MINUS_EXPR:
4983 case MULT_EXPR:
4984 case TRUNC_DIV_EXPR:
4985 case TRUNC_MOD_EXPR:
4986 case BIT_XOR_EXPR:
4987 case BIT_AND_EXPR:
4988 case BIT_IOR_EXPR:
4989 case LSHIFT_EXPR:
4990 case RSHIFT_EXPR:
4991
4992 case MODOP_EXPR:
4993
4994 case EQ_EXPR:
4995 case NE_EXPR:
4996 case LE_EXPR:
4997 case GE_EXPR:
4998 case LT_EXPR:
4999 case GT_EXPR:
5000
5001 case TRUTH_ANDIF_EXPR:
5002 case TRUTH_ORIF_EXPR:
5003
5004 case COMPOUND_EXPR:
5005
5006 case DOTSTAR_EXPR:
5007 case MEMBER_REF:
5008 return true;
5009
5010 default:
5011 return false;
5012 }
5013 }
5014
5015 /* If the next token is a suitable fold operator, consume it and return as
5016 the function above. */
5017
5018 static int
5019 cp_parser_fold_operator (cp_parser *parser)
5020 {
5021 cp_token* token = cp_lexer_peek_token (parser->lexer);
5022 int code = cp_parser_fold_operator (token);
5023 if (code != ERROR_MARK)
5024 cp_lexer_consume_token (parser->lexer);
5025 return code;
5026 }
5027
5028 /* Parse a fold-expression.
5029
5030 fold-expression:
5031 ( ... folding-operator cast-expression)
5032 ( cast-expression folding-operator ... )
5033 ( cast-expression folding operator ... folding-operator cast-expression)
5034
5035 Note that the '(' and ')' are matched in primary expression. */
5036
5037 static cp_expr
5038 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5039 {
5040 cp_id_kind pidk;
5041
5042 // Left fold.
5043 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5044 {
5045 cp_lexer_consume_token (parser->lexer);
5046 int op = cp_parser_fold_operator (parser);
5047 if (op == ERROR_MARK)
5048 {
5049 cp_parser_error (parser, "expected binary operator");
5050 return error_mark_node;
5051 }
5052
5053 tree expr = cp_parser_cast_expression (parser, false, false,
5054 false, &pidk);
5055 if (expr == error_mark_node)
5056 return error_mark_node;
5057 return finish_left_unary_fold_expr (expr, op);
5058 }
5059
5060 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5061 int op = cp_parser_fold_operator (parser);
5062 if (op == ERROR_MARK)
5063 {
5064 cp_parser_error (parser, "expected binary operator");
5065 return error_mark_node;
5066 }
5067
5068 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5069 {
5070 cp_parser_error (parser, "expected ...");
5071 return error_mark_node;
5072 }
5073 cp_lexer_consume_token (parser->lexer);
5074
5075 /* The operands of a fold-expression are cast-expressions, so binary or
5076 conditional expressions are not allowed. We check this here to avoid
5077 tentative parsing. */
5078 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5079 /* OK, the expression was parenthesized. */;
5080 else if (is_binary_op (TREE_CODE (expr1)))
5081 error_at (location_of (expr1),
5082 "binary expression in operand of fold-expression");
5083 else if (TREE_CODE (expr1) == COND_EXPR
5084 || (REFERENCE_REF_P (expr1)
5085 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5086 error_at (location_of (expr1),
5087 "conditional expression in operand of fold-expression");
5088
5089 // Right fold.
5090 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5091 return finish_right_unary_fold_expr (expr1, op);
5092
5093 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5094 {
5095 cp_parser_error (parser, "mismatched operator in fold-expression");
5096 return error_mark_node;
5097 }
5098 cp_lexer_consume_token (parser->lexer);
5099
5100 // Binary left or right fold.
5101 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5102 if (expr2 == error_mark_node)
5103 return error_mark_node;
5104 return finish_binary_fold_expr (expr1, expr2, op);
5105 }
5106
5107 /* Parse a primary-expression.
5108
5109 primary-expression:
5110 literal
5111 this
5112 ( expression )
5113 id-expression
5114 lambda-expression (C++11)
5115
5116 GNU Extensions:
5117
5118 primary-expression:
5119 ( compound-statement )
5120 __builtin_va_arg ( assignment-expression , type-id )
5121 __builtin_offsetof ( type-id , offsetof-expression )
5122
5123 C++ Extensions:
5124 __has_nothrow_assign ( type-id )
5125 __has_nothrow_constructor ( type-id )
5126 __has_nothrow_copy ( type-id )
5127 __has_trivial_assign ( type-id )
5128 __has_trivial_constructor ( type-id )
5129 __has_trivial_copy ( type-id )
5130 __has_trivial_destructor ( type-id )
5131 __has_virtual_destructor ( type-id )
5132 __is_abstract ( type-id )
5133 __is_base_of ( type-id , type-id )
5134 __is_class ( type-id )
5135 __is_empty ( type-id )
5136 __is_enum ( type-id )
5137 __is_final ( type-id )
5138 __is_literal_type ( type-id )
5139 __is_pod ( type-id )
5140 __is_polymorphic ( type-id )
5141 __is_std_layout ( type-id )
5142 __is_trivial ( type-id )
5143 __is_union ( type-id )
5144
5145 Objective-C++ Extension:
5146
5147 primary-expression:
5148 objc-expression
5149
5150 literal:
5151 __null
5152
5153 ADDRESS_P is true iff this expression was immediately preceded by
5154 "&" and therefore might denote a pointer-to-member. CAST_P is true
5155 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5156 true iff this expression is a template argument.
5157
5158 Returns a representation of the expression. Upon return, *IDK
5159 indicates what kind of id-expression (if any) was present. */
5160
5161 static cp_expr
5162 cp_parser_primary_expression (cp_parser *parser,
5163 bool address_p,
5164 bool cast_p,
5165 bool template_arg_p,
5166 bool decltype_p,
5167 cp_id_kind *idk)
5168 {
5169 cp_token *token = NULL;
5170
5171 /* Assume the primary expression is not an id-expression. */
5172 *idk = CP_ID_KIND_NONE;
5173
5174 /* Peek at the next token. */
5175 token = cp_lexer_peek_token (parser->lexer);
5176 switch ((int) token->type)
5177 {
5178 /* literal:
5179 integer-literal
5180 character-literal
5181 floating-literal
5182 string-literal
5183 boolean-literal
5184 pointer-literal
5185 user-defined-literal */
5186 case CPP_CHAR:
5187 case CPP_CHAR16:
5188 case CPP_CHAR32:
5189 case CPP_WCHAR:
5190 case CPP_UTF8CHAR:
5191 case CPP_NUMBER:
5192 case CPP_PREPARSED_EXPR:
5193 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5194 return cp_parser_userdef_numeric_literal (parser);
5195 token = cp_lexer_consume_token (parser->lexer);
5196 if (TREE_CODE (token->u.value) == FIXED_CST)
5197 {
5198 error_at (token->location,
5199 "fixed-point types not supported in C++");
5200 return error_mark_node;
5201 }
5202 /* Floating-point literals are only allowed in an integral
5203 constant expression if they are cast to an integral or
5204 enumeration type. */
5205 if (TREE_CODE (token->u.value) == REAL_CST
5206 && parser->integral_constant_expression_p
5207 && pedantic)
5208 {
5209 /* CAST_P will be set even in invalid code like "int(2.7 +
5210 ...)". Therefore, we have to check that the next token
5211 is sure to end the cast. */
5212 if (cast_p)
5213 {
5214 cp_token *next_token;
5215
5216 next_token = cp_lexer_peek_token (parser->lexer);
5217 if (/* The comma at the end of an
5218 enumerator-definition. */
5219 next_token->type != CPP_COMMA
5220 /* The curly brace at the end of an enum-specifier. */
5221 && next_token->type != CPP_CLOSE_BRACE
5222 /* The end of a statement. */
5223 && next_token->type != CPP_SEMICOLON
5224 /* The end of the cast-expression. */
5225 && next_token->type != CPP_CLOSE_PAREN
5226 /* The end of an array bound. */
5227 && next_token->type != CPP_CLOSE_SQUARE
5228 /* The closing ">" in a template-argument-list. */
5229 && (next_token->type != CPP_GREATER
5230 || parser->greater_than_is_operator_p)
5231 /* C++0x only: A ">>" treated like two ">" tokens,
5232 in a template-argument-list. */
5233 && (next_token->type != CPP_RSHIFT
5234 || (cxx_dialect == cxx98)
5235 || parser->greater_than_is_operator_p))
5236 cast_p = false;
5237 }
5238
5239 /* If we are within a cast, then the constraint that the
5240 cast is to an integral or enumeration type will be
5241 checked at that point. If we are not within a cast, then
5242 this code is invalid. */
5243 if (!cast_p)
5244 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5245 }
5246 return (cp_expr (token->u.value, token->location)
5247 .maybe_add_location_wrapper ());
5248
5249 case CPP_CHAR_USERDEF:
5250 case CPP_CHAR16_USERDEF:
5251 case CPP_CHAR32_USERDEF:
5252 case CPP_WCHAR_USERDEF:
5253 case CPP_UTF8CHAR_USERDEF:
5254 return cp_parser_userdef_char_literal (parser);
5255
5256 case CPP_STRING:
5257 case CPP_STRING16:
5258 case CPP_STRING32:
5259 case CPP_WSTRING:
5260 case CPP_UTF8STRING:
5261 case CPP_STRING_USERDEF:
5262 case CPP_STRING16_USERDEF:
5263 case CPP_STRING32_USERDEF:
5264 case CPP_WSTRING_USERDEF:
5265 case CPP_UTF8STRING_USERDEF:
5266 /* ??? Should wide strings be allowed when parser->translate_strings_p
5267 is false (i.e. in attributes)? If not, we can kill the third
5268 argument to cp_parser_string_literal. */
5269 return (cp_parser_string_literal (parser,
5270 parser->translate_strings_p,
5271 true)
5272 .maybe_add_location_wrapper ());
5273
5274 case CPP_OPEN_PAREN:
5275 /* If we see `( { ' then we are looking at the beginning of
5276 a GNU statement-expression. */
5277 if (cp_parser_allow_gnu_extensions_p (parser)
5278 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5279 {
5280 /* Statement-expressions are not allowed by the standard. */
5281 pedwarn (token->location, OPT_Wpedantic,
5282 "ISO C++ forbids braced-groups within expressions");
5283
5284 /* And they're not allowed outside of a function-body; you
5285 cannot, for example, write:
5286
5287 int i = ({ int j = 3; j + 1; });
5288
5289 at class or namespace scope. */
5290 if (!parser->in_function_body
5291 || parser->in_template_argument_list_p)
5292 {
5293 error_at (token->location,
5294 "statement-expressions are not allowed outside "
5295 "functions nor in template-argument lists");
5296 cp_parser_skip_to_end_of_block_or_statement (parser);
5297 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5298 cp_lexer_consume_token (parser->lexer);
5299 return error_mark_node;
5300 }
5301 else
5302 return cp_parser_statement_expr (parser);
5303 }
5304 /* Otherwise it's a normal parenthesized expression. */
5305 {
5306 cp_expr expr;
5307 bool saved_greater_than_is_operator_p;
5308
5309 location_t open_paren_loc = token->location;
5310
5311 /* Consume the `('. */
5312 matching_parens parens;
5313 parens.consume_open (parser);
5314 /* Within a parenthesized expression, a `>' token is always
5315 the greater-than operator. */
5316 saved_greater_than_is_operator_p
5317 = parser->greater_than_is_operator_p;
5318 parser->greater_than_is_operator_p = true;
5319
5320 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5321 /* Left fold expression. */
5322 expr = NULL_TREE;
5323 else
5324 /* Parse the parenthesized expression. */
5325 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5326
5327 token = cp_lexer_peek_token (parser->lexer);
5328 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5329 {
5330 expr = cp_parser_fold_expression (parser, expr);
5331 if (expr != error_mark_node
5332 && cxx_dialect < cxx17
5333 && !in_system_header_at (input_location))
5334 pedwarn (input_location, 0, "fold-expressions only available "
5335 "with %<-std=c++17%> or %<-std=gnu++17%>");
5336 }
5337 else
5338 /* Let the front end know that this expression was
5339 enclosed in parentheses. This matters in case, for
5340 example, the expression is of the form `A::B', since
5341 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5342 not. */
5343 expr = finish_parenthesized_expr (expr);
5344
5345 /* DR 705: Wrapping an unqualified name in parentheses
5346 suppresses arg-dependent lookup. We want to pass back
5347 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5348 (c++/37862), but none of the others. */
5349 if (*idk != CP_ID_KIND_QUALIFIED)
5350 *idk = CP_ID_KIND_NONE;
5351
5352 /* The `>' token might be the end of a template-id or
5353 template-parameter-list now. */
5354 parser->greater_than_is_operator_p
5355 = saved_greater_than_is_operator_p;
5356
5357 /* Consume the `)'. */
5358 token = cp_lexer_peek_token (parser->lexer);
5359 location_t close_paren_loc = token->location;
5360 expr.set_range (open_paren_loc, close_paren_loc);
5361 if (!parens.require_close (parser)
5362 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5363 cp_parser_skip_to_end_of_statement (parser);
5364
5365 return expr;
5366 }
5367
5368 case CPP_OPEN_SQUARE:
5369 {
5370 if (c_dialect_objc ())
5371 {
5372 /* We might have an Objective-C++ message. */
5373 cp_parser_parse_tentatively (parser);
5374 tree msg = cp_parser_objc_message_expression (parser);
5375 /* If that works out, we're done ... */
5376 if (cp_parser_parse_definitely (parser))
5377 return msg;
5378 /* ... else, fall though to see if it's a lambda. */
5379 }
5380 cp_expr lam = cp_parser_lambda_expression (parser);
5381 /* Don't warn about a failed tentative parse. */
5382 if (cp_parser_error_occurred (parser))
5383 return error_mark_node;
5384 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5385 return lam;
5386 }
5387
5388 case CPP_OBJC_STRING:
5389 if (c_dialect_objc ())
5390 /* We have an Objective-C++ string literal. */
5391 return cp_parser_objc_expression (parser);
5392 cp_parser_error (parser, "expected primary-expression");
5393 return error_mark_node;
5394
5395 case CPP_KEYWORD:
5396 switch (token->keyword)
5397 {
5398 /* These two are the boolean literals. */
5399 case RID_TRUE:
5400 cp_lexer_consume_token (parser->lexer);
5401 return cp_expr (boolean_true_node, token->location);
5402 case RID_FALSE:
5403 cp_lexer_consume_token (parser->lexer);
5404 return cp_expr (boolean_false_node, token->location);
5405
5406 /* The `__null' literal. */
5407 case RID_NULL:
5408 cp_lexer_consume_token (parser->lexer);
5409 return cp_expr (null_node, token->location);
5410
5411 /* The `nullptr' literal. */
5412 case RID_NULLPTR:
5413 cp_lexer_consume_token (parser->lexer);
5414 return cp_expr (nullptr_node, token->location);
5415
5416 /* Recognize the `this' keyword. */
5417 case RID_THIS:
5418 cp_lexer_consume_token (parser->lexer);
5419 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5420 {
5421 error_at (token->location,
5422 "%<this%> may not be used in this context");
5423 return error_mark_node;
5424 }
5425 /* Pointers cannot appear in constant-expressions. */
5426 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5427 return error_mark_node;
5428 return cp_expr (finish_this_expr (), token->location);
5429
5430 /* The `operator' keyword can be the beginning of an
5431 id-expression. */
5432 case RID_OPERATOR:
5433 goto id_expression;
5434
5435 case RID_FUNCTION_NAME:
5436 case RID_PRETTY_FUNCTION_NAME:
5437 case RID_C99_FUNCTION_NAME:
5438 {
5439 non_integral_constant name;
5440
5441 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5442 __func__ are the names of variables -- but they are
5443 treated specially. Therefore, they are handled here,
5444 rather than relying on the generic id-expression logic
5445 below. Grammatically, these names are id-expressions.
5446
5447 Consume the token. */
5448 token = cp_lexer_consume_token (parser->lexer);
5449
5450 switch (token->keyword)
5451 {
5452 case RID_FUNCTION_NAME:
5453 name = NIC_FUNC_NAME;
5454 break;
5455 case RID_PRETTY_FUNCTION_NAME:
5456 name = NIC_PRETTY_FUNC;
5457 break;
5458 case RID_C99_FUNCTION_NAME:
5459 name = NIC_C99_FUNC;
5460 break;
5461 default:
5462 gcc_unreachable ();
5463 }
5464
5465 if (cp_parser_non_integral_constant_expression (parser, name))
5466 return error_mark_node;
5467
5468 /* Look up the name. */
5469 return finish_fname (token->u.value);
5470 }
5471
5472 case RID_VA_ARG:
5473 {
5474 tree expression;
5475 tree type;
5476 location_t type_location;
5477 location_t start_loc
5478 = cp_lexer_peek_token (parser->lexer)->location;
5479 /* The `__builtin_va_arg' construct is used to handle
5480 `va_arg'. Consume the `__builtin_va_arg' token. */
5481 cp_lexer_consume_token (parser->lexer);
5482 /* Look for the opening `('. */
5483 matching_parens parens;
5484 parens.require_open (parser);
5485 /* Now, parse the assignment-expression. */
5486 expression = cp_parser_assignment_expression (parser);
5487 /* Look for the `,'. */
5488 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5489 type_location = cp_lexer_peek_token (parser->lexer)->location;
5490 /* Parse the type-id. */
5491 {
5492 type_id_in_expr_sentinel s (parser);
5493 type = cp_parser_type_id (parser);
5494 }
5495 /* Look for the closing `)'. */
5496 location_t finish_loc
5497 = cp_lexer_peek_token (parser->lexer)->location;
5498 parens.require_close (parser);
5499 /* Using `va_arg' in a constant-expression is not
5500 allowed. */
5501 if (cp_parser_non_integral_constant_expression (parser,
5502 NIC_VA_ARG))
5503 return error_mark_node;
5504 /* Construct a location of the form:
5505 __builtin_va_arg (v, int)
5506 ~~~~~~~~~~~~~~~~~~~~~^~~~
5507 with the caret at the type, ranging from the start of the
5508 "__builtin_va_arg" token to the close paren. */
5509 location_t combined_loc
5510 = make_location (type_location, start_loc, finish_loc);
5511 return build_x_va_arg (combined_loc, expression, type);
5512 }
5513
5514 case RID_OFFSETOF:
5515 return cp_parser_builtin_offsetof (parser);
5516
5517 case RID_HAS_NOTHROW_ASSIGN:
5518 case RID_HAS_NOTHROW_CONSTRUCTOR:
5519 case RID_HAS_NOTHROW_COPY:
5520 case RID_HAS_TRIVIAL_ASSIGN:
5521 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5522 case RID_HAS_TRIVIAL_COPY:
5523 case RID_HAS_TRIVIAL_DESTRUCTOR:
5524 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5525 case RID_HAS_VIRTUAL_DESTRUCTOR:
5526 case RID_IS_ABSTRACT:
5527 case RID_IS_AGGREGATE:
5528 case RID_IS_BASE_OF:
5529 case RID_IS_CLASS:
5530 case RID_IS_EMPTY:
5531 case RID_IS_ENUM:
5532 case RID_IS_FINAL:
5533 case RID_IS_LITERAL_TYPE:
5534 case RID_IS_POD:
5535 case RID_IS_POLYMORPHIC:
5536 case RID_IS_SAME_AS:
5537 case RID_IS_STD_LAYOUT:
5538 case RID_IS_TRIVIAL:
5539 case RID_IS_TRIVIALLY_ASSIGNABLE:
5540 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5541 case RID_IS_TRIVIALLY_COPYABLE:
5542 case RID_IS_UNION:
5543 case RID_IS_ASSIGNABLE:
5544 case RID_IS_CONSTRUCTIBLE:
5545 return cp_parser_trait_expr (parser, token->keyword);
5546
5547 // C++ concepts
5548 case RID_REQUIRES:
5549 return cp_parser_requires_expression (parser);
5550
5551 /* Objective-C++ expressions. */
5552 case RID_AT_ENCODE:
5553 case RID_AT_PROTOCOL:
5554 case RID_AT_SELECTOR:
5555 return cp_parser_objc_expression (parser);
5556
5557 case RID_TEMPLATE:
5558 if (parser->in_function_body
5559 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5560 == CPP_LESS))
5561 {
5562 error_at (token->location,
5563 "a template declaration cannot appear at block scope");
5564 cp_parser_skip_to_end_of_block_or_statement (parser);
5565 return error_mark_node;
5566 }
5567 /* FALLTHRU */
5568 default:
5569 cp_parser_error (parser, "expected primary-expression");
5570 return error_mark_node;
5571 }
5572
5573 /* An id-expression can start with either an identifier, a
5574 `::' as the beginning of a qualified-id, or the "operator"
5575 keyword. */
5576 case CPP_NAME:
5577 case CPP_SCOPE:
5578 case CPP_TEMPLATE_ID:
5579 case CPP_NESTED_NAME_SPECIFIER:
5580 {
5581 id_expression:
5582 cp_expr id_expression;
5583 cp_expr decl;
5584 const char *error_msg;
5585 bool template_p;
5586 bool done;
5587 cp_token *id_expr_token;
5588
5589 /* Parse the id-expression. */
5590 id_expression
5591 = cp_parser_id_expression (parser,
5592 /*template_keyword_p=*/false,
5593 /*check_dependency_p=*/true,
5594 &template_p,
5595 /*declarator_p=*/false,
5596 /*optional_p=*/false);
5597 if (id_expression == error_mark_node)
5598 return error_mark_node;
5599 id_expr_token = token;
5600 token = cp_lexer_peek_token (parser->lexer);
5601 done = (token->type != CPP_OPEN_SQUARE
5602 && token->type != CPP_OPEN_PAREN
5603 && token->type != CPP_DOT
5604 && token->type != CPP_DEREF
5605 && token->type != CPP_PLUS_PLUS
5606 && token->type != CPP_MINUS_MINUS);
5607 /* If we have a template-id, then no further lookup is
5608 required. If the template-id was for a template-class, we
5609 will sometimes have a TYPE_DECL at this point. */
5610 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5611 || TREE_CODE (id_expression) == TYPE_DECL)
5612 decl = id_expression;
5613 /* Look up the name. */
5614 else
5615 {
5616 tree ambiguous_decls;
5617
5618 /* If we already know that this lookup is ambiguous, then
5619 we've already issued an error message; there's no reason
5620 to check again. */
5621 if (id_expr_token->type == CPP_NAME
5622 && id_expr_token->error_reported)
5623 {
5624 cp_parser_simulate_error (parser);
5625 return error_mark_node;
5626 }
5627
5628 decl = cp_parser_lookup_name (parser, id_expression,
5629 none_type,
5630 template_p,
5631 /*is_namespace=*/false,
5632 /*check_dependency=*/true,
5633 &ambiguous_decls,
5634 id_expression.get_location ());
5635 /* If the lookup was ambiguous, an error will already have
5636 been issued. */
5637 if (ambiguous_decls)
5638 return error_mark_node;
5639
5640 /* In Objective-C++, we may have an Objective-C 2.0
5641 dot-syntax for classes here. */
5642 if (c_dialect_objc ()
5643 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5644 && TREE_CODE (decl) == TYPE_DECL
5645 && objc_is_class_name (decl))
5646 {
5647 tree component;
5648 cp_lexer_consume_token (parser->lexer);
5649 component = cp_parser_identifier (parser);
5650 if (component == error_mark_node)
5651 return error_mark_node;
5652
5653 tree result = objc_build_class_component_ref (id_expression,
5654 component);
5655 /* Build a location of the form:
5656 expr.component
5657 ~~~~~^~~~~~~~~
5658 with caret at the start of the component name (at
5659 input_location), ranging from the start of the id_expression
5660 to the end of the component name. */
5661 location_t combined_loc
5662 = make_location (input_location, id_expression.get_start (),
5663 get_finish (input_location));
5664 protected_set_expr_location (result, combined_loc);
5665 return result;
5666 }
5667
5668 /* In Objective-C++, an instance variable (ivar) may be preferred
5669 to whatever cp_parser_lookup_name() found.
5670 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5671 rest of c-family, we have to do a little extra work to preserve
5672 any location information in cp_expr "decl". Given that
5673 objc_lookup_ivar is implemented in "c-family" and "objc", we
5674 have a trip through the pure "tree" type, rather than cp_expr.
5675 Naively copying it back to "decl" would implicitly give the
5676 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5677 store an EXPR_LOCATION. Hence we only update "decl" (and
5678 hence its location_t) if we get back a different tree node. */
5679 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5680 id_expression);
5681 if (decl_tree != decl.get_value ())
5682 decl = cp_expr (decl_tree);
5683
5684 /* If name lookup gives us a SCOPE_REF, then the
5685 qualifying scope was dependent. */
5686 if (TREE_CODE (decl) == SCOPE_REF)
5687 {
5688 /* At this point, we do not know if DECL is a valid
5689 integral constant expression. We assume that it is
5690 in fact such an expression, so that code like:
5691
5692 template <int N> struct A {
5693 int a[B<N>::i];
5694 };
5695
5696 is accepted. At template-instantiation time, we
5697 will check that B<N>::i is actually a constant. */
5698 return decl;
5699 }
5700 /* Check to see if DECL is a local variable in a context
5701 where that is forbidden. */
5702 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
5703 && local_variable_p (decl))
5704 {
5705 error_at (id_expression.get_location (),
5706 "local variable %qD may not appear in this context",
5707 decl.get_value ());
5708 return error_mark_node;
5709 }
5710 }
5711
5712 decl = (finish_id_expression
5713 (id_expression, decl, parser->scope,
5714 idk,
5715 parser->integral_constant_expression_p,
5716 parser->allow_non_integral_constant_expression_p,
5717 &parser->non_integral_constant_expression_p,
5718 template_p, done, address_p,
5719 template_arg_p,
5720 &error_msg,
5721 id_expression.get_location ()));
5722 if (error_msg)
5723 cp_parser_error (parser, error_msg);
5724 /* Build a location for an id-expression of the form:
5725 ::ns::id
5726 ~~~~~~^~
5727 or:
5728 id
5729 ^~
5730 i.e. from the start of the first token to the end of the final
5731 token, with the caret at the start of the unqualified-id. */
5732 location_t caret_loc = get_pure_location (id_expression.get_location ());
5733 location_t start_loc = get_start (id_expr_token->location);
5734 location_t finish_loc = get_finish (id_expression.get_location ());
5735 location_t combined_loc
5736 = make_location (caret_loc, start_loc, finish_loc);
5737
5738 decl.set_location (combined_loc);
5739 return decl;
5740 }
5741
5742 /* Anything else is an error. */
5743 default:
5744 cp_parser_error (parser, "expected primary-expression");
5745 return error_mark_node;
5746 }
5747 }
5748
5749 static inline cp_expr
5750 cp_parser_primary_expression (cp_parser *parser,
5751 bool address_p,
5752 bool cast_p,
5753 bool template_arg_p,
5754 cp_id_kind *idk)
5755 {
5756 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5757 /*decltype*/false, idk);
5758 }
5759
5760 /* Parse an id-expression.
5761
5762 id-expression:
5763 unqualified-id
5764 qualified-id
5765
5766 qualified-id:
5767 :: [opt] nested-name-specifier template [opt] unqualified-id
5768 :: identifier
5769 :: operator-function-id
5770 :: template-id
5771
5772 Return a representation of the unqualified portion of the
5773 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5774 a `::' or nested-name-specifier.
5775
5776 Often, if the id-expression was a qualified-id, the caller will
5777 want to make a SCOPE_REF to represent the qualified-id. This
5778 function does not do this in order to avoid wastefully creating
5779 SCOPE_REFs when they are not required.
5780
5781 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5782 `template' keyword.
5783
5784 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5785 uninstantiated templates.
5786
5787 If *TEMPLATE_P is non-NULL, it is set to true iff the
5788 `template' keyword is used to explicitly indicate that the entity
5789 named is a template.
5790
5791 If DECLARATOR_P is true, the id-expression is appearing as part of
5792 a declarator, rather than as part of an expression. */
5793
5794 static cp_expr
5795 cp_parser_id_expression (cp_parser *parser,
5796 bool template_keyword_p,
5797 bool check_dependency_p,
5798 bool *template_p,
5799 bool declarator_p,
5800 bool optional_p)
5801 {
5802 bool global_scope_p;
5803 bool nested_name_specifier_p;
5804
5805 /* Assume the `template' keyword was not used. */
5806 if (template_p)
5807 *template_p = template_keyword_p;
5808
5809 /* Look for the optional `::' operator. */
5810 global_scope_p
5811 = (!template_keyword_p
5812 && (cp_parser_global_scope_opt (parser,
5813 /*current_scope_valid_p=*/false)
5814 != NULL_TREE));
5815
5816 /* Look for the optional nested-name-specifier. */
5817 nested_name_specifier_p
5818 = (cp_parser_nested_name_specifier_opt (parser,
5819 /*typename_keyword_p=*/false,
5820 check_dependency_p,
5821 /*type_p=*/false,
5822 declarator_p,
5823 template_keyword_p)
5824 != NULL_TREE);
5825
5826 /* If there is a nested-name-specifier, then we are looking at
5827 the first qualified-id production. */
5828 if (nested_name_specifier_p)
5829 {
5830 tree saved_scope;
5831 tree saved_object_scope;
5832 tree saved_qualifying_scope;
5833 cp_expr unqualified_id;
5834 bool is_template;
5835
5836 /* See if the next token is the `template' keyword. */
5837 if (!template_p)
5838 template_p = &is_template;
5839 *template_p = cp_parser_optional_template_keyword (parser);
5840 /* Name lookup we do during the processing of the
5841 unqualified-id might obliterate SCOPE. */
5842 saved_scope = parser->scope;
5843 saved_object_scope = parser->object_scope;
5844 saved_qualifying_scope = parser->qualifying_scope;
5845 /* Process the final unqualified-id. */
5846 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5847 check_dependency_p,
5848 declarator_p,
5849 /*optional_p=*/false);
5850 /* Restore the SAVED_SCOPE for our caller. */
5851 parser->scope = saved_scope;
5852 parser->object_scope = saved_object_scope;
5853 parser->qualifying_scope = saved_qualifying_scope;
5854
5855 return unqualified_id;
5856 }
5857 /* Otherwise, if we are in global scope, then we are looking at one
5858 of the other qualified-id productions. */
5859 else if (global_scope_p)
5860 {
5861 cp_token *token;
5862 tree id;
5863
5864 /* Peek at the next token. */
5865 token = cp_lexer_peek_token (parser->lexer);
5866
5867 /* If it's an identifier, and the next token is not a "<", then
5868 we can avoid the template-id case. This is an optimization
5869 for this common case. */
5870 if (token->type == CPP_NAME
5871 && !cp_parser_nth_token_starts_template_argument_list_p
5872 (parser, 2))
5873 return cp_parser_identifier (parser);
5874
5875 cp_parser_parse_tentatively (parser);
5876 /* Try a template-id. */
5877 id = cp_parser_template_id (parser,
5878 /*template_keyword_p=*/false,
5879 /*check_dependency_p=*/true,
5880 none_type,
5881 declarator_p);
5882 /* If that worked, we're done. */
5883 if (cp_parser_parse_definitely (parser))
5884 return id;
5885
5886 /* Peek at the next token. (Changes in the token buffer may
5887 have invalidated the pointer obtained above.) */
5888 token = cp_lexer_peek_token (parser->lexer);
5889
5890 switch (token->type)
5891 {
5892 case CPP_NAME:
5893 return cp_parser_identifier (parser);
5894
5895 case CPP_KEYWORD:
5896 if (token->keyword == RID_OPERATOR)
5897 return cp_parser_operator_function_id (parser);
5898 /* Fall through. */
5899
5900 default:
5901 cp_parser_error (parser, "expected id-expression");
5902 return error_mark_node;
5903 }
5904 }
5905 else
5906 return cp_parser_unqualified_id (parser, template_keyword_p,
5907 /*check_dependency_p=*/true,
5908 declarator_p,
5909 optional_p);
5910 }
5911
5912 /* Parse an unqualified-id.
5913
5914 unqualified-id:
5915 identifier
5916 operator-function-id
5917 conversion-function-id
5918 ~ class-name
5919 template-id
5920
5921 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5922 keyword, in a construct like `A::template ...'.
5923
5924 Returns a representation of unqualified-id. For the `identifier'
5925 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5926 production a BIT_NOT_EXPR is returned; the operand of the
5927 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5928 other productions, see the documentation accompanying the
5929 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5930 names are looked up in uninstantiated templates. If DECLARATOR_P
5931 is true, the unqualified-id is appearing as part of a declarator,
5932 rather than as part of an expression. */
5933
5934 static cp_expr
5935 cp_parser_unqualified_id (cp_parser* parser,
5936 bool template_keyword_p,
5937 bool check_dependency_p,
5938 bool declarator_p,
5939 bool optional_p)
5940 {
5941 cp_token *token;
5942
5943 /* Peek at the next token. */
5944 token = cp_lexer_peek_token (parser->lexer);
5945
5946 switch ((int) token->type)
5947 {
5948 case CPP_NAME:
5949 {
5950 tree id;
5951
5952 /* We don't know yet whether or not this will be a
5953 template-id. */
5954 cp_parser_parse_tentatively (parser);
5955 /* Try a template-id. */
5956 id = cp_parser_template_id (parser, template_keyword_p,
5957 check_dependency_p,
5958 none_type,
5959 declarator_p);
5960 /* If it worked, we're done. */
5961 if (cp_parser_parse_definitely (parser))
5962 return id;
5963 /* Otherwise, it's an ordinary identifier. */
5964 return cp_parser_identifier (parser);
5965 }
5966
5967 case CPP_TEMPLATE_ID:
5968 return cp_parser_template_id (parser, template_keyword_p,
5969 check_dependency_p,
5970 none_type,
5971 declarator_p);
5972
5973 case CPP_COMPL:
5974 {
5975 tree type_decl;
5976 tree qualifying_scope;
5977 tree object_scope;
5978 tree scope;
5979 bool done;
5980 location_t tilde_loc = token->location;
5981
5982 /* Consume the `~' token. */
5983 cp_lexer_consume_token (parser->lexer);
5984 /* Parse the class-name. The standard, as written, seems to
5985 say that:
5986
5987 template <typename T> struct S { ~S (); };
5988 template <typename T> S<T>::~S() {}
5989
5990 is invalid, since `~' must be followed by a class-name, but
5991 `S<T>' is dependent, and so not known to be a class.
5992 That's not right; we need to look in uninstantiated
5993 templates. A further complication arises from:
5994
5995 template <typename T> void f(T t) {
5996 t.T::~T();
5997 }
5998
5999 Here, it is not possible to look up `T' in the scope of `T'
6000 itself. We must look in both the current scope, and the
6001 scope of the containing complete expression.
6002
6003 Yet another issue is:
6004
6005 struct S {
6006 int S;
6007 ~S();
6008 };
6009
6010 S::~S() {}
6011
6012 The standard does not seem to say that the `S' in `~S'
6013 should refer to the type `S' and not the data member
6014 `S::S'. */
6015
6016 /* DR 244 says that we look up the name after the "~" in the
6017 same scope as we looked up the qualifying name. That idea
6018 isn't fully worked out; it's more complicated than that. */
6019 scope = parser->scope;
6020 object_scope = parser->object_scope;
6021 qualifying_scope = parser->qualifying_scope;
6022
6023 /* Check for invalid scopes. */
6024 if (scope == error_mark_node)
6025 {
6026 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6027 cp_lexer_consume_token (parser->lexer);
6028 return error_mark_node;
6029 }
6030 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6031 {
6032 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6033 error_at (token->location,
6034 "scope %qT before %<~%> is not a class-name",
6035 scope);
6036 cp_parser_simulate_error (parser);
6037 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6038 cp_lexer_consume_token (parser->lexer);
6039 return error_mark_node;
6040 }
6041 gcc_assert (!scope || TYPE_P (scope));
6042
6043 token = cp_lexer_peek_token (parser->lexer);
6044
6045 /* Create a location with caret == start at the tilde,
6046 finishing at the end of the peeked token, e.g:
6047 ~token
6048 ^~~~~~. */
6049 location_t loc
6050 = make_location (tilde_loc, tilde_loc, token->location);
6051
6052 /* If the name is of the form "X::~X" it's OK even if X is a
6053 typedef. */
6054
6055 if (scope
6056 && token->type == CPP_NAME
6057 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6058 != CPP_LESS)
6059 && (token->u.value == TYPE_IDENTIFIER (scope)
6060 || (CLASS_TYPE_P (scope)
6061 && constructor_name_p (token->u.value, scope))))
6062 {
6063 cp_lexer_consume_token (parser->lexer);
6064 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6065 }
6066
6067 /* ~auto means the destructor of whatever the object is. */
6068 if (cp_parser_is_keyword (token, RID_AUTO))
6069 {
6070 if (cxx_dialect < cxx14)
6071 pedwarn (loc, 0,
6072 "%<~auto%> only available with "
6073 "%<-std=c++14%> or %<-std=gnu++14%>");
6074 cp_lexer_consume_token (parser->lexer);
6075 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6076 }
6077
6078 /* If there was an explicit qualification (S::~T), first look
6079 in the scope given by the qualification (i.e., S).
6080
6081 Note: in the calls to cp_parser_class_name below we pass
6082 typename_type so that lookup finds the injected-class-name
6083 rather than the constructor. */
6084 done = false;
6085 type_decl = NULL_TREE;
6086 if (scope)
6087 {
6088 cp_parser_parse_tentatively (parser);
6089 type_decl = cp_parser_class_name (parser,
6090 /*typename_keyword_p=*/false,
6091 /*template_keyword_p=*/false,
6092 typename_type,
6093 /*check_dependency=*/false,
6094 /*class_head_p=*/false,
6095 declarator_p);
6096 if (cp_parser_parse_definitely (parser))
6097 done = true;
6098 }
6099 /* In "N::S::~S", look in "N" as well. */
6100 if (!done && scope && qualifying_scope)
6101 {
6102 cp_parser_parse_tentatively (parser);
6103 parser->scope = qualifying_scope;
6104 parser->object_scope = NULL_TREE;
6105 parser->qualifying_scope = NULL_TREE;
6106 type_decl
6107 = cp_parser_class_name (parser,
6108 /*typename_keyword_p=*/false,
6109 /*template_keyword_p=*/false,
6110 typename_type,
6111 /*check_dependency=*/false,
6112 /*class_head_p=*/false,
6113 declarator_p);
6114 if (cp_parser_parse_definitely (parser))
6115 done = true;
6116 }
6117 /* In "p->S::~T", look in the scope given by "*p" as well. */
6118 else if (!done && object_scope)
6119 {
6120 cp_parser_parse_tentatively (parser);
6121 parser->scope = object_scope;
6122 parser->object_scope = NULL_TREE;
6123 parser->qualifying_scope = NULL_TREE;
6124 type_decl
6125 = cp_parser_class_name (parser,
6126 /*typename_keyword_p=*/false,
6127 /*template_keyword_p=*/false,
6128 typename_type,
6129 /*check_dependency=*/false,
6130 /*class_head_p=*/false,
6131 declarator_p);
6132 if (cp_parser_parse_definitely (parser))
6133 done = true;
6134 }
6135 /* Look in the surrounding context. */
6136 if (!done)
6137 {
6138 parser->scope = NULL_TREE;
6139 parser->object_scope = NULL_TREE;
6140 parser->qualifying_scope = NULL_TREE;
6141 if (processing_template_decl)
6142 cp_parser_parse_tentatively (parser);
6143 type_decl
6144 = cp_parser_class_name (parser,
6145 /*typename_keyword_p=*/false,
6146 /*template_keyword_p=*/false,
6147 typename_type,
6148 /*check_dependency=*/false,
6149 /*class_head_p=*/false,
6150 declarator_p);
6151 if (processing_template_decl
6152 && ! cp_parser_parse_definitely (parser))
6153 {
6154 /* We couldn't find a type with this name. If we're parsing
6155 tentatively, fail and try something else. */
6156 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6157 {
6158 cp_parser_simulate_error (parser);
6159 return error_mark_node;
6160 }
6161 /* Otherwise, accept it and check for a match at instantiation
6162 time. */
6163 type_decl = cp_parser_identifier (parser);
6164 if (type_decl != error_mark_node)
6165 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6166 return type_decl;
6167 }
6168 }
6169 /* If an error occurred, assume that the name of the
6170 destructor is the same as the name of the qualifying
6171 class. That allows us to keep parsing after running
6172 into ill-formed destructor names. */
6173 if (type_decl == error_mark_node && scope)
6174 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6175 else if (type_decl == error_mark_node)
6176 return error_mark_node;
6177
6178 /* Check that destructor name and scope match. */
6179 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6180 {
6181 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6182 error_at (loc,
6183 "declaration of %<~%T%> as member of %qT",
6184 type_decl, scope);
6185 cp_parser_simulate_error (parser);
6186 return error_mark_node;
6187 }
6188
6189 /* [class.dtor]
6190
6191 A typedef-name that names a class shall not be used as the
6192 identifier in the declarator for a destructor declaration. */
6193 if (declarator_p
6194 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6195 && !DECL_SELF_REFERENCE_P (type_decl)
6196 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6197 error_at (loc,
6198 "typedef-name %qD used as destructor declarator",
6199 type_decl);
6200
6201 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6202 }
6203
6204 case CPP_KEYWORD:
6205 if (token->keyword == RID_OPERATOR)
6206 {
6207 cp_expr id;
6208
6209 /* This could be a template-id, so we try that first. */
6210 cp_parser_parse_tentatively (parser);
6211 /* Try a template-id. */
6212 id = cp_parser_template_id (parser, template_keyword_p,
6213 /*check_dependency_p=*/true,
6214 none_type,
6215 declarator_p);
6216 /* If that worked, we're done. */
6217 if (cp_parser_parse_definitely (parser))
6218 return id;
6219 /* We still don't know whether we're looking at an
6220 operator-function-id or a conversion-function-id. */
6221 cp_parser_parse_tentatively (parser);
6222 /* Try an operator-function-id. */
6223 id = cp_parser_operator_function_id (parser);
6224 /* If that didn't work, try a conversion-function-id. */
6225 if (!cp_parser_parse_definitely (parser))
6226 id = cp_parser_conversion_function_id (parser);
6227
6228 return id;
6229 }
6230 /* Fall through. */
6231
6232 default:
6233 if (optional_p)
6234 return NULL_TREE;
6235 cp_parser_error (parser, "expected unqualified-id");
6236 return error_mark_node;
6237 }
6238 }
6239
6240 /* Parse an (optional) nested-name-specifier.
6241
6242 nested-name-specifier: [C++98]
6243 class-or-namespace-name :: nested-name-specifier [opt]
6244 class-or-namespace-name :: template nested-name-specifier [opt]
6245
6246 nested-name-specifier: [C++0x]
6247 type-name ::
6248 namespace-name ::
6249 nested-name-specifier identifier ::
6250 nested-name-specifier template [opt] simple-template-id ::
6251
6252 PARSER->SCOPE should be set appropriately before this function is
6253 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6254 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6255 in name lookups.
6256
6257 Sets PARSER->SCOPE to the class (TYPE) or namespace
6258 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6259 it unchanged if there is no nested-name-specifier. Returns the new
6260 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6261
6262 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6263 part of a declaration and/or decl-specifier. */
6264
6265 static tree
6266 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6267 bool typename_keyword_p,
6268 bool check_dependency_p,
6269 bool type_p,
6270 bool is_declaration,
6271 bool template_keyword_p /* = false */)
6272 {
6273 bool success = false;
6274 cp_token_position start = 0;
6275 cp_token *token;
6276
6277 /* Remember where the nested-name-specifier starts. */
6278 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6279 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6280 {
6281 start = cp_lexer_token_position (parser->lexer, false);
6282 push_deferring_access_checks (dk_deferred);
6283 }
6284
6285 while (true)
6286 {
6287 tree new_scope;
6288 tree old_scope;
6289 tree saved_qualifying_scope;
6290
6291 /* Spot cases that cannot be the beginning of a
6292 nested-name-specifier. */
6293 token = cp_lexer_peek_token (parser->lexer);
6294
6295 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6296 the already parsed nested-name-specifier. */
6297 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6298 {
6299 /* Grab the nested-name-specifier and continue the loop. */
6300 cp_parser_pre_parsed_nested_name_specifier (parser);
6301 /* If we originally encountered this nested-name-specifier
6302 with IS_DECLARATION set to false, we will not have
6303 resolved TYPENAME_TYPEs, so we must do so here. */
6304 if (is_declaration
6305 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6306 {
6307 new_scope = resolve_typename_type (parser->scope,
6308 /*only_current_p=*/false);
6309 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6310 parser->scope = new_scope;
6311 }
6312 success = true;
6313 continue;
6314 }
6315
6316 /* Spot cases that cannot be the beginning of a
6317 nested-name-specifier. On the second and subsequent times
6318 through the loop, we look for the `template' keyword. */
6319 if (success && token->keyword == RID_TEMPLATE)
6320 ;
6321 /* A template-id can start a nested-name-specifier. */
6322 else if (token->type == CPP_TEMPLATE_ID)
6323 ;
6324 /* DR 743: decltype can be used in a nested-name-specifier. */
6325 else if (token_is_decltype (token))
6326 ;
6327 else
6328 {
6329 /* If the next token is not an identifier, then it is
6330 definitely not a type-name or namespace-name. */
6331 if (token->type != CPP_NAME)
6332 break;
6333 /* If the following token is neither a `<' (to begin a
6334 template-id), nor a `::', then we are not looking at a
6335 nested-name-specifier. */
6336 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6337
6338 if (token->type == CPP_COLON
6339 && parser->colon_corrects_to_scope_p
6340 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6341 {
6342 gcc_rich_location richloc (token->location);
6343 richloc.add_fixit_replace ("::");
6344 error_at (&richloc,
6345 "found %<:%> in nested-name-specifier, "
6346 "expected %<::%>");
6347 token->type = CPP_SCOPE;
6348 }
6349
6350 if (token->type != CPP_SCOPE
6351 && !cp_parser_nth_token_starts_template_argument_list_p
6352 (parser, 2))
6353 break;
6354 }
6355
6356 /* The nested-name-specifier is optional, so we parse
6357 tentatively. */
6358 cp_parser_parse_tentatively (parser);
6359
6360 /* Look for the optional `template' keyword, if this isn't the
6361 first time through the loop. */
6362 if (success)
6363 template_keyword_p = cp_parser_optional_template_keyword (parser);
6364
6365 /* Save the old scope since the name lookup we are about to do
6366 might destroy it. */
6367 old_scope = parser->scope;
6368 saved_qualifying_scope = parser->qualifying_scope;
6369 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6370 look up names in "X<T>::I" in order to determine that "Y" is
6371 a template. So, if we have a typename at this point, we make
6372 an effort to look through it. */
6373 if (is_declaration
6374 && !typename_keyword_p
6375 && parser->scope
6376 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6377 parser->scope = resolve_typename_type (parser->scope,
6378 /*only_current_p=*/false);
6379 /* Parse the qualifying entity. */
6380 new_scope
6381 = cp_parser_qualifying_entity (parser,
6382 typename_keyword_p,
6383 template_keyword_p,
6384 check_dependency_p,
6385 type_p,
6386 is_declaration);
6387 /* Look for the `::' token. */
6388 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6389
6390 /* If we found what we wanted, we keep going; otherwise, we're
6391 done. */
6392 if (!cp_parser_parse_definitely (parser))
6393 {
6394 bool error_p = false;
6395
6396 /* Restore the OLD_SCOPE since it was valid before the
6397 failed attempt at finding the last
6398 class-or-namespace-name. */
6399 parser->scope = old_scope;
6400 parser->qualifying_scope = saved_qualifying_scope;
6401
6402 /* If the next token is a decltype, and the one after that is a
6403 `::', then the decltype has failed to resolve to a class or
6404 enumeration type. Give this error even when parsing
6405 tentatively since it can't possibly be valid--and we're going
6406 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6407 won't get another chance.*/
6408 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6409 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6410 == CPP_SCOPE))
6411 {
6412 token = cp_lexer_consume_token (parser->lexer);
6413 error_at (token->location, "%<decltype%> evaluates to %qT, "
6414 "which is not a class or enumeration type",
6415 token->u.tree_check_value->value);
6416 parser->scope = error_mark_node;
6417 error_p = true;
6418 /* As below. */
6419 success = true;
6420 cp_lexer_consume_token (parser->lexer);
6421 }
6422
6423 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6424 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6425 {
6426 /* If we have a non-type template-id followed by ::, it can't
6427 possibly be valid. */
6428 token = cp_lexer_peek_token (parser->lexer);
6429 tree tid = token->u.tree_check_value->value;
6430 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6431 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6432 {
6433 tree tmpl = NULL_TREE;
6434 if (is_overloaded_fn (tid))
6435 {
6436 tree fns = get_fns (tid);
6437 if (OVL_SINGLE_P (fns))
6438 tmpl = OVL_FIRST (fns);
6439 error_at (token->location, "function template-id %qD "
6440 "in nested-name-specifier", tid);
6441 }
6442 else
6443 {
6444 /* Variable template. */
6445 tmpl = TREE_OPERAND (tid, 0);
6446 gcc_assert (variable_template_p (tmpl));
6447 error_at (token->location, "variable template-id %qD "
6448 "in nested-name-specifier", tid);
6449 }
6450 if (tmpl)
6451 inform (DECL_SOURCE_LOCATION (tmpl),
6452 "%qD declared here", tmpl);
6453
6454 parser->scope = error_mark_node;
6455 error_p = true;
6456 /* As below. */
6457 success = true;
6458 cp_lexer_consume_token (parser->lexer);
6459 cp_lexer_consume_token (parser->lexer);
6460 }
6461 }
6462
6463 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6464 break;
6465 /* If the next token is an identifier, and the one after
6466 that is a `::', then any valid interpretation would have
6467 found a class-or-namespace-name. */
6468 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6469 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6470 == CPP_SCOPE)
6471 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6472 != CPP_COMPL))
6473 {
6474 token = cp_lexer_consume_token (parser->lexer);
6475 if (!error_p)
6476 {
6477 if (!token->error_reported)
6478 {
6479 tree decl;
6480 tree ambiguous_decls;
6481
6482 decl = cp_parser_lookup_name (parser, token->u.value,
6483 none_type,
6484 /*is_template=*/false,
6485 /*is_namespace=*/false,
6486 /*check_dependency=*/true,
6487 &ambiguous_decls,
6488 token->location);
6489 if (TREE_CODE (decl) == TEMPLATE_DECL)
6490 error_at (token->location,
6491 "%qD used without template arguments",
6492 decl);
6493 else if (ambiguous_decls)
6494 {
6495 // cp_parser_lookup_name has the same diagnostic,
6496 // thus make sure to emit it at most once.
6497 if (cp_parser_uncommitted_to_tentative_parse_p
6498 (parser))
6499 {
6500 error_at (token->location,
6501 "reference to %qD is ambiguous",
6502 token->u.value);
6503 print_candidates (ambiguous_decls);
6504 }
6505 decl = error_mark_node;
6506 }
6507 else
6508 {
6509 if (cxx_dialect != cxx98)
6510 cp_parser_name_lookup_error
6511 (parser, token->u.value, decl, NLE_NOT_CXX98,
6512 token->location);
6513 else
6514 cp_parser_name_lookup_error
6515 (parser, token->u.value, decl, NLE_CXX98,
6516 token->location);
6517 }
6518 }
6519 parser->scope = error_mark_node;
6520 error_p = true;
6521 /* Treat this as a successful nested-name-specifier
6522 due to:
6523
6524 [basic.lookup.qual]
6525
6526 If the name found is not a class-name (clause
6527 _class_) or namespace-name (_namespace.def_), the
6528 program is ill-formed. */
6529 success = true;
6530 }
6531 cp_lexer_consume_token (parser->lexer);
6532 }
6533 break;
6534 }
6535 /* We've found one valid nested-name-specifier. */
6536 success = true;
6537 /* Name lookup always gives us a DECL. */
6538 if (TREE_CODE (new_scope) == TYPE_DECL)
6539 new_scope = TREE_TYPE (new_scope);
6540 /* Uses of "template" must be followed by actual templates. */
6541 if (template_keyword_p
6542 && !(CLASS_TYPE_P (new_scope)
6543 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6544 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6545 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6546 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6547 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6548 == TEMPLATE_ID_EXPR)))
6549 permerror (input_location, TYPE_P (new_scope)
6550 ? G_("%qT is not a template")
6551 : G_("%qD is not a template"),
6552 new_scope);
6553 /* If it is a class scope, try to complete it; we are about to
6554 be looking up names inside the class. */
6555 if (TYPE_P (new_scope)
6556 /* Since checking types for dependency can be expensive,
6557 avoid doing it if the type is already complete. */
6558 && !COMPLETE_TYPE_P (new_scope)
6559 /* Do not try to complete dependent types. */
6560 && !dependent_type_p (new_scope))
6561 {
6562 new_scope = complete_type (new_scope);
6563 /* If it is a typedef to current class, use the current
6564 class instead, as the typedef won't have any names inside
6565 it yet. */
6566 if (!COMPLETE_TYPE_P (new_scope)
6567 && currently_open_class (new_scope))
6568 new_scope = TYPE_MAIN_VARIANT (new_scope);
6569 }
6570 /* Make sure we look in the right scope the next time through
6571 the loop. */
6572 parser->scope = new_scope;
6573 }
6574
6575 /* If parsing tentatively, replace the sequence of tokens that makes
6576 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6577 token. That way, should we re-parse the token stream, we will
6578 not have to repeat the effort required to do the parse, nor will
6579 we issue duplicate error messages. */
6580 if (success && start)
6581 {
6582 cp_token *token;
6583
6584 token = cp_lexer_token_at (parser->lexer, start);
6585 /* Reset the contents of the START token. */
6586 token->type = CPP_NESTED_NAME_SPECIFIER;
6587 /* Retrieve any deferred checks. Do not pop this access checks yet
6588 so the memory will not be reclaimed during token replacing below. */
6589 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6590 token->u.tree_check_value->value = parser->scope;
6591 token->u.tree_check_value->checks = get_deferred_access_checks ();
6592 token->u.tree_check_value->qualifying_scope =
6593 parser->qualifying_scope;
6594 token->keyword = RID_MAX;
6595
6596 /* Purge all subsequent tokens. */
6597 cp_lexer_purge_tokens_after (parser->lexer, start);
6598 }
6599
6600 if (start)
6601 pop_to_parent_deferring_access_checks ();
6602
6603 return success ? parser->scope : NULL_TREE;
6604 }
6605
6606 /* Parse a nested-name-specifier. See
6607 cp_parser_nested_name_specifier_opt for details. This function
6608 behaves identically, except that it will an issue an error if no
6609 nested-name-specifier is present. */
6610
6611 static tree
6612 cp_parser_nested_name_specifier (cp_parser *parser,
6613 bool typename_keyword_p,
6614 bool check_dependency_p,
6615 bool type_p,
6616 bool is_declaration)
6617 {
6618 tree scope;
6619
6620 /* Look for the nested-name-specifier. */
6621 scope = cp_parser_nested_name_specifier_opt (parser,
6622 typename_keyword_p,
6623 check_dependency_p,
6624 type_p,
6625 is_declaration);
6626 /* If it was not present, issue an error message. */
6627 if (!scope)
6628 {
6629 cp_parser_error (parser, "expected nested-name-specifier");
6630 parser->scope = NULL_TREE;
6631 }
6632
6633 return scope;
6634 }
6635
6636 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6637 this is either a class-name or a namespace-name (which corresponds
6638 to the class-or-namespace-name production in the grammar). For
6639 C++0x, it can also be a type-name that refers to an enumeration
6640 type or a simple-template-id.
6641
6642 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6643 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6644 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6645 TYPE_P is TRUE iff the next name should be taken as a class-name,
6646 even the same name is declared to be another entity in the same
6647 scope.
6648
6649 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6650 specified by the class-or-namespace-name. If neither is found the
6651 ERROR_MARK_NODE is returned. */
6652
6653 static tree
6654 cp_parser_qualifying_entity (cp_parser *parser,
6655 bool typename_keyword_p,
6656 bool template_keyword_p,
6657 bool check_dependency_p,
6658 bool type_p,
6659 bool is_declaration)
6660 {
6661 tree saved_scope;
6662 tree saved_qualifying_scope;
6663 tree saved_object_scope;
6664 tree scope;
6665 bool only_class_p;
6666 bool successful_parse_p;
6667
6668 /* DR 743: decltype can appear in a nested-name-specifier. */
6669 if (cp_lexer_next_token_is_decltype (parser->lexer))
6670 {
6671 scope = cp_parser_decltype (parser);
6672 if (TREE_CODE (scope) != ENUMERAL_TYPE
6673 && !MAYBE_CLASS_TYPE_P (scope))
6674 {
6675 cp_parser_simulate_error (parser);
6676 return error_mark_node;
6677 }
6678 if (TYPE_NAME (scope))
6679 scope = TYPE_NAME (scope);
6680 return scope;
6681 }
6682
6683 /* Before we try to parse the class-name, we must save away the
6684 current PARSER->SCOPE since cp_parser_class_name will destroy
6685 it. */
6686 saved_scope = parser->scope;
6687 saved_qualifying_scope = parser->qualifying_scope;
6688 saved_object_scope = parser->object_scope;
6689 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6690 there is no need to look for a namespace-name. */
6691 only_class_p = template_keyword_p
6692 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6693 if (!only_class_p)
6694 cp_parser_parse_tentatively (parser);
6695 scope = cp_parser_class_name (parser,
6696 typename_keyword_p,
6697 template_keyword_p,
6698 type_p ? class_type : none_type,
6699 check_dependency_p,
6700 /*class_head_p=*/false,
6701 is_declaration,
6702 /*enum_ok=*/cxx_dialect > cxx98);
6703 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6704 /* If that didn't work, try for a namespace-name. */
6705 if (!only_class_p && !successful_parse_p)
6706 {
6707 /* Restore the saved scope. */
6708 parser->scope = saved_scope;
6709 parser->qualifying_scope = saved_qualifying_scope;
6710 parser->object_scope = saved_object_scope;
6711 /* If we are not looking at an identifier followed by the scope
6712 resolution operator, then this is not part of a
6713 nested-name-specifier. (Note that this function is only used
6714 to parse the components of a nested-name-specifier.) */
6715 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6716 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6717 return error_mark_node;
6718 scope = cp_parser_namespace_name (parser);
6719 }
6720
6721 return scope;
6722 }
6723
6724 /* Return true if we are looking at a compound-literal, false otherwise. */
6725
6726 static bool
6727 cp_parser_compound_literal_p (cp_parser *parser)
6728 {
6729 cp_lexer_save_tokens (parser->lexer);
6730
6731 /* Skip tokens until the next token is a closing parenthesis.
6732 If we find the closing `)', and the next token is a `{', then
6733 we are looking at a compound-literal. */
6734 bool compound_literal_p
6735 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6736 /*consume_paren=*/true)
6737 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6738
6739 /* Roll back the tokens we skipped. */
6740 cp_lexer_rollback_tokens (parser->lexer);
6741
6742 return compound_literal_p;
6743 }
6744
6745 /* Return true if EXPR is the integer constant zero or a complex constant
6746 of zero, without any folding, but ignoring location wrappers. */
6747
6748 bool
6749 literal_integer_zerop (const_tree expr)
6750 {
6751 return (location_wrapper_p (expr)
6752 && integer_zerop (TREE_OPERAND (expr, 0)));
6753 }
6754
6755 /* Parse a postfix-expression.
6756
6757 postfix-expression:
6758 primary-expression
6759 postfix-expression [ expression ]
6760 postfix-expression ( expression-list [opt] )
6761 simple-type-specifier ( expression-list [opt] )
6762 typename :: [opt] nested-name-specifier identifier
6763 ( expression-list [opt] )
6764 typename :: [opt] nested-name-specifier template [opt] template-id
6765 ( expression-list [opt] )
6766 postfix-expression . template [opt] id-expression
6767 postfix-expression -> template [opt] id-expression
6768 postfix-expression . pseudo-destructor-name
6769 postfix-expression -> pseudo-destructor-name
6770 postfix-expression ++
6771 postfix-expression --
6772 dynamic_cast < type-id > ( expression )
6773 static_cast < type-id > ( expression )
6774 reinterpret_cast < type-id > ( expression )
6775 const_cast < type-id > ( expression )
6776 typeid ( expression )
6777 typeid ( type-id )
6778
6779 GNU Extension:
6780
6781 postfix-expression:
6782 ( type-id ) { initializer-list , [opt] }
6783
6784 This extension is a GNU version of the C99 compound-literal
6785 construct. (The C99 grammar uses `type-name' instead of `type-id',
6786 but they are essentially the same concept.)
6787
6788 If ADDRESS_P is true, the postfix expression is the operand of the
6789 `&' operator. CAST_P is true if this expression is the target of a
6790 cast.
6791
6792 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6793 class member access expressions [expr.ref].
6794
6795 Returns a representation of the expression. */
6796
6797 static cp_expr
6798 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6799 bool member_access_only_p, bool decltype_p,
6800 cp_id_kind * pidk_return)
6801 {
6802 cp_token *token;
6803 location_t loc;
6804 enum rid keyword;
6805 cp_id_kind idk = CP_ID_KIND_NONE;
6806 cp_expr postfix_expression = NULL_TREE;
6807 bool is_member_access = false;
6808
6809 /* Peek at the next token. */
6810 token = cp_lexer_peek_token (parser->lexer);
6811 loc = token->location;
6812 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6813
6814 /* Some of the productions are determined by keywords. */
6815 keyword = token->keyword;
6816 switch (keyword)
6817 {
6818 case RID_DYNCAST:
6819 case RID_STATCAST:
6820 case RID_REINTCAST:
6821 case RID_CONSTCAST:
6822 {
6823 tree type;
6824 cp_expr expression;
6825 const char *saved_message;
6826 bool saved_in_type_id_in_expr_p;
6827
6828 /* All of these can be handled in the same way from the point
6829 of view of parsing. Begin by consuming the token
6830 identifying the cast. */
6831 cp_lexer_consume_token (parser->lexer);
6832
6833 /* New types cannot be defined in the cast. */
6834 saved_message = parser->type_definition_forbidden_message;
6835 parser->type_definition_forbidden_message
6836 = G_("types may not be defined in casts");
6837
6838 /* Look for the opening `<'. */
6839 cp_parser_require (parser, CPP_LESS, RT_LESS);
6840 /* Parse the type to which we are casting. */
6841 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6842 parser->in_type_id_in_expr_p = true;
6843 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
6844 NULL);
6845 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6846 /* Look for the closing `>'. */
6847 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6848 /* Restore the old message. */
6849 parser->type_definition_forbidden_message = saved_message;
6850
6851 bool saved_greater_than_is_operator_p
6852 = parser->greater_than_is_operator_p;
6853 parser->greater_than_is_operator_p = true;
6854
6855 /* And the expression which is being cast. */
6856 matching_parens parens;
6857 parens.require_open (parser);
6858 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6859 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6860 RT_CLOSE_PAREN);
6861 location_t end_loc = close_paren ?
6862 close_paren->location : UNKNOWN_LOCATION;
6863
6864 parser->greater_than_is_operator_p
6865 = saved_greater_than_is_operator_p;
6866
6867 /* Only type conversions to integral or enumeration types
6868 can be used in constant-expressions. */
6869 if (!cast_valid_in_integral_constant_expression_p (type)
6870 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6871 {
6872 postfix_expression = error_mark_node;
6873 break;
6874 }
6875
6876 switch (keyword)
6877 {
6878 case RID_DYNCAST:
6879 postfix_expression
6880 = build_dynamic_cast (type, expression, tf_warning_or_error);
6881 break;
6882 case RID_STATCAST:
6883 postfix_expression
6884 = build_static_cast (type, expression, tf_warning_or_error);
6885 break;
6886 case RID_REINTCAST:
6887 postfix_expression
6888 = build_reinterpret_cast (type, expression,
6889 tf_warning_or_error);
6890 break;
6891 case RID_CONSTCAST:
6892 postfix_expression
6893 = build_const_cast (type, expression, tf_warning_or_error);
6894 break;
6895 default:
6896 gcc_unreachable ();
6897 }
6898
6899 /* Construct a location e.g. :
6900 reinterpret_cast <int *> (expr)
6901 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6902 ranging from the start of the "*_cast" token to the final closing
6903 paren, with the caret at the start. */
6904 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6905 postfix_expression.set_location (cp_cast_loc);
6906 }
6907 break;
6908
6909 case RID_TYPEID:
6910 {
6911 tree type;
6912 const char *saved_message;
6913 bool saved_in_type_id_in_expr_p;
6914
6915 /* Consume the `typeid' token. */
6916 cp_lexer_consume_token (parser->lexer);
6917 /* Look for the `(' token. */
6918 matching_parens parens;
6919 parens.require_open (parser);
6920 /* Types cannot be defined in a `typeid' expression. */
6921 saved_message = parser->type_definition_forbidden_message;
6922 parser->type_definition_forbidden_message
6923 = G_("types may not be defined in a %<typeid%> expression");
6924 /* We can't be sure yet whether we're looking at a type-id or an
6925 expression. */
6926 cp_parser_parse_tentatively (parser);
6927 /* Try a type-id first. */
6928 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6929 parser->in_type_id_in_expr_p = true;
6930 type = cp_parser_type_id (parser);
6931 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6932 /* Look for the `)' token. Otherwise, we can't be sure that
6933 we're not looking at an expression: consider `typeid (int
6934 (3))', for example. */
6935 cp_token *close_paren = parens.require_close (parser);
6936 /* If all went well, simply lookup the type-id. */
6937 if (cp_parser_parse_definitely (parser))
6938 postfix_expression = get_typeid (type, tf_warning_or_error);
6939 /* Otherwise, fall back to the expression variant. */
6940 else
6941 {
6942 tree expression;
6943
6944 /* Look for an expression. */
6945 expression = cp_parser_expression (parser, & idk);
6946 /* Compute its typeid. */
6947 postfix_expression = build_typeid (expression, tf_warning_or_error);
6948 /* Look for the `)' token. */
6949 close_paren = parens.require_close (parser);
6950 }
6951 /* Restore the saved message. */
6952 parser->type_definition_forbidden_message = saved_message;
6953 /* `typeid' may not appear in an integral constant expression. */
6954 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6955 postfix_expression = error_mark_node;
6956
6957 /* Construct a location e.g. :
6958 typeid (expr)
6959 ^~~~~~~~~~~~~
6960 ranging from the start of the "typeid" token to the final closing
6961 paren, with the caret at the start. */
6962 if (close_paren)
6963 {
6964 location_t typeid_loc
6965 = make_location (start_loc, start_loc, close_paren->location);
6966 postfix_expression.set_location (typeid_loc);
6967 postfix_expression.maybe_add_location_wrapper ();
6968 }
6969 }
6970 break;
6971
6972 case RID_TYPENAME:
6973 {
6974 tree type;
6975 /* The syntax permitted here is the same permitted for an
6976 elaborated-type-specifier. */
6977 ++parser->prevent_constrained_type_specifiers;
6978 type = cp_parser_elaborated_type_specifier (parser,
6979 /*is_friend=*/false,
6980 /*is_declaration=*/false);
6981 --parser->prevent_constrained_type_specifiers;
6982 postfix_expression = cp_parser_functional_cast (parser, type);
6983 }
6984 break;
6985
6986 case RID_ADDRESSOF:
6987 case RID_BUILTIN_SHUFFLE:
6988 case RID_BUILTIN_LAUNDER:
6989 {
6990 vec<tree, va_gc> *vec;
6991 unsigned int i;
6992 tree p;
6993
6994 cp_lexer_consume_token (parser->lexer);
6995 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6996 /*cast_p=*/false, /*allow_expansion_p=*/true,
6997 /*non_constant_p=*/NULL);
6998 if (vec == NULL)
6999 {
7000 postfix_expression = error_mark_node;
7001 break;
7002 }
7003
7004 FOR_EACH_VEC_ELT (*vec, i, p)
7005 mark_exp_read (p);
7006
7007 switch (keyword)
7008 {
7009 case RID_ADDRESSOF:
7010 if (vec->length () == 1)
7011 postfix_expression
7012 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7013 else
7014 {
7015 error_at (loc, "wrong number of arguments to "
7016 "%<__builtin_addressof%>");
7017 postfix_expression = error_mark_node;
7018 }
7019 break;
7020
7021 case RID_BUILTIN_LAUNDER:
7022 if (vec->length () == 1)
7023 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7024 tf_warning_or_error);
7025 else
7026 {
7027 error_at (loc, "wrong number of arguments to "
7028 "%<__builtin_launder%>");
7029 postfix_expression = error_mark_node;
7030 }
7031 break;
7032
7033 case RID_BUILTIN_SHUFFLE:
7034 if (vec->length () == 2)
7035 postfix_expression
7036 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7037 (*vec)[1], tf_warning_or_error);
7038 else if (vec->length () == 3)
7039 postfix_expression
7040 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7041 (*vec)[2], tf_warning_or_error);
7042 else
7043 {
7044 error_at (loc, "wrong number of arguments to "
7045 "%<__builtin_shuffle%>");
7046 postfix_expression = error_mark_node;
7047 }
7048 break;
7049
7050 default:
7051 gcc_unreachable ();
7052 }
7053 break;
7054 }
7055
7056 case RID_BUILTIN_CONVERTVECTOR:
7057 {
7058 tree expression;
7059 tree type;
7060 /* Consume the `__builtin_convertvector' token. */
7061 cp_lexer_consume_token (parser->lexer);
7062 /* Look for the opening `('. */
7063 matching_parens parens;
7064 parens.require_open (parser);
7065 /* Now, parse the assignment-expression. */
7066 expression = cp_parser_assignment_expression (parser);
7067 /* Look for the `,'. */
7068 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7069 location_t type_location
7070 = cp_lexer_peek_token (parser->lexer)->location;
7071 /* Parse the type-id. */
7072 {
7073 type_id_in_expr_sentinel s (parser);
7074 type = cp_parser_type_id (parser);
7075 }
7076 /* Look for the closing `)'. */
7077 parens.require_close (parser);
7078 return cp_build_vec_convert (expression, type_location, type,
7079 tf_warning_or_error);
7080 }
7081
7082 default:
7083 {
7084 tree type;
7085
7086 /* If the next thing is a simple-type-specifier, we may be
7087 looking at a functional cast. We could also be looking at
7088 an id-expression. So, we try the functional cast, and if
7089 that doesn't work we fall back to the primary-expression. */
7090 cp_parser_parse_tentatively (parser);
7091 /* Look for the simple-type-specifier. */
7092 ++parser->prevent_constrained_type_specifiers;
7093 type = cp_parser_simple_type_specifier (parser,
7094 /*decl_specs=*/NULL,
7095 CP_PARSER_FLAGS_NONE);
7096 --parser->prevent_constrained_type_specifiers;
7097 /* Parse the cast itself. */
7098 if (!cp_parser_error_occurred (parser))
7099 postfix_expression
7100 = cp_parser_functional_cast (parser, type);
7101 /* If that worked, we're done. */
7102 if (cp_parser_parse_definitely (parser))
7103 break;
7104
7105 /* If the functional-cast didn't work out, try a
7106 compound-literal. */
7107 if (cp_parser_allow_gnu_extensions_p (parser)
7108 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7109 {
7110 cp_expr initializer = NULL_TREE;
7111
7112 cp_parser_parse_tentatively (parser);
7113
7114 matching_parens parens;
7115 parens.consume_open (parser);
7116
7117 /* Avoid calling cp_parser_type_id pointlessly, see comment
7118 in cp_parser_cast_expression about c++/29234. */
7119 if (!cp_parser_compound_literal_p (parser))
7120 cp_parser_simulate_error (parser);
7121 else
7122 {
7123 /* Parse the type. */
7124 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7125 parser->in_type_id_in_expr_p = true;
7126 type = cp_parser_type_id (parser);
7127 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7128 parens.require_close (parser);
7129 }
7130
7131 /* If things aren't going well, there's no need to
7132 keep going. */
7133 if (!cp_parser_error_occurred (parser))
7134 {
7135 bool non_constant_p;
7136 /* Parse the brace-enclosed initializer list. */
7137 initializer = cp_parser_braced_list (parser,
7138 &non_constant_p);
7139 }
7140 /* If that worked, we're definitely looking at a
7141 compound-literal expression. */
7142 if (cp_parser_parse_definitely (parser))
7143 {
7144 /* Warn the user that a compound literal is not
7145 allowed in standard C++. */
7146 pedwarn (input_location, OPT_Wpedantic,
7147 "ISO C++ forbids compound-literals");
7148 /* For simplicity, we disallow compound literals in
7149 constant-expressions. We could
7150 allow compound literals of integer type, whose
7151 initializer was a constant, in constant
7152 expressions. Permitting that usage, as a further
7153 extension, would not change the meaning of any
7154 currently accepted programs. (Of course, as
7155 compound literals are not part of ISO C++, the
7156 standard has nothing to say.) */
7157 if (cp_parser_non_integral_constant_expression (parser,
7158 NIC_NCC))
7159 {
7160 postfix_expression = error_mark_node;
7161 break;
7162 }
7163 /* Form the representation of the compound-literal. */
7164 postfix_expression
7165 = finish_compound_literal (type, initializer,
7166 tf_warning_or_error, fcl_c99);
7167 postfix_expression.set_location (initializer.get_location ());
7168 break;
7169 }
7170 }
7171
7172 /* It must be a primary-expression. */
7173 postfix_expression
7174 = cp_parser_primary_expression (parser, address_p, cast_p,
7175 /*template_arg_p=*/false,
7176 decltype_p,
7177 &idk);
7178 }
7179 break;
7180 }
7181
7182 /* Note that we don't need to worry about calling build_cplus_new on a
7183 class-valued CALL_EXPR in decltype when it isn't the end of the
7184 postfix-expression; unary_complex_lvalue will take care of that for
7185 all these cases. */
7186
7187 /* Keep looping until the postfix-expression is complete. */
7188 while (true)
7189 {
7190 if (idk == CP_ID_KIND_UNQUALIFIED
7191 && identifier_p (postfix_expression)
7192 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7193 /* It is not a Koenig lookup function call. */
7194 postfix_expression
7195 = unqualified_name_lookup_error (postfix_expression);
7196
7197 /* Peek at the next token. */
7198 token = cp_lexer_peek_token (parser->lexer);
7199
7200 switch (token->type)
7201 {
7202 case CPP_OPEN_SQUARE:
7203 if (cp_next_tokens_can_be_std_attribute_p (parser))
7204 {
7205 cp_parser_error (parser,
7206 "two consecutive %<[%> shall "
7207 "only introduce an attribute");
7208 return error_mark_node;
7209 }
7210 postfix_expression
7211 = cp_parser_postfix_open_square_expression (parser,
7212 postfix_expression,
7213 false,
7214 decltype_p);
7215 postfix_expression.set_range (start_loc,
7216 postfix_expression.get_location ());
7217
7218 idk = CP_ID_KIND_NONE;
7219 is_member_access = false;
7220 break;
7221
7222 case CPP_OPEN_PAREN:
7223 /* postfix-expression ( expression-list [opt] ) */
7224 {
7225 bool koenig_p;
7226 bool is_builtin_constant_p;
7227 bool saved_integral_constant_expression_p = false;
7228 bool saved_non_integral_constant_expression_p = false;
7229 tsubst_flags_t complain = complain_flags (decltype_p);
7230 vec<tree, va_gc> *args;
7231 location_t close_paren_loc = UNKNOWN_LOCATION;
7232
7233 is_member_access = false;
7234
7235 tree stripped_expression
7236 = tree_strip_any_location_wrapper (postfix_expression);
7237 is_builtin_constant_p
7238 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7239 if (is_builtin_constant_p)
7240 {
7241 /* The whole point of __builtin_constant_p is to allow
7242 non-constant expressions to appear as arguments. */
7243 saved_integral_constant_expression_p
7244 = parser->integral_constant_expression_p;
7245 saved_non_integral_constant_expression_p
7246 = parser->non_integral_constant_expression_p;
7247 parser->integral_constant_expression_p = false;
7248 }
7249 args = (cp_parser_parenthesized_expression_list
7250 (parser, non_attr,
7251 /*cast_p=*/false, /*allow_expansion_p=*/true,
7252 /*non_constant_p=*/NULL,
7253 /*close_paren_loc=*/&close_paren_loc,
7254 /*wrap_locations_p=*/true));
7255 if (is_builtin_constant_p)
7256 {
7257 parser->integral_constant_expression_p
7258 = saved_integral_constant_expression_p;
7259 parser->non_integral_constant_expression_p
7260 = saved_non_integral_constant_expression_p;
7261 }
7262
7263 if (args == NULL)
7264 {
7265 postfix_expression = error_mark_node;
7266 break;
7267 }
7268
7269 /* Function calls are not permitted in
7270 constant-expressions. */
7271 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7272 && cp_parser_non_integral_constant_expression (parser,
7273 NIC_FUNC_CALL))
7274 {
7275 postfix_expression = error_mark_node;
7276 release_tree_vector (args);
7277 break;
7278 }
7279
7280 koenig_p = false;
7281 if (idk == CP_ID_KIND_UNQUALIFIED
7282 || idk == CP_ID_KIND_TEMPLATE_ID)
7283 {
7284 if (identifier_p (postfix_expression)
7285 /* In C++2A, we may need to perform ADL for a template
7286 name. */
7287 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7288 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7289 {
7290 if (!args->is_empty ())
7291 {
7292 koenig_p = true;
7293 if (!any_type_dependent_arguments_p (args))
7294 postfix_expression
7295 = perform_koenig_lookup (postfix_expression, args,
7296 complain);
7297 }
7298 else
7299 postfix_expression
7300 = unqualified_fn_lookup_error (postfix_expression);
7301 }
7302 /* We do not perform argument-dependent lookup if
7303 normal lookup finds a non-function, in accordance
7304 with the expected resolution of DR 218. */
7305 else if (!args->is_empty ()
7306 && is_overloaded_fn (postfix_expression))
7307 {
7308 /* We only need to look at the first function,
7309 because all the fns share the attribute we're
7310 concerned with (all member fns or all local
7311 fns). */
7312 tree fn = get_first_fn (postfix_expression);
7313 fn = STRIP_TEMPLATE (fn);
7314
7315 /* Do not do argument dependent lookup if regular
7316 lookup finds a member function or a block-scope
7317 function declaration. [basic.lookup.argdep]/3 */
7318 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7319 || DECL_FUNCTION_MEMBER_P (fn)
7320 || DECL_LOCAL_FUNCTION_P (fn)))
7321 {
7322 koenig_p = true;
7323 if (!any_type_dependent_arguments_p (args))
7324 postfix_expression
7325 = perform_koenig_lookup (postfix_expression, args,
7326 complain);
7327 }
7328 }
7329 }
7330
7331 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7332 {
7333 tree instance = TREE_OPERAND (postfix_expression, 0);
7334 tree fn = TREE_OPERAND (postfix_expression, 1);
7335
7336 if (processing_template_decl
7337 && (type_dependent_object_expression_p (instance)
7338 || (!BASELINK_P (fn)
7339 && TREE_CODE (fn) != FIELD_DECL)
7340 || type_dependent_expression_p (fn)
7341 || any_type_dependent_arguments_p (args)))
7342 {
7343 maybe_generic_this_capture (instance, fn);
7344 postfix_expression
7345 = build_min_nt_call_vec (postfix_expression, args);
7346 release_tree_vector (args);
7347 break;
7348 }
7349
7350 if (BASELINK_P (fn))
7351 {
7352 postfix_expression
7353 = (build_new_method_call
7354 (instance, fn, &args, NULL_TREE,
7355 (idk == CP_ID_KIND_QUALIFIED
7356 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7357 : LOOKUP_NORMAL),
7358 /*fn_p=*/NULL,
7359 complain));
7360 }
7361 else
7362 postfix_expression
7363 = finish_call_expr (postfix_expression, &args,
7364 /*disallow_virtual=*/false,
7365 /*koenig_p=*/false,
7366 complain);
7367 }
7368 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7369 || TREE_CODE (postfix_expression) == MEMBER_REF
7370 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7371 postfix_expression = (build_offset_ref_call_from_tree
7372 (postfix_expression, &args,
7373 complain));
7374 else if (idk == CP_ID_KIND_QUALIFIED)
7375 /* A call to a static class member, or a namespace-scope
7376 function. */
7377 postfix_expression
7378 = finish_call_expr (postfix_expression, &args,
7379 /*disallow_virtual=*/true,
7380 koenig_p,
7381 complain);
7382 else
7383 /* All other function calls. */
7384 postfix_expression
7385 = finish_call_expr (postfix_expression, &args,
7386 /*disallow_virtual=*/false,
7387 koenig_p,
7388 complain);
7389
7390 if (close_paren_loc != UNKNOWN_LOCATION)
7391 {
7392 location_t combined_loc = make_location (token->location,
7393 start_loc,
7394 close_paren_loc);
7395 postfix_expression.set_location (combined_loc);
7396 }
7397
7398 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7399 idk = CP_ID_KIND_NONE;
7400
7401 release_tree_vector (args);
7402 }
7403 break;
7404
7405 case CPP_DOT:
7406 case CPP_DEREF:
7407 /* postfix-expression . template [opt] id-expression
7408 postfix-expression . pseudo-destructor-name
7409 postfix-expression -> template [opt] id-expression
7410 postfix-expression -> pseudo-destructor-name */
7411
7412 /* Consume the `.' or `->' operator. */
7413 cp_lexer_consume_token (parser->lexer);
7414
7415 postfix_expression
7416 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7417 postfix_expression,
7418 false, &idk, loc);
7419
7420 is_member_access = true;
7421 break;
7422
7423 case CPP_PLUS_PLUS:
7424 /* postfix-expression ++ */
7425 /* Consume the `++' token. */
7426 cp_lexer_consume_token (parser->lexer);
7427 /* Generate a representation for the complete expression. */
7428 postfix_expression
7429 = finish_increment_expr (postfix_expression,
7430 POSTINCREMENT_EXPR);
7431 /* Increments may not appear in constant-expressions. */
7432 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7433 postfix_expression = error_mark_node;
7434 idk = CP_ID_KIND_NONE;
7435 is_member_access = false;
7436 break;
7437
7438 case CPP_MINUS_MINUS:
7439 /* postfix-expression -- */
7440 /* Consume the `--' token. */
7441 cp_lexer_consume_token (parser->lexer);
7442 /* Generate a representation for the complete expression. */
7443 postfix_expression
7444 = finish_increment_expr (postfix_expression,
7445 POSTDECREMENT_EXPR);
7446 /* Decrements may not appear in constant-expressions. */
7447 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7448 postfix_expression = error_mark_node;
7449 idk = CP_ID_KIND_NONE;
7450 is_member_access = false;
7451 break;
7452
7453 default:
7454 if (pidk_return != NULL)
7455 * pidk_return = idk;
7456 if (member_access_only_p)
7457 return is_member_access
7458 ? postfix_expression
7459 : cp_expr (error_mark_node);
7460 else
7461 return postfix_expression;
7462 }
7463 }
7464
7465 /* We should never get here. */
7466 gcc_unreachable ();
7467 return error_mark_node;
7468 }
7469
7470 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7471 by cp_parser_builtin_offsetof. We're looking for
7472
7473 postfix-expression [ expression ]
7474 postfix-expression [ braced-init-list ] (C++11)
7475
7476 FOR_OFFSETOF is set if we're being called in that context, which
7477 changes how we deal with integer constant expressions. */
7478
7479 static tree
7480 cp_parser_postfix_open_square_expression (cp_parser *parser,
7481 tree postfix_expression,
7482 bool for_offsetof,
7483 bool decltype_p)
7484 {
7485 tree index = NULL_TREE;
7486 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7487 bool saved_greater_than_is_operator_p;
7488
7489 /* Consume the `[' token. */
7490 cp_lexer_consume_token (parser->lexer);
7491
7492 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7493 parser->greater_than_is_operator_p = true;
7494
7495 /* Parse the index expression. */
7496 /* ??? For offsetof, there is a question of what to allow here. If
7497 offsetof is not being used in an integral constant expression context,
7498 then we *could* get the right answer by computing the value at runtime.
7499 If we are in an integral constant expression context, then we might
7500 could accept any constant expression; hard to say without analysis.
7501 Rather than open the barn door too wide right away, allow only integer
7502 constant expressions here. */
7503 if (for_offsetof)
7504 index = cp_parser_constant_expression (parser);
7505 else
7506 {
7507 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7508 {
7509 bool expr_nonconst_p;
7510 cp_lexer_set_source_position (parser->lexer);
7511 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7512 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7513 }
7514 else
7515 index = cp_parser_expression (parser);
7516 }
7517
7518 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7519
7520 /* Look for the closing `]'. */
7521 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7522
7523 /* Build the ARRAY_REF. */
7524 postfix_expression = grok_array_decl (loc, postfix_expression,
7525 index, decltype_p);
7526
7527 /* When not doing offsetof, array references are not permitted in
7528 constant-expressions. */
7529 if (!for_offsetof
7530 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7531 postfix_expression = error_mark_node;
7532
7533 return postfix_expression;
7534 }
7535
7536 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7537 dereference of incomplete type, returns true if error_mark_node should
7538 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7539 and *DEPENDENT_P. */
7540
7541 bool
7542 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7543 bool *dependent_p)
7544 {
7545 /* In a template, be permissive by treating an object expression
7546 of incomplete type as dependent (after a pedwarn). */
7547 diagnostic_t kind = (processing_template_decl
7548 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7549
7550 switch (TREE_CODE (*postfix_expression))
7551 {
7552 case CAST_EXPR:
7553 case REINTERPRET_CAST_EXPR:
7554 case CONST_CAST_EXPR:
7555 case STATIC_CAST_EXPR:
7556 case DYNAMIC_CAST_EXPR:
7557 case IMPLICIT_CONV_EXPR:
7558 case VIEW_CONVERT_EXPR:
7559 case NON_LVALUE_EXPR:
7560 kind = DK_ERROR;
7561 break;
7562 case OVERLOAD:
7563 /* Don't emit any diagnostic for OVERLOADs. */
7564 kind = DK_IGNORED;
7565 break;
7566 default:
7567 /* Avoid clobbering e.g. DECLs. */
7568 if (!EXPR_P (*postfix_expression))
7569 kind = DK_ERROR;
7570 break;
7571 }
7572
7573 if (kind == DK_IGNORED)
7574 return false;
7575
7576 location_t exploc = location_of (*postfix_expression);
7577 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7578 if (!MAYBE_CLASS_TYPE_P (*scope))
7579 return true;
7580 if (kind == DK_ERROR)
7581 *scope = *postfix_expression = error_mark_node;
7582 else if (processing_template_decl)
7583 {
7584 *dependent_p = true;
7585 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7586 }
7587 return false;
7588 }
7589
7590 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7591 by cp_parser_builtin_offsetof. We're looking for
7592
7593 postfix-expression . template [opt] id-expression
7594 postfix-expression . pseudo-destructor-name
7595 postfix-expression -> template [opt] id-expression
7596 postfix-expression -> pseudo-destructor-name
7597
7598 FOR_OFFSETOF is set if we're being called in that context. That sorta
7599 limits what of the above we'll actually accept, but nevermind.
7600 TOKEN_TYPE is the "." or "->" token, which will already have been
7601 removed from the stream. */
7602
7603 static tree
7604 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7605 enum cpp_ttype token_type,
7606 cp_expr postfix_expression,
7607 bool for_offsetof, cp_id_kind *idk,
7608 location_t location)
7609 {
7610 tree name;
7611 bool dependent_p;
7612 bool pseudo_destructor_p;
7613 tree scope = NULL_TREE;
7614 location_t start_loc = postfix_expression.get_start ();
7615
7616 /* If this is a `->' operator, dereference the pointer. */
7617 if (token_type == CPP_DEREF)
7618 postfix_expression = build_x_arrow (location, postfix_expression,
7619 tf_warning_or_error);
7620 /* Check to see whether or not the expression is type-dependent and
7621 not the current instantiation. */
7622 dependent_p = type_dependent_object_expression_p (postfix_expression);
7623 /* The identifier following the `->' or `.' is not qualified. */
7624 parser->scope = NULL_TREE;
7625 parser->qualifying_scope = NULL_TREE;
7626 parser->object_scope = NULL_TREE;
7627 *idk = CP_ID_KIND_NONE;
7628
7629 /* Enter the scope corresponding to the type of the object
7630 given by the POSTFIX_EXPRESSION. */
7631 if (!dependent_p)
7632 {
7633 scope = TREE_TYPE (postfix_expression);
7634 /* According to the standard, no expression should ever have
7635 reference type. Unfortunately, we do not currently match
7636 the standard in this respect in that our internal representation
7637 of an expression may have reference type even when the standard
7638 says it does not. Therefore, we have to manually obtain the
7639 underlying type here. */
7640 scope = non_reference (scope);
7641 /* The type of the POSTFIX_EXPRESSION must be complete. */
7642 /* Unlike the object expression in other contexts, *this is not
7643 required to be of complete type for purposes of class member
7644 access (5.2.5) outside the member function body. */
7645 if (postfix_expression != current_class_ref
7646 && scope != error_mark_node
7647 && !currently_open_class (scope))
7648 {
7649 scope = complete_type (scope);
7650 if (!COMPLETE_TYPE_P (scope)
7651 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7652 &dependent_p))
7653 return error_mark_node;
7654 }
7655
7656 if (!dependent_p)
7657 {
7658 /* Let the name lookup machinery know that we are processing a
7659 class member access expression. */
7660 parser->context->object_type = scope;
7661 /* If something went wrong, we want to be able to discern that case,
7662 as opposed to the case where there was no SCOPE due to the type
7663 of expression being dependent. */
7664 if (!scope)
7665 scope = error_mark_node;
7666 /* If the SCOPE was erroneous, make the various semantic analysis
7667 functions exit quickly -- and without issuing additional error
7668 messages. */
7669 if (scope == error_mark_node)
7670 postfix_expression = error_mark_node;
7671 }
7672 }
7673
7674 if (dependent_p)
7675 /* Tell cp_parser_lookup_name that there was an object, even though it's
7676 type-dependent. */
7677 parser->context->object_type = unknown_type_node;
7678
7679 /* Assume this expression is not a pseudo-destructor access. */
7680 pseudo_destructor_p = false;
7681
7682 /* If the SCOPE is a scalar type, then, if this is a valid program,
7683 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7684 is type dependent, it can be pseudo-destructor-name or something else.
7685 Try to parse it as pseudo-destructor-name first. */
7686 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7687 {
7688 tree s;
7689 tree type;
7690
7691 cp_parser_parse_tentatively (parser);
7692 /* Parse the pseudo-destructor-name. */
7693 s = NULL_TREE;
7694 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7695 &s, &type);
7696 if (dependent_p
7697 && (cp_parser_error_occurred (parser)
7698 || !SCALAR_TYPE_P (type)))
7699 cp_parser_abort_tentative_parse (parser);
7700 else if (cp_parser_parse_definitely (parser))
7701 {
7702 pseudo_destructor_p = true;
7703 postfix_expression
7704 = finish_pseudo_destructor_expr (postfix_expression,
7705 s, type, location);
7706 }
7707 }
7708
7709 if (!pseudo_destructor_p)
7710 {
7711 /* If the SCOPE is not a scalar type, we are looking at an
7712 ordinary class member access expression, rather than a
7713 pseudo-destructor-name. */
7714 bool template_p;
7715 cp_token *token = cp_lexer_peek_token (parser->lexer);
7716 /* Parse the id-expression. */
7717 name = (cp_parser_id_expression
7718 (parser,
7719 cp_parser_optional_template_keyword (parser),
7720 /*check_dependency_p=*/true,
7721 &template_p,
7722 /*declarator_p=*/false,
7723 /*optional_p=*/false));
7724 /* In general, build a SCOPE_REF if the member name is qualified.
7725 However, if the name was not dependent and has already been
7726 resolved; there is no need to build the SCOPE_REF. For example;
7727
7728 struct X { void f(); };
7729 template <typename T> void f(T* t) { t->X::f(); }
7730
7731 Even though "t" is dependent, "X::f" is not and has been resolved
7732 to a BASELINK; there is no need to include scope information. */
7733
7734 /* But we do need to remember that there was an explicit scope for
7735 virtual function calls. */
7736 if (parser->scope)
7737 *idk = CP_ID_KIND_QUALIFIED;
7738
7739 /* If the name is a template-id that names a type, we will get a
7740 TYPE_DECL here. That is invalid code. */
7741 if (TREE_CODE (name) == TYPE_DECL)
7742 {
7743 error_at (token->location, "invalid use of %qD", name);
7744 postfix_expression = error_mark_node;
7745 }
7746 else
7747 {
7748 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7749 {
7750 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7751 {
7752 error_at (token->location, "%<%D::%D%> is not a class member",
7753 parser->scope, name);
7754 postfix_expression = error_mark_node;
7755 }
7756 else
7757 name = build_qualified_name (/*type=*/NULL_TREE,
7758 parser->scope,
7759 name,
7760 template_p);
7761 parser->scope = NULL_TREE;
7762 parser->qualifying_scope = NULL_TREE;
7763 parser->object_scope = NULL_TREE;
7764 }
7765 if (parser->scope && name && BASELINK_P (name))
7766 adjust_result_of_qualified_name_lookup
7767 (name, parser->scope, scope);
7768 postfix_expression
7769 = finish_class_member_access_expr (postfix_expression, name,
7770 template_p,
7771 tf_warning_or_error);
7772 /* Build a location e.g.:
7773 ptr->access_expr
7774 ~~~^~~~~~~~~~~~~
7775 where the caret is at the deref token, ranging from
7776 the start of postfix_expression to the end of the access expr. */
7777 location_t end_loc
7778 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7779 location_t combined_loc
7780 = make_location (input_location, start_loc, end_loc);
7781 protected_set_expr_location (postfix_expression, combined_loc);
7782 }
7783 }
7784
7785 /* We no longer need to look up names in the scope of the object on
7786 the left-hand side of the `.' or `->' operator. */
7787 parser->context->object_type = NULL_TREE;
7788
7789 /* Outside of offsetof, these operators may not appear in
7790 constant-expressions. */
7791 if (!for_offsetof
7792 && (cp_parser_non_integral_constant_expression
7793 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7794 postfix_expression = error_mark_node;
7795
7796 return postfix_expression;
7797 }
7798
7799 /* Parse a parenthesized expression-list.
7800
7801 expression-list:
7802 assignment-expression
7803 expression-list, assignment-expression
7804
7805 attribute-list:
7806 expression-list
7807 identifier
7808 identifier, expression-list
7809
7810 CAST_P is true if this expression is the target of a cast.
7811
7812 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7813 argument pack.
7814
7815 WRAP_LOCATIONS_P is true if expressions within this list for which
7816 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7817 their source locations.
7818
7819 Returns a vector of trees. Each element is a representation of an
7820 assignment-expression. NULL is returned if the ( and or ) are
7821 missing. An empty, but allocated, vector is returned on no
7822 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7823 if we are parsing an attribute list for an attribute that wants a
7824 plain identifier argument, normal_attr for an attribute that wants
7825 an expression, or non_attr if we aren't parsing an attribute list. If
7826 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7827 not all of the expressions in the list were constant.
7828 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7829 will be written to with the location of the closing parenthesis. If
7830 an error occurs, it may or may not be written to. */
7831
7832 static vec<tree, va_gc> *
7833 cp_parser_parenthesized_expression_list (cp_parser* parser,
7834 int is_attribute_list,
7835 bool cast_p,
7836 bool allow_expansion_p,
7837 bool *non_constant_p,
7838 location_t *close_paren_loc,
7839 bool wrap_locations_p)
7840 {
7841 vec<tree, va_gc> *expression_list;
7842 bool fold_expr_p = is_attribute_list != non_attr;
7843 tree identifier = NULL_TREE;
7844 bool saved_greater_than_is_operator_p;
7845
7846 /* Assume all the expressions will be constant. */
7847 if (non_constant_p)
7848 *non_constant_p = false;
7849
7850 matching_parens parens;
7851 if (!parens.require_open (parser))
7852 return NULL;
7853
7854 expression_list = make_tree_vector ();
7855
7856 /* Within a parenthesized expression, a `>' token is always
7857 the greater-than operator. */
7858 saved_greater_than_is_operator_p
7859 = parser->greater_than_is_operator_p;
7860 parser->greater_than_is_operator_p = true;
7861
7862 cp_expr expr (NULL_TREE);
7863
7864 /* Consume expressions until there are no more. */
7865 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7866 while (true)
7867 {
7868 /* At the beginning of attribute lists, check to see if the
7869 next token is an identifier. */
7870 if (is_attribute_list == id_attr
7871 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7872 {
7873 cp_token *token;
7874
7875 /* Consume the identifier. */
7876 token = cp_lexer_consume_token (parser->lexer);
7877 /* Save the identifier. */
7878 identifier = token->u.value;
7879 }
7880 else
7881 {
7882 bool expr_non_constant_p;
7883
7884 /* Parse the next assignment-expression. */
7885 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7886 {
7887 /* A braced-init-list. */
7888 cp_lexer_set_source_position (parser->lexer);
7889 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7890 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7891 if (non_constant_p && expr_non_constant_p)
7892 *non_constant_p = true;
7893 }
7894 else if (non_constant_p)
7895 {
7896 expr = (cp_parser_constant_expression
7897 (parser, /*allow_non_constant_p=*/true,
7898 &expr_non_constant_p));
7899 if (expr_non_constant_p)
7900 *non_constant_p = true;
7901 }
7902 else
7903 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7904 cast_p);
7905
7906 if (fold_expr_p)
7907 expr = instantiate_non_dependent_expr (expr);
7908
7909 /* If we have an ellipsis, then this is an expression
7910 expansion. */
7911 if (allow_expansion_p
7912 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7913 {
7914 /* Consume the `...'. */
7915 cp_lexer_consume_token (parser->lexer);
7916
7917 /* Build the argument pack. */
7918 expr = make_pack_expansion (expr);
7919 }
7920
7921 if (wrap_locations_p)
7922 expr.maybe_add_location_wrapper ();
7923
7924 /* Add it to the list. We add error_mark_node
7925 expressions to the list, so that we can still tell if
7926 the correct form for a parenthesized expression-list
7927 is found. That gives better errors. */
7928 vec_safe_push (expression_list, expr.get_value ());
7929
7930 if (expr == error_mark_node)
7931 goto skip_comma;
7932 }
7933
7934 /* After the first item, attribute lists look the same as
7935 expression lists. */
7936 is_attribute_list = non_attr;
7937
7938 get_comma:;
7939 /* If the next token isn't a `,', then we are done. */
7940 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7941 break;
7942
7943 /* Otherwise, consume the `,' and keep going. */
7944 cp_lexer_consume_token (parser->lexer);
7945 }
7946
7947 if (close_paren_loc)
7948 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7949
7950 if (!parens.require_close (parser))
7951 {
7952 int ending;
7953
7954 skip_comma:;
7955 /* We try and resync to an unnested comma, as that will give the
7956 user better diagnostics. */
7957 ending = cp_parser_skip_to_closing_parenthesis (parser,
7958 /*recovering=*/true,
7959 /*or_comma=*/true,
7960 /*consume_paren=*/true);
7961 if (ending < 0)
7962 goto get_comma;
7963 if (!ending)
7964 {
7965 parser->greater_than_is_operator_p
7966 = saved_greater_than_is_operator_p;
7967 return NULL;
7968 }
7969 }
7970
7971 parser->greater_than_is_operator_p
7972 = saved_greater_than_is_operator_p;
7973
7974 if (identifier)
7975 vec_safe_insert (expression_list, 0, identifier);
7976
7977 return expression_list;
7978 }
7979
7980 /* Parse a pseudo-destructor-name.
7981
7982 pseudo-destructor-name:
7983 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7984 :: [opt] nested-name-specifier template template-id :: ~ type-name
7985 :: [opt] nested-name-specifier [opt] ~ type-name
7986
7987 If either of the first two productions is used, sets *SCOPE to the
7988 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7989 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7990 or ERROR_MARK_NODE if the parse fails. */
7991
7992 static void
7993 cp_parser_pseudo_destructor_name (cp_parser* parser,
7994 tree object,
7995 tree* scope,
7996 tree* type)
7997 {
7998 bool nested_name_specifier_p;
7999
8000 /* Handle ~auto. */
8001 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8002 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8003 && !type_dependent_expression_p (object))
8004 {
8005 if (cxx_dialect < cxx14)
8006 pedwarn (input_location, 0,
8007 "%<~auto%> only available with "
8008 "%<-std=c++14%> or %<-std=gnu++14%>");
8009 cp_lexer_consume_token (parser->lexer);
8010 cp_lexer_consume_token (parser->lexer);
8011 *scope = NULL_TREE;
8012 *type = TREE_TYPE (object);
8013 return;
8014 }
8015
8016 /* Assume that things will not work out. */
8017 *type = error_mark_node;
8018
8019 /* Look for the optional `::' operator. */
8020 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8021 /* Look for the optional nested-name-specifier. */
8022 nested_name_specifier_p
8023 = (cp_parser_nested_name_specifier_opt (parser,
8024 /*typename_keyword_p=*/false,
8025 /*check_dependency_p=*/true,
8026 /*type_p=*/false,
8027 /*is_declaration=*/false)
8028 != NULL_TREE);
8029 /* Now, if we saw a nested-name-specifier, we might be doing the
8030 second production. */
8031 if (nested_name_specifier_p
8032 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8033 {
8034 /* Consume the `template' keyword. */
8035 cp_lexer_consume_token (parser->lexer);
8036 /* Parse the template-id. */
8037 cp_parser_template_id (parser,
8038 /*template_keyword_p=*/true,
8039 /*check_dependency_p=*/false,
8040 class_type,
8041 /*is_declaration=*/true);
8042 /* Look for the `::' token. */
8043 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8044 }
8045 /* If the next token is not a `~', then there might be some
8046 additional qualification. */
8047 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8048 {
8049 /* At this point, we're looking for "type-name :: ~". The type-name
8050 must not be a class-name, since this is a pseudo-destructor. So,
8051 it must be either an enum-name, or a typedef-name -- both of which
8052 are just identifiers. So, we peek ahead to check that the "::"
8053 and "~" tokens are present; if they are not, then we can avoid
8054 calling type_name. */
8055 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8056 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8057 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8058 {
8059 cp_parser_error (parser, "non-scalar type");
8060 return;
8061 }
8062
8063 /* Look for the type-name. */
8064 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8065 if (*scope == error_mark_node)
8066 return;
8067
8068 /* Look for the `::' token. */
8069 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8070 }
8071 else
8072 *scope = NULL_TREE;
8073
8074 /* Look for the `~'. */
8075 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8076
8077 /* Once we see the ~, this has to be a pseudo-destructor. */
8078 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8079 cp_parser_commit_to_topmost_tentative_parse (parser);
8080
8081 /* Look for the type-name again. We are not responsible for
8082 checking that it matches the first type-name. */
8083 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8084 }
8085
8086 /* Parse a unary-expression.
8087
8088 unary-expression:
8089 postfix-expression
8090 ++ cast-expression
8091 -- cast-expression
8092 unary-operator cast-expression
8093 sizeof unary-expression
8094 sizeof ( type-id )
8095 alignof ( type-id ) [C++0x]
8096 new-expression
8097 delete-expression
8098
8099 GNU Extensions:
8100
8101 unary-expression:
8102 __extension__ cast-expression
8103 __alignof__ unary-expression
8104 __alignof__ ( type-id )
8105 alignof unary-expression [C++0x]
8106 __real__ cast-expression
8107 __imag__ cast-expression
8108 && identifier
8109 sizeof ( type-id ) { initializer-list , [opt] }
8110 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8111 __alignof__ ( type-id ) { initializer-list , [opt] }
8112
8113 ADDRESS_P is true iff the unary-expression is appearing as the
8114 operand of the `&' operator. CAST_P is true if this expression is
8115 the target of a cast.
8116
8117 Returns a representation of the expression. */
8118
8119 static cp_expr
8120 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8121 bool address_p, bool cast_p, bool decltype_p)
8122 {
8123 cp_token *token;
8124 enum tree_code unary_operator;
8125
8126 /* Peek at the next token. */
8127 token = cp_lexer_peek_token (parser->lexer);
8128 /* Some keywords give away the kind of expression. */
8129 if (token->type == CPP_KEYWORD)
8130 {
8131 enum rid keyword = token->keyword;
8132
8133 switch (keyword)
8134 {
8135 case RID_ALIGNOF:
8136 case RID_SIZEOF:
8137 {
8138 tree operand, ret;
8139 enum tree_code op;
8140 location_t start_loc = token->location;
8141
8142 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8143 bool std_alignof = id_equal (token->u.value, "alignof");
8144
8145 /* Consume the token. */
8146 cp_lexer_consume_token (parser->lexer);
8147 /* Parse the operand. */
8148 operand = cp_parser_sizeof_operand (parser, keyword);
8149
8150 if (TYPE_P (operand))
8151 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8152 true);
8153 else
8154 {
8155 /* ISO C++ defines alignof only with types, not with
8156 expressions. So pedwarn if alignof is used with a non-
8157 type expression. However, __alignof__ is ok. */
8158 if (std_alignof)
8159 pedwarn (token->location, OPT_Wpedantic,
8160 "ISO C++ does not allow %<alignof%> "
8161 "with a non-type");
8162
8163 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8164 }
8165 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8166 SIZEOF_EXPR with the original operand. */
8167 if (op == SIZEOF_EXPR && ret != error_mark_node)
8168 {
8169 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8170 {
8171 if (!processing_template_decl && TYPE_P (operand))
8172 {
8173 ret = build_min (SIZEOF_EXPR, size_type_node,
8174 build1 (NOP_EXPR, operand,
8175 error_mark_node));
8176 SIZEOF_EXPR_TYPE_P (ret) = 1;
8177 }
8178 else
8179 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8180 TREE_SIDE_EFFECTS (ret) = 0;
8181 TREE_READONLY (ret) = 1;
8182 }
8183 }
8184
8185 /* Construct a location e.g. :
8186 alignof (expr)
8187 ^~~~~~~~~~~~~~
8188 with start == caret at the start of the "alignof"/"sizeof"
8189 token, with the endpoint at the final closing paren. */
8190 location_t finish_loc
8191 = cp_lexer_previous_token (parser->lexer)->location;
8192 location_t compound_loc
8193 = make_location (start_loc, start_loc, finish_loc);
8194
8195 cp_expr ret_expr (ret);
8196 ret_expr.set_location (compound_loc);
8197 ret_expr = ret_expr.maybe_add_location_wrapper ();
8198 return ret_expr;
8199 }
8200
8201 case RID_BUILTIN_HAS_ATTRIBUTE:
8202 return cp_parser_has_attribute_expression (parser);
8203
8204 case RID_NEW:
8205 return cp_parser_new_expression (parser);
8206
8207 case RID_DELETE:
8208 return cp_parser_delete_expression (parser);
8209
8210 case RID_EXTENSION:
8211 {
8212 /* The saved value of the PEDANTIC flag. */
8213 int saved_pedantic;
8214 tree expr;
8215
8216 /* Save away the PEDANTIC flag. */
8217 cp_parser_extension_opt (parser, &saved_pedantic);
8218 /* Parse the cast-expression. */
8219 expr = cp_parser_simple_cast_expression (parser);
8220 /* Restore the PEDANTIC flag. */
8221 pedantic = saved_pedantic;
8222
8223 return expr;
8224 }
8225
8226 case RID_REALPART:
8227 case RID_IMAGPART:
8228 {
8229 tree expression;
8230
8231 /* Consume the `__real__' or `__imag__' token. */
8232 cp_lexer_consume_token (parser->lexer);
8233 /* Parse the cast-expression. */
8234 expression = cp_parser_simple_cast_expression (parser);
8235 /* Create the complete representation. */
8236 return build_x_unary_op (token->location,
8237 (keyword == RID_REALPART
8238 ? REALPART_EXPR : IMAGPART_EXPR),
8239 expression,
8240 tf_warning_or_error);
8241 }
8242 break;
8243
8244 case RID_TRANSACTION_ATOMIC:
8245 case RID_TRANSACTION_RELAXED:
8246 return cp_parser_transaction_expression (parser, keyword);
8247
8248 case RID_NOEXCEPT:
8249 {
8250 tree expr;
8251 const char *saved_message;
8252 bool saved_integral_constant_expression_p;
8253 bool saved_non_integral_constant_expression_p;
8254 bool saved_greater_than_is_operator_p;
8255
8256 location_t start_loc = token->location;
8257
8258 cp_lexer_consume_token (parser->lexer);
8259 matching_parens parens;
8260 parens.require_open (parser);
8261
8262 saved_message = parser->type_definition_forbidden_message;
8263 parser->type_definition_forbidden_message
8264 = G_("types may not be defined in %<noexcept%> expressions");
8265
8266 saved_integral_constant_expression_p
8267 = parser->integral_constant_expression_p;
8268 saved_non_integral_constant_expression_p
8269 = parser->non_integral_constant_expression_p;
8270 parser->integral_constant_expression_p = false;
8271
8272 saved_greater_than_is_operator_p
8273 = parser->greater_than_is_operator_p;
8274 parser->greater_than_is_operator_p = true;
8275
8276 ++cp_unevaluated_operand;
8277 ++c_inhibit_evaluation_warnings;
8278 ++cp_noexcept_operand;
8279 expr = cp_parser_expression (parser);
8280 --cp_noexcept_operand;
8281 --c_inhibit_evaluation_warnings;
8282 --cp_unevaluated_operand;
8283
8284 parser->greater_than_is_operator_p
8285 = saved_greater_than_is_operator_p;
8286
8287 parser->integral_constant_expression_p
8288 = saved_integral_constant_expression_p;
8289 parser->non_integral_constant_expression_p
8290 = saved_non_integral_constant_expression_p;
8291
8292 parser->type_definition_forbidden_message = saved_message;
8293
8294 location_t finish_loc
8295 = cp_lexer_peek_token (parser->lexer)->location;
8296 parens.require_close (parser);
8297
8298 /* Construct a location of the form:
8299 noexcept (expr)
8300 ^~~~~~~~~~~~~~~
8301 with start == caret, finishing at the close-paren. */
8302 location_t noexcept_loc
8303 = make_location (start_loc, start_loc, finish_loc);
8304
8305 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8306 noexcept_loc);
8307 }
8308
8309 default:
8310 break;
8311 }
8312 }
8313
8314 /* Look for the `:: new' and `:: delete', which also signal the
8315 beginning of a new-expression, or delete-expression,
8316 respectively. If the next token is `::', then it might be one of
8317 these. */
8318 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8319 {
8320 enum rid keyword;
8321
8322 /* See if the token after the `::' is one of the keywords in
8323 which we're interested. */
8324 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8325 /* If it's `new', we have a new-expression. */
8326 if (keyword == RID_NEW)
8327 return cp_parser_new_expression (parser);
8328 /* Similarly, for `delete'. */
8329 else if (keyword == RID_DELETE)
8330 return cp_parser_delete_expression (parser);
8331 }
8332
8333 /* Look for a unary operator. */
8334 unary_operator = cp_parser_unary_operator (token);
8335 /* The `++' and `--' operators can be handled similarly, even though
8336 they are not technically unary-operators in the grammar. */
8337 if (unary_operator == ERROR_MARK)
8338 {
8339 if (token->type == CPP_PLUS_PLUS)
8340 unary_operator = PREINCREMENT_EXPR;
8341 else if (token->type == CPP_MINUS_MINUS)
8342 unary_operator = PREDECREMENT_EXPR;
8343 /* Handle the GNU address-of-label extension. */
8344 else if (cp_parser_allow_gnu_extensions_p (parser)
8345 && token->type == CPP_AND_AND)
8346 {
8347 tree identifier;
8348 tree expression;
8349 location_t start_loc = token->location;
8350
8351 /* Consume the '&&' token. */
8352 cp_lexer_consume_token (parser->lexer);
8353 /* Look for the identifier. */
8354 location_t finish_loc
8355 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8356 identifier = cp_parser_identifier (parser);
8357 /* Construct a location of the form:
8358 &&label
8359 ^~~~~~~
8360 with caret==start at the "&&", finish at the end of the label. */
8361 location_t combined_loc
8362 = make_location (start_loc, start_loc, finish_loc);
8363 /* Create an expression representing the address. */
8364 expression = finish_label_address_expr (identifier, combined_loc);
8365 if (cp_parser_non_integral_constant_expression (parser,
8366 NIC_ADDR_LABEL))
8367 expression = error_mark_node;
8368 return expression;
8369 }
8370 }
8371 if (unary_operator != ERROR_MARK)
8372 {
8373 cp_expr cast_expression;
8374 cp_expr expression = error_mark_node;
8375 non_integral_constant non_constant_p = NIC_NONE;
8376 location_t loc = token->location;
8377 tsubst_flags_t complain = complain_flags (decltype_p);
8378
8379 /* Consume the operator token. */
8380 token = cp_lexer_consume_token (parser->lexer);
8381 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8382
8383 /* Parse the cast-expression. */
8384 cast_expression
8385 = cp_parser_cast_expression (parser,
8386 unary_operator == ADDR_EXPR,
8387 /*cast_p=*/false,
8388 /*decltype*/false,
8389 pidk);
8390
8391 /* Make a location:
8392 OP_TOKEN CAST_EXPRESSION
8393 ^~~~~~~~~~~~~~~~~~~~~~~~~
8394 with start==caret at the operator token, and
8395 extending to the end of the cast_expression. */
8396 loc = make_location (loc, loc, cast_expression.get_finish ());
8397
8398 /* Now, build an appropriate representation. */
8399 switch (unary_operator)
8400 {
8401 case INDIRECT_REF:
8402 non_constant_p = NIC_STAR;
8403 expression = build_x_indirect_ref (loc, cast_expression,
8404 RO_UNARY_STAR,
8405 complain);
8406 /* TODO: build_x_indirect_ref does not always honor the
8407 location, so ensure it is set. */
8408 expression.set_location (loc);
8409 break;
8410
8411 case ADDR_EXPR:
8412 non_constant_p = NIC_ADDR;
8413 /* Fall through. */
8414 case BIT_NOT_EXPR:
8415 expression = build_x_unary_op (loc, unary_operator,
8416 cast_expression,
8417 complain);
8418 /* TODO: build_x_unary_op does not always honor the location,
8419 so ensure it is set. */
8420 expression.set_location (loc);
8421 break;
8422
8423 case PREINCREMENT_EXPR:
8424 case PREDECREMENT_EXPR:
8425 non_constant_p = unary_operator == PREINCREMENT_EXPR
8426 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8427 /* Fall through. */
8428 case NEGATE_EXPR:
8429 /* Immediately fold negation of a constant, unless the constant is 0
8430 (since -0 == 0) or it would overflow. */
8431 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8432 {
8433 tree stripped_expr
8434 = tree_strip_any_location_wrapper (cast_expression);
8435 if (CONSTANT_CLASS_P (stripped_expr)
8436 && !integer_zerop (stripped_expr)
8437 && !TREE_OVERFLOW (stripped_expr))
8438 {
8439 tree folded = fold_build1 (unary_operator,
8440 TREE_TYPE (stripped_expr),
8441 stripped_expr);
8442 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8443 {
8444 expression = maybe_wrap_with_location (folded, loc);
8445 break;
8446 }
8447 }
8448 }
8449 /* Fall through. */
8450 case UNARY_PLUS_EXPR:
8451 case TRUTH_NOT_EXPR:
8452 expression = finish_unary_op_expr (loc, unary_operator,
8453 cast_expression, complain);
8454 break;
8455
8456 default:
8457 gcc_unreachable ();
8458 }
8459
8460 if (non_constant_p != NIC_NONE
8461 && cp_parser_non_integral_constant_expression (parser,
8462 non_constant_p))
8463 expression = error_mark_node;
8464
8465 return expression;
8466 }
8467
8468 return cp_parser_postfix_expression (parser, address_p, cast_p,
8469 /*member_access_only_p=*/false,
8470 decltype_p,
8471 pidk);
8472 }
8473
8474 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8475 unary-operator, the corresponding tree code is returned. */
8476
8477 static enum tree_code
8478 cp_parser_unary_operator (cp_token* token)
8479 {
8480 switch (token->type)
8481 {
8482 case CPP_MULT:
8483 return INDIRECT_REF;
8484
8485 case CPP_AND:
8486 return ADDR_EXPR;
8487
8488 case CPP_PLUS:
8489 return UNARY_PLUS_EXPR;
8490
8491 case CPP_MINUS:
8492 return NEGATE_EXPR;
8493
8494 case CPP_NOT:
8495 return TRUTH_NOT_EXPR;
8496
8497 case CPP_COMPL:
8498 return BIT_NOT_EXPR;
8499
8500 default:
8501 return ERROR_MARK;
8502 }
8503 }
8504
8505 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8506 Returns a representation of the expression. */
8507
8508 static tree
8509 cp_parser_has_attribute_expression (cp_parser *parser)
8510 {
8511 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8512
8513 /* Consume the __builtin_has_attribute token. */
8514 cp_lexer_consume_token (parser->lexer);
8515
8516 matching_parens parens;
8517 if (!parens.require_open (parser))
8518 return error_mark_node;
8519
8520 /* Types cannot be defined in a `sizeof' expression. Save away the
8521 old message. */
8522 const char *saved_message = parser->type_definition_forbidden_message;
8523 const char *saved_message_arg
8524 = parser->type_definition_forbidden_message_arg;
8525 parser->type_definition_forbidden_message
8526 = G_("types may not be defined in %qs expressions");
8527 parser->type_definition_forbidden_message_arg
8528 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
8529
8530 /* The restrictions on constant-expressions do not apply inside
8531 sizeof expressions. */
8532 bool saved_integral_constant_expression_p
8533 = parser->integral_constant_expression_p;
8534 bool saved_non_integral_constant_expression_p
8535 = parser->non_integral_constant_expression_p;
8536 parser->integral_constant_expression_p = false;
8537
8538 /* Do not actually evaluate the expression. */
8539 ++cp_unevaluated_operand;
8540 ++c_inhibit_evaluation_warnings;
8541
8542 tree oper = NULL_TREE;
8543
8544 /* We can't be sure yet whether we're looking at a type-id or an
8545 expression. */
8546 cp_parser_parse_tentatively (parser);
8547
8548 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8549 parser->in_type_id_in_expr_p = true;
8550 /* Look for the type-id. */
8551 oper = cp_parser_type_id (parser);
8552 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8553
8554 cp_parser_parse_definitely (parser);
8555
8556 /* If the type-id production did not work out, then we must be
8557 looking at an expression. */
8558 if (!oper || oper == error_mark_node)
8559 oper = cp_parser_assignment_expression (parser);
8560
8561 STRIP_ANY_LOCATION_WRAPPER (oper);
8562
8563 /* Go back to evaluating expressions. */
8564 --cp_unevaluated_operand;
8565 --c_inhibit_evaluation_warnings;
8566
8567 /* And restore the old one. */
8568 parser->type_definition_forbidden_message = saved_message;
8569 parser->type_definition_forbidden_message_arg = saved_message_arg;
8570 parser->integral_constant_expression_p
8571 = saved_integral_constant_expression_p;
8572 parser->non_integral_constant_expression_p
8573 = saved_non_integral_constant_expression_p;
8574
8575 /* Consume the comma if it's there. */
8576 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
8577 {
8578 cp_parser_skip_to_closing_parenthesis (parser, false, false,
8579 /*consume_paren=*/true);
8580 return error_mark_node;
8581 }
8582
8583 /* Parse the attribute specification. */
8584 bool ret = false;
8585 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
8586 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
8587 {
8588 if (oper != error_mark_node)
8589 {
8590 /* Fold constant expressions used in attributes first. */
8591 cp_check_const_attributes (attr);
8592
8593 /* Finally, see if OPER has been declared with ATTR. */
8594 ret = has_attribute (atloc, oper, attr, default_conversion);
8595 }
8596
8597 parens.require_close (parser);
8598 }
8599 else
8600 {
8601 error_at (atloc, "expected identifier");
8602 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8603 }
8604
8605 /* Construct a location e.g. :
8606 __builtin_has_attribute (oper, attr)
8607 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8608 with start == caret at the start of the built-in token,
8609 and with the endpoint at the final closing paren. */
8610 location_t finish_loc
8611 = cp_lexer_previous_token (parser->lexer)->location;
8612 location_t compound_loc
8613 = make_location (start_loc, start_loc, finish_loc);
8614
8615 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
8616 ret_expr.set_location (compound_loc);
8617 ret_expr = ret_expr.maybe_add_location_wrapper ();
8618 return ret_expr;
8619 }
8620
8621 /* Parse a new-expression.
8622
8623 new-expression:
8624 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8625 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8626
8627 Returns a representation of the expression. */
8628
8629 static tree
8630 cp_parser_new_expression (cp_parser* parser)
8631 {
8632 bool global_scope_p;
8633 vec<tree, va_gc> *placement;
8634 tree type;
8635 vec<tree, va_gc> *initializer;
8636 tree nelts = NULL_TREE;
8637 tree ret;
8638
8639 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8640
8641 /* Look for the optional `::' operator. */
8642 global_scope_p
8643 = (cp_parser_global_scope_opt (parser,
8644 /*current_scope_valid_p=*/false)
8645 != NULL_TREE);
8646 /* Look for the `new' operator. */
8647 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8648 /* There's no easy way to tell a new-placement from the
8649 `( type-id )' construct. */
8650 cp_parser_parse_tentatively (parser);
8651 /* Look for a new-placement. */
8652 placement = cp_parser_new_placement (parser);
8653 /* If that didn't work out, there's no new-placement. */
8654 if (!cp_parser_parse_definitely (parser))
8655 {
8656 if (placement != NULL)
8657 release_tree_vector (placement);
8658 placement = NULL;
8659 }
8660
8661 /* If the next token is a `(', then we have a parenthesized
8662 type-id. */
8663 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8664 {
8665 cp_token *token;
8666 const char *saved_message = parser->type_definition_forbidden_message;
8667
8668 /* Consume the `('. */
8669 matching_parens parens;
8670 parens.consume_open (parser);
8671
8672 /* Parse the type-id. */
8673 parser->type_definition_forbidden_message
8674 = G_("types may not be defined in a new-expression");
8675 {
8676 type_id_in_expr_sentinel s (parser);
8677 type = cp_parser_type_id (parser);
8678 }
8679 parser->type_definition_forbidden_message = saved_message;
8680
8681 /* Look for the closing `)'. */
8682 parens.require_close (parser);
8683 token = cp_lexer_peek_token (parser->lexer);
8684 /* There should not be a direct-new-declarator in this production,
8685 but GCC used to allowed this, so we check and emit a sensible error
8686 message for this case. */
8687 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8688 {
8689 error_at (token->location,
8690 "array bound forbidden after parenthesized type-id");
8691 inform (token->location,
8692 "try removing the parentheses around the type-id");
8693 cp_parser_direct_new_declarator (parser);
8694 }
8695 }
8696 /* Otherwise, there must be a new-type-id. */
8697 else
8698 type = cp_parser_new_type_id (parser, &nelts);
8699
8700 /* If the next token is a `(' or '{', then we have a new-initializer. */
8701 cp_token *token = cp_lexer_peek_token (parser->lexer);
8702 if (token->type == CPP_OPEN_PAREN
8703 || token->type == CPP_OPEN_BRACE)
8704 initializer = cp_parser_new_initializer (parser);
8705 else
8706 initializer = NULL;
8707
8708 /* A new-expression may not appear in an integral constant
8709 expression. */
8710 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8711 ret = error_mark_node;
8712 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8713 of a new-type-id or type-id of a new-expression, the new-expression shall
8714 contain a new-initializer of the form ( assignment-expression )".
8715 Additionally, consistently with the spirit of DR 1467, we want to accept
8716 'new auto { 2 }' too. */
8717 else if ((ret = type_uses_auto (type))
8718 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8719 && (vec_safe_length (initializer) != 1
8720 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8721 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8722 {
8723 error_at (token->location,
8724 "initialization of new-expression for type %<auto%> "
8725 "requires exactly one element");
8726 ret = error_mark_node;
8727 }
8728 else
8729 {
8730 /* Construct a location e.g.:
8731 ptr = new int[100]
8732 ^~~~~~~~~~~~
8733 with caret == start at the start of the "new" token, and the end
8734 at the end of the final token we consumed. */
8735 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8736 location_t end_loc = get_finish (end_tok->location);
8737 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8738
8739 /* Create a representation of the new-expression. */
8740 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8741 tf_warning_or_error);
8742 protected_set_expr_location (ret, combined_loc);
8743 }
8744
8745 if (placement != NULL)
8746 release_tree_vector (placement);
8747 if (initializer != NULL)
8748 release_tree_vector (initializer);
8749
8750 return ret;
8751 }
8752
8753 /* Parse a new-placement.
8754
8755 new-placement:
8756 ( expression-list )
8757
8758 Returns the same representation as for an expression-list. */
8759
8760 static vec<tree, va_gc> *
8761 cp_parser_new_placement (cp_parser* parser)
8762 {
8763 vec<tree, va_gc> *expression_list;
8764
8765 /* Parse the expression-list. */
8766 expression_list = (cp_parser_parenthesized_expression_list
8767 (parser, non_attr, /*cast_p=*/false,
8768 /*allow_expansion_p=*/true,
8769 /*non_constant_p=*/NULL));
8770
8771 if (expression_list && expression_list->is_empty ())
8772 error ("expected expression-list or type-id");
8773
8774 return expression_list;
8775 }
8776
8777 /* Parse a new-type-id.
8778
8779 new-type-id:
8780 type-specifier-seq new-declarator [opt]
8781
8782 Returns the TYPE allocated. If the new-type-id indicates an array
8783 type, *NELTS is set to the number of elements in the last array
8784 bound; the TYPE will not include the last array bound. */
8785
8786 static tree
8787 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8788 {
8789 cp_decl_specifier_seq type_specifier_seq;
8790 cp_declarator *new_declarator;
8791 cp_declarator *declarator;
8792 cp_declarator *outer_declarator;
8793 const char *saved_message;
8794
8795 /* The type-specifier sequence must not contain type definitions.
8796 (It cannot contain declarations of new types either, but if they
8797 are not definitions we will catch that because they are not
8798 complete.) */
8799 saved_message = parser->type_definition_forbidden_message;
8800 parser->type_definition_forbidden_message
8801 = G_("types may not be defined in a new-type-id");
8802 /* Parse the type-specifier-seq. */
8803 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
8804 /*is_declaration=*/false,
8805 /*is_trailing_return=*/false,
8806 &type_specifier_seq);
8807 /* Restore the old message. */
8808 parser->type_definition_forbidden_message = saved_message;
8809
8810 if (type_specifier_seq.type == error_mark_node)
8811 return error_mark_node;
8812
8813 /* Parse the new-declarator. */
8814 new_declarator = cp_parser_new_declarator_opt (parser);
8815
8816 /* Determine the number of elements in the last array dimension, if
8817 any. */
8818 *nelts = NULL_TREE;
8819 /* Skip down to the last array dimension. */
8820 declarator = new_declarator;
8821 outer_declarator = NULL;
8822 while (declarator && (declarator->kind == cdk_pointer
8823 || declarator->kind == cdk_ptrmem))
8824 {
8825 outer_declarator = declarator;
8826 declarator = declarator->declarator;
8827 }
8828 while (declarator
8829 && declarator->kind == cdk_array
8830 && declarator->declarator
8831 && declarator->declarator->kind == cdk_array)
8832 {
8833 outer_declarator = declarator;
8834 declarator = declarator->declarator;
8835 }
8836
8837 if (declarator && declarator->kind == cdk_array)
8838 {
8839 *nelts = declarator->u.array.bounds;
8840 if (*nelts == error_mark_node)
8841 *nelts = integer_one_node;
8842
8843 if (outer_declarator)
8844 outer_declarator->declarator = declarator->declarator;
8845 else
8846 new_declarator = NULL;
8847 }
8848
8849 return groktypename (&type_specifier_seq, new_declarator, false);
8850 }
8851
8852 /* Parse an (optional) new-declarator.
8853
8854 new-declarator:
8855 ptr-operator new-declarator [opt]
8856 direct-new-declarator
8857
8858 Returns the declarator. */
8859
8860 static cp_declarator *
8861 cp_parser_new_declarator_opt (cp_parser* parser)
8862 {
8863 enum tree_code code;
8864 tree type, std_attributes = NULL_TREE;
8865 cp_cv_quals cv_quals;
8866
8867 /* We don't know if there's a ptr-operator next, or not. */
8868 cp_parser_parse_tentatively (parser);
8869 /* Look for a ptr-operator. */
8870 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8871 /* If that worked, look for more new-declarators. */
8872 if (cp_parser_parse_definitely (parser))
8873 {
8874 cp_declarator *declarator;
8875
8876 /* Parse another optional declarator. */
8877 declarator = cp_parser_new_declarator_opt (parser);
8878
8879 declarator = cp_parser_make_indirect_declarator
8880 (code, type, cv_quals, declarator, std_attributes);
8881
8882 return declarator;
8883 }
8884
8885 /* If the next token is a `[', there is a direct-new-declarator. */
8886 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8887 return cp_parser_direct_new_declarator (parser);
8888
8889 return NULL;
8890 }
8891
8892 /* Parse a direct-new-declarator.
8893
8894 direct-new-declarator:
8895 [ expression ]
8896 direct-new-declarator [constant-expression]
8897
8898 */
8899
8900 static cp_declarator *
8901 cp_parser_direct_new_declarator (cp_parser* parser)
8902 {
8903 cp_declarator *declarator = NULL;
8904
8905 while (true)
8906 {
8907 tree expression;
8908 cp_token *token;
8909
8910 /* Look for the opening `['. */
8911 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8912
8913 token = cp_lexer_peek_token (parser->lexer);
8914 expression = cp_parser_expression (parser);
8915 /* The standard requires that the expression have integral
8916 type. DR 74 adds enumeration types. We believe that the
8917 real intent is that these expressions be handled like the
8918 expression in a `switch' condition, which also allows
8919 classes with a single conversion to integral or
8920 enumeration type. */
8921 if (!processing_template_decl)
8922 {
8923 expression
8924 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8925 expression,
8926 /*complain=*/true);
8927 if (!expression)
8928 {
8929 error_at (token->location,
8930 "expression in new-declarator must have integral "
8931 "or enumeration type");
8932 expression = error_mark_node;
8933 }
8934 }
8935
8936 /* Look for the closing `]'. */
8937 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8938
8939 /* Add this bound to the declarator. */
8940 declarator = make_array_declarator (declarator, expression);
8941
8942 /* If the next token is not a `[', then there are no more
8943 bounds. */
8944 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8945 break;
8946 }
8947
8948 return declarator;
8949 }
8950
8951 /* Parse a new-initializer.
8952
8953 new-initializer:
8954 ( expression-list [opt] )
8955 braced-init-list
8956
8957 Returns a representation of the expression-list. */
8958
8959 static vec<tree, va_gc> *
8960 cp_parser_new_initializer (cp_parser* parser)
8961 {
8962 vec<tree, va_gc> *expression_list;
8963
8964 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8965 {
8966 tree t;
8967 bool expr_non_constant_p;
8968 cp_lexer_set_source_position (parser->lexer);
8969 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8970 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8971 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8972 expression_list = make_tree_vector_single (t);
8973 }
8974 else
8975 expression_list = (cp_parser_parenthesized_expression_list
8976 (parser, non_attr, /*cast_p=*/false,
8977 /*allow_expansion_p=*/true,
8978 /*non_constant_p=*/NULL));
8979
8980 return expression_list;
8981 }
8982
8983 /* Parse a delete-expression.
8984
8985 delete-expression:
8986 :: [opt] delete cast-expression
8987 :: [opt] delete [ ] cast-expression
8988
8989 Returns a representation of the expression. */
8990
8991 static tree
8992 cp_parser_delete_expression (cp_parser* parser)
8993 {
8994 bool global_scope_p;
8995 bool array_p;
8996 tree expression;
8997
8998 /* Look for the optional `::' operator. */
8999 global_scope_p
9000 = (cp_parser_global_scope_opt (parser,
9001 /*current_scope_valid_p=*/false)
9002 != NULL_TREE);
9003 /* Look for the `delete' keyword. */
9004 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9005 /* See if the array syntax is in use. */
9006 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9007 {
9008 /* Consume the `[' token. */
9009 cp_lexer_consume_token (parser->lexer);
9010 /* Look for the `]' token. */
9011 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9012 /* Remember that this is the `[]' construct. */
9013 array_p = true;
9014 }
9015 else
9016 array_p = false;
9017
9018 /* Parse the cast-expression. */
9019 expression = cp_parser_simple_cast_expression (parser);
9020
9021 /* A delete-expression may not appear in an integral constant
9022 expression. */
9023 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9024 return error_mark_node;
9025
9026 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
9027 tf_warning_or_error);
9028 }
9029
9030 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9031 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9032 0 otherwise. */
9033
9034 static int
9035 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9036 {
9037 cp_token *token = cp_lexer_peek_token (parser->lexer);
9038 switch (token->type)
9039 {
9040 case CPP_COMMA:
9041 case CPP_SEMICOLON:
9042 case CPP_QUERY:
9043 case CPP_COLON:
9044 case CPP_CLOSE_SQUARE:
9045 case CPP_CLOSE_PAREN:
9046 case CPP_CLOSE_BRACE:
9047 case CPP_OPEN_BRACE:
9048 case CPP_DOT:
9049 case CPP_DOT_STAR:
9050 case CPP_DEREF:
9051 case CPP_DEREF_STAR:
9052 case CPP_DIV:
9053 case CPP_MOD:
9054 case CPP_LSHIFT:
9055 case CPP_RSHIFT:
9056 case CPP_LESS:
9057 case CPP_GREATER:
9058 case CPP_LESS_EQ:
9059 case CPP_GREATER_EQ:
9060 case CPP_EQ_EQ:
9061 case CPP_NOT_EQ:
9062 case CPP_EQ:
9063 case CPP_MULT_EQ:
9064 case CPP_DIV_EQ:
9065 case CPP_MOD_EQ:
9066 case CPP_PLUS_EQ:
9067 case CPP_MINUS_EQ:
9068 case CPP_RSHIFT_EQ:
9069 case CPP_LSHIFT_EQ:
9070 case CPP_AND_EQ:
9071 case CPP_XOR_EQ:
9072 case CPP_OR_EQ:
9073 case CPP_XOR:
9074 case CPP_OR:
9075 case CPP_OR_OR:
9076 case CPP_EOF:
9077 case CPP_ELLIPSIS:
9078 return 0;
9079
9080 case CPP_OPEN_PAREN:
9081 /* In ((type ()) () the last () isn't a valid cast-expression,
9082 so the whole must be parsed as postfix-expression. */
9083 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9084 != CPP_CLOSE_PAREN;
9085
9086 case CPP_OPEN_SQUARE:
9087 /* '[' may start a primary-expression in obj-c++ and in C++11,
9088 as a lambda-expression, eg, '(void)[]{}'. */
9089 if (cxx_dialect >= cxx11)
9090 return -1;
9091 return c_dialect_objc ();
9092
9093 case CPP_PLUS_PLUS:
9094 case CPP_MINUS_MINUS:
9095 /* '++' and '--' may or may not start a cast-expression:
9096
9097 struct T { void operator++(int); };
9098 void f() { (T())++; }
9099
9100 vs
9101
9102 int a;
9103 (int)++a; */
9104 return -1;
9105
9106 default:
9107 return 1;
9108 }
9109 }
9110
9111 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9112 in the order: const_cast, static_cast, reinterpret_cast.
9113
9114 Don't suggest dynamic_cast.
9115
9116 Return the first legal cast kind found, or NULL otherwise. */
9117
9118 static const char *
9119 get_cast_suggestion (tree dst_type, tree orig_expr)
9120 {
9121 tree trial;
9122
9123 /* Reuse the parser logic by attempting to build the various kinds of
9124 cast, with "complain" disabled.
9125 Identify the first such cast that is valid. */
9126
9127 /* Don't attempt to run such logic within template processing. */
9128 if (processing_template_decl)
9129 return NULL;
9130
9131 /* First try const_cast. */
9132 trial = build_const_cast (dst_type, orig_expr, tf_none);
9133 if (trial != error_mark_node)
9134 return "const_cast";
9135
9136 /* If that fails, try static_cast. */
9137 trial = build_static_cast (dst_type, orig_expr, tf_none);
9138 if (trial != error_mark_node)
9139 return "static_cast";
9140
9141 /* Finally, try reinterpret_cast. */
9142 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
9143 if (trial != error_mark_node)
9144 return "reinterpret_cast";
9145
9146 /* No such cast possible. */
9147 return NULL;
9148 }
9149
9150 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9151 suggesting how to convert a C-style cast of the form:
9152
9153 (DST_TYPE)ORIG_EXPR
9154
9155 to a C++-style cast.
9156
9157 The primary range of RICHLOC is asssumed to be that of the original
9158 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9159 of the parens in the C-style cast. */
9160
9161 static void
9162 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9163 location_t close_paren_loc, tree orig_expr,
9164 tree dst_type)
9165 {
9166 /* This function is non-trivial, so bail out now if the warning isn't
9167 going to be emitted. */
9168 if (!warn_old_style_cast)
9169 return;
9170
9171 /* Try to find a legal C++ cast, trying them in order:
9172 const_cast, static_cast, reinterpret_cast. */
9173 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9174 if (!cast_suggestion)
9175 return;
9176
9177 /* Replace the open paren with "CAST_SUGGESTION<". */
9178 pretty_printer pp;
9179 pp_printf (&pp, "%s<", cast_suggestion);
9180 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9181
9182 /* Replace the close paren with "> (". */
9183 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9184
9185 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9186 rich_loc->add_fixit_insert_after (")");
9187 }
9188
9189
9190 /* Parse a cast-expression.
9191
9192 cast-expression:
9193 unary-expression
9194 ( type-id ) cast-expression
9195
9196 ADDRESS_P is true iff the unary-expression is appearing as the
9197 operand of the `&' operator. CAST_P is true if this expression is
9198 the target of a cast.
9199
9200 Returns a representation of the expression. */
9201
9202 static cp_expr
9203 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9204 bool decltype_p, cp_id_kind * pidk)
9205 {
9206 /* If it's a `(', then we might be looking at a cast. */
9207 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9208 {
9209 tree type = NULL_TREE;
9210 cp_expr expr (NULL_TREE);
9211 int cast_expression = 0;
9212 const char *saved_message;
9213
9214 /* There's no way to know yet whether or not this is a cast.
9215 For example, `(int (3))' is a unary-expression, while `(int)
9216 3' is a cast. So, we resort to parsing tentatively. */
9217 cp_parser_parse_tentatively (parser);
9218 /* Types may not be defined in a cast. */
9219 saved_message = parser->type_definition_forbidden_message;
9220 parser->type_definition_forbidden_message
9221 = G_("types may not be defined in casts");
9222 /* Consume the `('. */
9223 matching_parens parens;
9224 cp_token *open_paren = parens.consume_open (parser);
9225 location_t open_paren_loc = open_paren->location;
9226 location_t close_paren_loc = UNKNOWN_LOCATION;
9227
9228 /* A very tricky bit is that `(struct S) { 3 }' is a
9229 compound-literal (which we permit in C++ as an extension).
9230 But, that construct is not a cast-expression -- it is a
9231 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9232 is legal; if the compound-literal were a cast-expression,
9233 you'd need an extra set of parentheses.) But, if we parse
9234 the type-id, and it happens to be a class-specifier, then we
9235 will commit to the parse at that point, because we cannot
9236 undo the action that is done when creating a new class. So,
9237 then we cannot back up and do a postfix-expression.
9238
9239 Another tricky case is the following (c++/29234):
9240
9241 struct S { void operator () (); };
9242
9243 void foo ()
9244 {
9245 ( S()() );
9246 }
9247
9248 As a type-id we parse the parenthesized S()() as a function
9249 returning a function, groktypename complains and we cannot
9250 back up in this case either.
9251
9252 Therefore, we scan ahead to the closing `)', and check to see
9253 if the tokens after the `)' can start a cast-expression. Otherwise
9254 we are dealing with an unary-expression, a postfix-expression
9255 or something else.
9256
9257 Yet another tricky case, in C++11, is the following (c++/54891):
9258
9259 (void)[]{};
9260
9261 The issue is that usually, besides the case of lambda-expressions,
9262 the parenthesized type-id cannot be followed by '[', and, eg, we
9263 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9264 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9265 we don't commit, we try a cast-expression, then an unary-expression.
9266
9267 Save tokens so that we can put them back. */
9268 cp_lexer_save_tokens (parser->lexer);
9269
9270 /* We may be looking at a cast-expression. */
9271 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9272 /*consume_paren=*/true))
9273 cast_expression
9274 = cp_parser_tokens_start_cast_expression (parser);
9275
9276 /* Roll back the tokens we skipped. */
9277 cp_lexer_rollback_tokens (parser->lexer);
9278 /* If we aren't looking at a cast-expression, simulate an error so
9279 that the call to cp_parser_error_occurred below returns true. */
9280 if (!cast_expression)
9281 cp_parser_simulate_error (parser);
9282 else
9283 {
9284 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9285 parser->in_type_id_in_expr_p = true;
9286 /* Look for the type-id. */
9287 type = cp_parser_type_id (parser);
9288 /* Look for the closing `)'. */
9289 cp_token *close_paren = parens.require_close (parser);
9290 if (close_paren)
9291 close_paren_loc = close_paren->location;
9292 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9293 }
9294
9295 /* Restore the saved message. */
9296 parser->type_definition_forbidden_message = saved_message;
9297
9298 /* At this point this can only be either a cast or a
9299 parenthesized ctor such as `(T ())' that looks like a cast to
9300 function returning T. */
9301 if (!cp_parser_error_occurred (parser))
9302 {
9303 /* Only commit if the cast-expression doesn't start with
9304 '++', '--', or '[' in C++11. */
9305 if (cast_expression > 0)
9306 cp_parser_commit_to_topmost_tentative_parse (parser);
9307
9308 expr = cp_parser_cast_expression (parser,
9309 /*address_p=*/false,
9310 /*cast_p=*/true,
9311 /*decltype_p=*/false,
9312 pidk);
9313
9314 if (cp_parser_parse_definitely (parser))
9315 {
9316 /* Warn about old-style casts, if so requested. */
9317 if (warn_old_style_cast
9318 && !in_system_header_at (input_location)
9319 && !VOID_TYPE_P (type)
9320 && current_lang_name != lang_name_c)
9321 {
9322 gcc_rich_location rich_loc (input_location);
9323 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9324 expr, type);
9325 warning_at (&rich_loc, OPT_Wold_style_cast,
9326 "use of old-style cast to %q#T", type);
9327 }
9328
9329 /* Only type conversions to integral or enumeration types
9330 can be used in constant-expressions. */
9331 if (!cast_valid_in_integral_constant_expression_p (type)
9332 && cp_parser_non_integral_constant_expression (parser,
9333 NIC_CAST))
9334 return error_mark_node;
9335
9336 /* Perform the cast. */
9337 /* Make a location:
9338 (TYPE) EXPR
9339 ^~~~~~~~~~~
9340 with start==caret at the open paren, extending to the
9341 end of "expr". */
9342 location_t cast_loc = make_location (open_paren_loc,
9343 open_paren_loc,
9344 expr.get_finish ());
9345 expr = build_c_cast (cast_loc, type, expr);
9346 return expr;
9347 }
9348 }
9349 else
9350 cp_parser_abort_tentative_parse (parser);
9351 }
9352
9353 /* If we get here, then it's not a cast, so it must be a
9354 unary-expression. */
9355 return cp_parser_unary_expression (parser, pidk, address_p,
9356 cast_p, decltype_p);
9357 }
9358
9359 /* Parse a binary expression of the general form:
9360
9361 pm-expression:
9362 cast-expression
9363 pm-expression .* cast-expression
9364 pm-expression ->* cast-expression
9365
9366 multiplicative-expression:
9367 pm-expression
9368 multiplicative-expression * pm-expression
9369 multiplicative-expression / pm-expression
9370 multiplicative-expression % pm-expression
9371
9372 additive-expression:
9373 multiplicative-expression
9374 additive-expression + multiplicative-expression
9375 additive-expression - multiplicative-expression
9376
9377 shift-expression:
9378 additive-expression
9379 shift-expression << additive-expression
9380 shift-expression >> additive-expression
9381
9382 relational-expression:
9383 shift-expression
9384 relational-expression < shift-expression
9385 relational-expression > shift-expression
9386 relational-expression <= shift-expression
9387 relational-expression >= shift-expression
9388
9389 GNU Extension:
9390
9391 relational-expression:
9392 relational-expression <? shift-expression
9393 relational-expression >? shift-expression
9394
9395 equality-expression:
9396 relational-expression
9397 equality-expression == relational-expression
9398 equality-expression != relational-expression
9399
9400 and-expression:
9401 equality-expression
9402 and-expression & equality-expression
9403
9404 exclusive-or-expression:
9405 and-expression
9406 exclusive-or-expression ^ and-expression
9407
9408 inclusive-or-expression:
9409 exclusive-or-expression
9410 inclusive-or-expression | exclusive-or-expression
9411
9412 logical-and-expression:
9413 inclusive-or-expression
9414 logical-and-expression && inclusive-or-expression
9415
9416 logical-or-expression:
9417 logical-and-expression
9418 logical-or-expression || logical-and-expression
9419
9420 All these are implemented with a single function like:
9421
9422 binary-expression:
9423 simple-cast-expression
9424 binary-expression <token> binary-expression
9425
9426 CAST_P is true if this expression is the target of a cast.
9427
9428 The binops_by_token map is used to get the tree codes for each <token> type.
9429 binary-expressions are associated according to a precedence table. */
9430
9431 #define TOKEN_PRECEDENCE(token) \
9432 (((token->type == CPP_GREATER \
9433 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9434 && !parser->greater_than_is_operator_p) \
9435 ? PREC_NOT_OPERATOR \
9436 : binops_by_token[token->type].prec)
9437
9438 static cp_expr
9439 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9440 bool no_toplevel_fold_p,
9441 bool decltype_p,
9442 enum cp_parser_prec prec,
9443 cp_id_kind * pidk)
9444 {
9445 cp_parser_expression_stack stack;
9446 cp_parser_expression_stack_entry *sp = &stack[0];
9447 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
9448 cp_parser_expression_stack_entry current;
9449 cp_expr rhs;
9450 cp_token *token;
9451 enum tree_code rhs_type;
9452 enum cp_parser_prec new_prec, lookahead_prec;
9453 tree overload;
9454
9455 /* Parse the first expression. */
9456 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9457 ? TRUTH_NOT_EXPR : ERROR_MARK);
9458 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9459 cast_p, decltype_p, pidk);
9460 current.prec = prec;
9461
9462 if (cp_parser_error_occurred (parser))
9463 return error_mark_node;
9464
9465 for (;;)
9466 {
9467 /* Get an operator token. */
9468 token = cp_lexer_peek_token (parser->lexer);
9469
9470 if (warn_cxx11_compat
9471 && token->type == CPP_RSHIFT
9472 && !parser->greater_than_is_operator_p)
9473 {
9474 if (warning_at (token->location, OPT_Wc__11_compat,
9475 "%<>>%> operator is treated"
9476 " as two right angle brackets in C++11"))
9477 inform (token->location,
9478 "suggest parentheses around %<>>%> expression");
9479 }
9480
9481 new_prec = TOKEN_PRECEDENCE (token);
9482 if (new_prec != PREC_NOT_OPERATOR
9483 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9484 /* This is a fold-expression; handle it later. */
9485 new_prec = PREC_NOT_OPERATOR;
9486
9487 /* Popping an entry off the stack means we completed a subexpression:
9488 - either we found a token which is not an operator (`>' where it is not
9489 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9490 will happen repeatedly;
9491 - or, we found an operator which has lower priority. This is the case
9492 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9493 parsing `3 * 4'. */
9494 if (new_prec <= current.prec)
9495 {
9496 if (sp == stack)
9497 break;
9498 else
9499 goto pop;
9500 }
9501
9502 get_rhs:
9503 current.tree_type = binops_by_token[token->type].tree_type;
9504 current.loc = token->location;
9505
9506 /* We used the operator token. */
9507 cp_lexer_consume_token (parser->lexer);
9508
9509 /* For "false && x" or "true || x", x will never be executed;
9510 disable warnings while evaluating it. */
9511 if ((current.tree_type == TRUTH_ANDIF_EXPR
9512 && cp_fully_fold (current.lhs) == truthvalue_false_node)
9513 || (current.tree_type == TRUTH_ORIF_EXPR
9514 && cp_fully_fold (current.lhs) == truthvalue_true_node))
9515 {
9516 disable_warnings_sp = sp;
9517 ++c_inhibit_evaluation_warnings;
9518 }
9519
9520 /* Extract another operand. It may be the RHS of this expression
9521 or the LHS of a new, higher priority expression. */
9522 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9523 ? TRUTH_NOT_EXPR : ERROR_MARK);
9524 rhs = cp_parser_simple_cast_expression (parser);
9525
9526 /* Get another operator token. Look up its precedence to avoid
9527 building a useless (immediately popped) stack entry for common
9528 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9529 token = cp_lexer_peek_token (parser->lexer);
9530 lookahead_prec = TOKEN_PRECEDENCE (token);
9531 if (lookahead_prec != PREC_NOT_OPERATOR
9532 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9533 lookahead_prec = PREC_NOT_OPERATOR;
9534 if (lookahead_prec > new_prec)
9535 {
9536 /* ... and prepare to parse the RHS of the new, higher priority
9537 expression. Since precedence levels on the stack are
9538 monotonically increasing, we do not have to care about
9539 stack overflows. */
9540 *sp = current;
9541 ++sp;
9542 current.lhs = rhs;
9543 current.lhs_type = rhs_type;
9544 current.prec = new_prec;
9545 new_prec = lookahead_prec;
9546 goto get_rhs;
9547
9548 pop:
9549 lookahead_prec = new_prec;
9550 /* If the stack is not empty, we have parsed into LHS the right side
9551 (`4' in the example above) of an expression we had suspended.
9552 We can use the information on the stack to recover the LHS (`3')
9553 from the stack together with the tree code (`MULT_EXPR'), and
9554 the precedence of the higher level subexpression
9555 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9556 which will be used to actually build the additive expression. */
9557 rhs = current.lhs;
9558 rhs_type = current.lhs_type;
9559 --sp;
9560 current = *sp;
9561 }
9562
9563 /* Undo the disabling of warnings done above. */
9564 if (sp == disable_warnings_sp)
9565 {
9566 disable_warnings_sp = NULL;
9567 --c_inhibit_evaluation_warnings;
9568 }
9569
9570 if (warn_logical_not_paren
9571 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9572 && current.lhs_type == TRUTH_NOT_EXPR
9573 /* Avoid warning for !!x == y. */
9574 && (TREE_CODE (current.lhs) != NE_EXPR
9575 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9576 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9577 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9578 /* Avoid warning for !b == y where b is boolean. */
9579 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9580 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9581 != BOOLEAN_TYPE))))
9582 /* Avoid warning for !!b == y where b is boolean. */
9583 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
9584 || TREE_TYPE (current.lhs) == NULL_TREE
9585 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9586 warn_logical_not_parentheses (current.loc, current.tree_type,
9587 current.lhs, maybe_constant_value (rhs));
9588
9589 overload = NULL;
9590
9591 location_t combined_loc = make_location (current.loc,
9592 current.lhs.get_start (),
9593 rhs.get_finish ());
9594
9595 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9596 ERROR_MARK for everything that is not a binary expression.
9597 This makes warn_about_parentheses miss some warnings that
9598 involve unary operators. For unary expressions we should
9599 pass the correct tree_code unless the unary expression was
9600 surrounded by parentheses.
9601 */
9602 if (no_toplevel_fold_p
9603 && lookahead_prec <= current.prec
9604 && sp == stack)
9605 {
9606 if (current.lhs == error_mark_node || rhs == error_mark_node)
9607 current.lhs = error_mark_node;
9608 else
9609 {
9610 current.lhs
9611 = build_min (current.tree_type,
9612 TREE_CODE_CLASS (current.tree_type)
9613 == tcc_comparison
9614 ? boolean_type_node : TREE_TYPE (current.lhs),
9615 current.lhs.get_value (), rhs.get_value ());
9616 SET_EXPR_LOCATION (current.lhs, combined_loc);
9617 }
9618 }
9619 else
9620 {
9621 op_location_t op_loc (current.loc, combined_loc);
9622 current.lhs = build_x_binary_op (op_loc, current.tree_type,
9623 current.lhs, current.lhs_type,
9624 rhs, rhs_type, &overload,
9625 complain_flags (decltype_p));
9626 /* TODO: build_x_binary_op doesn't always honor the location. */
9627 current.lhs.set_location (combined_loc);
9628 }
9629 current.lhs_type = current.tree_type;
9630
9631 /* If the binary operator required the use of an overloaded operator,
9632 then this expression cannot be an integral constant-expression.
9633 An overloaded operator can be used even if both operands are
9634 otherwise permissible in an integral constant-expression if at
9635 least one of the operands is of enumeration type. */
9636
9637 if (overload
9638 && cp_parser_non_integral_constant_expression (parser,
9639 NIC_OVERLOADED))
9640 return error_mark_node;
9641 }
9642
9643 return current.lhs;
9644 }
9645
9646 static cp_expr
9647 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9648 bool no_toplevel_fold_p,
9649 enum cp_parser_prec prec,
9650 cp_id_kind * pidk)
9651 {
9652 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9653 /*decltype*/false, prec, pidk);
9654 }
9655
9656 /* Parse the `? expression : assignment-expression' part of a
9657 conditional-expression. The LOGICAL_OR_EXPR is the
9658 logical-or-expression that started the conditional-expression.
9659 Returns a representation of the entire conditional-expression.
9660
9661 This routine is used by cp_parser_assignment_expression.
9662
9663 ? expression : assignment-expression
9664
9665 GNU Extensions:
9666
9667 ? : assignment-expression */
9668
9669 static tree
9670 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9671 {
9672 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9673 cp_expr assignment_expr;
9674 struct cp_token *token;
9675 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9676
9677 /* Consume the `?' token. */
9678 cp_lexer_consume_token (parser->lexer);
9679 token = cp_lexer_peek_token (parser->lexer);
9680 if (cp_parser_allow_gnu_extensions_p (parser)
9681 && token->type == CPP_COLON)
9682 {
9683 pedwarn (token->location, OPT_Wpedantic,
9684 "ISO C++ does not allow %<?:%> with omitted middle operand");
9685 /* Implicit true clause. */
9686 expr = NULL_TREE;
9687 c_inhibit_evaluation_warnings +=
9688 folded_logical_or_expr == truthvalue_true_node;
9689 warn_for_omitted_condop (token->location, logical_or_expr);
9690 }
9691 else
9692 {
9693 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9694 parser->colon_corrects_to_scope_p = false;
9695 /* Parse the expression. */
9696 c_inhibit_evaluation_warnings +=
9697 folded_logical_or_expr == truthvalue_false_node;
9698 expr = cp_parser_expression (parser);
9699 c_inhibit_evaluation_warnings +=
9700 ((folded_logical_or_expr == truthvalue_true_node)
9701 - (folded_logical_or_expr == truthvalue_false_node));
9702 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9703 }
9704
9705 /* The next token should be a `:'. */
9706 cp_parser_require (parser, CPP_COLON, RT_COLON);
9707 /* Parse the assignment-expression. */
9708 assignment_expr = cp_parser_assignment_expression (parser);
9709 c_inhibit_evaluation_warnings -=
9710 folded_logical_or_expr == truthvalue_true_node;
9711
9712 /* Make a location:
9713 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9714 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9715 with the caret at the "?", ranging from the start of
9716 the logical_or_expr to the end of the assignment_expr. */
9717 loc = make_location (loc,
9718 logical_or_expr.get_start (),
9719 assignment_expr.get_finish ());
9720
9721 /* Build the conditional-expression. */
9722 return build_x_conditional_expr (loc, logical_or_expr,
9723 expr,
9724 assignment_expr,
9725 tf_warning_or_error);
9726 }
9727
9728 /* Parse an assignment-expression.
9729
9730 assignment-expression:
9731 conditional-expression
9732 logical-or-expression assignment-operator assignment_expression
9733 throw-expression
9734
9735 CAST_P is true if this expression is the target of a cast.
9736 DECLTYPE_P is true if this expression is the operand of decltype.
9737
9738 Returns a representation for the expression. */
9739
9740 static cp_expr
9741 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9742 bool cast_p, bool decltype_p)
9743 {
9744 cp_expr expr;
9745
9746 /* If the next token is the `throw' keyword, then we're looking at
9747 a throw-expression. */
9748 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9749 expr = cp_parser_throw_expression (parser);
9750 /* Otherwise, it must be that we are looking at a
9751 logical-or-expression. */
9752 else
9753 {
9754 /* Parse the binary expressions (logical-or-expression). */
9755 expr = cp_parser_binary_expression (parser, cast_p, false,
9756 decltype_p,
9757 PREC_NOT_OPERATOR, pidk);
9758 /* If the next token is a `?' then we're actually looking at a
9759 conditional-expression. */
9760 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9761 return cp_parser_question_colon_clause (parser, expr);
9762 else
9763 {
9764 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9765
9766 /* If it's an assignment-operator, we're using the second
9767 production. */
9768 enum tree_code assignment_operator
9769 = cp_parser_assignment_operator_opt (parser);
9770 if (assignment_operator != ERROR_MARK)
9771 {
9772 bool non_constant_p;
9773
9774 /* Parse the right-hand side of the assignment. */
9775 cp_expr rhs = cp_parser_initializer_clause (parser,
9776 &non_constant_p);
9777
9778 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9779 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9780
9781 /* An assignment may not appear in a
9782 constant-expression. */
9783 if (cp_parser_non_integral_constant_expression (parser,
9784 NIC_ASSIGNMENT))
9785 return error_mark_node;
9786 /* Build the assignment expression. Its default
9787 location:
9788 LHS = RHS
9789 ~~~~^~~~~
9790 is the location of the '=' token as the
9791 caret, ranging from the start of the lhs to the
9792 end of the rhs. */
9793 loc = make_location (loc,
9794 expr.get_start (),
9795 rhs.get_finish ());
9796 expr = build_x_modify_expr (loc, expr,
9797 assignment_operator,
9798 rhs,
9799 complain_flags (decltype_p));
9800 /* TODO: build_x_modify_expr doesn't honor the location,
9801 so we must set it here. */
9802 expr.set_location (loc);
9803 }
9804 }
9805 }
9806
9807 return expr;
9808 }
9809
9810 /* Parse an (optional) assignment-operator.
9811
9812 assignment-operator: one of
9813 = *= /= %= += -= >>= <<= &= ^= |=
9814
9815 GNU Extension:
9816
9817 assignment-operator: one of
9818 <?= >?=
9819
9820 If the next token is an assignment operator, the corresponding tree
9821 code is returned, and the token is consumed. For example, for
9822 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9823 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9824 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9825 operator, ERROR_MARK is returned. */
9826
9827 static enum tree_code
9828 cp_parser_assignment_operator_opt (cp_parser* parser)
9829 {
9830 enum tree_code op;
9831 cp_token *token;
9832
9833 /* Peek at the next token. */
9834 token = cp_lexer_peek_token (parser->lexer);
9835
9836 switch (token->type)
9837 {
9838 case CPP_EQ:
9839 op = NOP_EXPR;
9840 break;
9841
9842 case CPP_MULT_EQ:
9843 op = MULT_EXPR;
9844 break;
9845
9846 case CPP_DIV_EQ:
9847 op = TRUNC_DIV_EXPR;
9848 break;
9849
9850 case CPP_MOD_EQ:
9851 op = TRUNC_MOD_EXPR;
9852 break;
9853
9854 case CPP_PLUS_EQ:
9855 op = PLUS_EXPR;
9856 break;
9857
9858 case CPP_MINUS_EQ:
9859 op = MINUS_EXPR;
9860 break;
9861
9862 case CPP_RSHIFT_EQ:
9863 op = RSHIFT_EXPR;
9864 break;
9865
9866 case CPP_LSHIFT_EQ:
9867 op = LSHIFT_EXPR;
9868 break;
9869
9870 case CPP_AND_EQ:
9871 op = BIT_AND_EXPR;
9872 break;
9873
9874 case CPP_XOR_EQ:
9875 op = BIT_XOR_EXPR;
9876 break;
9877
9878 case CPP_OR_EQ:
9879 op = BIT_IOR_EXPR;
9880 break;
9881
9882 default:
9883 /* Nothing else is an assignment operator. */
9884 op = ERROR_MARK;
9885 }
9886
9887 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9888 if (op != ERROR_MARK
9889 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9890 op = ERROR_MARK;
9891
9892 /* If it was an assignment operator, consume it. */
9893 if (op != ERROR_MARK)
9894 cp_lexer_consume_token (parser->lexer);
9895
9896 return op;
9897 }
9898
9899 /* Parse an expression.
9900
9901 expression:
9902 assignment-expression
9903 expression , assignment-expression
9904
9905 CAST_P is true if this expression is the target of a cast.
9906 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9907 except possibly parenthesized or on the RHS of a comma (N3276).
9908
9909 Returns a representation of the expression. */
9910
9911 static cp_expr
9912 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9913 bool cast_p, bool decltype_p)
9914 {
9915 cp_expr expression = NULL_TREE;
9916 location_t loc = UNKNOWN_LOCATION;
9917
9918 while (true)
9919 {
9920 cp_expr assignment_expression;
9921
9922 /* Parse the next assignment-expression. */
9923 assignment_expression
9924 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9925
9926 /* We don't create a temporary for a call that is the immediate operand
9927 of decltype or on the RHS of a comma. But when we see a comma, we
9928 need to create a temporary for a call on the LHS. */
9929 if (decltype_p && !processing_template_decl
9930 && TREE_CODE (assignment_expression) == CALL_EXPR
9931 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9932 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9933 assignment_expression
9934 = build_cplus_new (TREE_TYPE (assignment_expression),
9935 assignment_expression, tf_warning_or_error);
9936
9937 /* If this is the first assignment-expression, we can just
9938 save it away. */
9939 if (!expression)
9940 expression = assignment_expression;
9941 else
9942 {
9943 /* Create a location with caret at the comma, ranging
9944 from the start of the LHS to the end of the RHS. */
9945 loc = make_location (loc,
9946 expression.get_start (),
9947 assignment_expression.get_finish ());
9948 expression = build_x_compound_expr (loc, expression,
9949 assignment_expression,
9950 complain_flags (decltype_p));
9951 expression.set_location (loc);
9952 }
9953 /* If the next token is not a comma, or we're in a fold-expression, then
9954 we are done with the expression. */
9955 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9956 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9957 break;
9958 /* Consume the `,'. */
9959 loc = cp_lexer_peek_token (parser->lexer)->location;
9960 cp_lexer_consume_token (parser->lexer);
9961 /* A comma operator cannot appear in a constant-expression. */
9962 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9963 expression = error_mark_node;
9964 }
9965
9966 return expression;
9967 }
9968
9969 /* Parse a constant-expression.
9970
9971 constant-expression:
9972 conditional-expression
9973
9974 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9975 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9976 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9977 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9978 only parse a conditional-expression, otherwise parse an
9979 assignment-expression. See below for rationale. */
9980
9981 static cp_expr
9982 cp_parser_constant_expression (cp_parser* parser,
9983 bool allow_non_constant_p,
9984 bool *non_constant_p,
9985 bool strict_p)
9986 {
9987 bool saved_integral_constant_expression_p;
9988 bool saved_allow_non_integral_constant_expression_p;
9989 bool saved_non_integral_constant_expression_p;
9990 cp_expr expression;
9991
9992 /* It might seem that we could simply parse the
9993 conditional-expression, and then check to see if it were
9994 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9995 one that the compiler can figure out is constant, possibly after
9996 doing some simplifications or optimizations. The standard has a
9997 precise definition of constant-expression, and we must honor
9998 that, even though it is somewhat more restrictive.
9999
10000 For example:
10001
10002 int i[(2, 3)];
10003
10004 is not a legal declaration, because `(2, 3)' is not a
10005 constant-expression. The `,' operator is forbidden in a
10006 constant-expression. However, GCC's constant-folding machinery
10007 will fold this operation to an INTEGER_CST for `3'. */
10008
10009 /* Save the old settings. */
10010 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10011 saved_allow_non_integral_constant_expression_p
10012 = parser->allow_non_integral_constant_expression_p;
10013 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10014 /* We are now parsing a constant-expression. */
10015 parser->integral_constant_expression_p = true;
10016 parser->allow_non_integral_constant_expression_p
10017 = (allow_non_constant_p || cxx_dialect >= cxx11);
10018 parser->non_integral_constant_expression_p = false;
10019 /* Although the grammar says "conditional-expression", when not STRICT_P,
10020 we parse an "assignment-expression", which also permits
10021 "throw-expression" and the use of assignment operators. In the case
10022 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10023 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10024 actually essential that we look for an assignment-expression.
10025 For example, cp_parser_initializer_clauses uses this function to
10026 determine whether a particular assignment-expression is in fact
10027 constant. */
10028 if (strict_p)
10029 {
10030 /* Parse the binary expressions (logical-or-expression). */
10031 expression = cp_parser_binary_expression (parser, false, false, false,
10032 PREC_NOT_OPERATOR, NULL);
10033 /* If the next token is a `?' then we're actually looking at
10034 a conditional-expression; otherwise we're done. */
10035 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10036 expression = cp_parser_question_colon_clause (parser, expression);
10037 }
10038 else
10039 expression = cp_parser_assignment_expression (parser);
10040 /* Restore the old settings. */
10041 parser->integral_constant_expression_p
10042 = saved_integral_constant_expression_p;
10043 parser->allow_non_integral_constant_expression_p
10044 = saved_allow_non_integral_constant_expression_p;
10045 if (cxx_dialect >= cxx11)
10046 {
10047 /* Require an rvalue constant expression here; that's what our
10048 callers expect. Reference constant expressions are handled
10049 separately in e.g. cp_parser_template_argument. */
10050 tree decay = expression;
10051 if (TREE_TYPE (expression)
10052 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10053 decay = build_address (expression);
10054 bool is_const = potential_rvalue_constant_expression (decay);
10055 parser->non_integral_constant_expression_p = !is_const;
10056 if (!is_const && !allow_non_constant_p)
10057 require_potential_rvalue_constant_expression (decay);
10058 }
10059 if (allow_non_constant_p)
10060 *non_constant_p = parser->non_integral_constant_expression_p;
10061 parser->non_integral_constant_expression_p
10062 = saved_non_integral_constant_expression_p;
10063
10064 return expression;
10065 }
10066
10067 /* Parse __builtin_offsetof.
10068
10069 offsetof-expression:
10070 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10071
10072 offsetof-member-designator:
10073 id-expression
10074 | offsetof-member-designator "." id-expression
10075 | offsetof-member-designator "[" expression "]"
10076 | offsetof-member-designator "->" id-expression */
10077
10078 static cp_expr
10079 cp_parser_builtin_offsetof (cp_parser *parser)
10080 {
10081 int save_ice_p, save_non_ice_p;
10082 tree type;
10083 cp_expr expr;
10084 cp_id_kind dummy;
10085 cp_token *token;
10086 location_t finish_loc;
10087
10088 /* We're about to accept non-integral-constant things, but will
10089 definitely yield an integral constant expression. Save and
10090 restore these values around our local parsing. */
10091 save_ice_p = parser->integral_constant_expression_p;
10092 save_non_ice_p = parser->non_integral_constant_expression_p;
10093
10094 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10095
10096 /* Consume the "__builtin_offsetof" token. */
10097 cp_lexer_consume_token (parser->lexer);
10098 /* Consume the opening `('. */
10099 matching_parens parens;
10100 parens.require_open (parser);
10101 /* Parse the type-id. */
10102 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10103 {
10104 const char *saved_message = parser->type_definition_forbidden_message;
10105 parser->type_definition_forbidden_message
10106 = G_("types may not be defined within %<__builtin_offsetof%>");
10107 type = cp_parser_type_id (parser);
10108 parser->type_definition_forbidden_message = saved_message;
10109 }
10110 /* Look for the `,'. */
10111 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10112 token = cp_lexer_peek_token (parser->lexer);
10113
10114 /* Build the (type *)null that begins the traditional offsetof macro. */
10115 tree object_ptr
10116 = build_static_cast (build_pointer_type (type), null_pointer_node,
10117 tf_warning_or_error);
10118
10119 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10120 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10121 true, &dummy, token->location);
10122 while (true)
10123 {
10124 token = cp_lexer_peek_token (parser->lexer);
10125 switch (token->type)
10126 {
10127 case CPP_OPEN_SQUARE:
10128 /* offsetof-member-designator "[" expression "]" */
10129 expr = cp_parser_postfix_open_square_expression (parser, expr,
10130 true, false);
10131 break;
10132
10133 case CPP_DEREF:
10134 /* offsetof-member-designator "->" identifier */
10135 expr = grok_array_decl (token->location, expr,
10136 integer_zero_node, false);
10137 /* FALLTHRU */
10138
10139 case CPP_DOT:
10140 /* offsetof-member-designator "." identifier */
10141 cp_lexer_consume_token (parser->lexer);
10142 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10143 expr, true, &dummy,
10144 token->location);
10145 break;
10146
10147 case CPP_CLOSE_PAREN:
10148 /* Consume the ")" token. */
10149 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10150 cp_lexer_consume_token (parser->lexer);
10151 goto success;
10152
10153 default:
10154 /* Error. We know the following require will fail, but
10155 that gives the proper error message. */
10156 parens.require_close (parser);
10157 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10158 expr = error_mark_node;
10159 goto failure;
10160 }
10161 }
10162
10163 success:
10164 /* Make a location of the form:
10165 __builtin_offsetof (struct s, f)
10166 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10167 with caret at the type-id, ranging from the start of the
10168 "_builtin_offsetof" token to the close paren. */
10169 loc = make_location (loc, start_loc, finish_loc);
10170 /* The result will be an INTEGER_CST, so we need to explicitly
10171 preserve the location. */
10172 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10173
10174 failure:
10175 parser->integral_constant_expression_p = save_ice_p;
10176 parser->non_integral_constant_expression_p = save_non_ice_p;
10177
10178 expr = expr.maybe_add_location_wrapper ();
10179 return expr;
10180 }
10181
10182 /* Parse a trait expression.
10183
10184 Returns a representation of the expression, the underlying type
10185 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10186
10187 static cp_expr
10188 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10189 {
10190 cp_trait_kind kind;
10191 tree type1, type2 = NULL_TREE;
10192 bool binary = false;
10193 bool variadic = false;
10194
10195 switch (keyword)
10196 {
10197 case RID_HAS_NOTHROW_ASSIGN:
10198 kind = CPTK_HAS_NOTHROW_ASSIGN;
10199 break;
10200 case RID_HAS_NOTHROW_CONSTRUCTOR:
10201 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10202 break;
10203 case RID_HAS_NOTHROW_COPY:
10204 kind = CPTK_HAS_NOTHROW_COPY;
10205 break;
10206 case RID_HAS_TRIVIAL_ASSIGN:
10207 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10208 break;
10209 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10210 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10211 break;
10212 case RID_HAS_TRIVIAL_COPY:
10213 kind = CPTK_HAS_TRIVIAL_COPY;
10214 break;
10215 case RID_HAS_TRIVIAL_DESTRUCTOR:
10216 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10217 break;
10218 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10219 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10220 break;
10221 case RID_HAS_VIRTUAL_DESTRUCTOR:
10222 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10223 break;
10224 case RID_IS_ABSTRACT:
10225 kind = CPTK_IS_ABSTRACT;
10226 break;
10227 case RID_IS_AGGREGATE:
10228 kind = CPTK_IS_AGGREGATE;
10229 break;
10230 case RID_IS_BASE_OF:
10231 kind = CPTK_IS_BASE_OF;
10232 binary = true;
10233 break;
10234 case RID_IS_CLASS:
10235 kind = CPTK_IS_CLASS;
10236 break;
10237 case RID_IS_EMPTY:
10238 kind = CPTK_IS_EMPTY;
10239 break;
10240 case RID_IS_ENUM:
10241 kind = CPTK_IS_ENUM;
10242 break;
10243 case RID_IS_FINAL:
10244 kind = CPTK_IS_FINAL;
10245 break;
10246 case RID_IS_LITERAL_TYPE:
10247 kind = CPTK_IS_LITERAL_TYPE;
10248 break;
10249 case RID_IS_POD:
10250 kind = CPTK_IS_POD;
10251 break;
10252 case RID_IS_POLYMORPHIC:
10253 kind = CPTK_IS_POLYMORPHIC;
10254 break;
10255 case RID_IS_SAME_AS:
10256 kind = CPTK_IS_SAME_AS;
10257 binary = true;
10258 break;
10259 case RID_IS_STD_LAYOUT:
10260 kind = CPTK_IS_STD_LAYOUT;
10261 break;
10262 case RID_IS_TRIVIAL:
10263 kind = CPTK_IS_TRIVIAL;
10264 break;
10265 case RID_IS_TRIVIALLY_ASSIGNABLE:
10266 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10267 binary = true;
10268 break;
10269 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10270 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10271 variadic = true;
10272 break;
10273 case RID_IS_TRIVIALLY_COPYABLE:
10274 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10275 break;
10276 case RID_IS_UNION:
10277 kind = CPTK_IS_UNION;
10278 break;
10279 case RID_UNDERLYING_TYPE:
10280 kind = CPTK_UNDERLYING_TYPE;
10281 break;
10282 case RID_BASES:
10283 kind = CPTK_BASES;
10284 break;
10285 case RID_DIRECT_BASES:
10286 kind = CPTK_DIRECT_BASES;
10287 break;
10288 case RID_IS_ASSIGNABLE:
10289 kind = CPTK_IS_ASSIGNABLE;
10290 binary = true;
10291 break;
10292 case RID_IS_CONSTRUCTIBLE:
10293 kind = CPTK_IS_CONSTRUCTIBLE;
10294 variadic = true;
10295 break;
10296 default:
10297 gcc_unreachable ();
10298 }
10299
10300 /* Get location of initial token. */
10301 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10302
10303 /* Consume the token. */
10304 cp_lexer_consume_token (parser->lexer);
10305
10306 matching_parens parens;
10307 parens.require_open (parser);
10308
10309 {
10310 type_id_in_expr_sentinel s (parser);
10311 type1 = cp_parser_type_id (parser);
10312 }
10313
10314 if (type1 == error_mark_node)
10315 return error_mark_node;
10316
10317 if (binary)
10318 {
10319 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10320
10321 {
10322 type_id_in_expr_sentinel s (parser);
10323 type2 = cp_parser_type_id (parser);
10324 }
10325
10326 if (type2 == error_mark_node)
10327 return error_mark_node;
10328 }
10329 else if (variadic)
10330 {
10331 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10332 {
10333 cp_lexer_consume_token (parser->lexer);
10334 tree elt = cp_parser_type_id (parser);
10335 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10336 {
10337 cp_lexer_consume_token (parser->lexer);
10338 elt = make_pack_expansion (elt);
10339 }
10340 if (elt == error_mark_node)
10341 return error_mark_node;
10342 type2 = tree_cons (NULL_TREE, elt, type2);
10343 }
10344 }
10345
10346 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10347 parens.require_close (parser);
10348
10349 /* Construct a location of the form:
10350 __is_trivially_copyable(_Tp)
10351 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10352 with start == caret, finishing at the close-paren. */
10353 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10354
10355 /* Complete the trait expression, which may mean either processing
10356 the trait expr now or saving it for template instantiation. */
10357 switch (kind)
10358 {
10359 case CPTK_UNDERLYING_TYPE:
10360 return cp_expr (finish_underlying_type (type1), trait_loc);
10361 case CPTK_BASES:
10362 return cp_expr (finish_bases (type1, false), trait_loc);
10363 case CPTK_DIRECT_BASES:
10364 return cp_expr (finish_bases (type1, true), trait_loc);
10365 default:
10366 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10367 }
10368 }
10369
10370 /* Parse a lambda expression.
10371
10372 lambda-expression:
10373 lambda-introducer lambda-declarator [opt] compound-statement
10374
10375 Returns a representation of the expression. */
10376
10377 static cp_expr
10378 cp_parser_lambda_expression (cp_parser* parser)
10379 {
10380 tree lambda_expr = build_lambda_expr ();
10381 tree type;
10382 bool ok = true;
10383 cp_token *token = cp_lexer_peek_token (parser->lexer);
10384 cp_token_position start = 0;
10385
10386 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10387
10388 if (cxx_dialect >= cxx2a)
10389 /* C++20 allows lambdas in unevaluated context. */;
10390 else if (cp_unevaluated_operand)
10391 {
10392 if (!token->error_reported)
10393 {
10394 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10395 "lambda-expression in unevaluated context"
10396 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10397 token->error_reported = true;
10398 }
10399 ok = false;
10400 }
10401 else if (parser->in_template_argument_list_p || processing_template_parmlist)
10402 {
10403 if (!token->error_reported)
10404 {
10405 error_at (token->location, "lambda-expression in template-argument"
10406 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10407 token->error_reported = true;
10408 }
10409 ok = false;
10410 }
10411
10412 /* We may be in the middle of deferred access check. Disable
10413 it now. */
10414 push_deferring_access_checks (dk_no_deferred);
10415
10416 cp_parser_lambda_introducer (parser, lambda_expr);
10417 if (cp_parser_error_occurred (parser))
10418 return error_mark_node;
10419
10420 type = begin_lambda_type (lambda_expr);
10421 if (type == error_mark_node)
10422 return error_mark_node;
10423
10424 record_lambda_scope (lambda_expr);
10425
10426 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10427 determine_visibility (TYPE_NAME (type));
10428
10429 /* Now that we've started the type, add the capture fields for any
10430 explicit captures. */
10431 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10432
10433 {
10434 /* Inside the class, surrounding template-parameter-lists do not apply. */
10435 unsigned int saved_num_template_parameter_lists
10436 = parser->num_template_parameter_lists;
10437 unsigned char in_statement = parser->in_statement;
10438 bool in_switch_statement_p = parser->in_switch_statement_p;
10439 bool fully_implicit_function_template_p
10440 = parser->fully_implicit_function_template_p;
10441 tree implicit_template_parms = parser->implicit_template_parms;
10442 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10443 bool auto_is_implicit_function_template_parm_p
10444 = parser->auto_is_implicit_function_template_parm_p;
10445
10446 parser->num_template_parameter_lists = 0;
10447 parser->in_statement = 0;
10448 parser->in_switch_statement_p = false;
10449 parser->fully_implicit_function_template_p = false;
10450 parser->implicit_template_parms = 0;
10451 parser->implicit_template_scope = 0;
10452 parser->auto_is_implicit_function_template_parm_p = false;
10453
10454 /* By virtue of defining a local class, a lambda expression has access to
10455 the private variables of enclosing classes. */
10456
10457 if (cp_parser_start_tentative_firewall (parser))
10458 start = token;
10459
10460 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10461
10462 if (ok && cp_parser_error_occurred (parser))
10463 ok = false;
10464
10465 if (ok)
10466 {
10467 cp_parser_lambda_body (parser, lambda_expr);
10468 }
10469 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10470 {
10471 if (cp_parser_skip_to_closing_brace (parser))
10472 cp_lexer_consume_token (parser->lexer);
10473 }
10474
10475 /* The capture list was built up in reverse order; fix that now. */
10476 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10477 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10478
10479 if (ok)
10480 maybe_add_lambda_conv_op (type);
10481
10482 finish_struct (type, /*attributes=*/NULL_TREE);
10483
10484 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10485 parser->in_statement = in_statement;
10486 parser->in_switch_statement_p = in_switch_statement_p;
10487 parser->fully_implicit_function_template_p
10488 = fully_implicit_function_template_p;
10489 parser->implicit_template_parms = implicit_template_parms;
10490 parser->implicit_template_scope = implicit_template_scope;
10491 parser->auto_is_implicit_function_template_parm_p
10492 = auto_is_implicit_function_template_parm_p;
10493 }
10494
10495 /* This field is only used during parsing of the lambda. */
10496 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10497
10498 /* This lambda shouldn't have any proxies left at this point. */
10499 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10500 /* And now that we're done, push proxies for an enclosing lambda. */
10501 insert_pending_capture_proxies ();
10502
10503 /* Update the lambda expression to a range. */
10504 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10505 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10506 token->location,
10507 end_tok->location);
10508
10509 if (ok)
10510 lambda_expr = build_lambda_object (lambda_expr);
10511 else
10512 lambda_expr = error_mark_node;
10513
10514 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10515
10516 pop_deferring_access_checks ();
10517
10518 return lambda_expr;
10519 }
10520
10521 /* Parse the beginning of a lambda expression.
10522
10523 lambda-introducer:
10524 [ lambda-capture [opt] ]
10525
10526 LAMBDA_EXPR is the current representation of the lambda expression. */
10527
10528 static void
10529 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10530 {
10531 /* Need commas after the first capture. */
10532 bool first = true;
10533
10534 /* Eat the leading `['. */
10535 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10536
10537 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10538 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10539 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
10540 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10541 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10542 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10543 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10544
10545 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10546 {
10547 cp_lexer_consume_token (parser->lexer);
10548 first = false;
10549
10550 if (!(at_function_scope_p () || parsing_nsdmi ()))
10551 error ("non-local lambda expression cannot have a capture-default");
10552 }
10553
10554 hash_set<tree, true> ids;
10555 tree first_capture_id = NULL_TREE;
10556 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10557 {
10558 cp_token* capture_token;
10559 tree capture_id;
10560 tree capture_init_expr;
10561 cp_id_kind idk = CP_ID_KIND_NONE;
10562 bool explicit_init_p = false;
10563
10564 enum capture_kind_type
10565 {
10566 BY_COPY,
10567 BY_REFERENCE
10568 };
10569 enum capture_kind_type capture_kind = BY_COPY;
10570
10571 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10572 {
10573 error ("expected end of capture-list");
10574 return;
10575 }
10576
10577 if (first)
10578 first = false;
10579 else
10580 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10581
10582 /* Possibly capture `this'. */
10583 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10584 {
10585 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10586 if (cxx_dialect < cxx2a
10587 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10588 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10589 "with by-copy capture default");
10590 cp_lexer_consume_token (parser->lexer);
10591 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10592 pedwarn (input_location, 0,
10593 "already captured %qD in lambda expression",
10594 this_identifier);
10595 else
10596 add_capture (lambda_expr, /*id=*/this_identifier,
10597 /*initializer=*/finish_this_expr (),
10598 /*by_reference_p=*/true, explicit_init_p);
10599 continue;
10600 }
10601
10602 /* Possibly capture `*this'. */
10603 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10604 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10605 {
10606 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10607 if (cxx_dialect < cxx17)
10608 pedwarn (loc, 0, "%<*this%> capture only available with "
10609 "%<-std=c++17%> or %<-std=gnu++17%>");
10610 cp_lexer_consume_token (parser->lexer);
10611 cp_lexer_consume_token (parser->lexer);
10612 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10613 pedwarn (input_location, 0,
10614 "already captured %qD in lambda expression",
10615 this_identifier);
10616 else
10617 add_capture (lambda_expr, /*id=*/this_identifier,
10618 /*initializer=*/finish_this_expr (),
10619 /*by_reference_p=*/false, explicit_init_p);
10620 continue;
10621 }
10622
10623 /* But reject `&this'. */
10624 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10625 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10626 {
10627 error_at (cp_lexer_peek_token (parser->lexer)->location,
10628 "%<this%> cannot be captured by reference");
10629 cp_lexer_consume_token (parser->lexer);
10630 cp_lexer_consume_token (parser->lexer);
10631 continue;
10632 }
10633
10634 bool init_pack_expansion = false;
10635 location_t ellipsis_loc = UNKNOWN_LOCATION;
10636 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10637 {
10638 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
10639 if (cxx_dialect < cxx2a)
10640 pedwarn (ellipsis_loc, 0, "pack init-capture only available with "
10641 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10642 cp_lexer_consume_token (parser->lexer);
10643 init_pack_expansion = true;
10644 }
10645
10646 /* Remember whether we want to capture as a reference or not. */
10647 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10648 {
10649 capture_kind = BY_REFERENCE;
10650 cp_lexer_consume_token (parser->lexer);
10651 }
10652
10653 /* Get the identifier. */
10654 capture_token = cp_lexer_peek_token (parser->lexer);
10655 capture_id = cp_parser_identifier (parser);
10656
10657 if (capture_id == error_mark_node)
10658 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10659 delimiters, but I modified this to stop on unnested ']' as well. It
10660 was already changed to stop on unnested '}', so the
10661 "closing_parenthesis" name is no more misleading with my change. */
10662 {
10663 cp_parser_skip_to_closing_parenthesis (parser,
10664 /*recovering=*/true,
10665 /*or_comma=*/true,
10666 /*consume_paren=*/true);
10667 break;
10668 }
10669
10670 /* Find the initializer for this capture. */
10671 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10672 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10673 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10674 {
10675 bool direct, non_constant;
10676 /* An explicit initializer exists. */
10677 if (cxx_dialect < cxx14)
10678 pedwarn (input_location, 0,
10679 "lambda capture initializers "
10680 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
10681 capture_init_expr = cp_parser_initializer (parser, &direct,
10682 &non_constant, true);
10683 explicit_init_p = true;
10684 if (capture_init_expr == NULL_TREE)
10685 {
10686 error ("empty initializer for lambda init-capture");
10687 capture_init_expr = error_mark_node;
10688 }
10689 if (init_pack_expansion)
10690 capture_init_expr = make_pack_expansion (capture_init_expr);
10691 }
10692 else
10693 {
10694 const char* error_msg;
10695
10696 /* Turn the identifier into an id-expression. */
10697 capture_init_expr
10698 = cp_parser_lookup_name_simple (parser, capture_id,
10699 capture_token->location);
10700
10701 if (capture_init_expr == error_mark_node)
10702 {
10703 unqualified_name_lookup_error (capture_id);
10704 continue;
10705 }
10706 else if (!VAR_P (capture_init_expr)
10707 && TREE_CODE (capture_init_expr) != PARM_DECL)
10708 {
10709 error_at (capture_token->location,
10710 "capture of non-variable %qE",
10711 capture_init_expr);
10712 if (DECL_P (capture_init_expr))
10713 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10714 "%q#D declared here", capture_init_expr);
10715 continue;
10716 }
10717 if (VAR_P (capture_init_expr)
10718 && decl_storage_duration (capture_init_expr) != dk_auto)
10719 {
10720 if (pedwarn (capture_token->location, 0, "capture of variable "
10721 "%qD with non-automatic storage duration",
10722 capture_init_expr))
10723 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10724 "%q#D declared here", capture_init_expr);
10725 continue;
10726 }
10727
10728 capture_init_expr
10729 = finish_id_expression
10730 (capture_id,
10731 capture_init_expr,
10732 parser->scope,
10733 &idk,
10734 /*integral_constant_expression_p=*/false,
10735 /*allow_non_integral_constant_expression_p=*/false,
10736 /*non_integral_constant_expression_p=*/NULL,
10737 /*template_p=*/false,
10738 /*done=*/true,
10739 /*address_p=*/false,
10740 /*template_arg_p=*/false,
10741 &error_msg,
10742 capture_token->location);
10743
10744 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10745 {
10746 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10747 cp_lexer_consume_token (parser->lexer);
10748 capture_init_expr = make_pack_expansion (capture_init_expr);
10749 if (init_pack_expansion)
10750 {
10751 /* If what follows is an initializer, the second '...' is
10752 invalid. But for cases like [...xs...], the first one
10753 is invalid. */
10754 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10755 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10756 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10757 ellipsis_loc = loc;
10758 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
10759 continue;
10760 }
10761 }
10762 }
10763
10764 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10765 && !explicit_init_p)
10766 {
10767 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10768 && capture_kind == BY_COPY)
10769 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10770 "of %qD redundant with by-copy capture default",
10771 capture_id);
10772 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10773 && capture_kind == BY_REFERENCE)
10774 pedwarn (capture_token->location, 0, "explicit by-reference "
10775 "capture of %qD redundant with by-reference capture "
10776 "default", capture_id);
10777 }
10778
10779 /* Check for duplicates.
10780 Optimize for the zero or one explicit captures cases and only create
10781 the hash_set after adding second capture. */
10782 bool found = false;
10783 if (!ids.is_empty ())
10784 found = ids.add (capture_id);
10785 else if (first_capture_id == NULL_TREE)
10786 first_capture_id = capture_id;
10787 else if (capture_id == first_capture_id)
10788 found = true;
10789 else
10790 {
10791 ids.add (first_capture_id);
10792 ids.add (capture_id);
10793 }
10794 if (found)
10795 pedwarn (input_location, 0,
10796 "already captured %qD in lambda expression", capture_id);
10797 else
10798 add_capture (lambda_expr, capture_id, capture_init_expr,
10799 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10800 explicit_init_p);
10801
10802 /* If there is any qualification still in effect, clear it
10803 now; we will be starting fresh with the next capture. */
10804 parser->scope = NULL_TREE;
10805 parser->qualifying_scope = NULL_TREE;
10806 parser->object_scope = NULL_TREE;
10807 }
10808
10809 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10810 }
10811
10812 /* Parse the (optional) middle of a lambda expression.
10813
10814 lambda-declarator:
10815 < template-parameter-list [opt] >
10816 ( parameter-declaration-clause [opt] )
10817 attribute-specifier [opt]
10818 decl-specifier-seq [opt]
10819 exception-specification [opt]
10820 lambda-return-type-clause [opt]
10821
10822 LAMBDA_EXPR is the current representation of the lambda expression. */
10823
10824 static bool
10825 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10826 {
10827 /* 5.1.1.4 of the standard says:
10828 If a lambda-expression does not include a lambda-declarator, it is as if
10829 the lambda-declarator were ().
10830 This means an empty parameter list, no attributes, and no exception
10831 specification. */
10832 tree param_list = void_list_node;
10833 tree std_attrs = NULL_TREE;
10834 tree gnu_attrs = NULL_TREE;
10835 tree exception_spec = NULL_TREE;
10836 tree template_param_list = NULL_TREE;
10837 tree tx_qual = NULL_TREE;
10838 tree return_type = NULL_TREE;
10839 cp_decl_specifier_seq lambda_specs;
10840 clear_decl_specs (&lambda_specs);
10841
10842 /* The template-parameter-list is optional, but must begin with
10843 an opening angle if present. */
10844 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10845 {
10846 if (cxx_dialect < cxx14)
10847 pedwarn (parser->lexer->next_token->location, 0,
10848 "lambda templates are only available with "
10849 "%<-std=c++14%> or %<-std=gnu++14%>");
10850 else if (cxx_dialect < cxx2a)
10851 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10852 "lambda templates are only available with "
10853 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10854
10855 cp_lexer_consume_token (parser->lexer);
10856
10857 template_param_list = cp_parser_template_parameter_list (parser);
10858
10859 cp_parser_skip_to_end_of_template_parameter_list (parser);
10860
10861 /* We just processed one more parameter list. */
10862 ++parser->num_template_parameter_lists;
10863 }
10864
10865 /* The parameter-declaration-clause is optional (unless
10866 template-parameter-list was given), but must begin with an
10867 opening parenthesis if present. */
10868 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10869 {
10870 matching_parens parens;
10871 parens.consume_open (parser);
10872
10873 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10874
10875 /* Parse parameters. */
10876 param_list
10877 = cp_parser_parameter_declaration_clause
10878 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
10879
10880 /* Default arguments shall not be specified in the
10881 parameter-declaration-clause of a lambda-declarator. */
10882 if (cxx_dialect < cxx14)
10883 for (tree t = param_list; t; t = TREE_CHAIN (t))
10884 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10885 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10886 "default argument specified for lambda parameter");
10887
10888 parens.require_close (parser);
10889
10890 /* In the decl-specifier-seq of the lambda-declarator, each
10891 decl-specifier shall either be mutable or constexpr. */
10892 int declares_class_or_enum;
10893 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
10894 && !cp_next_tokens_can_be_gnu_attribute_p (parser))
10895 cp_parser_decl_specifier_seq (parser,
10896 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10897 &lambda_specs, &declares_class_or_enum);
10898 if (lambda_specs.storage_class == sc_mutable)
10899 {
10900 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10901 if (lambda_specs.conflicting_specifiers_p)
10902 error_at (lambda_specs.locations[ds_storage_class],
10903 "duplicate %<mutable%>");
10904 }
10905
10906 tx_qual = cp_parser_tx_qualifier_opt (parser);
10907
10908 /* Parse optional exception specification. */
10909 exception_spec = cp_parser_exception_specification_opt (parser);
10910
10911 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10912
10913 /* Parse optional trailing return type. */
10914 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10915 {
10916 cp_lexer_consume_token (parser->lexer);
10917 return_type = cp_parser_trailing_type_id (parser);
10918 }
10919
10920 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
10921 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
10922
10923 /* The function parameters must be in scope all the way until after the
10924 trailing-return-type in case of decltype. */
10925 pop_bindings_and_leave_scope ();
10926 }
10927 else if (template_param_list != NULL_TREE) // generate diagnostic
10928 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10929
10930 /* Create the function call operator.
10931
10932 Messing with declarators like this is no uglier than building up the
10933 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10934 other code. */
10935 {
10936 cp_decl_specifier_seq return_type_specs;
10937 cp_declarator* declarator;
10938 tree fco;
10939 int quals;
10940 void *p;
10941
10942 clear_decl_specs (&return_type_specs);
10943 return_type_specs.type = make_auto ();
10944
10945 if (lambda_specs.locations[ds_constexpr])
10946 {
10947 if (cxx_dialect >= cxx17)
10948 return_type_specs.locations[ds_constexpr]
10949 = lambda_specs.locations[ds_constexpr];
10950 else
10951 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10952 "lambda only available with %<-std=c++17%> or "
10953 "%<-std=gnu++17%>");
10954 }
10955
10956 p = obstack_alloc (&declarator_obstack, 0);
10957
10958 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
10959 LAMBDA_EXPR_LOCATION (lambda_expr));
10960
10961 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10962 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10963 declarator = make_call_declarator (declarator, param_list, quals,
10964 VIRT_SPEC_UNSPECIFIED,
10965 REF_QUAL_NONE,
10966 tx_qual,
10967 exception_spec,
10968 return_type,
10969 /*requires_clause*/NULL_TREE);
10970 declarator->std_attributes = std_attrs;
10971
10972 fco = grokmethod (&return_type_specs,
10973 declarator,
10974 gnu_attrs);
10975 if (fco != error_mark_node)
10976 {
10977 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10978 DECL_ARTIFICIAL (fco) = 1;
10979 /* Give the object parameter a different name. */
10980 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10981 DECL_LAMBDA_FUNCTION (fco) = 1;
10982 }
10983 if (template_param_list)
10984 {
10985 fco = finish_member_template_decl (fco);
10986 finish_template_decl (template_param_list);
10987 --parser->num_template_parameter_lists;
10988 }
10989 else if (parser->fully_implicit_function_template_p)
10990 fco = finish_fully_implicit_template (parser, fco);
10991
10992 finish_member_declaration (fco);
10993
10994 obstack_free (&declarator_obstack, p);
10995
10996 return (fco != error_mark_node);
10997 }
10998 }
10999
11000 /* Parse the body of a lambda expression, which is simply
11001
11002 compound-statement
11003
11004 but which requires special handling.
11005 LAMBDA_EXPR is the current representation of the lambda expression. */
11006
11007 static void
11008 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
11009 {
11010 bool nested = (current_function_decl != NULL_TREE);
11011 unsigned char local_variables_forbidden_p
11012 = parser->local_variables_forbidden_p;
11013 bool in_function_body = parser->in_function_body;
11014
11015 /* The body of a lambda-expression is not a subexpression of the enclosing
11016 expression. */
11017 cp_evaluated ev;
11018
11019 if (nested)
11020 push_function_context ();
11021 else
11022 /* Still increment function_depth so that we don't GC in the
11023 middle of an expression. */
11024 ++function_depth;
11025
11026 vec<tree> omp_privatization_save;
11027 save_omp_privatization_clauses (omp_privatization_save);
11028 /* Clear this in case we're in the middle of a default argument. */
11029 parser->local_variables_forbidden_p = 0;
11030 parser->in_function_body = true;
11031
11032 {
11033 local_specialization_stack s (lss_copy);
11034 tree fco = lambda_function (lambda_expr);
11035 tree body = start_lambda_function (fco, lambda_expr);
11036 matching_braces braces;
11037
11038 if (braces.require_open (parser))
11039 {
11040 tree compound_stmt = begin_compound_stmt (0);
11041
11042 /* Originally C++11 required us to peek for 'return expr'; and
11043 process it specially here to deduce the return type. N3638
11044 removed the need for that. */
11045
11046 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11047 cp_parser_label_declaration (parser);
11048 cp_parser_statement_seq_opt (parser, NULL_TREE);
11049 braces.require_close (parser);
11050
11051 finish_compound_stmt (compound_stmt);
11052 }
11053
11054 finish_lambda_function (body);
11055 }
11056
11057 restore_omp_privatization_clauses (omp_privatization_save);
11058 parser->local_variables_forbidden_p = local_variables_forbidden_p;
11059 parser->in_function_body = in_function_body;
11060 if (nested)
11061 pop_function_context();
11062 else
11063 --function_depth;
11064 }
11065
11066 /* Statements [gram.stmt.stmt] */
11067
11068 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11069
11070 static void
11071 add_debug_begin_stmt (location_t loc)
11072 {
11073 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11074 return;
11075 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11076 /* A concept is never expanded normally. */
11077 return;
11078
11079 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11080 SET_EXPR_LOCATION (stmt, loc);
11081 add_stmt (stmt);
11082 }
11083
11084 /* Parse a statement.
11085
11086 statement:
11087 labeled-statement
11088 expression-statement
11089 compound-statement
11090 selection-statement
11091 iteration-statement
11092 jump-statement
11093 declaration-statement
11094 try-block
11095
11096 C++11:
11097
11098 statement:
11099 labeled-statement
11100 attribute-specifier-seq (opt) expression-statement
11101 attribute-specifier-seq (opt) compound-statement
11102 attribute-specifier-seq (opt) selection-statement
11103 attribute-specifier-seq (opt) iteration-statement
11104 attribute-specifier-seq (opt) jump-statement
11105 declaration-statement
11106 attribute-specifier-seq (opt) try-block
11107
11108 init-statement:
11109 expression-statement
11110 simple-declaration
11111
11112 TM Extension:
11113
11114 statement:
11115 atomic-statement
11116
11117 IN_COMPOUND is true when the statement is nested inside a
11118 cp_parser_compound_statement; this matters for certain pragmas.
11119
11120 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11121 is a (possibly labeled) if statement which is not enclosed in braces
11122 and has an else clause. This is used to implement -Wparentheses.
11123
11124 CHAIN is a vector of if-else-if conditions. */
11125
11126 static void
11127 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
11128 bool in_compound, bool *if_p, vec<tree> *chain,
11129 location_t *loc_after_labels)
11130 {
11131 tree statement, std_attrs = NULL_TREE;
11132 cp_token *token;
11133 location_t statement_location, attrs_loc;
11134
11135 restart:
11136 if (if_p != NULL)
11137 *if_p = false;
11138 /* There is no statement yet. */
11139 statement = NULL_TREE;
11140
11141 saved_token_sentinel saved_tokens (parser->lexer);
11142 attrs_loc = cp_lexer_peek_token (parser->lexer)->location;
11143 if (c_dialect_objc ())
11144 /* In obj-c++, seeing '[[' might be the either the beginning of
11145 c++11 attributes, or a nested objc-message-expression. So
11146 let's parse the c++11 attributes tentatively. */
11147 cp_parser_parse_tentatively (parser);
11148 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11149 if (std_attrs)
11150 {
11151 location_t end_loc
11152 = cp_lexer_previous_token (parser->lexer)->location;
11153 attrs_loc = make_location (attrs_loc, attrs_loc, end_loc);
11154 }
11155 if (c_dialect_objc ())
11156 {
11157 if (!cp_parser_parse_definitely (parser))
11158 std_attrs = NULL_TREE;
11159 }
11160
11161 /* Peek at the next token. */
11162 token = cp_lexer_peek_token (parser->lexer);
11163 /* Remember the location of the first token in the statement. */
11164 cp_token *statement_token = token;
11165 statement_location = token->location;
11166 add_debug_begin_stmt (statement_location);
11167 /* If this is a keyword, then that will often determine what kind of
11168 statement we have. */
11169 if (token->type == CPP_KEYWORD)
11170 {
11171 enum rid keyword = token->keyword;
11172
11173 switch (keyword)
11174 {
11175 case RID_CASE:
11176 case RID_DEFAULT:
11177 /* Looks like a labeled-statement with a case label.
11178 Parse the label, and then use tail recursion to parse
11179 the statement. */
11180 cp_parser_label_for_labeled_statement (parser, std_attrs);
11181 in_compound = false;
11182 goto restart;
11183
11184 case RID_IF:
11185 case RID_SWITCH:
11186 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11187 statement = cp_parser_selection_statement (parser, if_p, chain);
11188 break;
11189
11190 case RID_WHILE:
11191 case RID_DO:
11192 case RID_FOR:
11193 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11194 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11195 break;
11196
11197 case RID_BREAK:
11198 case RID_CONTINUE:
11199 case RID_RETURN:
11200 case RID_GOTO:
11201 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11202 statement = cp_parser_jump_statement (parser);
11203 break;
11204
11205 /* Objective-C++ exception-handling constructs. */
11206 case RID_AT_TRY:
11207 case RID_AT_CATCH:
11208 case RID_AT_FINALLY:
11209 case RID_AT_SYNCHRONIZED:
11210 case RID_AT_THROW:
11211 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11212 statement = cp_parser_objc_statement (parser);
11213 break;
11214
11215 case RID_TRY:
11216 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11217 statement = cp_parser_try_block (parser);
11218 break;
11219
11220 case RID_NAMESPACE:
11221 /* This must be a namespace alias definition. */
11222 if (std_attrs != NULL_TREE)
11223 {
11224 /* Attributes should be parsed as part of the the
11225 declaration, so let's un-parse them. */
11226 saved_tokens.rollback();
11227 std_attrs = NULL_TREE;
11228 }
11229 cp_parser_declaration_statement (parser);
11230 return;
11231
11232 case RID_TRANSACTION_ATOMIC:
11233 case RID_TRANSACTION_RELAXED:
11234 case RID_SYNCHRONIZED:
11235 case RID_ATOMIC_NOEXCEPT:
11236 case RID_ATOMIC_CANCEL:
11237 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11238 statement = cp_parser_transaction (parser, token);
11239 break;
11240 case RID_TRANSACTION_CANCEL:
11241 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11242 statement = cp_parser_transaction_cancel (parser);
11243 break;
11244
11245 default:
11246 /* It might be a keyword like `int' that can start a
11247 declaration-statement. */
11248 break;
11249 }
11250 }
11251 else if (token->type == CPP_NAME)
11252 {
11253 /* If the next token is a `:', then we are looking at a
11254 labeled-statement. */
11255 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11256 if (token->type == CPP_COLON)
11257 {
11258 /* Looks like a labeled-statement with an ordinary label.
11259 Parse the label, and then use tail recursion to parse
11260 the statement. */
11261
11262 cp_parser_label_for_labeled_statement (parser, std_attrs);
11263 in_compound = false;
11264 goto restart;
11265 }
11266 }
11267 /* Anything that starts with a `{' must be a compound-statement. */
11268 else if (token->type == CPP_OPEN_BRACE)
11269 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11270 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11271 a statement all its own. */
11272 else if (token->type == CPP_PRAGMA)
11273 {
11274 /* Only certain OpenMP pragmas are attached to statements, and thus
11275 are considered statements themselves. All others are not. In
11276 the context of a compound, accept the pragma as a "statement" and
11277 return so that we can check for a close brace. Otherwise we
11278 require a real statement and must go back and read one. */
11279 if (in_compound)
11280 cp_parser_pragma (parser, pragma_compound, if_p);
11281 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11282 goto restart;
11283 return;
11284 }
11285 else if (token->type == CPP_EOF)
11286 {
11287 cp_parser_error (parser, "expected statement");
11288 return;
11289 }
11290
11291 /* Everything else must be a declaration-statement or an
11292 expression-statement. Try for the declaration-statement
11293 first, unless we are looking at a `;', in which case we know that
11294 we have an expression-statement. */
11295 if (!statement)
11296 {
11297 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11298 {
11299 if (std_attrs != NULL_TREE)
11300 /* Attributes should be parsed as part of the declaration,
11301 so let's un-parse them. */
11302 saved_tokens.rollback();
11303
11304 cp_parser_parse_tentatively (parser);
11305 /* Try to parse the declaration-statement. */
11306 cp_parser_declaration_statement (parser);
11307 /* If that worked, we're done. */
11308 if (cp_parser_parse_definitely (parser))
11309 return;
11310 /* It didn't work, restore the post-attribute position. */
11311 if (std_attrs)
11312 cp_lexer_set_token_position (parser->lexer, statement_token);
11313 }
11314 /* All preceding labels have been parsed at this point. */
11315 if (loc_after_labels != NULL)
11316 *loc_after_labels = statement_location;
11317
11318 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11319
11320 /* Look for an expression-statement instead. */
11321 statement = cp_parser_expression_statement (parser, in_statement_expr);
11322
11323 /* Handle [[fallthrough]];. */
11324 if (attribute_fallthrough_p (std_attrs))
11325 {
11326 /* The next token after the fallthrough attribute is ';'. */
11327 if (statement == NULL_TREE)
11328 {
11329 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11330 statement = build_call_expr_internal_loc (statement_location,
11331 IFN_FALLTHROUGH,
11332 void_type_node, 0);
11333 finish_expr_stmt (statement);
11334 }
11335 else
11336 warning_at (statement_location, OPT_Wattributes,
11337 "%<fallthrough%> attribute not followed by %<;%>");
11338 std_attrs = NULL_TREE;
11339 }
11340 }
11341
11342 /* Set the line number for the statement. */
11343 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11344 SET_EXPR_LOCATION (statement, statement_location);
11345
11346 /* Allow "[[fallthrough]];", but warn otherwise. */
11347 if (std_attrs != NULL_TREE)
11348 warning_at (attrs_loc,
11349 OPT_Wattributes,
11350 "attributes at the beginning of statement are ignored");
11351 }
11352
11353 /* Append ATTR to attribute list ATTRS. */
11354
11355 static tree
11356 attr_chainon (tree attrs, tree attr)
11357 {
11358 if (attrs == error_mark_node)
11359 return error_mark_node;
11360 if (attr == error_mark_node)
11361 return error_mark_node;
11362 return chainon (attrs, attr);
11363 }
11364
11365 /* Parse the label for a labeled-statement, i.e.
11366
11367 identifier :
11368 case constant-expression :
11369 default :
11370
11371 GNU Extension:
11372 case constant-expression ... constant-expression : statement
11373
11374 When a label is parsed without errors, the label is added to the
11375 parse tree by the finish_* functions, so this function doesn't
11376 have to return the label. */
11377
11378 static void
11379 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11380 {
11381 cp_token *token;
11382 tree label = NULL_TREE;
11383 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11384
11385 /* The next token should be an identifier. */
11386 token = cp_lexer_peek_token (parser->lexer);
11387 if (token->type != CPP_NAME
11388 && token->type != CPP_KEYWORD)
11389 {
11390 cp_parser_error (parser, "expected labeled-statement");
11391 return;
11392 }
11393
11394 /* Remember whether this case or a user-defined label is allowed to fall
11395 through to. */
11396 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11397
11398 parser->colon_corrects_to_scope_p = false;
11399 switch (token->keyword)
11400 {
11401 case RID_CASE:
11402 {
11403 tree expr, expr_hi;
11404 cp_token *ellipsis;
11405
11406 /* Consume the `case' token. */
11407 cp_lexer_consume_token (parser->lexer);
11408 /* Parse the constant-expression. */
11409 expr = cp_parser_constant_expression (parser);
11410 if (check_for_bare_parameter_packs (expr))
11411 expr = error_mark_node;
11412
11413 ellipsis = cp_lexer_peek_token (parser->lexer);
11414 if (ellipsis->type == CPP_ELLIPSIS)
11415 {
11416 /* Consume the `...' token. */
11417 cp_lexer_consume_token (parser->lexer);
11418 expr_hi = cp_parser_constant_expression (parser);
11419 if (check_for_bare_parameter_packs (expr_hi))
11420 expr_hi = error_mark_node;
11421
11422 /* We don't need to emit warnings here, as the common code
11423 will do this for us. */
11424 }
11425 else
11426 expr_hi = NULL_TREE;
11427
11428 if (parser->in_switch_statement_p)
11429 {
11430 tree l = finish_case_label (token->location, expr, expr_hi);
11431 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11432 {
11433 label = CASE_LABEL (l);
11434 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11435 }
11436 }
11437 else
11438 error_at (token->location,
11439 "case label %qE not within a switch statement",
11440 expr);
11441 }
11442 break;
11443
11444 case RID_DEFAULT:
11445 /* Consume the `default' token. */
11446 cp_lexer_consume_token (parser->lexer);
11447
11448 if (parser->in_switch_statement_p)
11449 {
11450 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11451 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11452 {
11453 label = CASE_LABEL (l);
11454 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11455 }
11456 }
11457 else
11458 error_at (token->location, "case label not within a switch statement");
11459 break;
11460
11461 default:
11462 /* Anything else must be an ordinary label. */
11463 label = finish_label_stmt (cp_parser_identifier (parser));
11464 if (label && TREE_CODE (label) == LABEL_DECL)
11465 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11466 break;
11467 }
11468
11469 /* Require the `:' token. */
11470 cp_parser_require (parser, CPP_COLON, RT_COLON);
11471
11472 /* An ordinary label may optionally be followed by attributes.
11473 However, this is only permitted if the attributes are then
11474 followed by a semicolon. This is because, for backward
11475 compatibility, when parsing
11476 lab: __attribute__ ((unused)) int i;
11477 we want the attribute to attach to "i", not "lab". */
11478 if (label != NULL_TREE
11479 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11480 {
11481 tree attrs;
11482 cp_parser_parse_tentatively (parser);
11483 attrs = cp_parser_gnu_attributes_opt (parser);
11484 if (attrs == NULL_TREE
11485 /* And fallthrough always binds to the expression-statement. */
11486 || attribute_fallthrough_p (attrs)
11487 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11488 cp_parser_abort_tentative_parse (parser);
11489 else if (!cp_parser_parse_definitely (parser))
11490 ;
11491 else
11492 attributes = attr_chainon (attributes, attrs);
11493 }
11494
11495 if (attributes != NULL_TREE)
11496 cplus_decl_attributes (&label, attributes, 0);
11497
11498 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11499 }
11500
11501 /* Parse an expression-statement.
11502
11503 expression-statement:
11504 expression [opt] ;
11505
11506 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11507 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11508 indicates whether this expression-statement is part of an
11509 expression statement. */
11510
11511 static tree
11512 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11513 {
11514 tree statement = NULL_TREE;
11515 cp_token *token = cp_lexer_peek_token (parser->lexer);
11516 location_t loc = token->location;
11517
11518 /* There might be attribute fallthrough. */
11519 tree attr = cp_parser_gnu_attributes_opt (parser);
11520
11521 /* If the next token is a ';', then there is no expression
11522 statement. */
11523 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11524 {
11525 statement = cp_parser_expression (parser);
11526 if (statement == error_mark_node
11527 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11528 {
11529 cp_parser_skip_to_end_of_block_or_statement (parser);
11530 return error_mark_node;
11531 }
11532 }
11533
11534 /* Handle [[fallthrough]];. */
11535 if (attribute_fallthrough_p (attr))
11536 {
11537 /* The next token after the fallthrough attribute is ';'. */
11538 if (statement == NULL_TREE)
11539 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11540 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11541 void_type_node, 0);
11542 else
11543 warning_at (loc, OPT_Wattributes,
11544 "%<fallthrough%> attribute not followed by %<;%>");
11545 attr = NULL_TREE;
11546 }
11547
11548 /* Allow "[[fallthrough]];", but warn otherwise. */
11549 if (attr != NULL_TREE)
11550 warning_at (loc, OPT_Wattributes,
11551 "attributes at the beginning of statement are ignored");
11552
11553 /* Give a helpful message for "A<T>::type t;" and the like. */
11554 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11555 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11556 {
11557 if (TREE_CODE (statement) == SCOPE_REF)
11558 error_at (token->location, "need %<typename%> before %qE because "
11559 "%qT is a dependent scope",
11560 statement, TREE_OPERAND (statement, 0));
11561 else if (is_overloaded_fn (statement)
11562 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11563 {
11564 /* A::A a; */
11565 tree fn = get_first_fn (statement);
11566 error_at (token->location,
11567 "%<%T::%D%> names the constructor, not the type",
11568 DECL_CONTEXT (fn), DECL_NAME (fn));
11569 }
11570 }
11571
11572 /* Consume the final `;'. */
11573 cp_parser_consume_semicolon_at_end_of_statement (parser);
11574
11575 if (in_statement_expr
11576 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11577 /* This is the final expression statement of a statement
11578 expression. */
11579 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11580 else if (statement)
11581 statement = finish_expr_stmt (statement);
11582
11583 return statement;
11584 }
11585
11586 /* Parse a compound-statement.
11587
11588 compound-statement:
11589 { statement-seq [opt] }
11590
11591 GNU extension:
11592
11593 compound-statement:
11594 { label-declaration-seq [opt] statement-seq [opt] }
11595
11596 label-declaration-seq:
11597 label-declaration
11598 label-declaration-seq label-declaration
11599
11600 Returns a tree representing the statement. */
11601
11602 static tree
11603 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11604 int bcs_flags, bool function_body)
11605 {
11606 tree compound_stmt;
11607 matching_braces braces;
11608
11609 /* Consume the `{'. */
11610 if (!braces.require_open (parser))
11611 return error_mark_node;
11612 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11613 && !function_body && cxx_dialect < cxx14)
11614 pedwarn (input_location, OPT_Wpedantic,
11615 "compound-statement in %<constexpr%> function");
11616 /* Begin the compound-statement. */
11617 compound_stmt = begin_compound_stmt (bcs_flags);
11618 /* If the next keyword is `__label__' we have a label declaration. */
11619 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11620 cp_parser_label_declaration (parser);
11621 /* Parse an (optional) statement-seq. */
11622 cp_parser_statement_seq_opt (parser, in_statement_expr);
11623 /* Finish the compound-statement. */
11624 finish_compound_stmt (compound_stmt);
11625 /* Consume the `}'. */
11626 braces.require_close (parser);
11627
11628 return compound_stmt;
11629 }
11630
11631 /* Parse an (optional) statement-seq.
11632
11633 statement-seq:
11634 statement
11635 statement-seq [opt] statement */
11636
11637 static void
11638 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11639 {
11640 /* Scan statements until there aren't any more. */
11641 while (true)
11642 {
11643 cp_token *token = cp_lexer_peek_token (parser->lexer);
11644
11645 /* If we are looking at a `}', then we have run out of
11646 statements; the same is true if we have reached the end
11647 of file, or have stumbled upon a stray '@end'. */
11648 if (token->type == CPP_CLOSE_BRACE
11649 || token->type == CPP_EOF
11650 || token->type == CPP_PRAGMA_EOL
11651 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11652 break;
11653
11654 /* If we are in a compound statement and find 'else' then
11655 something went wrong. */
11656 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11657 {
11658 if (parser->in_statement & IN_IF_STMT)
11659 break;
11660 else
11661 {
11662 token = cp_lexer_consume_token (parser->lexer);
11663 error_at (token->location, "%<else%> without a previous %<if%>");
11664 }
11665 }
11666
11667 /* Parse the statement. */
11668 cp_parser_statement (parser, in_statement_expr, true, NULL);
11669 }
11670 }
11671
11672 /* Return true if this is the C++20 version of range-based-for with
11673 init-statement. */
11674
11675 static bool
11676 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11677 {
11678 bool r = false;
11679
11680 /* Save tokens so that we can put them back. */
11681 cp_lexer_save_tokens (parser->lexer);
11682
11683 /* There has to be an unnested ; followed by an unnested :. */
11684 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11685 /*recovering=*/false,
11686 CPP_SEMICOLON,
11687 /*consume_paren=*/false) != -1)
11688 goto out;
11689
11690 /* We found the semicolon, eat it now. */
11691 cp_lexer_consume_token (parser->lexer);
11692
11693 /* Now look for ':' that is not nested in () or {}. */
11694 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11695 /*recovering=*/false,
11696 CPP_COLON,
11697 /*consume_paren=*/false) == -1);
11698
11699 out:
11700 /* Roll back the tokens we skipped. */
11701 cp_lexer_rollback_tokens (parser->lexer);
11702
11703 return r;
11704 }
11705
11706 /* Return true if we're looking at (init; cond), false otherwise. */
11707
11708 static bool
11709 cp_parser_init_statement_p (cp_parser *parser)
11710 {
11711 /* Save tokens so that we can put them back. */
11712 cp_lexer_save_tokens (parser->lexer);
11713
11714 /* Look for ';' that is not nested in () or {}. */
11715 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11716 /*recovering=*/false,
11717 CPP_SEMICOLON,
11718 /*consume_paren=*/false);
11719
11720 /* Roll back the tokens we skipped. */
11721 cp_lexer_rollback_tokens (parser->lexer);
11722
11723 return ret == -1;
11724 }
11725
11726 /* Parse a selection-statement.
11727
11728 selection-statement:
11729 if ( init-statement [opt] condition ) statement
11730 if ( init-statement [opt] condition ) statement else statement
11731 switch ( init-statement [opt] condition ) statement
11732
11733 Returns the new IF_STMT or SWITCH_STMT.
11734
11735 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11736 is a (possibly labeled) if statement which is not enclosed in
11737 braces and has an else clause. This is used to implement
11738 -Wparentheses.
11739
11740 CHAIN is a vector of if-else-if conditions. This is used to implement
11741 -Wduplicated-cond. */
11742
11743 static tree
11744 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11745 vec<tree> *chain)
11746 {
11747 cp_token *token;
11748 enum rid keyword;
11749 token_indent_info guard_tinfo;
11750
11751 if (if_p != NULL)
11752 *if_p = false;
11753
11754 /* Peek at the next token. */
11755 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11756 guard_tinfo = get_token_indent_info (token);
11757
11758 /* See what kind of keyword it is. */
11759 keyword = token->keyword;
11760 switch (keyword)
11761 {
11762 case RID_IF:
11763 case RID_SWITCH:
11764 {
11765 tree statement;
11766 tree condition;
11767
11768 bool cx = false;
11769 if (keyword == RID_IF
11770 && cp_lexer_next_token_is_keyword (parser->lexer,
11771 RID_CONSTEXPR))
11772 {
11773 cx = true;
11774 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11775 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11776 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11777 "with %<-std=c++17%> or %<-std=gnu++17%>");
11778 }
11779
11780 /* Look for the `('. */
11781 matching_parens parens;
11782 if (!parens.require_open (parser))
11783 {
11784 cp_parser_skip_to_end_of_statement (parser);
11785 return error_mark_node;
11786 }
11787
11788 /* Begin the selection-statement. */
11789 if (keyword == RID_IF)
11790 {
11791 statement = begin_if_stmt ();
11792 IF_STMT_CONSTEXPR_P (statement) = cx;
11793 }
11794 else
11795 statement = begin_switch_stmt ();
11796
11797 /* Parse the optional init-statement. */
11798 if (cp_parser_init_statement_p (parser))
11799 {
11800 tree decl;
11801 if (cxx_dialect < cxx17)
11802 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11803 "init-statement in selection statements only available "
11804 "with %<-std=c++17%> or %<-std=gnu++17%>");
11805 cp_parser_init_statement (parser, &decl);
11806 }
11807
11808 /* Parse the condition. */
11809 condition = cp_parser_condition (parser);
11810 /* Look for the `)'. */
11811 if (!parens.require_close (parser))
11812 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11813 /*consume_paren=*/true);
11814
11815 if (keyword == RID_IF)
11816 {
11817 bool nested_if;
11818 unsigned char in_statement;
11819
11820 /* Add the condition. */
11821 condition = finish_if_stmt_cond (condition, statement);
11822
11823 if (warn_duplicated_cond)
11824 warn_duplicated_cond_add_or_warn (token->location, condition,
11825 &chain);
11826
11827 /* Parse the then-clause. */
11828 in_statement = parser->in_statement;
11829 parser->in_statement |= IN_IF_STMT;
11830
11831 /* Outside a template, the non-selected branch of a constexpr
11832 if is a 'discarded statement', i.e. unevaluated. */
11833 bool was_discarded = in_discarded_stmt;
11834 bool discard_then = (cx && !processing_template_decl
11835 && integer_zerop (condition));
11836 if (discard_then)
11837 {
11838 in_discarded_stmt = true;
11839 ++c_inhibit_evaluation_warnings;
11840 }
11841
11842 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11843 guard_tinfo);
11844
11845 parser->in_statement = in_statement;
11846
11847 finish_then_clause (statement);
11848
11849 if (discard_then)
11850 {
11851 THEN_CLAUSE (statement) = NULL_TREE;
11852 in_discarded_stmt = was_discarded;
11853 --c_inhibit_evaluation_warnings;
11854 }
11855
11856 /* If the next token is `else', parse the else-clause. */
11857 if (cp_lexer_next_token_is_keyword (parser->lexer,
11858 RID_ELSE))
11859 {
11860 bool discard_else = (cx && !processing_template_decl
11861 && integer_nonzerop (condition));
11862 if (discard_else)
11863 {
11864 in_discarded_stmt = true;
11865 ++c_inhibit_evaluation_warnings;
11866 }
11867
11868 guard_tinfo
11869 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11870 /* Consume the `else' keyword. */
11871 cp_lexer_consume_token (parser->lexer);
11872 if (warn_duplicated_cond)
11873 {
11874 if (cp_lexer_next_token_is_keyword (parser->lexer,
11875 RID_IF)
11876 && chain == NULL)
11877 {
11878 /* We've got "if (COND) else if (COND2)". Start
11879 the condition chain and add COND as the first
11880 element. */
11881 chain = new vec<tree> ();
11882 if (!CONSTANT_CLASS_P (condition)
11883 && !TREE_SIDE_EFFECTS (condition))
11884 {
11885 /* Wrap it in a NOP_EXPR so that we can set the
11886 location of the condition. */
11887 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11888 condition);
11889 SET_EXPR_LOCATION (e, token->location);
11890 chain->safe_push (e);
11891 }
11892 }
11893 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11894 RID_IF))
11895 {
11896 /* This is if-else without subsequent if. Zap the
11897 condition chain; we would have already warned at
11898 this point. */
11899 delete chain;
11900 chain = NULL;
11901 }
11902 }
11903 begin_else_clause (statement);
11904 /* Parse the else-clause. */
11905 cp_parser_implicitly_scoped_statement (parser, NULL,
11906 guard_tinfo, chain);
11907
11908 finish_else_clause (statement);
11909
11910 /* If we are currently parsing a then-clause, then
11911 IF_P will not be NULL. We set it to true to
11912 indicate that this if statement has an else clause.
11913 This may trigger the Wparentheses warning below
11914 when we get back up to the parent if statement. */
11915 if (if_p != NULL)
11916 *if_p = true;
11917
11918 if (discard_else)
11919 {
11920 ELSE_CLAUSE (statement) = NULL_TREE;
11921 in_discarded_stmt = was_discarded;
11922 --c_inhibit_evaluation_warnings;
11923 }
11924 }
11925 else
11926 {
11927 /* This if statement does not have an else clause. If
11928 NESTED_IF is true, then the then-clause has an if
11929 statement which does have an else clause. We warn
11930 about the potential ambiguity. */
11931 if (nested_if)
11932 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11933 "suggest explicit braces to avoid ambiguous"
11934 " %<else%>");
11935 if (warn_duplicated_cond)
11936 {
11937 /* We don't need the condition chain anymore. */
11938 delete chain;
11939 chain = NULL;
11940 }
11941 }
11942
11943 /* Now we're all done with the if-statement. */
11944 finish_if_stmt (statement);
11945 }
11946 else
11947 {
11948 bool in_switch_statement_p;
11949 unsigned char in_statement;
11950
11951 /* Add the condition. */
11952 finish_switch_cond (condition, statement);
11953
11954 /* Parse the body of the switch-statement. */
11955 in_switch_statement_p = parser->in_switch_statement_p;
11956 in_statement = parser->in_statement;
11957 parser->in_switch_statement_p = true;
11958 parser->in_statement |= IN_SWITCH_STMT;
11959 cp_parser_implicitly_scoped_statement (parser, if_p,
11960 guard_tinfo);
11961 parser->in_switch_statement_p = in_switch_statement_p;
11962 parser->in_statement = in_statement;
11963
11964 /* Now we're all done with the switch-statement. */
11965 finish_switch_stmt (statement);
11966 }
11967
11968 return statement;
11969 }
11970 break;
11971
11972 default:
11973 cp_parser_error (parser, "expected selection-statement");
11974 return error_mark_node;
11975 }
11976 }
11977
11978 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11979 If we have seen at least one decl-specifier, and the next token
11980 is not a parenthesis, then we must be looking at a declaration.
11981 (After "int (" we might be looking at a functional cast.) */
11982
11983 static void
11984 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11985 bool any_specifiers_p)
11986 {
11987 if (any_specifiers_p
11988 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11989 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11990 && !cp_parser_error_occurred (parser))
11991 cp_parser_commit_to_tentative_parse (parser);
11992 }
11993
11994 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11995 The declarator shall not specify a function or an array. Returns
11996 TRUE if the declarator is valid, FALSE otherwise. */
11997
11998 static bool
11999 cp_parser_check_condition_declarator (cp_parser* parser,
12000 cp_declarator *declarator,
12001 location_t loc)
12002 {
12003 if (declarator == cp_error_declarator
12004 || function_declarator_p (declarator)
12005 || declarator->kind == cdk_array)
12006 {
12007 if (declarator == cp_error_declarator)
12008 /* Already complained. */;
12009 else if (declarator->kind == cdk_array)
12010 error_at (loc, "condition declares an array");
12011 else
12012 error_at (loc, "condition declares a function");
12013 if (parser->fully_implicit_function_template_p)
12014 abort_fully_implicit_template (parser);
12015 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
12016 /*or_comma=*/false,
12017 /*consume_paren=*/false);
12018 return false;
12019 }
12020 else
12021 return true;
12022 }
12023
12024 /* Parse a condition.
12025
12026 condition:
12027 expression
12028 type-specifier-seq declarator = initializer-clause
12029 type-specifier-seq declarator braced-init-list
12030
12031 GNU Extension:
12032
12033 condition:
12034 type-specifier-seq declarator asm-specification [opt]
12035 attributes [opt] = assignment-expression
12036
12037 Returns the expression that should be tested. */
12038
12039 static tree
12040 cp_parser_condition (cp_parser* parser)
12041 {
12042 cp_decl_specifier_seq type_specifiers;
12043 const char *saved_message;
12044 int declares_class_or_enum;
12045
12046 /* Try the declaration first. */
12047 cp_parser_parse_tentatively (parser);
12048 /* New types are not allowed in the type-specifier-seq for a
12049 condition. */
12050 saved_message = parser->type_definition_forbidden_message;
12051 parser->type_definition_forbidden_message
12052 = G_("types may not be defined in conditions");
12053 /* Parse the type-specifier-seq. */
12054 cp_parser_decl_specifier_seq (parser,
12055 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
12056 &type_specifiers,
12057 &declares_class_or_enum);
12058 /* Restore the saved message. */
12059 parser->type_definition_forbidden_message = saved_message;
12060
12061 cp_parser_maybe_commit_to_declaration (parser,
12062 type_specifiers.any_specifiers_p);
12063
12064 /* If all is well, we might be looking at a declaration. */
12065 if (!cp_parser_error_occurred (parser))
12066 {
12067 tree decl;
12068 tree asm_specification;
12069 tree attributes;
12070 cp_declarator *declarator;
12071 tree initializer = NULL_TREE;
12072 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12073
12074 /* Parse the declarator. */
12075 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12076 CP_PARSER_FLAGS_NONE,
12077 /*ctor_dtor_or_conv_p=*/NULL,
12078 /*parenthesized_p=*/NULL,
12079 /*member_p=*/false,
12080 /*friend_p=*/false,
12081 /*static_p=*/false);
12082 /* Parse the attributes. */
12083 attributes = cp_parser_attributes_opt (parser);
12084 /* Parse the asm-specification. */
12085 asm_specification = cp_parser_asm_specification_opt (parser);
12086 /* If the next token is not an `=' or '{', then we might still be
12087 looking at an expression. For example:
12088
12089 if (A(a).x)
12090
12091 looks like a decl-specifier-seq and a declarator -- but then
12092 there is no `=', so this is an expression. */
12093 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12094 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12095 cp_parser_simulate_error (parser);
12096
12097 /* If we did see an `=' or '{', then we are looking at a declaration
12098 for sure. */
12099 if (cp_parser_parse_definitely (parser))
12100 {
12101 tree pushed_scope;
12102 bool non_constant_p = false;
12103 int flags = LOOKUP_ONLYCONVERTING;
12104
12105 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
12106 return error_mark_node;
12107
12108 /* Create the declaration. */
12109 decl = start_decl (declarator, &type_specifiers,
12110 /*initialized_p=*/true,
12111 attributes, /*prefix_attributes=*/NULL_TREE,
12112 &pushed_scope);
12113
12114 /* Parse the initializer. */
12115 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12116 {
12117 initializer = cp_parser_braced_list (parser, &non_constant_p);
12118 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
12119 flags = 0;
12120 }
12121 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12122 {
12123 /* Consume the `='. */
12124 cp_lexer_consume_token (parser->lexer);
12125 initializer = cp_parser_initializer_clause (parser,
12126 &non_constant_p);
12127 }
12128 else
12129 {
12130 cp_parser_error (parser, "expected initializer");
12131 initializer = error_mark_node;
12132 }
12133 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
12134 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12135
12136 /* Process the initializer. */
12137 cp_finish_decl (decl,
12138 initializer, !non_constant_p,
12139 asm_specification,
12140 flags);
12141
12142 if (pushed_scope)
12143 pop_scope (pushed_scope);
12144
12145 return convert_from_reference (decl);
12146 }
12147 }
12148 /* If we didn't even get past the declarator successfully, we are
12149 definitely not looking at a declaration. */
12150 else
12151 cp_parser_abort_tentative_parse (parser);
12152
12153 /* Otherwise, we are looking at an expression. */
12154 return cp_parser_expression (parser);
12155 }
12156
12157 /* Parses a for-statement or range-for-statement until the closing ')',
12158 not included. */
12159
12160 static tree
12161 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12162 {
12163 tree init, scope, decl;
12164 bool is_range_for;
12165
12166 /* Begin the for-statement. */
12167 scope = begin_for_scope (&init);
12168
12169 /* Parse the initialization. */
12170 is_range_for = cp_parser_init_statement (parser, &decl);
12171
12172 if (is_range_for)
12173 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12174 false);
12175 else
12176 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12177 }
12178
12179 static tree
12180 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12181 unsigned short unroll)
12182 {
12183 /* Normal for loop */
12184 tree condition = NULL_TREE;
12185 tree expression = NULL_TREE;
12186 tree stmt;
12187
12188 stmt = begin_for_stmt (scope, init);
12189 /* The init-statement has already been parsed in
12190 cp_parser_init_statement, so no work is needed here. */
12191 finish_init_stmt (stmt);
12192
12193 /* If there's a condition, process it. */
12194 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12195 condition = cp_parser_condition (parser);
12196 else if (ivdep)
12197 {
12198 cp_parser_error (parser, "missing loop condition in loop with "
12199 "%<GCC ivdep%> pragma");
12200 condition = error_mark_node;
12201 }
12202 else if (unroll)
12203 {
12204 cp_parser_error (parser, "missing loop condition in loop with "
12205 "%<GCC unroll%> pragma");
12206 condition = error_mark_node;
12207 }
12208 finish_for_cond (condition, stmt, ivdep, unroll);
12209 /* Look for the `;'. */
12210 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12211
12212 /* If there's an expression, process it. */
12213 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12214 expression = cp_parser_expression (parser);
12215 finish_for_expr (expression, stmt);
12216
12217 return stmt;
12218 }
12219
12220 /* Tries to parse a range-based for-statement:
12221
12222 range-based-for:
12223 decl-specifier-seq declarator : expression
12224
12225 The decl-specifier-seq declarator and the `:' are already parsed by
12226 cp_parser_init_statement. If processing_template_decl it returns a
12227 newly created RANGE_FOR_STMT; if not, it is converted to a
12228 regular FOR_STMT. */
12229
12230 static tree
12231 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12232 bool ivdep, unsigned short unroll, bool is_omp)
12233 {
12234 tree stmt, range_expr;
12235 auto_vec <cxx_binding *, 16> bindings;
12236 auto_vec <tree, 16> names;
12237 tree decomp_first_name = NULL_TREE;
12238 unsigned int decomp_cnt = 0;
12239
12240 /* Get the range declaration momentarily out of the way so that
12241 the range expression doesn't clash with it. */
12242 if (range_decl != error_mark_node)
12243 {
12244 if (DECL_HAS_VALUE_EXPR_P (range_decl))
12245 {
12246 tree v = DECL_VALUE_EXPR (range_decl);
12247 /* For decomposition declaration get all of the corresponding
12248 declarations out of the way. */
12249 if (TREE_CODE (v) == ARRAY_REF
12250 && VAR_P (TREE_OPERAND (v, 0))
12251 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12252 {
12253 tree d = range_decl;
12254 range_decl = TREE_OPERAND (v, 0);
12255 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12256 decomp_first_name = d;
12257 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12258 {
12259 tree name = DECL_NAME (d);
12260 names.safe_push (name);
12261 bindings.safe_push (IDENTIFIER_BINDING (name));
12262 IDENTIFIER_BINDING (name)
12263 = IDENTIFIER_BINDING (name)->previous;
12264 }
12265 }
12266 }
12267 if (names.is_empty ())
12268 {
12269 tree name = DECL_NAME (range_decl);
12270 names.safe_push (name);
12271 bindings.safe_push (IDENTIFIER_BINDING (name));
12272 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12273 }
12274 }
12275
12276 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12277 {
12278 bool expr_non_constant_p;
12279 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12280 }
12281 else
12282 range_expr = cp_parser_expression (parser);
12283
12284 /* Put the range declaration(s) back into scope. */
12285 for (unsigned int i = 0; i < names.length (); i++)
12286 {
12287 cxx_binding *binding = bindings[i];
12288 binding->previous = IDENTIFIER_BINDING (names[i]);
12289 IDENTIFIER_BINDING (names[i]) = binding;
12290 }
12291
12292 /* finish_omp_for has its own code for the following, so just
12293 return the range_expr instead. */
12294 if (is_omp)
12295 return range_expr;
12296
12297 /* If in template, STMT is converted to a normal for-statement
12298 at instantiation. If not, it is done just ahead. */
12299 if (processing_template_decl)
12300 {
12301 if (check_for_bare_parameter_packs (range_expr))
12302 range_expr = error_mark_node;
12303 stmt = begin_range_for_stmt (scope, init);
12304 if (ivdep)
12305 RANGE_FOR_IVDEP (stmt) = 1;
12306 if (unroll)
12307 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12308 finish_range_for_decl (stmt, range_decl, range_expr);
12309 if (!type_dependent_expression_p (range_expr)
12310 /* do_auto_deduction doesn't mess with template init-lists. */
12311 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12312 do_range_for_auto_deduction (range_decl, range_expr);
12313 }
12314 else
12315 {
12316 stmt = begin_for_stmt (scope, init);
12317 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12318 decomp_first_name, decomp_cnt, ivdep,
12319 unroll);
12320 }
12321 return stmt;
12322 }
12323
12324 /* Subroutine of cp_convert_range_for: given the initializer expression,
12325 builds up the range temporary. */
12326
12327 static tree
12328 build_range_temp (tree range_expr)
12329 {
12330 tree range_type, range_temp;
12331
12332 /* Find out the type deduced by the declaration
12333 `auto &&__range = range_expr'. */
12334 range_type = cp_build_reference_type (make_auto (), true);
12335 range_type = do_auto_deduction (range_type, range_expr,
12336 type_uses_auto (range_type));
12337
12338 /* Create the __range variable. */
12339 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12340 range_type);
12341 TREE_USED (range_temp) = 1;
12342 DECL_ARTIFICIAL (range_temp) = 1;
12343
12344 return range_temp;
12345 }
12346
12347 /* Used by cp_parser_range_for in template context: we aren't going to
12348 do a full conversion yet, but we still need to resolve auto in the
12349 type of the for-range-declaration if present. This is basically
12350 a shortcut version of cp_convert_range_for. */
12351
12352 static void
12353 do_range_for_auto_deduction (tree decl, tree range_expr)
12354 {
12355 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12356 if (auto_node)
12357 {
12358 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12359 range_temp = convert_from_reference (build_range_temp (range_expr));
12360 iter_type = (cp_parser_perform_range_for_lookup
12361 (range_temp, &begin_dummy, &end_dummy));
12362 if (iter_type)
12363 {
12364 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12365 iter_type);
12366 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12367 RO_UNARY_STAR,
12368 tf_warning_or_error);
12369 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12370 iter_decl, auto_node);
12371 }
12372 }
12373 }
12374
12375 /* Converts a range-based for-statement into a normal
12376 for-statement, as per the definition.
12377
12378 for (RANGE_DECL : RANGE_EXPR)
12379 BLOCK
12380
12381 should be equivalent to:
12382
12383 {
12384 auto &&__range = RANGE_EXPR;
12385 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12386 __begin != __end;
12387 ++__begin)
12388 {
12389 RANGE_DECL = *__begin;
12390 BLOCK
12391 }
12392 }
12393
12394 If RANGE_EXPR is an array:
12395 BEGIN_EXPR = __range
12396 END_EXPR = __range + ARRAY_SIZE(__range)
12397 Else if RANGE_EXPR has a member 'begin' or 'end':
12398 BEGIN_EXPR = __range.begin()
12399 END_EXPR = __range.end()
12400 Else:
12401 BEGIN_EXPR = begin(__range)
12402 END_EXPR = end(__range);
12403
12404 If __range has a member 'begin' but not 'end', or vice versa, we must
12405 still use the second alternative (it will surely fail, however).
12406 When calling begin()/end() in the third alternative we must use
12407 argument dependent lookup, but always considering 'std' as an associated
12408 namespace. */
12409
12410 tree
12411 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12412 tree decomp_first_name, unsigned int decomp_cnt,
12413 bool ivdep, unsigned short unroll)
12414 {
12415 tree begin, end;
12416 tree iter_type, begin_expr, end_expr;
12417 tree condition, expression;
12418
12419 range_expr = mark_lvalue_use (range_expr);
12420
12421 if (range_decl == error_mark_node || range_expr == error_mark_node)
12422 /* If an error happened previously do nothing or else a lot of
12423 unhelpful errors would be issued. */
12424 begin_expr = end_expr = iter_type = error_mark_node;
12425 else
12426 {
12427 tree range_temp;
12428
12429 if (VAR_P (range_expr)
12430 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12431 /* Can't bind a reference to an array of runtime bound. */
12432 range_temp = range_expr;
12433 else
12434 {
12435 range_temp = build_range_temp (range_expr);
12436 pushdecl (range_temp);
12437 cp_finish_decl (range_temp, range_expr,
12438 /*is_constant_init*/false, NULL_TREE,
12439 LOOKUP_ONLYCONVERTING);
12440 range_temp = convert_from_reference (range_temp);
12441 }
12442 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12443 &begin_expr, &end_expr);
12444 }
12445
12446 /* The new for initialization statement. */
12447 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12448 iter_type);
12449 TREE_USED (begin) = 1;
12450 DECL_ARTIFICIAL (begin) = 1;
12451 pushdecl (begin);
12452 cp_finish_decl (begin, begin_expr,
12453 /*is_constant_init*/false, NULL_TREE,
12454 LOOKUP_ONLYCONVERTING);
12455
12456 if (cxx_dialect >= cxx17)
12457 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12458 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12459 TREE_USED (end) = 1;
12460 DECL_ARTIFICIAL (end) = 1;
12461 pushdecl (end);
12462 cp_finish_decl (end, end_expr,
12463 /*is_constant_init*/false, NULL_TREE,
12464 LOOKUP_ONLYCONVERTING);
12465
12466 finish_init_stmt (statement);
12467
12468 /* The new for condition. */
12469 condition = build_x_binary_op (input_location, NE_EXPR,
12470 begin, ERROR_MARK,
12471 end, ERROR_MARK,
12472 NULL, tf_warning_or_error);
12473 finish_for_cond (condition, statement, ivdep, unroll);
12474
12475 /* The new increment expression. */
12476 expression = finish_unary_op_expr (input_location,
12477 PREINCREMENT_EXPR, begin,
12478 tf_warning_or_error);
12479 finish_for_expr (expression, statement);
12480
12481 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12482 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12483
12484 /* The declaration is initialized with *__begin inside the loop body. */
12485 cp_finish_decl (range_decl,
12486 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12487 tf_warning_or_error),
12488 /*is_constant_init*/false, NULL_TREE,
12489 LOOKUP_ONLYCONVERTING);
12490 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12491 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12492
12493 return statement;
12494 }
12495
12496 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12497 We need to solve both at the same time because the method used
12498 depends on the existence of members begin or end.
12499 Returns the type deduced for the iterator expression. */
12500
12501 static tree
12502 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12503 {
12504 if (error_operand_p (range))
12505 {
12506 *begin = *end = error_mark_node;
12507 return error_mark_node;
12508 }
12509
12510 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12511 {
12512 error ("range-based %<for%> expression of type %qT "
12513 "has incomplete type", TREE_TYPE (range));
12514 *begin = *end = error_mark_node;
12515 return error_mark_node;
12516 }
12517 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12518 {
12519 /* If RANGE is an array, we will use pointer arithmetic. */
12520 *begin = decay_conversion (range, tf_warning_or_error);
12521 *end = build_binary_op (input_location, PLUS_EXPR,
12522 range,
12523 array_type_nelts_top (TREE_TYPE (range)),
12524 false);
12525 return TREE_TYPE (*begin);
12526 }
12527 else
12528 {
12529 /* If it is not an array, we must do a bit of magic. */
12530 tree id_begin, id_end;
12531 tree member_begin, member_end;
12532
12533 *begin = *end = error_mark_node;
12534
12535 id_begin = get_identifier ("begin");
12536 id_end = get_identifier ("end");
12537 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12538 /*protect=*/2, /*want_type=*/false,
12539 tf_warning_or_error);
12540 member_end = lookup_member (TREE_TYPE (range), id_end,
12541 /*protect=*/2, /*want_type=*/false,
12542 tf_warning_or_error);
12543
12544 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12545 {
12546 /* Use the member functions. */
12547 *begin = cp_parser_range_for_member_function (range, id_begin);
12548 *end = cp_parser_range_for_member_function (range, id_end);
12549 }
12550 else
12551 {
12552 /* Use global functions with ADL. */
12553 releasing_vec vec;
12554
12555 vec_safe_push (vec, range);
12556
12557 member_begin = perform_koenig_lookup (id_begin, vec,
12558 tf_warning_or_error);
12559 *begin = finish_call_expr (member_begin, &vec, false, true,
12560 tf_warning_or_error);
12561 member_end = perform_koenig_lookup (id_end, vec,
12562 tf_warning_or_error);
12563 *end = finish_call_expr (member_end, &vec, false, true,
12564 tf_warning_or_error);
12565 }
12566
12567 /* Last common checks. */
12568 if (*begin == error_mark_node || *end == error_mark_node)
12569 {
12570 /* If one of the expressions is an error do no more checks. */
12571 *begin = *end = error_mark_node;
12572 return error_mark_node;
12573 }
12574 else if (type_dependent_expression_p (*begin)
12575 || type_dependent_expression_p (*end))
12576 /* Can happen, when, eg, in a template context, Koenig lookup
12577 can't resolve begin/end (c++/58503). */
12578 return NULL_TREE;
12579 else
12580 {
12581 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12582 /* The unqualified type of the __begin and __end temporaries should
12583 be the same, as required by the multiple auto declaration. */
12584 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12585 {
12586 if (cxx_dialect >= cxx17
12587 && (build_x_binary_op (input_location, NE_EXPR,
12588 *begin, ERROR_MARK,
12589 *end, ERROR_MARK,
12590 NULL, tf_none)
12591 != error_mark_node))
12592 /* P0184R0 allows __begin and __end to have different types,
12593 but make sure they are comparable so we can give a better
12594 diagnostic. */;
12595 else
12596 error ("inconsistent begin/end types in range-based %<for%> "
12597 "statement: %qT and %qT",
12598 TREE_TYPE (*begin), TREE_TYPE (*end));
12599 }
12600 return iter_type;
12601 }
12602 }
12603 }
12604
12605 /* Helper function for cp_parser_perform_range_for_lookup.
12606 Builds a tree for RANGE.IDENTIFIER(). */
12607
12608 static tree
12609 cp_parser_range_for_member_function (tree range, tree identifier)
12610 {
12611 tree member, res;
12612
12613 member = finish_class_member_access_expr (range, identifier,
12614 false, tf_warning_or_error);
12615 if (member == error_mark_node)
12616 return error_mark_node;
12617
12618 releasing_vec vec;
12619 res = finish_call_expr (member, &vec,
12620 /*disallow_virtual=*/false,
12621 /*koenig_p=*/false,
12622 tf_warning_or_error);
12623 return res;
12624 }
12625
12626 /* Parse an iteration-statement.
12627
12628 iteration-statement:
12629 while ( condition ) statement
12630 do statement while ( expression ) ;
12631 for ( init-statement condition [opt] ; expression [opt] )
12632 statement
12633
12634 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12635
12636 static tree
12637 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12638 unsigned short unroll)
12639 {
12640 cp_token *token;
12641 enum rid keyword;
12642 tree statement;
12643 unsigned char in_statement;
12644 token_indent_info guard_tinfo;
12645
12646 /* Peek at the next token. */
12647 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12648 if (!token)
12649 return error_mark_node;
12650
12651 guard_tinfo = get_token_indent_info (token);
12652
12653 /* Remember whether or not we are already within an iteration
12654 statement. */
12655 in_statement = parser->in_statement;
12656
12657 /* See what kind of keyword it is. */
12658 keyword = token->keyword;
12659 switch (keyword)
12660 {
12661 case RID_WHILE:
12662 {
12663 tree condition;
12664
12665 /* Begin the while-statement. */
12666 statement = begin_while_stmt ();
12667 /* Look for the `('. */
12668 matching_parens parens;
12669 parens.require_open (parser);
12670 /* Parse the condition. */
12671 condition = cp_parser_condition (parser);
12672 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12673 /* Look for the `)'. */
12674 parens.require_close (parser);
12675 /* Parse the dependent statement. */
12676 parser->in_statement = IN_ITERATION_STMT;
12677 bool prev = note_iteration_stmt_body_start ();
12678 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12679 note_iteration_stmt_body_end (prev);
12680 parser->in_statement = in_statement;
12681 /* We're done with the while-statement. */
12682 finish_while_stmt (statement);
12683 }
12684 break;
12685
12686 case RID_DO:
12687 {
12688 tree expression;
12689
12690 /* Begin the do-statement. */
12691 statement = begin_do_stmt ();
12692 /* Parse the body of the do-statement. */
12693 parser->in_statement = IN_ITERATION_STMT;
12694 bool prev = note_iteration_stmt_body_start ();
12695 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12696 note_iteration_stmt_body_end (prev);
12697 parser->in_statement = in_statement;
12698 finish_do_body (statement);
12699 /* Look for the `while' keyword. */
12700 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12701 /* Look for the `('. */
12702 matching_parens parens;
12703 parens.require_open (parser);
12704 /* Parse the expression. */
12705 expression = cp_parser_expression (parser);
12706 /* We're done with the do-statement. */
12707 finish_do_stmt (expression, statement, ivdep, unroll);
12708 /* Look for the `)'. */
12709 parens.require_close (parser);
12710 /* Look for the `;'. */
12711 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12712 }
12713 break;
12714
12715 case RID_FOR:
12716 {
12717 /* Look for the `('. */
12718 matching_parens parens;
12719 parens.require_open (parser);
12720
12721 statement = cp_parser_for (parser, ivdep, unroll);
12722
12723 /* Look for the `)'. */
12724 parens.require_close (parser);
12725
12726 /* Parse the body of the for-statement. */
12727 parser->in_statement = IN_ITERATION_STMT;
12728 bool prev = note_iteration_stmt_body_start ();
12729 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12730 note_iteration_stmt_body_end (prev);
12731 parser->in_statement = in_statement;
12732
12733 /* We're done with the for-statement. */
12734 finish_for_stmt (statement);
12735 }
12736 break;
12737
12738 default:
12739 cp_parser_error (parser, "expected iteration-statement");
12740 statement = error_mark_node;
12741 break;
12742 }
12743
12744 return statement;
12745 }
12746
12747 /* Parse a init-statement or the declarator of a range-based-for.
12748 Returns true if a range-based-for declaration is seen.
12749
12750 init-statement:
12751 expression-statement
12752 simple-declaration */
12753
12754 static bool
12755 cp_parser_init_statement (cp_parser *parser, tree *decl)
12756 {
12757 /* If the next token is a `;', then we have an empty
12758 expression-statement. Grammatically, this is also a
12759 simple-declaration, but an invalid one, because it does not
12760 declare anything. Therefore, if we did not handle this case
12761 specially, we would issue an error message about an invalid
12762 declaration. */
12763 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12764 {
12765 bool is_range_for = false;
12766 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12767
12768 /* Try to parse the init-statement. */
12769 if (cp_parser_range_based_for_with_init_p (parser))
12770 {
12771 tree dummy;
12772 cp_parser_parse_tentatively (parser);
12773 /* Parse the declaration. */
12774 cp_parser_simple_declaration (parser,
12775 /*function_definition_allowed_p=*/false,
12776 &dummy);
12777 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12778 if (!cp_parser_parse_definitely (parser))
12779 /* That didn't work, try to parse it as an expression-statement. */
12780 cp_parser_expression_statement (parser, NULL_TREE);
12781
12782 if (cxx_dialect < cxx2a)
12783 {
12784 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12785 "range-based %<for%> loops with initializer only "
12786 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
12787 *decl = error_mark_node;
12788 }
12789 }
12790
12791 /* A colon is used in range-based for. */
12792 parser->colon_corrects_to_scope_p = false;
12793
12794 /* We're going to speculatively look for a declaration, falling back
12795 to an expression, if necessary. */
12796 cp_parser_parse_tentatively (parser);
12797 /* Parse the declaration. */
12798 cp_parser_simple_declaration (parser,
12799 /*function_definition_allowed_p=*/false,
12800 decl);
12801 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12802 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12803 {
12804 /* It is a range-for, consume the ':'. */
12805 cp_lexer_consume_token (parser->lexer);
12806 is_range_for = true;
12807 if (cxx_dialect < cxx11)
12808 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12809 "range-based %<for%> loops only available with "
12810 "%<-std=c++11%> or %<-std=gnu++11%>");
12811 }
12812 else
12813 /* The ';' is not consumed yet because we told
12814 cp_parser_simple_declaration not to. */
12815 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12816
12817 if (cp_parser_parse_definitely (parser))
12818 return is_range_for;
12819 /* If the tentative parse failed, then we shall need to look for an
12820 expression-statement. */
12821 }
12822 /* If we are here, it is an expression-statement. */
12823 cp_parser_expression_statement (parser, NULL_TREE);
12824 return false;
12825 }
12826
12827 /* Parse a jump-statement.
12828
12829 jump-statement:
12830 break ;
12831 continue ;
12832 return expression [opt] ;
12833 return braced-init-list ;
12834 goto identifier ;
12835
12836 GNU extension:
12837
12838 jump-statement:
12839 goto * expression ;
12840
12841 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12842
12843 static tree
12844 cp_parser_jump_statement (cp_parser* parser)
12845 {
12846 tree statement = error_mark_node;
12847 cp_token *token;
12848 enum rid keyword;
12849 unsigned char in_statement;
12850
12851 /* Peek at the next token. */
12852 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12853 if (!token)
12854 return error_mark_node;
12855
12856 /* See what kind of keyword it is. */
12857 keyword = token->keyword;
12858 switch (keyword)
12859 {
12860 case RID_BREAK:
12861 in_statement = parser->in_statement & ~IN_IF_STMT;
12862 switch (in_statement)
12863 {
12864 case 0:
12865 error_at (token->location, "break statement not within loop or switch");
12866 break;
12867 default:
12868 gcc_assert ((in_statement & IN_SWITCH_STMT)
12869 || in_statement == IN_ITERATION_STMT);
12870 statement = finish_break_stmt ();
12871 if (in_statement == IN_ITERATION_STMT)
12872 break_maybe_infinite_loop ();
12873 break;
12874 case IN_OMP_BLOCK:
12875 error_at (token->location, "invalid exit from OpenMP structured block");
12876 break;
12877 case IN_OMP_FOR:
12878 error_at (token->location, "break statement used with OpenMP for loop");
12879 break;
12880 }
12881 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12882 break;
12883
12884 case RID_CONTINUE:
12885 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12886 {
12887 case 0:
12888 error_at (token->location, "continue statement not within a loop");
12889 break;
12890 /* Fall through. */
12891 case IN_ITERATION_STMT:
12892 case IN_OMP_FOR:
12893 statement = finish_continue_stmt ();
12894 break;
12895 case IN_OMP_BLOCK:
12896 error_at (token->location, "invalid exit from OpenMP structured block");
12897 break;
12898 default:
12899 gcc_unreachable ();
12900 }
12901 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12902 break;
12903
12904 case RID_RETURN:
12905 {
12906 tree expr;
12907 bool expr_non_constant_p;
12908
12909 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12910 {
12911 cp_lexer_set_source_position (parser->lexer);
12912 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12913 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12914 }
12915 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12916 expr = cp_parser_expression (parser);
12917 else
12918 /* If the next token is a `;', then there is no
12919 expression. */
12920 expr = NULL_TREE;
12921 /* Build the return-statement. */
12922 if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
12923 /* Don't deduce from a discarded return statement. */;
12924 else
12925 statement = finish_return_stmt (expr);
12926 /* Look for the final `;'. */
12927 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12928 }
12929 break;
12930
12931 case RID_GOTO:
12932 if (parser->in_function_body
12933 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12934 {
12935 error ("%<goto%> in %<constexpr%> function");
12936 cp_function_chain->invalid_constexpr = true;
12937 }
12938
12939 /* Create the goto-statement. */
12940 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12941 {
12942 /* Issue a warning about this use of a GNU extension. */
12943 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12944 /* Consume the '*' token. */
12945 cp_lexer_consume_token (parser->lexer);
12946 /* Parse the dependent expression. */
12947 finish_goto_stmt (cp_parser_expression (parser));
12948 }
12949 else
12950 finish_goto_stmt (cp_parser_identifier (parser));
12951 /* Look for the final `;'. */
12952 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12953 break;
12954
12955 default:
12956 cp_parser_error (parser, "expected jump-statement");
12957 break;
12958 }
12959
12960 return statement;
12961 }
12962
12963 /* Parse a declaration-statement.
12964
12965 declaration-statement:
12966 block-declaration */
12967
12968 static void
12969 cp_parser_declaration_statement (cp_parser* parser)
12970 {
12971 void *p;
12972
12973 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12974 p = obstack_alloc (&declarator_obstack, 0);
12975
12976 /* Parse the block-declaration. */
12977 cp_parser_block_declaration (parser, /*statement_p=*/true);
12978
12979 /* Free any declarators allocated. */
12980 obstack_free (&declarator_obstack, p);
12981 }
12982
12983 /* Some dependent statements (like `if (cond) statement'), are
12984 implicitly in their own scope. In other words, if the statement is
12985 a single statement (as opposed to a compound-statement), it is
12986 none-the-less treated as if it were enclosed in braces. Any
12987 declarations appearing in the dependent statement are out of scope
12988 after control passes that point. This function parses a statement,
12989 but ensures that is in its own scope, even if it is not a
12990 compound-statement.
12991
12992 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12993 is a (possibly labeled) if statement which is not enclosed in
12994 braces and has an else clause. This is used to implement
12995 -Wparentheses.
12996
12997 CHAIN is a vector of if-else-if conditions. This is used to implement
12998 -Wduplicated-cond.
12999
13000 Returns the new statement. */
13001
13002 static tree
13003 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
13004 const token_indent_info &guard_tinfo,
13005 vec<tree> *chain)
13006 {
13007 tree statement;
13008 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
13009 location_t body_loc_after_labels = UNKNOWN_LOCATION;
13010 token_indent_info body_tinfo
13011 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13012
13013 if (if_p != NULL)
13014 *if_p = false;
13015
13016 /* Mark if () ; with a special NOP_EXPR. */
13017 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13018 {
13019 cp_lexer_consume_token (parser->lexer);
13020 statement = add_stmt (build_empty_stmt (body_loc));
13021
13022 if (guard_tinfo.keyword == RID_IF
13023 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
13024 warning_at (body_loc, OPT_Wempty_body,
13025 "suggest braces around empty body in an %<if%> statement");
13026 else if (guard_tinfo.keyword == RID_ELSE)
13027 warning_at (body_loc, OPT_Wempty_body,
13028 "suggest braces around empty body in an %<else%> statement");
13029 }
13030 /* if a compound is opened, we simply parse the statement directly. */
13031 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13032 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
13033 /* If the token is not a `{', then we must take special action. */
13034 else
13035 {
13036 /* Create a compound-statement. */
13037 statement = begin_compound_stmt (0);
13038 /* Parse the dependent-statement. */
13039 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
13040 &body_loc_after_labels);
13041 /* Finish the dummy compound-statement. */
13042 finish_compound_stmt (statement);
13043 }
13044
13045 token_indent_info next_tinfo
13046 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13047 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13048
13049 if (body_loc_after_labels != UNKNOWN_LOCATION
13050 && next_tinfo.type != CPP_SEMICOLON)
13051 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
13052 guard_tinfo.location, guard_tinfo.keyword);
13053
13054 /* Return the statement. */
13055 return statement;
13056 }
13057
13058 /* For some dependent statements (like `while (cond) statement'), we
13059 have already created a scope. Therefore, even if the dependent
13060 statement is a compound-statement, we do not want to create another
13061 scope. */
13062
13063 static void
13064 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
13065 const token_indent_info &guard_tinfo)
13066 {
13067 /* If the token is a `{', then we must take special action. */
13068 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13069 {
13070 token_indent_info body_tinfo
13071 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13072 location_t loc_after_labels = UNKNOWN_LOCATION;
13073
13074 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
13075 &loc_after_labels);
13076 token_indent_info next_tinfo
13077 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13078 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13079
13080 if (loc_after_labels != UNKNOWN_LOCATION
13081 && next_tinfo.type != CPP_SEMICOLON)
13082 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
13083 guard_tinfo.location,
13084 guard_tinfo.keyword);
13085 }
13086 else
13087 {
13088 /* Avoid calling cp_parser_compound_statement, so that we
13089 don't create a new scope. Do everything else by hand. */
13090 matching_braces braces;
13091 braces.require_open (parser);
13092 /* If the next keyword is `__label__' we have a label declaration. */
13093 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13094 cp_parser_label_declaration (parser);
13095 /* Parse an (optional) statement-seq. */
13096 cp_parser_statement_seq_opt (parser, NULL_TREE);
13097 braces.require_close (parser);
13098 }
13099 }
13100
13101 /* Declarations [gram.dcl.dcl] */
13102
13103 /* Parse an optional declaration-sequence.
13104
13105 declaration-seq:
13106 declaration
13107 declaration-seq declaration */
13108
13109 static void
13110 cp_parser_declaration_seq_opt (cp_parser* parser)
13111 {
13112 while (true)
13113 {
13114 cp_token *token = cp_lexer_peek_token (parser->lexer);
13115
13116 if (token->type == CPP_CLOSE_BRACE
13117 || token->type == CPP_EOF)
13118 break;
13119 else
13120 cp_parser_toplevel_declaration (parser);
13121 }
13122 }
13123
13124 /* Parse a declaration.
13125
13126 declaration:
13127 block-declaration
13128 function-definition
13129 template-declaration
13130 explicit-instantiation
13131 explicit-specialization
13132 linkage-specification
13133 namespace-definition
13134
13135 C++17:
13136 deduction-guide
13137
13138 GNU extension:
13139
13140 declaration:
13141 __extension__ declaration */
13142
13143 static void
13144 cp_parser_declaration (cp_parser* parser)
13145 {
13146 cp_token token1;
13147 cp_token token2;
13148 int saved_pedantic;
13149 void *p;
13150 tree attributes = NULL_TREE;
13151
13152 /* Check for the `__extension__' keyword. */
13153 if (cp_parser_extension_opt (parser, &saved_pedantic))
13154 {
13155 /* Parse the qualified declaration. */
13156 cp_parser_declaration (parser);
13157 /* Restore the PEDANTIC flag. */
13158 pedantic = saved_pedantic;
13159
13160 return;
13161 }
13162
13163 /* Try to figure out what kind of declaration is present. */
13164 token1 = *cp_lexer_peek_token (parser->lexer);
13165
13166 if (token1.type != CPP_EOF)
13167 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
13168 else
13169 {
13170 token2.type = CPP_EOF;
13171 token2.keyword = RID_MAX;
13172 }
13173
13174 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13175 p = obstack_alloc (&declarator_obstack, 0);
13176
13177 /* If the next token is `extern' and the following token is a string
13178 literal, then we have a linkage specification. */
13179 if (token1.keyword == RID_EXTERN
13180 && cp_parser_is_pure_string_literal (&token2))
13181 cp_parser_linkage_specification (parser);
13182 /* If the next token is `template', then we have either a template
13183 declaration, an explicit instantiation, or an explicit
13184 specialization. */
13185 else if (token1.keyword == RID_TEMPLATE)
13186 {
13187 /* `template <>' indicates a template specialization. */
13188 if (token2.type == CPP_LESS
13189 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13190 cp_parser_explicit_specialization (parser);
13191 /* `template <' indicates a template declaration. */
13192 else if (token2.type == CPP_LESS)
13193 cp_parser_template_declaration (parser, /*member_p=*/false);
13194 /* Anything else must be an explicit instantiation. */
13195 else
13196 cp_parser_explicit_instantiation (parser);
13197 }
13198 /* If the next token is `export', then we have a template
13199 declaration. */
13200 else if (token1.keyword == RID_EXPORT)
13201 cp_parser_template_declaration (parser, /*member_p=*/false);
13202 /* If the next token is `extern', 'static' or 'inline' and the one
13203 after that is `template', we have a GNU extended explicit
13204 instantiation directive. */
13205 else if (cp_parser_allow_gnu_extensions_p (parser)
13206 && (token1.keyword == RID_EXTERN
13207 || token1.keyword == RID_STATIC
13208 || token1.keyword == RID_INLINE)
13209 && token2.keyword == RID_TEMPLATE)
13210 cp_parser_explicit_instantiation (parser);
13211 /* If the next token is `namespace', check for a named or unnamed
13212 namespace definition. */
13213 else if (token1.keyword == RID_NAMESPACE
13214 && (/* A named namespace definition. */
13215 (token2.type == CPP_NAME
13216 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
13217 != CPP_EQ))
13218 || (token2.type == CPP_OPEN_SQUARE
13219 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
13220 == CPP_OPEN_SQUARE)
13221 /* An unnamed namespace definition. */
13222 || token2.type == CPP_OPEN_BRACE
13223 || token2.keyword == RID_ATTRIBUTE))
13224 cp_parser_namespace_definition (parser);
13225 /* An inline (associated) namespace definition. */
13226 else if (token1.keyword == RID_INLINE
13227 && token2.keyword == RID_NAMESPACE)
13228 cp_parser_namespace_definition (parser);
13229 /* Objective-C++ declaration/definition. */
13230 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
13231 cp_parser_objc_declaration (parser, NULL_TREE);
13232 else if (c_dialect_objc ()
13233 && token1.keyword == RID_ATTRIBUTE
13234 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
13235 cp_parser_objc_declaration (parser, attributes);
13236 /* At this point we may have a template declared by a concept
13237 introduction. */
13238 else if (flag_concepts
13239 && cp_parser_template_declaration_after_export (parser,
13240 /*member_p=*/false))
13241 /* We did. */;
13242 else
13243 /* Try to parse a block-declaration, or a function-definition. */
13244 cp_parser_block_declaration (parser, /*statement_p=*/false);
13245
13246 /* Free any declarators allocated. */
13247 obstack_free (&declarator_obstack, p);
13248 }
13249
13250 /* Parse a namespace-scope declaration. */
13251
13252 static void
13253 cp_parser_toplevel_declaration (cp_parser* parser)
13254 {
13255 cp_token *token = cp_lexer_peek_token (parser->lexer);
13256
13257 if (token->type == CPP_PRAGMA)
13258 /* A top-level declaration can consist solely of a #pragma. A
13259 nested declaration cannot, so this is done here and not in
13260 cp_parser_declaration. (A #pragma at block scope is
13261 handled in cp_parser_statement.) */
13262 cp_parser_pragma (parser, pragma_external, NULL);
13263 else if (token->type == CPP_SEMICOLON)
13264 {
13265 /* A declaration consisting of a single semicolon is
13266 invalid. Allow it unless we're being pedantic. */
13267 cp_lexer_consume_token (parser->lexer);
13268 if (!in_system_header_at (input_location))
13269 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
13270 }
13271 else
13272 /* Parse the declaration itself. */
13273 cp_parser_declaration (parser);
13274 }
13275
13276 /* Parse a block-declaration.
13277
13278 block-declaration:
13279 simple-declaration
13280 asm-definition
13281 namespace-alias-definition
13282 using-declaration
13283 using-directive
13284
13285 GNU Extension:
13286
13287 block-declaration:
13288 __extension__ block-declaration
13289
13290 C++0x Extension:
13291
13292 block-declaration:
13293 static_assert-declaration
13294
13295 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13296 part of a declaration-statement. */
13297
13298 static void
13299 cp_parser_block_declaration (cp_parser *parser,
13300 bool statement_p)
13301 {
13302 cp_token *token1;
13303 int saved_pedantic;
13304
13305 /* Check for the `__extension__' keyword. */
13306 if (cp_parser_extension_opt (parser, &saved_pedantic))
13307 {
13308 /* Parse the qualified declaration. */
13309 cp_parser_block_declaration (parser, statement_p);
13310 /* Restore the PEDANTIC flag. */
13311 pedantic = saved_pedantic;
13312
13313 return;
13314 }
13315
13316 /* Peek at the next token to figure out which kind of declaration is
13317 present. */
13318 token1 = cp_lexer_peek_token (parser->lexer);
13319
13320 /* If the next keyword is `asm', we have an asm-definition. */
13321 if (token1->keyword == RID_ASM)
13322 {
13323 if (statement_p)
13324 cp_parser_commit_to_tentative_parse (parser);
13325 cp_parser_asm_definition (parser);
13326 }
13327 /* If the next keyword is `namespace', we have a
13328 namespace-alias-definition. */
13329 else if (token1->keyword == RID_NAMESPACE)
13330 cp_parser_namespace_alias_definition (parser);
13331 /* If the next keyword is `using', we have a
13332 using-declaration, a using-directive, or an alias-declaration. */
13333 else if (token1->keyword == RID_USING)
13334 {
13335 cp_token *token2;
13336
13337 if (statement_p)
13338 cp_parser_commit_to_tentative_parse (parser);
13339 /* If the token after `using' is `namespace', then we have a
13340 using-directive. */
13341 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13342 if (token2->keyword == RID_NAMESPACE)
13343 cp_parser_using_directive (parser);
13344 /* If the second token after 'using' is '=', then we have an
13345 alias-declaration. */
13346 else if (cxx_dialect >= cxx11
13347 && token2->type == CPP_NAME
13348 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13349 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13350 cp_parser_alias_declaration (parser);
13351 /* Otherwise, it's a using-declaration. */
13352 else
13353 cp_parser_using_declaration (parser,
13354 /*access_declaration_p=*/false);
13355 }
13356 /* If the next keyword is `__label__' we have a misplaced label
13357 declaration. */
13358 else if (token1->keyword == RID_LABEL)
13359 {
13360 cp_lexer_consume_token (parser->lexer);
13361 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13362 cp_parser_skip_to_end_of_statement (parser);
13363 /* If the next token is now a `;', consume it. */
13364 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13365 cp_lexer_consume_token (parser->lexer);
13366 }
13367 /* If the next token is `static_assert' we have a static assertion. */
13368 else if (token1->keyword == RID_STATIC_ASSERT)
13369 cp_parser_static_assert (parser, /*member_p=*/false);
13370 /* Anything else must be a simple-declaration. */
13371 else
13372 cp_parser_simple_declaration (parser, !statement_p,
13373 /*maybe_range_for_decl*/NULL);
13374 }
13375
13376 /* Parse a simple-declaration.
13377
13378 simple-declaration:
13379 decl-specifier-seq [opt] init-declarator-list [opt] ;
13380 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13381 brace-or-equal-initializer ;
13382
13383 init-declarator-list:
13384 init-declarator
13385 init-declarator-list , init-declarator
13386
13387 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13388 function-definition as a simple-declaration.
13389
13390 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13391 parsed declaration if it is an uninitialized single declarator not followed
13392 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13393 if present, will not be consumed. */
13394
13395 static void
13396 cp_parser_simple_declaration (cp_parser* parser,
13397 bool function_definition_allowed_p,
13398 tree *maybe_range_for_decl)
13399 {
13400 cp_decl_specifier_seq decl_specifiers;
13401 int declares_class_or_enum;
13402 bool saw_declarator;
13403 location_t comma_loc = UNKNOWN_LOCATION;
13404 location_t init_loc = UNKNOWN_LOCATION;
13405
13406 if (maybe_range_for_decl)
13407 *maybe_range_for_decl = NULL_TREE;
13408
13409 /* Defer access checks until we know what is being declared; the
13410 checks for names appearing in the decl-specifier-seq should be
13411 done as if we were in the scope of the thing being declared. */
13412 push_deferring_access_checks (dk_deferred);
13413
13414 /* Parse the decl-specifier-seq. We have to keep track of whether
13415 or not the decl-specifier-seq declares a named class or
13416 enumeration type, since that is the only case in which the
13417 init-declarator-list is allowed to be empty.
13418
13419 [dcl.dcl]
13420
13421 In a simple-declaration, the optional init-declarator-list can be
13422 omitted only when declaring a class or enumeration, that is when
13423 the decl-specifier-seq contains either a class-specifier, an
13424 elaborated-type-specifier, or an enum-specifier. */
13425 cp_parser_decl_specifier_seq (parser,
13426 CP_PARSER_FLAGS_OPTIONAL,
13427 &decl_specifiers,
13428 &declares_class_or_enum);
13429 /* We no longer need to defer access checks. */
13430 stop_deferring_access_checks ();
13431
13432 /* In a block scope, a valid declaration must always have a
13433 decl-specifier-seq. By not trying to parse declarators, we can
13434 resolve the declaration/expression ambiguity more quickly. */
13435 if (!function_definition_allowed_p
13436 && !decl_specifiers.any_specifiers_p)
13437 {
13438 cp_parser_error (parser, "expected declaration");
13439 goto done;
13440 }
13441
13442 /* If the next two tokens are both identifiers, the code is
13443 erroneous. The usual cause of this situation is code like:
13444
13445 T t;
13446
13447 where "T" should name a type -- but does not. */
13448 if (!decl_specifiers.any_type_specifiers_p
13449 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13450 {
13451 /* If parsing tentatively, we should commit; we really are
13452 looking at a declaration. */
13453 cp_parser_commit_to_tentative_parse (parser);
13454 /* Give up. */
13455 goto done;
13456 }
13457
13458 cp_parser_maybe_commit_to_declaration (parser,
13459 decl_specifiers.any_specifiers_p);
13460
13461 /* Look for C++17 decomposition declaration. */
13462 for (size_t n = 1; ; n++)
13463 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13464 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13465 continue;
13466 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13467 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13468 && decl_specifiers.any_specifiers_p)
13469 {
13470 tree decl
13471 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13472 maybe_range_for_decl,
13473 &init_loc);
13474
13475 /* The next token should be either a `,' or a `;'. */
13476 cp_token *token = cp_lexer_peek_token (parser->lexer);
13477 /* If it's a `;', we are done. */
13478 if (token->type == CPP_SEMICOLON)
13479 goto finish;
13480 else if (maybe_range_for_decl)
13481 {
13482 if (*maybe_range_for_decl == NULL_TREE)
13483 *maybe_range_for_decl = error_mark_node;
13484 goto finish;
13485 }
13486 /* Anything else is an error. */
13487 else
13488 {
13489 /* If we have already issued an error message we don't need
13490 to issue another one. */
13491 if ((decl != error_mark_node
13492 && DECL_INITIAL (decl) != error_mark_node)
13493 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13494 cp_parser_error (parser, "expected %<,%> or %<;%>");
13495 /* Skip tokens until we reach the end of the statement. */
13496 cp_parser_skip_to_end_of_statement (parser);
13497 /* If the next token is now a `;', consume it. */
13498 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13499 cp_lexer_consume_token (parser->lexer);
13500 goto done;
13501 }
13502 }
13503 else
13504 break;
13505
13506 tree last_type;
13507 bool auto_specifier_p;
13508 /* NULL_TREE if both variable and function declaration are allowed,
13509 error_mark_node if function declaration are not allowed and
13510 a FUNCTION_DECL that should be diagnosed if it is followed by
13511 variable declarations. */
13512 tree auto_function_declaration;
13513
13514 last_type = NULL_TREE;
13515 auto_specifier_p
13516 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13517 auto_function_declaration = NULL_TREE;
13518
13519 /* Keep going until we hit the `;' at the end of the simple
13520 declaration. */
13521 saw_declarator = false;
13522 while (cp_lexer_next_token_is_not (parser->lexer,
13523 CPP_SEMICOLON))
13524 {
13525 cp_token *token;
13526 bool function_definition_p;
13527 tree decl;
13528 tree auto_result = NULL_TREE;
13529
13530 if (saw_declarator)
13531 {
13532 /* If we are processing next declarator, comma is expected */
13533 token = cp_lexer_peek_token (parser->lexer);
13534 gcc_assert (token->type == CPP_COMMA);
13535 cp_lexer_consume_token (parser->lexer);
13536 if (maybe_range_for_decl)
13537 {
13538 *maybe_range_for_decl = error_mark_node;
13539 if (comma_loc == UNKNOWN_LOCATION)
13540 comma_loc = token->location;
13541 }
13542 }
13543 else
13544 saw_declarator = true;
13545
13546 /* Parse the init-declarator. */
13547 decl = cp_parser_init_declarator (parser,
13548 CP_PARSER_FLAGS_NONE,
13549 &decl_specifiers,
13550 /*checks=*/NULL,
13551 function_definition_allowed_p,
13552 /*member_p=*/false,
13553 declares_class_or_enum,
13554 &function_definition_p,
13555 maybe_range_for_decl,
13556 &init_loc,
13557 &auto_result);
13558 /* If an error occurred while parsing tentatively, exit quickly.
13559 (That usually happens when in the body of a function; each
13560 statement is treated as a declaration-statement until proven
13561 otherwise.) */
13562 if (cp_parser_error_occurred (parser))
13563 goto done;
13564
13565 if (auto_specifier_p && cxx_dialect >= cxx14)
13566 {
13567 /* If the init-declarator-list contains more than one
13568 init-declarator, they shall all form declarations of
13569 variables. */
13570 if (auto_function_declaration == NULL_TREE)
13571 auto_function_declaration
13572 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13573 else if (TREE_CODE (decl) == FUNCTION_DECL
13574 || auto_function_declaration != error_mark_node)
13575 {
13576 error_at (decl_specifiers.locations[ds_type_spec],
13577 "non-variable %qD in declaration with more than one "
13578 "declarator with placeholder type",
13579 TREE_CODE (decl) == FUNCTION_DECL
13580 ? decl : auto_function_declaration);
13581 auto_function_declaration = error_mark_node;
13582 }
13583 }
13584
13585 if (auto_result
13586 && (!processing_template_decl || !type_uses_auto (auto_result)))
13587 {
13588 if (last_type
13589 && last_type != error_mark_node
13590 && !same_type_p (auto_result, last_type))
13591 {
13592 /* If the list of declarators contains more than one declarator,
13593 the type of each declared variable is determined as described
13594 above. If the type deduced for the template parameter U is not
13595 the same in each deduction, the program is ill-formed. */
13596 error_at (decl_specifiers.locations[ds_type_spec],
13597 "inconsistent deduction for %qT: %qT and then %qT",
13598 decl_specifiers.type, last_type, auto_result);
13599 last_type = error_mark_node;
13600 }
13601 else
13602 last_type = auto_result;
13603 }
13604
13605 /* Handle function definitions specially. */
13606 if (function_definition_p)
13607 {
13608 /* If the next token is a `,', then we are probably
13609 processing something like:
13610
13611 void f() {}, *p;
13612
13613 which is erroneous. */
13614 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13615 {
13616 cp_token *token = cp_lexer_peek_token (parser->lexer);
13617 error_at (token->location,
13618 "mixing"
13619 " declarations and function-definitions is forbidden");
13620 }
13621 /* Otherwise, we're done with the list of declarators. */
13622 else
13623 {
13624 pop_deferring_access_checks ();
13625 return;
13626 }
13627 }
13628 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13629 *maybe_range_for_decl = decl;
13630 /* The next token should be either a `,' or a `;'. */
13631 token = cp_lexer_peek_token (parser->lexer);
13632 /* If it's a `,', there are more declarators to come. */
13633 if (token->type == CPP_COMMA)
13634 /* will be consumed next time around */;
13635 /* If it's a `;', we are done. */
13636 else if (token->type == CPP_SEMICOLON)
13637 break;
13638 else if (maybe_range_for_decl)
13639 {
13640 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13641 permerror (decl_specifiers.locations[ds_type_spec],
13642 "types may not be defined in a for-range-declaration");
13643 break;
13644 }
13645 /* Anything else is an error. */
13646 else
13647 {
13648 /* If we have already issued an error message we don't need
13649 to issue another one. */
13650 if ((decl != error_mark_node
13651 && DECL_INITIAL (decl) != error_mark_node)
13652 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13653 cp_parser_error (parser, "expected %<,%> or %<;%>");
13654 /* Skip tokens until we reach the end of the statement. */
13655 cp_parser_skip_to_end_of_statement (parser);
13656 /* If the next token is now a `;', consume it. */
13657 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13658 cp_lexer_consume_token (parser->lexer);
13659 goto done;
13660 }
13661 /* After the first time around, a function-definition is not
13662 allowed -- even if it was OK at first. For example:
13663
13664 int i, f() {}
13665
13666 is not valid. */
13667 function_definition_allowed_p = false;
13668 }
13669
13670 /* Issue an error message if no declarators are present, and the
13671 decl-specifier-seq does not itself declare a class or
13672 enumeration: [dcl.dcl]/3. */
13673 if (!saw_declarator)
13674 {
13675 if (cp_parser_declares_only_class_p (parser))
13676 {
13677 if (!declares_class_or_enum
13678 && decl_specifiers.type
13679 && OVERLOAD_TYPE_P (decl_specifiers.type))
13680 /* Ensure an error is issued anyway when finish_decltype_type,
13681 called via cp_parser_decl_specifier_seq, returns a class or
13682 an enumeration (c++/51786). */
13683 decl_specifiers.type = NULL_TREE;
13684 shadow_tag (&decl_specifiers);
13685 }
13686 /* Perform any deferred access checks. */
13687 perform_deferred_access_checks (tf_warning_or_error);
13688 }
13689
13690 /* Consume the `;'. */
13691 finish:
13692 if (!maybe_range_for_decl)
13693 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13694 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13695 {
13696 if (init_loc != UNKNOWN_LOCATION)
13697 error_at (init_loc, "initializer in range-based %<for%> loop");
13698 if (comma_loc != UNKNOWN_LOCATION)
13699 error_at (comma_loc,
13700 "multiple declarations in range-based %<for%> loop");
13701 }
13702
13703 done:
13704 pop_deferring_access_checks ();
13705 }
13706
13707 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13708 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13709 initializer ; */
13710
13711 static tree
13712 cp_parser_decomposition_declaration (cp_parser *parser,
13713 cp_decl_specifier_seq *decl_specifiers,
13714 tree *maybe_range_for_decl,
13715 location_t *init_loc)
13716 {
13717 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13718 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13719 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13720
13721 /* Parse the identifier-list. */
13722 auto_vec<cp_expr, 10> v;
13723 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13724 while (true)
13725 {
13726 cp_expr e = cp_parser_identifier (parser);
13727 if (e.get_value () == error_mark_node)
13728 break;
13729 v.safe_push (e);
13730 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13731 break;
13732 cp_lexer_consume_token (parser->lexer);
13733 }
13734
13735 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13736 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13737 {
13738 end_loc = UNKNOWN_LOCATION;
13739 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13740 false);
13741 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13742 cp_lexer_consume_token (parser->lexer);
13743 else
13744 {
13745 cp_parser_skip_to_end_of_statement (parser);
13746 return error_mark_node;
13747 }
13748 }
13749
13750 if (cxx_dialect < cxx17)
13751 pedwarn (loc, 0, "structured bindings only available with "
13752 "%<-std=c++17%> or %<-std=gnu++17%>");
13753
13754 tree pushed_scope;
13755 cp_declarator *declarator = make_declarator (cdk_decomp);
13756 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13757 declarator->id_loc = loc;
13758 if (ref_qual != REF_QUAL_NONE)
13759 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13760 ref_qual == REF_QUAL_RVALUE,
13761 NULL_TREE);
13762 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13763 NULL_TREE, decl_specifiers->attributes,
13764 &pushed_scope);
13765 tree orig_decl = decl;
13766
13767 unsigned int i;
13768 cp_expr e;
13769 cp_decl_specifier_seq decl_specs;
13770 clear_decl_specs (&decl_specs);
13771 decl_specs.type = make_auto ();
13772 tree prev = decl;
13773 FOR_EACH_VEC_ELT (v, i, e)
13774 {
13775 if (i == 0)
13776 declarator = make_id_declarator (NULL_TREE, e.get_value (),
13777 sfk_none, e.get_location ());
13778 else
13779 {
13780 declarator->u.id.unqualified_name = e.get_value ();
13781 declarator->id_loc = e.get_location ();
13782 }
13783 tree elt_pushed_scope;
13784 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13785 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13786 if (decl2 == error_mark_node)
13787 decl = error_mark_node;
13788 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13789 {
13790 /* Ensure we've diagnosed redeclaration if we aren't creating
13791 a new VAR_DECL. */
13792 gcc_assert (errorcount);
13793 decl = error_mark_node;
13794 }
13795 else
13796 prev = decl2;
13797 if (elt_pushed_scope)
13798 pop_scope (elt_pushed_scope);
13799 }
13800
13801 if (v.is_empty ())
13802 {
13803 error_at (loc, "empty structured binding declaration");
13804 decl = error_mark_node;
13805 }
13806
13807 if (maybe_range_for_decl == NULL
13808 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13809 {
13810 bool non_constant_p = false, is_direct_init = false;
13811 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13812 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13813 &non_constant_p);
13814 if (initializer == NULL_TREE
13815 || (TREE_CODE (initializer) == TREE_LIST
13816 && TREE_CHAIN (initializer))
13817 || (is_direct_init
13818 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13819 && CONSTRUCTOR_NELTS (initializer) != 1))
13820 {
13821 error_at (loc, "invalid initializer for structured binding "
13822 "declaration");
13823 initializer = error_mark_node;
13824 }
13825
13826 if (decl != error_mark_node)
13827 {
13828 cp_maybe_mangle_decomp (decl, prev, v.length ());
13829 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13830 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13831 cp_finish_decomp (decl, prev, v.length ());
13832 }
13833 }
13834 else if (decl != error_mark_node)
13835 {
13836 *maybe_range_for_decl = prev;
13837 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13838 the underlying DECL. */
13839 cp_finish_decomp (decl, prev, v.length ());
13840 }
13841
13842 if (pushed_scope)
13843 pop_scope (pushed_scope);
13844
13845 if (decl == error_mark_node && DECL_P (orig_decl))
13846 {
13847 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13848 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13849 }
13850
13851 return decl;
13852 }
13853
13854 /* Parse a decl-specifier-seq.
13855
13856 decl-specifier-seq:
13857 decl-specifier-seq [opt] decl-specifier
13858 decl-specifier attribute-specifier-seq [opt] (C++11)
13859
13860 decl-specifier:
13861 storage-class-specifier
13862 type-specifier
13863 function-specifier
13864 friend
13865 typedef
13866
13867 GNU Extension:
13868
13869 decl-specifier:
13870 attributes
13871
13872 Concepts Extension:
13873
13874 decl-specifier:
13875 concept
13876
13877 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13878
13879 The parser flags FLAGS is used to control type-specifier parsing.
13880
13881 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13882 flags:
13883
13884 1: one of the decl-specifiers is an elaborated-type-specifier
13885 (i.e., a type declaration)
13886 2: one of the decl-specifiers is an enum-specifier or a
13887 class-specifier (i.e., a type definition)
13888
13889 */
13890
13891 static void
13892 cp_parser_decl_specifier_seq (cp_parser* parser,
13893 cp_parser_flags flags,
13894 cp_decl_specifier_seq *decl_specs,
13895 int* declares_class_or_enum)
13896 {
13897 bool constructor_possible_p = !parser->in_declarator_p;
13898 bool found_decl_spec = false;
13899 cp_token *start_token = NULL;
13900 cp_decl_spec ds;
13901
13902 /* Clear DECL_SPECS. */
13903 clear_decl_specs (decl_specs);
13904
13905 /* Assume no class or enumeration type is declared. */
13906 *declares_class_or_enum = 0;
13907
13908 /* Keep reading specifiers until there are no more to read. */
13909 while (true)
13910 {
13911 bool constructor_p;
13912 cp_token *token;
13913 ds = ds_last;
13914
13915 /* Peek at the next token. */
13916 token = cp_lexer_peek_token (parser->lexer);
13917
13918 /* Save the first token of the decl spec list for error
13919 reporting. */
13920 if (!start_token)
13921 start_token = token;
13922 /* Handle attributes. */
13923 if (cp_next_tokens_can_be_attribute_p (parser))
13924 {
13925 /* Parse the attributes. */
13926 tree attrs = cp_parser_attributes_opt (parser);
13927
13928 /* In a sequence of declaration specifiers, c++11 attributes
13929 appertain to the type that precede them. In that case
13930 [dcl.spec]/1 says:
13931
13932 The attribute-specifier-seq affects the type only for
13933 the declaration it appears in, not other declarations
13934 involving the same type.
13935
13936 But for now let's force the user to position the
13937 attribute either at the beginning of the declaration or
13938 after the declarator-id, which would clearly mean that it
13939 applies to the declarator. */
13940 if (cxx11_attribute_p (attrs))
13941 {
13942 if (!found_decl_spec)
13943 /* The c++11 attribute is at the beginning of the
13944 declaration. It appertains to the entity being
13945 declared. */;
13946 else
13947 {
13948 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13949 {
13950 /* This is an attribute following a
13951 class-specifier. */
13952 if (decl_specs->type_definition_p)
13953 warn_misplaced_attr_for_class_type (token->location,
13954 decl_specs->type);
13955 attrs = NULL_TREE;
13956 }
13957 else
13958 {
13959 decl_specs->std_attributes
13960 = attr_chainon (decl_specs->std_attributes, attrs);
13961 if (decl_specs->locations[ds_std_attribute] == 0)
13962 decl_specs->locations[ds_std_attribute] = token->location;
13963 }
13964 continue;
13965 }
13966 }
13967
13968 decl_specs->attributes
13969 = attr_chainon (decl_specs->attributes, attrs);
13970 if (decl_specs->locations[ds_attribute] == 0)
13971 decl_specs->locations[ds_attribute] = token->location;
13972 continue;
13973 }
13974 /* Assume we will find a decl-specifier keyword. */
13975 found_decl_spec = true;
13976 /* If the next token is an appropriate keyword, we can simply
13977 add it to the list. */
13978 switch (token->keyword)
13979 {
13980 /* decl-specifier:
13981 friend
13982 constexpr */
13983 case RID_FRIEND:
13984 if (!at_class_scope_p ())
13985 {
13986 gcc_rich_location richloc (token->location);
13987 richloc.add_fixit_remove ();
13988 error_at (&richloc, "%<friend%> used outside of class");
13989 cp_lexer_purge_token (parser->lexer);
13990 }
13991 else
13992 {
13993 ds = ds_friend;
13994 /* Consume the token. */
13995 cp_lexer_consume_token (parser->lexer);
13996 }
13997 break;
13998
13999 case RID_CONSTEXPR:
14000 ds = ds_constexpr;
14001 cp_lexer_consume_token (parser->lexer);
14002 break;
14003
14004 case RID_CONCEPT:
14005 ds = ds_concept;
14006 cp_lexer_consume_token (parser->lexer);
14007 /* In C++20 a concept definition is just 'concept name = expr;'
14008 Support that syntax by pretending we've seen 'bool'. */
14009 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14010 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
14011 {
14012 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
14013 token, /*type_definition*/false);
14014 decl_specs->any_type_specifiers_p = true;
14015 }
14016 break;
14017
14018 /* function-specifier:
14019 inline
14020 virtual
14021 explicit */
14022 case RID_INLINE:
14023 case RID_VIRTUAL:
14024 case RID_EXPLICIT:
14025 cp_parser_function_specifier_opt (parser, decl_specs);
14026 break;
14027
14028 /* decl-specifier:
14029 typedef */
14030 case RID_TYPEDEF:
14031 ds = ds_typedef;
14032 /* Consume the token. */
14033 cp_lexer_consume_token (parser->lexer);
14034 /* A constructor declarator cannot appear in a typedef. */
14035 constructor_possible_p = false;
14036 /* The "typedef" keyword can only occur in a declaration; we
14037 may as well commit at this point. */
14038 cp_parser_commit_to_tentative_parse (parser);
14039
14040 if (decl_specs->storage_class != sc_none)
14041 decl_specs->conflicting_specifiers_p = true;
14042 break;
14043
14044 /* storage-class-specifier:
14045 auto
14046 register
14047 static
14048 extern
14049 mutable
14050
14051 GNU Extension:
14052 thread */
14053 case RID_AUTO:
14054 if (cxx_dialect == cxx98)
14055 {
14056 /* Consume the token. */
14057 cp_lexer_consume_token (parser->lexer);
14058
14059 /* Complain about `auto' as a storage specifier, if
14060 we're complaining about C++0x compatibility. */
14061 gcc_rich_location richloc (token->location);
14062 richloc.add_fixit_remove ();
14063 warning_at (&richloc, OPT_Wc__11_compat,
14064 "%<auto%> changes meaning in C++11; "
14065 "please remove it");
14066
14067 /* Set the storage class anyway. */
14068 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
14069 token);
14070 }
14071 else
14072 /* C++0x auto type-specifier. */
14073 found_decl_spec = false;
14074 break;
14075
14076 case RID_REGISTER:
14077 case RID_STATIC:
14078 case RID_EXTERN:
14079 case RID_MUTABLE:
14080 /* Consume the token. */
14081 cp_lexer_consume_token (parser->lexer);
14082 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
14083 token);
14084 break;
14085 case RID_THREAD:
14086 /* Consume the token. */
14087 ds = ds_thread;
14088 cp_lexer_consume_token (parser->lexer);
14089 break;
14090
14091 default:
14092 /* We did not yet find a decl-specifier yet. */
14093 found_decl_spec = false;
14094 break;
14095 }
14096
14097 if (found_decl_spec
14098 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
14099 && token->keyword != RID_CONSTEXPR)
14100 error ("%<decl-specifier%> invalid in condition");
14101
14102 if (found_decl_spec
14103 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14104 && token->keyword != RID_MUTABLE
14105 && token->keyword != RID_CONSTEXPR)
14106 error_at (token->location, "%qD invalid in lambda",
14107 ridpointers[token->keyword]);
14108
14109 if (ds != ds_last)
14110 set_and_check_decl_spec_loc (decl_specs, ds, token);
14111
14112 /* Constructors are a special case. The `S' in `S()' is not a
14113 decl-specifier; it is the beginning of the declarator. */
14114 constructor_p
14115 = (!found_decl_spec
14116 && constructor_possible_p
14117 && (cp_parser_constructor_declarator_p
14118 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
14119 ds_friend))));
14120
14121 /* If we don't have a DECL_SPEC yet, then we must be looking at
14122 a type-specifier. */
14123 if (!found_decl_spec && !constructor_p)
14124 {
14125 int decl_spec_declares_class_or_enum;
14126 bool is_cv_qualifier;
14127 tree type_spec;
14128
14129 type_spec
14130 = cp_parser_type_specifier (parser, flags,
14131 decl_specs,
14132 /*is_declaration=*/true,
14133 &decl_spec_declares_class_or_enum,
14134 &is_cv_qualifier);
14135 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
14136
14137 /* If this type-specifier referenced a user-defined type
14138 (a typedef, class-name, etc.), then we can't allow any
14139 more such type-specifiers henceforth.
14140
14141 [dcl.spec]
14142
14143 The longest sequence of decl-specifiers that could
14144 possibly be a type name is taken as the
14145 decl-specifier-seq of a declaration. The sequence shall
14146 be self-consistent as described below.
14147
14148 [dcl.type]
14149
14150 As a general rule, at most one type-specifier is allowed
14151 in the complete decl-specifier-seq of a declaration. The
14152 only exceptions are the following:
14153
14154 -- const or volatile can be combined with any other
14155 type-specifier.
14156
14157 -- signed or unsigned can be combined with char, long,
14158 short, or int.
14159
14160 -- ..
14161
14162 Example:
14163
14164 typedef char* Pc;
14165 void g (const int Pc);
14166
14167 Here, Pc is *not* part of the decl-specifier seq; it's
14168 the declarator. Therefore, once we see a type-specifier
14169 (other than a cv-qualifier), we forbid any additional
14170 user-defined types. We *do* still allow things like `int
14171 int' to be considered a decl-specifier-seq, and issue the
14172 error message later. */
14173 if (type_spec && !is_cv_qualifier)
14174 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14175 /* A constructor declarator cannot follow a type-specifier. */
14176 if (type_spec)
14177 {
14178 constructor_possible_p = false;
14179 found_decl_spec = true;
14180 if (!is_cv_qualifier)
14181 decl_specs->any_type_specifiers_p = true;
14182
14183 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
14184 error_at (token->location, "type-specifier invalid in lambda");
14185 }
14186 }
14187
14188 /* If we still do not have a DECL_SPEC, then there are no more
14189 decl-specifiers. */
14190 if (!found_decl_spec)
14191 break;
14192
14193 decl_specs->any_specifiers_p = true;
14194 /* After we see one decl-specifier, further decl-specifiers are
14195 always optional. */
14196 flags |= CP_PARSER_FLAGS_OPTIONAL;
14197 }
14198
14199 /* Don't allow a friend specifier with a class definition. */
14200 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
14201 && (*declares_class_or_enum & 2))
14202 error_at (decl_specs->locations[ds_friend],
14203 "class definition may not be declared a friend");
14204 }
14205
14206 /* Parse an (optional) storage-class-specifier.
14207
14208 storage-class-specifier:
14209 auto
14210 register
14211 static
14212 extern
14213 mutable
14214
14215 GNU Extension:
14216
14217 storage-class-specifier:
14218 thread
14219
14220 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14221
14222 static tree
14223 cp_parser_storage_class_specifier_opt (cp_parser* parser)
14224 {
14225 switch (cp_lexer_peek_token (parser->lexer)->keyword)
14226 {
14227 case RID_AUTO:
14228 if (cxx_dialect != cxx98)
14229 return NULL_TREE;
14230 /* Fall through for C++98. */
14231 gcc_fallthrough ();
14232
14233 case RID_REGISTER:
14234 case RID_STATIC:
14235 case RID_EXTERN:
14236 case RID_MUTABLE:
14237 case RID_THREAD:
14238 /* Consume the token. */
14239 return cp_lexer_consume_token (parser->lexer)->u.value;
14240
14241 default:
14242 return NULL_TREE;
14243 }
14244 }
14245
14246 /* Parse an (optional) function-specifier.
14247
14248 function-specifier:
14249 inline
14250 virtual
14251 explicit
14252
14253 C++2A Extension:
14254 explicit(constant-expression)
14255
14256 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14257 Updates DECL_SPECS, if it is non-NULL. */
14258
14259 static tree
14260 cp_parser_function_specifier_opt (cp_parser* parser,
14261 cp_decl_specifier_seq *decl_specs)
14262 {
14263 cp_token *token = cp_lexer_peek_token (parser->lexer);
14264 switch (token->keyword)
14265 {
14266 case RID_INLINE:
14267 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
14268 break;
14269
14270 case RID_VIRTUAL:
14271 /* 14.5.2.3 [temp.mem]
14272
14273 A member function template shall not be virtual. */
14274 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14275 && current_class_type)
14276 error_at (token->location, "templates may not be %<virtual%>");
14277 else
14278 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
14279 break;
14280
14281 case RID_EXPLICIT:
14282 {
14283 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
14284 /* If we see '(', it's C++20 explicit(bool). */
14285 tree expr;
14286 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14287 {
14288 matching_parens parens;
14289 parens.consume_open (parser);
14290
14291 /* New types are not allowed in an explicit-specifier. */
14292 const char *saved_message
14293 = parser->type_definition_forbidden_message;
14294 parser->type_definition_forbidden_message
14295 = G_("types may not be defined in explicit-specifier");
14296
14297 if (cxx_dialect < cxx2a)
14298 pedwarn (token->location, 0,
14299 "%<explicit(bool)%> only available with %<-std=c++2a%> "
14300 "or %<-std=gnu++2a%>");
14301
14302 /* Parse the constant-expression. */
14303 expr = cp_parser_constant_expression (parser);
14304
14305 /* Restore the saved message. */
14306 parser->type_definition_forbidden_message = saved_message;
14307 parens.require_close (parser);
14308 }
14309 else
14310 /* The explicit-specifier explicit without a constant-expression is
14311 equivalent to the explicit-specifier explicit(true). */
14312 expr = boolean_true_node;
14313
14314 /* [dcl.fct.spec]
14315 "the constant-expression, if supplied, shall be a contextually
14316 converted constant expression of type bool." */
14317 expr = build_explicit_specifier (expr, tf_warning_or_error);
14318 /* We could evaluate it -- mark the decl as appropriate. */
14319 if (expr == boolean_true_node)
14320 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14321 else if (expr == boolean_false_node)
14322 /* Don't mark the decl as explicit. */;
14323 else if (decl_specs)
14324 /* The expression was value-dependent. Remember it so that we can
14325 substitute it later. */
14326 decl_specs->explicit_specifier = expr;
14327 return id;
14328 }
14329
14330 default:
14331 return NULL_TREE;
14332 }
14333
14334 /* Consume the token. */
14335 return cp_lexer_consume_token (parser->lexer)->u.value;
14336 }
14337
14338 /* Parse a linkage-specification.
14339
14340 linkage-specification:
14341 extern string-literal { declaration-seq [opt] }
14342 extern string-literal declaration */
14343
14344 static void
14345 cp_parser_linkage_specification (cp_parser* parser)
14346 {
14347 tree linkage;
14348
14349 /* Look for the `extern' keyword. */
14350 cp_token *extern_token
14351 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14352
14353 /* Look for the string-literal. */
14354 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14355 linkage = cp_parser_string_literal (parser, false, false);
14356
14357 /* Transform the literal into an identifier. If the literal is a
14358 wide-character string, or contains embedded NULs, then we can't
14359 handle it as the user wants. */
14360 if (strlen (TREE_STRING_POINTER (linkage))
14361 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14362 {
14363 cp_parser_error (parser, "invalid linkage-specification");
14364 /* Assume C++ linkage. */
14365 linkage = lang_name_cplusplus;
14366 }
14367 else
14368 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14369
14370 /* We're now using the new linkage. */
14371 push_lang_context (linkage);
14372
14373 /* Preserve the location of the the innermost linkage specification,
14374 tracking the locations of nested specifications via a local. */
14375 location_t saved_location
14376 = parser->innermost_linkage_specification_location;
14377 /* Construct a location ranging from the start of the "extern" to
14378 the end of the string-literal, with the caret at the start, e.g.:
14379 extern "C" {
14380 ^~~~~~~~~~
14381 */
14382 parser->innermost_linkage_specification_location
14383 = make_location (extern_token->location,
14384 extern_token->location,
14385 get_finish (string_token->location));
14386
14387 /* If the next token is a `{', then we're using the first
14388 production. */
14389 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14390 {
14391 cp_ensure_no_omp_declare_simd (parser);
14392 cp_ensure_no_oacc_routine (parser);
14393
14394 /* Consume the `{' token. */
14395 matching_braces braces;
14396 braces.consume_open (parser);
14397 /* Parse the declarations. */
14398 cp_parser_declaration_seq_opt (parser);
14399 /* Look for the closing `}'. */
14400 braces.require_close (parser);
14401 }
14402 /* Otherwise, there's just one declaration. */
14403 else
14404 {
14405 bool saved_in_unbraced_linkage_specification_p;
14406
14407 saved_in_unbraced_linkage_specification_p
14408 = parser->in_unbraced_linkage_specification_p;
14409 parser->in_unbraced_linkage_specification_p = true;
14410 cp_parser_declaration (parser);
14411 parser->in_unbraced_linkage_specification_p
14412 = saved_in_unbraced_linkage_specification_p;
14413 }
14414
14415 /* We're done with the linkage-specification. */
14416 pop_lang_context ();
14417
14418 /* Restore location of parent linkage specification, if any. */
14419 parser->innermost_linkage_specification_location = saved_location;
14420 }
14421
14422 /* Parse a static_assert-declaration.
14423
14424 static_assert-declaration:
14425 static_assert ( constant-expression , string-literal ) ;
14426 static_assert ( constant-expression ) ; (C++17)
14427
14428 If MEMBER_P, this static_assert is a class member. */
14429
14430 static void
14431 cp_parser_static_assert(cp_parser *parser, bool member_p)
14432 {
14433 cp_expr condition;
14434 location_t token_loc;
14435 tree message;
14436 bool dummy;
14437
14438 /* Peek at the `static_assert' token so we can keep track of exactly
14439 where the static assertion started. */
14440 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14441
14442 /* Look for the `static_assert' keyword. */
14443 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14444 RT_STATIC_ASSERT))
14445 return;
14446
14447 /* We know we are in a static assertion; commit to any tentative
14448 parse. */
14449 if (cp_parser_parsing_tentatively (parser))
14450 cp_parser_commit_to_tentative_parse (parser);
14451
14452 /* Parse the `(' starting the static assertion condition. */
14453 matching_parens parens;
14454 parens.require_open (parser);
14455
14456 /* Parse the constant-expression. Allow a non-constant expression
14457 here in order to give better diagnostics in finish_static_assert. */
14458 condition =
14459 cp_parser_constant_expression (parser,
14460 /*allow_non_constant_p=*/true,
14461 /*non_constant_p=*/&dummy);
14462
14463 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14464 {
14465 if (cxx_dialect < cxx17)
14466 pedwarn (input_location, OPT_Wpedantic,
14467 "%<static_assert%> without a message "
14468 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
14469 /* Eat the ')' */
14470 cp_lexer_consume_token (parser->lexer);
14471 message = build_string (1, "");
14472 TREE_TYPE (message) = char_array_type_node;
14473 fix_string_type (message);
14474 }
14475 else
14476 {
14477 /* Parse the separating `,'. */
14478 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14479
14480 /* Parse the string-literal message. */
14481 message = cp_parser_string_literal (parser,
14482 /*translate=*/false,
14483 /*wide_ok=*/true);
14484
14485 /* A `)' completes the static assertion. */
14486 if (!parens.require_close (parser))
14487 cp_parser_skip_to_closing_parenthesis (parser,
14488 /*recovering=*/true,
14489 /*or_comma=*/false,
14490 /*consume_paren=*/true);
14491 }
14492
14493 /* A semicolon terminates the declaration. */
14494 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14495
14496 /* Get the location for the static assertion. Use that of the
14497 condition if available, otherwise, use that of the "static_assert"
14498 token. */
14499 location_t assert_loc = condition.get_location ();
14500 if (assert_loc == UNKNOWN_LOCATION)
14501 assert_loc = token_loc;
14502
14503 /* Complete the static assertion, which may mean either processing
14504 the static assert now or saving it for template instantiation. */
14505 finish_static_assert (condition, message, assert_loc, member_p);
14506 }
14507
14508 /* Parse the expression in decltype ( expression ). */
14509
14510 static tree
14511 cp_parser_decltype_expr (cp_parser *parser,
14512 bool &id_expression_or_member_access_p)
14513 {
14514 cp_token *id_expr_start_token;
14515 tree expr;
14516
14517 /* Since we're going to preserve any side-effects from this parse, set up a
14518 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14519 in the expression. */
14520 tentative_firewall firewall (parser);
14521
14522 /* First, try parsing an id-expression. */
14523 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14524 cp_parser_parse_tentatively (parser);
14525 expr = cp_parser_id_expression (parser,
14526 /*template_keyword_p=*/false,
14527 /*check_dependency_p=*/true,
14528 /*template_p=*/NULL,
14529 /*declarator_p=*/false,
14530 /*optional_p=*/false);
14531
14532 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14533 {
14534 bool non_integral_constant_expression_p = false;
14535 tree id_expression = expr;
14536 cp_id_kind idk;
14537 const char *error_msg;
14538
14539 if (identifier_p (expr))
14540 /* Lookup the name we got back from the id-expression. */
14541 expr = cp_parser_lookup_name_simple (parser, expr,
14542 id_expr_start_token->location);
14543
14544 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14545 /* A template without args is not a complete id-expression. */
14546 expr = error_mark_node;
14547
14548 if (expr
14549 && expr != error_mark_node
14550 && TREE_CODE (expr) != TYPE_DECL
14551 && (TREE_CODE (expr) != BIT_NOT_EXPR
14552 || !TYPE_P (TREE_OPERAND (expr, 0)))
14553 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14554 {
14555 /* Complete lookup of the id-expression. */
14556 expr = (finish_id_expression
14557 (id_expression, expr, parser->scope, &idk,
14558 /*integral_constant_expression_p=*/false,
14559 /*allow_non_integral_constant_expression_p=*/true,
14560 &non_integral_constant_expression_p,
14561 /*template_p=*/false,
14562 /*done=*/true,
14563 /*address_p=*/false,
14564 /*template_arg_p=*/false,
14565 &error_msg,
14566 id_expr_start_token->location));
14567
14568 if (expr == error_mark_node)
14569 /* We found an id-expression, but it was something that we
14570 should not have found. This is an error, not something
14571 we can recover from, so note that we found an
14572 id-expression and we'll recover as gracefully as
14573 possible. */
14574 id_expression_or_member_access_p = true;
14575 }
14576
14577 if (expr
14578 && expr != error_mark_node
14579 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14580 /* We have an id-expression. */
14581 id_expression_or_member_access_p = true;
14582 }
14583
14584 if (!id_expression_or_member_access_p)
14585 {
14586 /* Abort the id-expression parse. */
14587 cp_parser_abort_tentative_parse (parser);
14588
14589 /* Parsing tentatively, again. */
14590 cp_parser_parse_tentatively (parser);
14591
14592 /* Parse a class member access. */
14593 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14594 /*cast_p=*/false, /*decltype*/true,
14595 /*member_access_only_p=*/true, NULL);
14596
14597 if (expr
14598 && expr != error_mark_node
14599 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14600 /* We have an id-expression. */
14601 id_expression_or_member_access_p = true;
14602 }
14603
14604 if (id_expression_or_member_access_p)
14605 /* We have parsed the complete id-expression or member access. */
14606 cp_parser_parse_definitely (parser);
14607 else
14608 {
14609 /* Abort our attempt to parse an id-expression or member access
14610 expression. */
14611 cp_parser_abort_tentative_parse (parser);
14612
14613 /* Commit to the tentative_firewall so we get syntax errors. */
14614 cp_parser_commit_to_tentative_parse (parser);
14615
14616 /* Parse a full expression. */
14617 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14618 /*decltype_p=*/true);
14619 }
14620
14621 return expr;
14622 }
14623
14624 /* Parse a `decltype' type. Returns the type.
14625
14626 simple-type-specifier:
14627 decltype ( expression )
14628 C++14 proposal:
14629 decltype ( auto ) */
14630
14631 static tree
14632 cp_parser_decltype (cp_parser *parser)
14633 {
14634 bool id_expression_or_member_access_p = false;
14635 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14636
14637 if (start_token->type == CPP_DECLTYPE)
14638 {
14639 /* Already parsed. */
14640 cp_lexer_consume_token (parser->lexer);
14641 return saved_checks_value (start_token->u.tree_check_value);
14642 }
14643
14644 /* Look for the `decltype' token. */
14645 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14646 return error_mark_node;
14647
14648 /* Parse the opening `('. */
14649 matching_parens parens;
14650 if (!parens.require_open (parser))
14651 return error_mark_node;
14652
14653 push_deferring_access_checks (dk_deferred);
14654
14655 tree expr = NULL_TREE;
14656
14657 if (cxx_dialect >= cxx14
14658 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14659 /* decltype (auto) */
14660 cp_lexer_consume_token (parser->lexer);
14661 else
14662 {
14663 /* decltype (expression) */
14664
14665 /* Types cannot be defined in a `decltype' expression. Save away the
14666 old message and set the new one. */
14667 const char *saved_message = parser->type_definition_forbidden_message;
14668 parser->type_definition_forbidden_message
14669 = G_("types may not be defined in %<decltype%> expressions");
14670
14671 /* The restrictions on constant-expressions do not apply inside
14672 decltype expressions. */
14673 bool saved_integral_constant_expression_p
14674 = parser->integral_constant_expression_p;
14675 bool saved_non_integral_constant_expression_p
14676 = parser->non_integral_constant_expression_p;
14677 parser->integral_constant_expression_p = false;
14678
14679 /* Within a parenthesized expression, a `>' token is always
14680 the greater-than operator. */
14681 bool saved_greater_than_is_operator_p
14682 = parser->greater_than_is_operator_p;
14683 parser->greater_than_is_operator_p = true;
14684
14685 /* Do not actually evaluate the expression. */
14686 ++cp_unevaluated_operand;
14687
14688 /* Do not warn about problems with the expression. */
14689 ++c_inhibit_evaluation_warnings;
14690
14691 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14692 STRIP_ANY_LOCATION_WRAPPER (expr);
14693
14694 /* Go back to evaluating expressions. */
14695 --cp_unevaluated_operand;
14696 --c_inhibit_evaluation_warnings;
14697
14698 /* The `>' token might be the end of a template-id or
14699 template-parameter-list now. */
14700 parser->greater_than_is_operator_p
14701 = saved_greater_than_is_operator_p;
14702
14703 /* Restore the old message and the integral constant expression
14704 flags. */
14705 parser->type_definition_forbidden_message = saved_message;
14706 parser->integral_constant_expression_p
14707 = saved_integral_constant_expression_p;
14708 parser->non_integral_constant_expression_p
14709 = saved_non_integral_constant_expression_p;
14710 }
14711
14712 /* Parse to the closing `)'. */
14713 if (!parens.require_close (parser))
14714 {
14715 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14716 /*consume_paren=*/true);
14717 pop_deferring_access_checks ();
14718 return error_mark_node;
14719 }
14720
14721 if (!expr)
14722 {
14723 /* Build auto. */
14724 expr = make_decltype_auto ();
14725 AUTO_IS_DECLTYPE (expr) = true;
14726 }
14727 else
14728 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14729 tf_warning_or_error);
14730
14731 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14732 it again. */
14733 start_token->type = CPP_DECLTYPE;
14734 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14735 start_token->u.tree_check_value->value = expr;
14736 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14737 start_token->keyword = RID_MAX;
14738 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14739
14740 pop_to_parent_deferring_access_checks ();
14741
14742 return expr;
14743 }
14744
14745 /* Special member functions [gram.special] */
14746
14747 /* Parse a conversion-function-id.
14748
14749 conversion-function-id:
14750 operator conversion-type-id
14751
14752 Returns an IDENTIFIER_NODE representing the operator. */
14753
14754 static tree
14755 cp_parser_conversion_function_id (cp_parser* parser)
14756 {
14757 tree type;
14758 tree saved_scope;
14759 tree saved_qualifying_scope;
14760 tree saved_object_scope;
14761 tree pushed_scope = NULL_TREE;
14762
14763 /* Look for the `operator' token. */
14764 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14765 return error_mark_node;
14766 /* When we parse the conversion-type-id, the current scope will be
14767 reset. However, we need that information in able to look up the
14768 conversion function later, so we save it here. */
14769 saved_scope = parser->scope;
14770 saved_qualifying_scope = parser->qualifying_scope;
14771 saved_object_scope = parser->object_scope;
14772 /* We must enter the scope of the class so that the names of
14773 entities declared within the class are available in the
14774 conversion-type-id. For example, consider:
14775
14776 struct S {
14777 typedef int I;
14778 operator I();
14779 };
14780
14781 S::operator I() { ... }
14782
14783 In order to see that `I' is a type-name in the definition, we
14784 must be in the scope of `S'. */
14785 if (saved_scope)
14786 pushed_scope = push_scope (saved_scope);
14787 /* Parse the conversion-type-id. */
14788 type = cp_parser_conversion_type_id (parser);
14789 /* Leave the scope of the class, if any. */
14790 if (pushed_scope)
14791 pop_scope (pushed_scope);
14792 /* Restore the saved scope. */
14793 parser->scope = saved_scope;
14794 parser->qualifying_scope = saved_qualifying_scope;
14795 parser->object_scope = saved_object_scope;
14796 /* If the TYPE is invalid, indicate failure. */
14797 if (type == error_mark_node)
14798 return error_mark_node;
14799 return make_conv_op_name (type);
14800 }
14801
14802 /* Parse a conversion-type-id:
14803
14804 conversion-type-id:
14805 type-specifier-seq conversion-declarator [opt]
14806
14807 Returns the TYPE specified. */
14808
14809 static tree
14810 cp_parser_conversion_type_id (cp_parser* parser)
14811 {
14812 tree attributes;
14813 cp_decl_specifier_seq type_specifiers;
14814 cp_declarator *declarator;
14815 tree type_specified;
14816 const char *saved_message;
14817
14818 /* Parse the attributes. */
14819 attributes = cp_parser_attributes_opt (parser);
14820
14821 saved_message = parser->type_definition_forbidden_message;
14822 parser->type_definition_forbidden_message
14823 = G_("types may not be defined in a conversion-type-id");
14824
14825 /* Parse the type-specifiers. */
14826 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
14827 /*is_declaration=*/false,
14828 /*is_trailing_return=*/false,
14829 &type_specifiers);
14830
14831 parser->type_definition_forbidden_message = saved_message;
14832
14833 /* If that didn't work, stop. */
14834 if (type_specifiers.type == error_mark_node)
14835 return error_mark_node;
14836 /* Parse the conversion-declarator. */
14837 declarator = cp_parser_conversion_declarator_opt (parser);
14838
14839 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14840 /*initialized=*/0, &attributes);
14841 if (attributes)
14842 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14843
14844 /* Don't give this error when parsing tentatively. This happens to
14845 work because we always parse this definitively once. */
14846 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14847 && type_uses_auto (type_specified))
14848 {
14849 if (cxx_dialect < cxx14)
14850 {
14851 error ("invalid use of %<auto%> in conversion operator");
14852 return error_mark_node;
14853 }
14854 else if (template_parm_scope_p ())
14855 warning (0, "use of %<auto%> in member template "
14856 "conversion operator can never be deduced");
14857 }
14858
14859 return type_specified;
14860 }
14861
14862 /* Parse an (optional) conversion-declarator.
14863
14864 conversion-declarator:
14865 ptr-operator conversion-declarator [opt]
14866
14867 */
14868
14869 static cp_declarator *
14870 cp_parser_conversion_declarator_opt (cp_parser* parser)
14871 {
14872 enum tree_code code;
14873 tree class_type, std_attributes = NULL_TREE;
14874 cp_cv_quals cv_quals;
14875
14876 /* We don't know if there's a ptr-operator next, or not. */
14877 cp_parser_parse_tentatively (parser);
14878 /* Try the ptr-operator. */
14879 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14880 &std_attributes);
14881 /* If it worked, look for more conversion-declarators. */
14882 if (cp_parser_parse_definitely (parser))
14883 {
14884 cp_declarator *declarator;
14885
14886 /* Parse another optional declarator. */
14887 declarator = cp_parser_conversion_declarator_opt (parser);
14888
14889 declarator = cp_parser_make_indirect_declarator
14890 (code, class_type, cv_quals, declarator, std_attributes);
14891
14892 return declarator;
14893 }
14894
14895 return NULL;
14896 }
14897
14898 /* Parse an (optional) ctor-initializer.
14899
14900 ctor-initializer:
14901 : mem-initializer-list */
14902
14903 static void
14904 cp_parser_ctor_initializer_opt (cp_parser* parser)
14905 {
14906 /* If the next token is not a `:', then there is no
14907 ctor-initializer. */
14908 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14909 {
14910 /* Do default initialization of any bases and members. */
14911 if (DECL_CONSTRUCTOR_P (current_function_decl))
14912 finish_mem_initializers (NULL_TREE);
14913 return;
14914 }
14915
14916 /* Consume the `:' token. */
14917 cp_lexer_consume_token (parser->lexer);
14918 /* And the mem-initializer-list. */
14919 cp_parser_mem_initializer_list (parser);
14920 }
14921
14922 /* Parse a mem-initializer-list.
14923
14924 mem-initializer-list:
14925 mem-initializer ... [opt]
14926 mem-initializer ... [opt] , mem-initializer-list */
14927
14928 static void
14929 cp_parser_mem_initializer_list (cp_parser* parser)
14930 {
14931 tree mem_initializer_list = NULL_TREE;
14932 tree target_ctor = error_mark_node;
14933 cp_token *token = cp_lexer_peek_token (parser->lexer);
14934
14935 /* Let the semantic analysis code know that we are starting the
14936 mem-initializer-list. */
14937 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14938 error_at (token->location,
14939 "only constructors take member initializers");
14940
14941 /* Loop through the list. */
14942 while (true)
14943 {
14944 tree mem_initializer;
14945
14946 token = cp_lexer_peek_token (parser->lexer);
14947 /* Parse the mem-initializer. */
14948 mem_initializer = cp_parser_mem_initializer (parser);
14949 /* If the next token is a `...', we're expanding member initializers. */
14950 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14951 if (ellipsis
14952 || (mem_initializer != error_mark_node
14953 && check_for_bare_parameter_packs (TREE_PURPOSE
14954 (mem_initializer))))
14955 {
14956 /* Consume the `...'. */
14957 if (ellipsis)
14958 cp_lexer_consume_token (parser->lexer);
14959
14960 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14961 can be expanded but members cannot. */
14962 if (mem_initializer != error_mark_node
14963 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14964 {
14965 error_at (token->location,
14966 "cannot expand initializer for member %qD",
14967 TREE_PURPOSE (mem_initializer));
14968 mem_initializer = error_mark_node;
14969 }
14970
14971 /* Construct the pack expansion type. */
14972 if (mem_initializer != error_mark_node)
14973 mem_initializer = make_pack_expansion (mem_initializer);
14974 }
14975 if (target_ctor != error_mark_node
14976 && mem_initializer != error_mark_node)
14977 {
14978 error ("mem-initializer for %qD follows constructor delegation",
14979 TREE_PURPOSE (mem_initializer));
14980 mem_initializer = error_mark_node;
14981 }
14982 /* Look for a target constructor. */
14983 if (mem_initializer != error_mark_node
14984 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14985 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14986 {
14987 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14988 if (mem_initializer_list)
14989 {
14990 error ("constructor delegation follows mem-initializer for %qD",
14991 TREE_PURPOSE (mem_initializer_list));
14992 mem_initializer = error_mark_node;
14993 }
14994 target_ctor = mem_initializer;
14995 }
14996 /* Add it to the list, unless it was erroneous. */
14997 if (mem_initializer != error_mark_node)
14998 {
14999 TREE_CHAIN (mem_initializer) = mem_initializer_list;
15000 mem_initializer_list = mem_initializer;
15001 }
15002 /* If the next token is not a `,', we're done. */
15003 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15004 break;
15005 /* Consume the `,' token. */
15006 cp_lexer_consume_token (parser->lexer);
15007 }
15008
15009 /* Perform semantic analysis. */
15010 if (DECL_CONSTRUCTOR_P (current_function_decl))
15011 finish_mem_initializers (mem_initializer_list);
15012 }
15013
15014 /* Parse a mem-initializer.
15015
15016 mem-initializer:
15017 mem-initializer-id ( expression-list [opt] )
15018 mem-initializer-id braced-init-list
15019
15020 GNU extension:
15021
15022 mem-initializer:
15023 ( expression-list [opt] )
15024
15025 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
15026 class) or FIELD_DECL (for a non-static data member) to initialize;
15027 the TREE_VALUE is the expression-list. An empty initialization
15028 list is represented by void_list_node. */
15029
15030 static tree
15031 cp_parser_mem_initializer (cp_parser* parser)
15032 {
15033 tree mem_initializer_id;
15034 tree expression_list;
15035 tree member;
15036 cp_token *token = cp_lexer_peek_token (parser->lexer);
15037
15038 /* Find out what is being initialized. */
15039 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15040 {
15041 permerror (token->location,
15042 "anachronistic old-style base class initializer");
15043 mem_initializer_id = NULL_TREE;
15044 }
15045 else
15046 {
15047 mem_initializer_id = cp_parser_mem_initializer_id (parser);
15048 if (mem_initializer_id == error_mark_node)
15049 return mem_initializer_id;
15050 }
15051 member = expand_member_init (mem_initializer_id);
15052 if (member && !DECL_P (member))
15053 in_base_initializer = 1;
15054
15055 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15056 {
15057 bool expr_non_constant_p;
15058 cp_lexer_set_source_position (parser->lexer);
15059 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15060 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
15061 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
15062 expression_list = build_tree_list (NULL_TREE, expression_list);
15063 }
15064 else
15065 {
15066 vec<tree, va_gc> *vec;
15067 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15068 /*cast_p=*/false,
15069 /*allow_expansion_p=*/true,
15070 /*non_constant_p=*/NULL,
15071 /*close_paren_loc=*/NULL,
15072 /*wrap_locations_p=*/true);
15073 if (vec == NULL)
15074 return error_mark_node;
15075 expression_list = build_tree_list_vec (vec);
15076 release_tree_vector (vec);
15077 }
15078
15079 if (expression_list == error_mark_node)
15080 return error_mark_node;
15081 if (!expression_list)
15082 expression_list = void_type_node;
15083
15084 in_base_initializer = 0;
15085
15086 return member ? build_tree_list (member, expression_list) : error_mark_node;
15087 }
15088
15089 /* Parse a mem-initializer-id.
15090
15091 mem-initializer-id:
15092 :: [opt] nested-name-specifier [opt] class-name
15093 decltype-specifier (C++11)
15094 identifier
15095
15096 Returns a TYPE indicating the class to be initialized for the first
15097 production (and the second in C++11). Returns an IDENTIFIER_NODE
15098 indicating the data member to be initialized for the last production. */
15099
15100 static tree
15101 cp_parser_mem_initializer_id (cp_parser* parser)
15102 {
15103 bool global_scope_p;
15104 bool nested_name_specifier_p;
15105 bool template_p = false;
15106 tree id;
15107
15108 cp_token *token = cp_lexer_peek_token (parser->lexer);
15109
15110 /* `typename' is not allowed in this context ([temp.res]). */
15111 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15112 {
15113 error_at (token->location,
15114 "keyword %<typename%> not allowed in this context (a qualified "
15115 "member initializer is implicitly a type)");
15116 cp_lexer_consume_token (parser->lexer);
15117 }
15118 /* Look for the optional `::' operator. */
15119 global_scope_p
15120 = (cp_parser_global_scope_opt (parser,
15121 /*current_scope_valid_p=*/false)
15122 != NULL_TREE);
15123 /* Look for the optional nested-name-specifier. The simplest way to
15124 implement:
15125
15126 [temp.res]
15127
15128 The keyword `typename' is not permitted in a base-specifier or
15129 mem-initializer; in these contexts a qualified name that
15130 depends on a template-parameter is implicitly assumed to be a
15131 type name.
15132
15133 is to assume that we have seen the `typename' keyword at this
15134 point. */
15135 nested_name_specifier_p
15136 = (cp_parser_nested_name_specifier_opt (parser,
15137 /*typename_keyword_p=*/true,
15138 /*check_dependency_p=*/true,
15139 /*type_p=*/true,
15140 /*is_declaration=*/true)
15141 != NULL_TREE);
15142 if (nested_name_specifier_p)
15143 template_p = cp_parser_optional_template_keyword (parser);
15144 /* If there is a `::' operator or a nested-name-specifier, then we
15145 are definitely looking for a class-name. */
15146 if (global_scope_p || nested_name_specifier_p)
15147 return cp_parser_class_name (parser,
15148 /*typename_keyword_p=*/true,
15149 /*template_keyword_p=*/template_p,
15150 typename_type,
15151 /*check_dependency_p=*/true,
15152 /*class_head_p=*/false,
15153 /*is_declaration=*/true);
15154 /* Otherwise, we could also be looking for an ordinary identifier. */
15155 cp_parser_parse_tentatively (parser);
15156 if (cp_lexer_next_token_is_decltype (parser->lexer))
15157 /* Try a decltype-specifier. */
15158 id = cp_parser_decltype (parser);
15159 else
15160 /* Otherwise, try a class-name. */
15161 id = cp_parser_class_name (parser,
15162 /*typename_keyword_p=*/true,
15163 /*template_keyword_p=*/false,
15164 none_type,
15165 /*check_dependency_p=*/true,
15166 /*class_head_p=*/false,
15167 /*is_declaration=*/true);
15168 /* If we found one, we're done. */
15169 if (cp_parser_parse_definitely (parser))
15170 return id;
15171 /* Otherwise, look for an ordinary identifier. */
15172 return cp_parser_identifier (parser);
15173 }
15174
15175 /* Overloading [gram.over] */
15176
15177 /* Parse an operator-function-id.
15178
15179 operator-function-id:
15180 operator operator
15181
15182 Returns an IDENTIFIER_NODE for the operator which is a
15183 human-readable spelling of the identifier, e.g., `operator +'. */
15184
15185 static cp_expr
15186 cp_parser_operator_function_id (cp_parser* parser)
15187 {
15188 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
15189 /* Look for the `operator' keyword. */
15190 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15191 return error_mark_node;
15192 /* And then the name of the operator itself. */
15193 return cp_parser_operator (parser, start_loc);
15194 }
15195
15196 /* Return an identifier node for a user-defined literal operator.
15197 The suffix identifier is chained to the operator name identifier. */
15198
15199 tree
15200 cp_literal_operator_id (const char* name)
15201 {
15202 tree identifier;
15203 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
15204 + strlen (name) + 10);
15205 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
15206 identifier = get_identifier (buffer);
15207
15208 return identifier;
15209 }
15210
15211 /* Parse an operator.
15212
15213 operator:
15214 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15215 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15216 || ++ -- , ->* -> () []
15217
15218 GNU Extensions:
15219
15220 operator:
15221 <? >? <?= >?=
15222
15223 Returns an IDENTIFIER_NODE for the operator which is a
15224 human-readable spelling of the identifier, e.g., `operator +'. */
15225
15226 static cp_expr
15227 cp_parser_operator (cp_parser* parser, location_t start_loc)
15228 {
15229 tree id = NULL_TREE;
15230 cp_token *token;
15231 bool utf8 = false;
15232
15233 /* Peek at the next token. */
15234 token = cp_lexer_peek_token (parser->lexer);
15235
15236 location_t end_loc = token->location;
15237
15238 /* Figure out which operator we have. */
15239 enum tree_code op = ERROR_MARK;
15240 bool assop = false;
15241 bool consumed = false;
15242 switch (token->type)
15243 {
15244 case CPP_KEYWORD:
15245 {
15246 /* The keyword should be either `new' or `delete'. */
15247 if (token->keyword == RID_NEW)
15248 op = NEW_EXPR;
15249 else if (token->keyword == RID_DELETE)
15250 op = DELETE_EXPR;
15251 else
15252 break;
15253
15254 /* Consume the `new' or `delete' token. */
15255 end_loc = cp_lexer_consume_token (parser->lexer)->location;
15256
15257 /* Peek at the next token. */
15258 token = cp_lexer_peek_token (parser->lexer);
15259 /* If it's a `[' token then this is the array variant of the
15260 operator. */
15261 if (token->type == CPP_OPEN_SQUARE)
15262 {
15263 /* Consume the `[' token. */
15264 cp_lexer_consume_token (parser->lexer);
15265 /* Look for the `]' token. */
15266 if (cp_token *close_token
15267 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15268 end_loc = close_token->location;
15269 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
15270 }
15271 consumed = true;
15272 break;
15273 }
15274
15275 case CPP_PLUS:
15276 op = PLUS_EXPR;
15277 break;
15278
15279 case CPP_MINUS:
15280 op = MINUS_EXPR;
15281 break;
15282
15283 case CPP_MULT:
15284 op = MULT_EXPR;
15285 break;
15286
15287 case CPP_DIV:
15288 op = TRUNC_DIV_EXPR;
15289 break;
15290
15291 case CPP_MOD:
15292 op = TRUNC_MOD_EXPR;
15293 break;
15294
15295 case CPP_XOR:
15296 op = BIT_XOR_EXPR;
15297 break;
15298
15299 case CPP_AND:
15300 op = BIT_AND_EXPR;
15301 break;
15302
15303 case CPP_OR:
15304 op = BIT_IOR_EXPR;
15305 break;
15306
15307 case CPP_COMPL:
15308 op = BIT_NOT_EXPR;
15309 break;
15310
15311 case CPP_NOT:
15312 op = TRUTH_NOT_EXPR;
15313 break;
15314
15315 case CPP_EQ:
15316 assop = true;
15317 op = NOP_EXPR;
15318 break;
15319
15320 case CPP_LESS:
15321 op = LT_EXPR;
15322 break;
15323
15324 case CPP_GREATER:
15325 op = GT_EXPR;
15326 break;
15327
15328 case CPP_PLUS_EQ:
15329 assop = true;
15330 op = PLUS_EXPR;
15331 break;
15332
15333 case CPP_MINUS_EQ:
15334 assop = true;
15335 op = MINUS_EXPR;
15336 break;
15337
15338 case CPP_MULT_EQ:
15339 assop = true;
15340 op = MULT_EXPR;
15341 break;
15342
15343 case CPP_DIV_EQ:
15344 assop = true;
15345 op = TRUNC_DIV_EXPR;
15346 break;
15347
15348 case CPP_MOD_EQ:
15349 assop = true;
15350 op = TRUNC_MOD_EXPR;
15351 break;
15352
15353 case CPP_XOR_EQ:
15354 assop = true;
15355 op = BIT_XOR_EXPR;
15356 break;
15357
15358 case CPP_AND_EQ:
15359 assop = true;
15360 op = BIT_AND_EXPR;
15361 break;
15362
15363 case CPP_OR_EQ:
15364 assop = true;
15365 op = BIT_IOR_EXPR;
15366 break;
15367
15368 case CPP_LSHIFT:
15369 op = LSHIFT_EXPR;
15370 break;
15371
15372 case CPP_RSHIFT:
15373 op = RSHIFT_EXPR;
15374 break;
15375
15376 case CPP_LSHIFT_EQ:
15377 assop = true;
15378 op = LSHIFT_EXPR;
15379 break;
15380
15381 case CPP_RSHIFT_EQ:
15382 assop = true;
15383 op = RSHIFT_EXPR;
15384 break;
15385
15386 case CPP_EQ_EQ:
15387 op = EQ_EXPR;
15388 break;
15389
15390 case CPP_NOT_EQ:
15391 op = NE_EXPR;
15392 break;
15393
15394 case CPP_LESS_EQ:
15395 op = LE_EXPR;
15396 break;
15397
15398 case CPP_GREATER_EQ:
15399 op = GE_EXPR;
15400 break;
15401
15402 case CPP_AND_AND:
15403 op = TRUTH_ANDIF_EXPR;
15404 break;
15405
15406 case CPP_OR_OR:
15407 op = TRUTH_ORIF_EXPR;
15408 break;
15409
15410 case CPP_PLUS_PLUS:
15411 op = POSTINCREMENT_EXPR;
15412 break;
15413
15414 case CPP_MINUS_MINUS:
15415 op = PREDECREMENT_EXPR;
15416 break;
15417
15418 case CPP_COMMA:
15419 op = COMPOUND_EXPR;
15420 break;
15421
15422 case CPP_DEREF_STAR:
15423 op = MEMBER_REF;
15424 break;
15425
15426 case CPP_DEREF:
15427 op = COMPONENT_REF;
15428 break;
15429
15430 case CPP_OPEN_PAREN:
15431 {
15432 /* Consume the `('. */
15433 matching_parens parens;
15434 parens.consume_open (parser);
15435 /* Look for the matching `)'. */
15436 token = parens.require_close (parser);
15437 if (token)
15438 end_loc = token->location;
15439 op = CALL_EXPR;
15440 consumed = true;
15441 break;
15442 }
15443
15444 case CPP_OPEN_SQUARE:
15445 /* Consume the `['. */
15446 cp_lexer_consume_token (parser->lexer);
15447 /* Look for the matching `]'. */
15448 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15449 if (token)
15450 end_loc = token->location;
15451 op = ARRAY_REF;
15452 consumed = true;
15453 break;
15454
15455 case CPP_UTF8STRING:
15456 case CPP_UTF8STRING_USERDEF:
15457 utf8 = true;
15458 /* FALLTHRU */
15459 case CPP_STRING:
15460 case CPP_WSTRING:
15461 case CPP_STRING16:
15462 case CPP_STRING32:
15463 case CPP_STRING_USERDEF:
15464 case CPP_WSTRING_USERDEF:
15465 case CPP_STRING16_USERDEF:
15466 case CPP_STRING32_USERDEF:
15467 {
15468 cp_expr str;
15469 tree string_tree;
15470 int sz, len;
15471
15472 if (cxx_dialect == cxx98)
15473 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15474
15475 /* Consume the string. */
15476 str = cp_parser_string_literal (parser, /*translate=*/true,
15477 /*wide_ok=*/true, /*lookup_udlit=*/false);
15478 if (str == error_mark_node)
15479 return error_mark_node;
15480 else if (TREE_CODE (str) == USERDEF_LITERAL)
15481 {
15482 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
15483 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
15484 end_loc = str.get_location ();
15485 }
15486 else
15487 {
15488 string_tree = str;
15489 /* Look for the suffix identifier. */
15490 token = cp_lexer_peek_token (parser->lexer);
15491 if (token->type == CPP_NAME)
15492 {
15493 id = cp_parser_identifier (parser);
15494 end_loc = token->location;
15495 }
15496 else if (token->type == CPP_KEYWORD)
15497 {
15498 error ("unexpected keyword;"
15499 " remove space between quotes and suffix identifier");
15500 return error_mark_node;
15501 }
15502 else
15503 {
15504 error ("expected suffix identifier");
15505 return error_mark_node;
15506 }
15507 }
15508 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15509 (TREE_TYPE (TREE_TYPE (string_tree))));
15510 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15511 if (len != 0)
15512 {
15513 error ("expected empty string after %<operator%> keyword");
15514 return error_mark_node;
15515 }
15516 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15517 != char_type_node)
15518 {
15519 error ("invalid encoding prefix in literal operator");
15520 return error_mark_node;
15521 }
15522 if (id != error_mark_node)
15523 {
15524 const char *name = IDENTIFIER_POINTER (id);
15525 id = cp_literal_operator_id (name);
15526 }
15527 /* Generate a location of the form:
15528 "" _suffix_identifier
15529 ^~~~~~~~~~~~~~~~~~~~~
15530 with caret == start at the start token, finish at the end of the
15531 suffix identifier. */
15532 location_t finish_loc
15533 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15534 location_t combined_loc
15535 = make_location (start_loc, start_loc, finish_loc);
15536 return cp_expr (id, combined_loc);
15537 }
15538
15539 default:
15540 /* Anything else is an error. */
15541 break;
15542 }
15543
15544 /* If we have selected an identifier, we need to consume the
15545 operator token. */
15546 if (op != ERROR_MARK)
15547 {
15548 id = ovl_op_identifier (assop, op);
15549 if (!consumed)
15550 cp_lexer_consume_token (parser->lexer);
15551 }
15552 /* Otherwise, no valid operator name was present. */
15553 else
15554 {
15555 cp_parser_error (parser, "expected operator");
15556 id = error_mark_node;
15557 }
15558
15559 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15560 return cp_expr (id, start_loc);
15561 }
15562
15563 /* Parse a template-declaration.
15564
15565 template-declaration:
15566 export [opt] template < template-parameter-list > declaration
15567
15568 If MEMBER_P is TRUE, this template-declaration occurs within a
15569 class-specifier.
15570
15571 The grammar rule given by the standard isn't correct. What
15572 is really meant is:
15573
15574 template-declaration:
15575 export [opt] template-parameter-list-seq
15576 decl-specifier-seq [opt] init-declarator [opt] ;
15577 export [opt] template-parameter-list-seq
15578 function-definition
15579
15580 template-parameter-list-seq:
15581 template-parameter-list-seq [opt]
15582 template < template-parameter-list >
15583
15584 Concept Extensions:
15585
15586 template-parameter-list-seq:
15587 template < template-parameter-list > requires-clause [opt]
15588
15589 requires-clause:
15590 requires logical-or-expression */
15591
15592 static void
15593 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15594 {
15595 /* Check for `export'. */
15596 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15597 {
15598 /* Consume the `export' token. */
15599 cp_lexer_consume_token (parser->lexer);
15600 /* Warn that we do not support `export'. */
15601 warning (0, "keyword %<export%> not implemented, and will be ignored");
15602 }
15603
15604 cp_parser_template_declaration_after_export (parser, member_p);
15605 }
15606
15607 /* Parse a template-parameter-list.
15608
15609 template-parameter-list:
15610 template-parameter
15611 template-parameter-list , template-parameter
15612
15613 Returns a TREE_LIST. Each node represents a template parameter.
15614 The nodes are connected via their TREE_CHAINs. */
15615
15616 static tree
15617 cp_parser_template_parameter_list (cp_parser* parser)
15618 {
15619 tree parameter_list = NULL_TREE;
15620
15621 /* Don't create wrapper nodes within a template-parameter-list,
15622 since we don't want to have different types based on the
15623 spelling location of constants and decls within them. */
15624 auto_suppress_location_wrappers sentinel;
15625
15626 begin_template_parm_list ();
15627
15628 /* The loop below parses the template parms. We first need to know
15629 the total number of template parms to be able to compute proper
15630 canonical types of each dependent type. So after the loop, when
15631 we know the total number of template parms,
15632 end_template_parm_list computes the proper canonical types and
15633 fixes up the dependent types accordingly. */
15634 while (true)
15635 {
15636 tree parameter;
15637 bool is_non_type;
15638 bool is_parameter_pack;
15639 location_t parm_loc;
15640
15641 /* Parse the template-parameter. */
15642 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15643 parameter = cp_parser_template_parameter (parser,
15644 &is_non_type,
15645 &is_parameter_pack);
15646 /* Add it to the list. */
15647 if (parameter != error_mark_node)
15648 parameter_list = process_template_parm (parameter_list,
15649 parm_loc,
15650 parameter,
15651 is_non_type,
15652 is_parameter_pack);
15653 else
15654 {
15655 tree err_parm = build_tree_list (parameter, parameter);
15656 parameter_list = chainon (parameter_list, err_parm);
15657 }
15658
15659 /* If the next token is not a `,', we're done. */
15660 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15661 break;
15662 /* Otherwise, consume the `,' token. */
15663 cp_lexer_consume_token (parser->lexer);
15664 }
15665
15666 return end_template_parm_list (parameter_list);
15667 }
15668
15669 /* Parse a introduction-list.
15670
15671 introduction-list:
15672 introduced-parameter
15673 introduction-list , introduced-parameter
15674
15675 introduced-parameter:
15676 ...[opt] identifier
15677
15678 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15679 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15680 WILDCARD_DECL will also have DECL_NAME set and token location in
15681 DECL_SOURCE_LOCATION. */
15682
15683 static tree
15684 cp_parser_introduction_list (cp_parser *parser)
15685 {
15686 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15687
15688 while (true)
15689 {
15690 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15691 if (is_pack)
15692 cp_lexer_consume_token (parser->lexer);
15693
15694 tree identifier = cp_parser_identifier (parser);
15695 if (identifier == error_mark_node)
15696 break;
15697
15698 /* Build placeholder. */
15699 tree parm = build_nt (WILDCARD_DECL);
15700 DECL_SOURCE_LOCATION (parm)
15701 = cp_lexer_peek_token (parser->lexer)->location;
15702 DECL_NAME (parm) = identifier;
15703 WILDCARD_PACK_P (parm) = is_pack;
15704 vec_safe_push (introduction_vec, parm);
15705
15706 /* If the next token is not a `,', we're done. */
15707 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15708 break;
15709 /* Otherwise, consume the `,' token. */
15710 cp_lexer_consume_token (parser->lexer);
15711 }
15712
15713 /* Convert the vec into a TREE_VEC. */
15714 tree introduction_list = make_tree_vec (introduction_vec->length ());
15715 unsigned int n;
15716 tree parm;
15717 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15718 TREE_VEC_ELT (introduction_list, n) = parm;
15719
15720 release_tree_vector (introduction_vec);
15721 return introduction_list;
15722 }
15723
15724 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15725 is an abstract declarator. */
15726
15727 static inline cp_declarator*
15728 get_id_declarator (cp_declarator *declarator)
15729 {
15730 cp_declarator *d = declarator;
15731 while (d && d->kind != cdk_id)
15732 d = d->declarator;
15733 return d;
15734 }
15735
15736 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15737 is an abstract declarator. */
15738
15739 static inline tree
15740 get_unqualified_id (cp_declarator *declarator)
15741 {
15742 declarator = get_id_declarator (declarator);
15743 if (declarator)
15744 return declarator->u.id.unqualified_name;
15745 else
15746 return NULL_TREE;
15747 }
15748
15749 /* Returns true if DECL represents a constrained-parameter. */
15750
15751 static inline bool
15752 is_constrained_parameter (tree decl)
15753 {
15754 return (decl
15755 && TREE_CODE (decl) == TYPE_DECL
15756 && CONSTRAINED_PARM_CONCEPT (decl)
15757 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15758 }
15759
15760 /* Returns true if PARM declares a constrained-parameter. */
15761
15762 static inline bool
15763 is_constrained_parameter (cp_parameter_declarator *parm)
15764 {
15765 return is_constrained_parameter (parm->decl_specifiers.type);
15766 }
15767
15768 /* Check that the type parameter is only a declarator-id, and that its
15769 type is not cv-qualified. */
15770
15771 bool
15772 cp_parser_check_constrained_type_parm (cp_parser *parser,
15773 cp_parameter_declarator *parm)
15774 {
15775 if (!parm->declarator)
15776 return true;
15777
15778 if (parm->declarator->kind != cdk_id)
15779 {
15780 cp_parser_error (parser, "invalid constrained type parameter");
15781 return false;
15782 }
15783
15784 /* Don't allow cv-qualified type parameters. */
15785 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15786 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15787 {
15788 cp_parser_error (parser, "cv-qualified type parameter");
15789 return false;
15790 }
15791
15792 return true;
15793 }
15794
15795 /* Finish parsing/processing a template type parameter and checking
15796 various restrictions. */
15797
15798 static inline tree
15799 cp_parser_constrained_type_template_parm (cp_parser *parser,
15800 tree id,
15801 cp_parameter_declarator* parmdecl)
15802 {
15803 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15804 return finish_template_type_parm (class_type_node, id);
15805 else
15806 return error_mark_node;
15807 }
15808
15809 static tree
15810 finish_constrained_template_template_parm (tree proto, tree id)
15811 {
15812 /* FIXME: This should probably be copied, and we may need to adjust
15813 the template parameter depths. */
15814 tree saved_parms = current_template_parms;
15815 begin_template_parm_list ();
15816 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15817 end_template_parm_list ();
15818
15819 tree parm = finish_template_template_parm (class_type_node, id);
15820 current_template_parms = saved_parms;
15821
15822 return parm;
15823 }
15824
15825 /* Finish parsing/processing a template template parameter by borrowing
15826 the template parameter list from the prototype parameter. */
15827
15828 static tree
15829 cp_parser_constrained_template_template_parm (cp_parser *parser,
15830 tree proto,
15831 tree id,
15832 cp_parameter_declarator *parmdecl)
15833 {
15834 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15835 return error_mark_node;
15836 return finish_constrained_template_template_parm (proto, id);
15837 }
15838
15839 /* Create a new non-type template parameter from the given PARM
15840 declarator. */
15841
15842 static tree
15843 constrained_non_type_template_parm (bool *is_non_type,
15844 cp_parameter_declarator *parm)
15845 {
15846 *is_non_type = true;
15847 cp_declarator *decl = parm->declarator;
15848 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15849 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15850 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15851 }
15852
15853 /* Build a constrained template parameter based on the PARMDECL
15854 declarator. The type of PARMDECL is the constrained type, which
15855 refers to the prototype template parameter that ultimately
15856 specifies the type of the declared parameter. */
15857
15858 static tree
15859 finish_constrained_parameter (cp_parser *parser,
15860 cp_parameter_declarator *parmdecl,
15861 bool *is_non_type,
15862 bool *is_parameter_pack)
15863 {
15864 tree decl = parmdecl->decl_specifiers.type;
15865 tree id = get_unqualified_id (parmdecl->declarator);
15866 tree def = parmdecl->default_argument;
15867 tree proto = DECL_INITIAL (decl);
15868
15869 /* A template parameter constrained by a variadic concept shall also
15870 be declared as a template parameter pack. */
15871 bool is_variadic = template_parameter_pack_p (proto);
15872 if (is_variadic && !*is_parameter_pack)
15873 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15874
15875 /* Build the parameter. Return an error if the declarator was invalid. */
15876 tree parm;
15877 if (TREE_CODE (proto) == TYPE_DECL)
15878 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15879 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15880 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15881 parmdecl);
15882 else
15883 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15884 if (parm == error_mark_node)
15885 return error_mark_node;
15886
15887 /* Finish the parameter decl and create a node attaching the
15888 default argument and constraint. */
15889 parm = build_tree_list (def, parm);
15890 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15891
15892 return parm;
15893 }
15894
15895 /* Returns true if the parsed type actually represents the declaration
15896 of a type template-parameter. */
15897
15898 static inline bool
15899 declares_constrained_type_template_parameter (tree type)
15900 {
15901 return (is_constrained_parameter (type)
15902 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15903 }
15904
15905
15906 /* Returns true if the parsed type actually represents the declaration of
15907 a template template-parameter. */
15908
15909 static bool
15910 declares_constrained_template_template_parameter (tree type)
15911 {
15912 return (is_constrained_parameter (type)
15913 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15914 }
15915
15916 /* Parse a default argument for a type template-parameter.
15917 Note that diagnostics are handled in cp_parser_template_parameter. */
15918
15919 static tree
15920 cp_parser_default_type_template_argument (cp_parser *parser)
15921 {
15922 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15923
15924 /* Consume the `=' token. */
15925 cp_lexer_consume_token (parser->lexer);
15926
15927 cp_token *token = cp_lexer_peek_token (parser->lexer);
15928
15929 /* Parse the default-argument. */
15930 push_deferring_access_checks (dk_no_deferred);
15931 tree default_argument = cp_parser_type_id (parser,
15932 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15933 NULL);
15934 pop_deferring_access_checks ();
15935
15936 if (flag_concepts && type_uses_auto (default_argument))
15937 {
15938 error_at (token->location,
15939 "invalid use of %<auto%> in default template argument");
15940 return error_mark_node;
15941 }
15942
15943 return default_argument;
15944 }
15945
15946 /* Parse a default argument for a template template-parameter. */
15947
15948 static tree
15949 cp_parser_default_template_template_argument (cp_parser *parser)
15950 {
15951 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15952
15953 bool is_template;
15954
15955 /* Consume the `='. */
15956 cp_lexer_consume_token (parser->lexer);
15957 /* Parse the id-expression. */
15958 push_deferring_access_checks (dk_no_deferred);
15959 /* save token before parsing the id-expression, for error
15960 reporting */
15961 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15962 tree default_argument
15963 = cp_parser_id_expression (parser,
15964 /*template_keyword_p=*/false,
15965 /*check_dependency_p=*/true,
15966 /*template_p=*/&is_template,
15967 /*declarator_p=*/false,
15968 /*optional_p=*/false);
15969 if (TREE_CODE (default_argument) == TYPE_DECL)
15970 /* If the id-expression was a template-id that refers to
15971 a template-class, we already have the declaration here,
15972 so no further lookup is needed. */
15973 ;
15974 else
15975 /* Look up the name. */
15976 default_argument
15977 = cp_parser_lookup_name (parser, default_argument,
15978 none_type,
15979 /*is_template=*/is_template,
15980 /*is_namespace=*/false,
15981 /*check_dependency=*/true,
15982 /*ambiguous_decls=*/NULL,
15983 token->location);
15984 /* See if the default argument is valid. */
15985 default_argument = check_template_template_default_arg (default_argument);
15986 pop_deferring_access_checks ();
15987 return default_argument;
15988 }
15989
15990 /* Parse a template-parameter.
15991
15992 template-parameter:
15993 type-parameter
15994 parameter-declaration
15995
15996 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15997 the parameter. The TREE_PURPOSE is the default value, if any.
15998 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15999 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
16000 set to true iff this parameter is a parameter pack. */
16001
16002 static tree
16003 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
16004 bool *is_parameter_pack)
16005 {
16006 cp_token *token;
16007 cp_parameter_declarator *parameter_declarator;
16008 tree parm;
16009
16010 /* Assume it is a type parameter or a template parameter. */
16011 *is_non_type = false;
16012 /* Assume it not a parameter pack. */
16013 *is_parameter_pack = false;
16014 /* Peek at the next token. */
16015 token = cp_lexer_peek_token (parser->lexer);
16016 /* If it is `template', we have a type-parameter. */
16017 if (token->keyword == RID_TEMPLATE)
16018 return cp_parser_type_parameter (parser, is_parameter_pack);
16019 /* If it is `class' or `typename' we do not know yet whether it is a
16020 type parameter or a non-type parameter. Consider:
16021
16022 template <typename T, typename T::X X> ...
16023
16024 or:
16025
16026 template <class C, class D*> ...
16027
16028 Here, the first parameter is a type parameter, and the second is
16029 a non-type parameter. We can tell by looking at the token after
16030 the identifier -- if it is a `,', `=', or `>' then we have a type
16031 parameter. */
16032 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
16033 {
16034 /* Peek at the token after `class' or `typename'. */
16035 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16036 /* If it's an ellipsis, we have a template type parameter
16037 pack. */
16038 if (token->type == CPP_ELLIPSIS)
16039 return cp_parser_type_parameter (parser, is_parameter_pack);
16040 /* If it's an identifier, skip it. */
16041 if (token->type == CPP_NAME)
16042 token = cp_lexer_peek_nth_token (parser->lexer, 3);
16043 /* Now, see if the token looks like the end of a template
16044 parameter. */
16045 if (token->type == CPP_COMMA
16046 || token->type == CPP_EQ
16047 || token->type == CPP_GREATER)
16048 return cp_parser_type_parameter (parser, is_parameter_pack);
16049 }
16050
16051 /* Otherwise, it is a non-type parameter or a constrained parameter.
16052
16053 [temp.param]
16054
16055 When parsing a default template-argument for a non-type
16056 template-parameter, the first non-nested `>' is taken as the end
16057 of the template parameter-list rather than a greater-than
16058 operator. */
16059 parameter_declarator
16060 = cp_parser_parameter_declaration (parser,
16061 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16062 /*template_parm_p=*/true,
16063 /*parenthesized_p=*/NULL);
16064
16065 if (!parameter_declarator)
16066 return error_mark_node;
16067
16068 /* If the parameter declaration is marked as a parameter pack, set
16069 *IS_PARAMETER_PACK to notify the caller. */
16070 if (parameter_declarator->template_parameter_pack_p)
16071 *is_parameter_pack = true;
16072
16073 if (parameter_declarator->default_argument)
16074 {
16075 /* Can happen in some cases of erroneous input (c++/34892). */
16076 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16077 /* Consume the `...' for better error recovery. */
16078 cp_lexer_consume_token (parser->lexer);
16079 }
16080
16081 // The parameter may have been constrained.
16082 if (is_constrained_parameter (parameter_declarator))
16083 return finish_constrained_parameter (parser,
16084 parameter_declarator,
16085 is_non_type,
16086 is_parameter_pack);
16087
16088 // Now we're sure that the parameter is a non-type parameter.
16089 *is_non_type = true;
16090
16091 parm = grokdeclarator (parameter_declarator->declarator,
16092 &parameter_declarator->decl_specifiers,
16093 TPARM, /*initialized=*/0,
16094 /*attrlist=*/NULL);
16095 if (parm == error_mark_node)
16096 return error_mark_node;
16097
16098 return build_tree_list (parameter_declarator->default_argument, parm);
16099 }
16100
16101 /* Parse a type-parameter.
16102
16103 type-parameter:
16104 class identifier [opt]
16105 class identifier [opt] = type-id
16106 typename identifier [opt]
16107 typename identifier [opt] = type-id
16108 template < template-parameter-list > class identifier [opt]
16109 template < template-parameter-list > class identifier [opt]
16110 = id-expression
16111
16112 GNU Extension (variadic templates):
16113
16114 type-parameter:
16115 class ... identifier [opt]
16116 typename ... identifier [opt]
16117
16118 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16119 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16120 the declaration of the parameter.
16121
16122 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16123
16124 static tree
16125 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
16126 {
16127 cp_token *token;
16128 tree parameter;
16129
16130 /* Look for a keyword to tell us what kind of parameter this is. */
16131 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
16132 if (!token)
16133 return error_mark_node;
16134
16135 switch (token->keyword)
16136 {
16137 case RID_CLASS:
16138 case RID_TYPENAME:
16139 {
16140 tree identifier;
16141 tree default_argument;
16142
16143 /* If the next token is an ellipsis, we have a template
16144 argument pack. */
16145 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16146 {
16147 /* Consume the `...' token. */
16148 cp_lexer_consume_token (parser->lexer);
16149 maybe_warn_variadic_templates ();
16150
16151 *is_parameter_pack = true;
16152 }
16153
16154 /* If the next token is an identifier, then it names the
16155 parameter. */
16156 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16157 identifier = cp_parser_identifier (parser);
16158 else
16159 identifier = NULL_TREE;
16160
16161 /* Create the parameter. */
16162 parameter = finish_template_type_parm (class_type_node, identifier);
16163
16164 /* If the next token is an `=', we have a default argument. */
16165 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16166 {
16167 default_argument
16168 = cp_parser_default_type_template_argument (parser);
16169
16170 /* Template parameter packs cannot have default
16171 arguments. */
16172 if (*is_parameter_pack)
16173 {
16174 if (identifier)
16175 error_at (token->location,
16176 "template parameter pack %qD cannot have a "
16177 "default argument", identifier);
16178 else
16179 error_at (token->location,
16180 "template parameter packs cannot have "
16181 "default arguments");
16182 default_argument = NULL_TREE;
16183 }
16184 else if (check_for_bare_parameter_packs (default_argument))
16185 default_argument = error_mark_node;
16186 }
16187 else
16188 default_argument = NULL_TREE;
16189
16190 /* Create the combined representation of the parameter and the
16191 default argument. */
16192 parameter = build_tree_list (default_argument, parameter);
16193 }
16194 break;
16195
16196 case RID_TEMPLATE:
16197 {
16198 tree identifier;
16199 tree default_argument;
16200
16201 /* Look for the `<'. */
16202 cp_parser_require (parser, CPP_LESS, RT_LESS);
16203 /* Parse the template-parameter-list. */
16204 cp_parser_template_parameter_list (parser);
16205 /* Look for the `>'. */
16206 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16207
16208 // If template requirements are present, parse them.
16209 if (flag_concepts)
16210 {
16211 tree reqs = get_shorthand_constraints (current_template_parms);
16212 if (tree r = cp_parser_requires_clause_opt (parser))
16213 reqs = conjoin_constraints (reqs, normalize_expression (r));
16214 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
16215 }
16216
16217 /* Look for the `class' or 'typename' keywords. */
16218 cp_parser_type_parameter_key (parser);
16219 /* If the next token is an ellipsis, we have a template
16220 argument pack. */
16221 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16222 {
16223 /* Consume the `...' token. */
16224 cp_lexer_consume_token (parser->lexer);
16225 maybe_warn_variadic_templates ();
16226
16227 *is_parameter_pack = true;
16228 }
16229 /* If the next token is an `=', then there is a
16230 default-argument. If the next token is a `>', we are at
16231 the end of the parameter-list. If the next token is a `,',
16232 then we are at the end of this parameter. */
16233 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16234 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
16235 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16236 {
16237 identifier = cp_parser_identifier (parser);
16238 /* Treat invalid names as if the parameter were nameless. */
16239 if (identifier == error_mark_node)
16240 identifier = NULL_TREE;
16241 }
16242 else
16243 identifier = NULL_TREE;
16244
16245 /* Create the template parameter. */
16246 parameter = finish_template_template_parm (class_type_node,
16247 identifier);
16248
16249 /* If the next token is an `=', then there is a
16250 default-argument. */
16251 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16252 {
16253 default_argument
16254 = cp_parser_default_template_template_argument (parser);
16255
16256 /* Template parameter packs cannot have default
16257 arguments. */
16258 if (*is_parameter_pack)
16259 {
16260 if (identifier)
16261 error_at (token->location,
16262 "template parameter pack %qD cannot "
16263 "have a default argument",
16264 identifier);
16265 else
16266 error_at (token->location, "template parameter packs cannot "
16267 "have default arguments");
16268 default_argument = NULL_TREE;
16269 }
16270 }
16271 else
16272 default_argument = NULL_TREE;
16273
16274 /* Create the combined representation of the parameter and the
16275 default argument. */
16276 parameter = build_tree_list (default_argument, parameter);
16277 }
16278 break;
16279
16280 default:
16281 gcc_unreachable ();
16282 break;
16283 }
16284
16285 return parameter;
16286 }
16287
16288 /* Parse a template-id.
16289
16290 template-id:
16291 template-name < template-argument-list [opt] >
16292
16293 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16294 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16295 returned. Otherwise, if the template-name names a function, or set
16296 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16297 names a class, returns a TYPE_DECL for the specialization.
16298
16299 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16300 uninstantiated templates. */
16301
16302 static tree
16303 cp_parser_template_id (cp_parser *parser,
16304 bool template_keyword_p,
16305 bool check_dependency_p,
16306 enum tag_types tag_type,
16307 bool is_declaration)
16308 {
16309 tree templ;
16310 tree arguments;
16311 tree template_id;
16312 cp_token_position start_of_id = 0;
16313 cp_token *next_token = NULL, *next_token_2 = NULL;
16314 bool is_identifier;
16315
16316 /* If the next token corresponds to a template-id, there is no need
16317 to reparse it. */
16318 cp_token *token = cp_lexer_peek_token (parser->lexer);
16319 if (token->type == CPP_TEMPLATE_ID)
16320 {
16321 cp_lexer_consume_token (parser->lexer);
16322 return saved_checks_value (token->u.tree_check_value);
16323 }
16324
16325 /* Avoid performing name lookup if there is no possibility of
16326 finding a template-id. */
16327 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16328 || (token->type == CPP_NAME
16329 && !cp_parser_nth_token_starts_template_argument_list_p
16330 (parser, 2)))
16331 {
16332 cp_parser_error (parser, "expected template-id");
16333 return error_mark_node;
16334 }
16335
16336 /* Remember where the template-id starts. */
16337 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16338 start_of_id = cp_lexer_token_position (parser->lexer, false);
16339
16340 push_deferring_access_checks (dk_deferred);
16341
16342 /* Parse the template-name. */
16343 is_identifier = false;
16344 templ = cp_parser_template_name (parser, template_keyword_p,
16345 check_dependency_p,
16346 is_declaration,
16347 tag_type,
16348 &is_identifier);
16349
16350 /* Push any access checks inside the firewall we're about to create. */
16351 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
16352 pop_deferring_access_checks ();
16353 if (templ == error_mark_node || is_identifier)
16354 return templ;
16355
16356 /* Since we're going to preserve any side-effects from this parse, set up a
16357 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16358 in the template arguments. */
16359 tentative_firewall firewall (parser);
16360 reopen_deferring_access_checks (checks);
16361
16362 /* If we find the sequence `[:' after a template-name, it's probably
16363 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16364 parse correctly the argument list. */
16365 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16366 == CPP_OPEN_SQUARE)
16367 && next_token->flags & DIGRAPH
16368 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16369 == CPP_COLON)
16370 && !(next_token_2->flags & PREV_WHITE))
16371 {
16372 cp_parser_parse_tentatively (parser);
16373 /* Change `:' into `::'. */
16374 next_token_2->type = CPP_SCOPE;
16375 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16376 CPP_LESS. */
16377 cp_lexer_consume_token (parser->lexer);
16378
16379 /* Parse the arguments. */
16380 arguments = cp_parser_enclosed_template_argument_list (parser);
16381 if (!cp_parser_parse_definitely (parser))
16382 {
16383 /* If we couldn't parse an argument list, then we revert our changes
16384 and return simply an error. Maybe this is not a template-id
16385 after all. */
16386 next_token_2->type = CPP_COLON;
16387 cp_parser_error (parser, "expected %<<%>");
16388 pop_deferring_access_checks ();
16389 return error_mark_node;
16390 }
16391 /* Otherwise, emit an error about the invalid digraph, but continue
16392 parsing because we got our argument list. */
16393 if (permerror (next_token->location,
16394 "%<<::%> cannot begin a template-argument list"))
16395 {
16396 static bool hint = false;
16397 inform (next_token->location,
16398 "%<<:%> is an alternate spelling for %<[%>."
16399 " Insert whitespace between %<<%> and %<::%>");
16400 if (!hint && !flag_permissive)
16401 {
16402 inform (next_token->location, "(if you use %<-fpermissive%> "
16403 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16404 "accept your code)");
16405 hint = true;
16406 }
16407 }
16408 }
16409 else
16410 {
16411 /* Look for the `<' that starts the template-argument-list. */
16412 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16413 {
16414 pop_deferring_access_checks ();
16415 return error_mark_node;
16416 }
16417 /* Parse the arguments. */
16418 arguments = cp_parser_enclosed_template_argument_list (parser);
16419
16420 if ((cxx_dialect > cxx17)
16421 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16422 && !template_keyword_p
16423 && (cp_parser_error_occurred (parser)
16424 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16425 {
16426 /* This didn't go well. */
16427 if (TREE_CODE (templ) == FUNCTION_DECL)
16428 {
16429 /* C++2A says that "function-name < a;" is now ill-formed. */
16430 if (cp_parser_error_occurred (parser))
16431 {
16432 error_at (token->location, "invalid template-argument-list");
16433 inform (token->location, "function name as the left hand "
16434 "operand of %<<%> is ill-formed in C++2a; wrap the "
16435 "function name in %<()%>");
16436 }
16437 else
16438 /* We expect "f<targs>" to be followed by "(args)". */
16439 error_at (cp_lexer_peek_token (parser->lexer)->location,
16440 "expected %<(%> after template-argument-list");
16441 if (start_of_id)
16442 /* Purge all subsequent tokens. */
16443 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16444 }
16445 else
16446 cp_parser_simulate_error (parser);
16447 pop_deferring_access_checks ();
16448 return error_mark_node;
16449 }
16450 }
16451
16452 /* Set the location to be of the form:
16453 template-name < template-argument-list [opt] >
16454 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16455 with caret == start at the start of the template-name,
16456 ranging until the closing '>'. */
16457 location_t finish_loc
16458 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16459 location_t combined_loc
16460 = make_location (token->location, token->location, finish_loc);
16461
16462 /* Check for concepts autos where they don't belong. We could
16463 identify types in some cases of idnetifier TEMPL, looking ahead
16464 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16465 types. We reject them in functions, but if what we have is an
16466 identifier, even with none_type we can't conclude it's NOT a
16467 type, we have to wait for template substitution. */
16468 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16469 template_id = error_mark_node;
16470 /* Build a representation of the specialization. */
16471 else if (identifier_p (templ))
16472 template_id = build_min_nt_loc (combined_loc,
16473 TEMPLATE_ID_EXPR,
16474 templ, arguments);
16475 else if (DECL_TYPE_TEMPLATE_P (templ)
16476 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16477 {
16478 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16479 template (rather than some instantiation thereof) only if
16480 is not nested within some other construct. For example, in
16481 "template <typename T> void f(T) { A<T>::", A<T> is just an
16482 instantiation of A. */
16483 bool entering_scope
16484 = (template_parm_scope_p ()
16485 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16486 template_id
16487 = finish_template_type (templ, arguments, entering_scope);
16488 }
16489 /* A template-like identifier may be a partial concept id. */
16490 else if (flag_concepts
16491 && (template_id = (cp_parser_maybe_partial_concept_id
16492 (parser, templ, arguments))))
16493 return template_id;
16494 else if (variable_template_p (templ))
16495 {
16496 template_id = lookup_template_variable (templ, arguments);
16497 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16498 SET_EXPR_LOCATION (template_id, combined_loc);
16499 }
16500 else
16501 {
16502 /* If it's not a class-template or a template-template, it should be
16503 a function-template. */
16504 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
16505
16506 template_id = lookup_template_function (templ, arguments);
16507 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16508 SET_EXPR_LOCATION (template_id, combined_loc);
16509 }
16510
16511 /* If parsing tentatively, replace the sequence of tokens that makes
16512 up the template-id with a CPP_TEMPLATE_ID token. That way,
16513 should we re-parse the token stream, we will not have to repeat
16514 the effort required to do the parse, nor will we issue duplicate
16515 error messages about problems during instantiation of the
16516 template. */
16517 if (start_of_id
16518 /* Don't do this if we had a parse error in a declarator; re-parsing
16519 might succeed if a name changes meaning (60361). */
16520 && !(cp_parser_error_occurred (parser)
16521 && cp_parser_parsing_tentatively (parser)
16522 && parser->in_declarator_p))
16523 {
16524 /* Reset the contents of the START_OF_ID token. */
16525 token->type = CPP_TEMPLATE_ID;
16526 token->location = combined_loc;
16527
16528 /* Retrieve any deferred checks. Do not pop this access checks yet
16529 so the memory will not be reclaimed during token replacing below. */
16530 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16531 token->u.tree_check_value->value = template_id;
16532 token->u.tree_check_value->checks = get_deferred_access_checks ();
16533 token->keyword = RID_MAX;
16534
16535 /* Purge all subsequent tokens. */
16536 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16537
16538 /* ??? Can we actually assume that, if template_id ==
16539 error_mark_node, we will have issued a diagnostic to the
16540 user, as opposed to simply marking the tentative parse as
16541 failed? */
16542 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16543 error_at (token->location, "parse error in template argument list");
16544 }
16545
16546 pop_to_parent_deferring_access_checks ();
16547 return template_id;
16548 }
16549
16550 /* Parse a template-name.
16551
16552 template-name:
16553 identifier
16554
16555 The standard should actually say:
16556
16557 template-name:
16558 identifier
16559 operator-function-id
16560
16561 A defect report has been filed about this issue.
16562
16563 A conversion-function-id cannot be a template name because they cannot
16564 be part of a template-id. In fact, looking at this code:
16565
16566 a.operator K<int>()
16567
16568 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16569 It is impossible to call a templated conversion-function-id with an
16570 explicit argument list, since the only allowed template parameter is
16571 the type to which it is converting.
16572
16573 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16574 `template' keyword, in a construction like:
16575
16576 T::template f<3>()
16577
16578 In that case `f' is taken to be a template-name, even though there
16579 is no way of knowing for sure.
16580
16581 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16582 name refers to a set of overloaded functions, at least one of which
16583 is a template, or an IDENTIFIER_NODE with the name of the template,
16584 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16585 names are looked up inside uninstantiated templates. */
16586
16587 static tree
16588 cp_parser_template_name (cp_parser* parser,
16589 bool template_keyword_p,
16590 bool check_dependency_p,
16591 bool is_declaration,
16592 enum tag_types tag_type,
16593 bool *is_identifier)
16594 {
16595 tree identifier;
16596 tree decl;
16597 cp_token *token = cp_lexer_peek_token (parser->lexer);
16598
16599 /* If the next token is `operator', then we have either an
16600 operator-function-id or a conversion-function-id. */
16601 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16602 {
16603 /* We don't know whether we're looking at an
16604 operator-function-id or a conversion-function-id. */
16605 cp_parser_parse_tentatively (parser);
16606 /* Try an operator-function-id. */
16607 identifier = cp_parser_operator_function_id (parser);
16608 /* If that didn't work, try a conversion-function-id. */
16609 if (!cp_parser_parse_definitely (parser))
16610 {
16611 cp_parser_error (parser, "expected template-name");
16612 return error_mark_node;
16613 }
16614 }
16615 /* Look for the identifier. */
16616 else
16617 identifier = cp_parser_identifier (parser);
16618
16619 /* If we didn't find an identifier, we don't have a template-id. */
16620 if (identifier == error_mark_node)
16621 return error_mark_node;
16622
16623 /* If the name immediately followed the `template' keyword, then it
16624 is a template-name. However, if the next token is not `<', then
16625 we do not treat it as a template-name, since it is not being used
16626 as part of a template-id. This enables us to handle constructs
16627 like:
16628
16629 template <typename T> struct S { S(); };
16630 template <typename T> S<T>::S();
16631
16632 correctly. We would treat `S' as a template -- if it were `S<T>'
16633 -- but we do not if there is no `<'. */
16634
16635 if (processing_template_decl
16636 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16637 {
16638 /* In a declaration, in a dependent context, we pretend that the
16639 "template" keyword was present in order to improve error
16640 recovery. For example, given:
16641
16642 template <typename T> void f(T::X<int>);
16643
16644 we want to treat "X<int>" as a template-id. */
16645 if (is_declaration
16646 && !template_keyword_p
16647 && parser->scope && TYPE_P (parser->scope)
16648 && check_dependency_p
16649 && dependent_scope_p (parser->scope)
16650 /* Do not do this for dtors (or ctors), since they never
16651 need the template keyword before their name. */
16652 && !constructor_name_p (identifier, parser->scope))
16653 {
16654 cp_token_position start = 0;
16655
16656 /* Explain what went wrong. */
16657 error_at (token->location, "non-template %qD used as template",
16658 identifier);
16659 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16660 parser->scope, identifier);
16661 /* If parsing tentatively, find the location of the "<" token. */
16662 if (cp_parser_simulate_error (parser))
16663 start = cp_lexer_token_position (parser->lexer, true);
16664 /* Parse the template arguments so that we can issue error
16665 messages about them. */
16666 cp_lexer_consume_token (parser->lexer);
16667 cp_parser_enclosed_template_argument_list (parser);
16668 /* Skip tokens until we find a good place from which to
16669 continue parsing. */
16670 cp_parser_skip_to_closing_parenthesis (parser,
16671 /*recovering=*/true,
16672 /*or_comma=*/true,
16673 /*consume_paren=*/false);
16674 /* If parsing tentatively, permanently remove the
16675 template argument list. That will prevent duplicate
16676 error messages from being issued about the missing
16677 "template" keyword. */
16678 if (start)
16679 cp_lexer_purge_tokens_after (parser->lexer, start);
16680 if (is_identifier)
16681 *is_identifier = true;
16682 parser->context->object_type = NULL_TREE;
16683 return identifier;
16684 }
16685
16686 /* If the "template" keyword is present, then there is generally
16687 no point in doing name-lookup, so we just return IDENTIFIER.
16688 But, if the qualifying scope is non-dependent then we can
16689 (and must) do name-lookup normally. */
16690 if (template_keyword_p)
16691 {
16692 tree scope = (parser->scope ? parser->scope
16693 : parser->context->object_type);
16694 if (scope && TYPE_P (scope)
16695 && (!CLASS_TYPE_P (scope)
16696 || (check_dependency_p && dependent_type_p (scope))))
16697 {
16698 /* We're optimizing away the call to cp_parser_lookup_name, but
16699 we still need to do this. */
16700 parser->context->object_type = NULL_TREE;
16701 return identifier;
16702 }
16703 }
16704 }
16705
16706 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16707 const bool scoped_p = ((parser->scope ? parser->scope
16708 : parser->context->object_type) != NULL_TREE);
16709
16710 /* Look up the name. */
16711 decl = cp_parser_lookup_name (parser, identifier,
16712 tag_type,
16713 /*is_template=*/true,
16714 /*is_namespace=*/false,
16715 check_dependency_p,
16716 /*ambiguous_decls=*/NULL,
16717 token->location);
16718
16719 decl = strip_using_decl (decl);
16720
16721 /* If DECL is a template, then the name was a template-name. */
16722 if (TREE_CODE (decl) == TEMPLATE_DECL)
16723 {
16724 if (TREE_DEPRECATED (decl)
16725 && deprecated_state != DEPRECATED_SUPPRESS)
16726 warn_deprecated_use (decl, NULL_TREE);
16727 }
16728 else
16729 {
16730 /* The standard does not explicitly indicate whether a name that
16731 names a set of overloaded declarations, some of which are
16732 templates, is a template-name. However, such a name should
16733 be a template-name; otherwise, there is no way to form a
16734 template-id for the overloaded templates. */
16735 bool found = false;
16736
16737 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16738 !found && iter; ++iter)
16739 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16740 found = true;
16741
16742 if (!found
16743 && (cxx_dialect > cxx17)
16744 && !scoped_p
16745 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
16746 && tag_type == none_type)
16747 {
16748 /* [temp.names] says "A name is also considered to refer to a template
16749 if it is an unqualified-id followed by a < and name lookup finds
16750 either one or more functions or finds nothing." */
16751
16752 /* The "more functions" case. Just use the OVERLOAD as normally.
16753 We don't use is_overloaded_fn here to avoid considering
16754 BASELINKs. */
16755 if (TREE_CODE (decl) == OVERLOAD
16756 /* Name lookup found one function. */
16757 || TREE_CODE (decl) == FUNCTION_DECL)
16758 found = true;
16759 /* Name lookup found nothing. */
16760 else if (decl == error_mark_node)
16761 return identifier;
16762 }
16763
16764 if (!found)
16765 {
16766 /* The name does not name a template. */
16767 cp_parser_error (parser, "expected template-name");
16768 return error_mark_node;
16769 }
16770 }
16771
16772 return decl;
16773 }
16774
16775 /* Parse a template-argument-list.
16776
16777 template-argument-list:
16778 template-argument ... [opt]
16779 template-argument-list , template-argument ... [opt]
16780
16781 Returns a TREE_VEC containing the arguments. */
16782
16783 static tree
16784 cp_parser_template_argument_list (cp_parser* parser)
16785 {
16786 tree fixed_args[10];
16787 unsigned n_args = 0;
16788 unsigned alloced = 10;
16789 tree *arg_ary = fixed_args;
16790 tree vec;
16791 bool saved_in_template_argument_list_p;
16792 bool saved_ice_p;
16793 bool saved_non_ice_p;
16794
16795 /* Don't create location wrapper nodes within a template-argument-list. */
16796 auto_suppress_location_wrappers sentinel;
16797
16798 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16799 parser->in_template_argument_list_p = true;
16800 /* Even if the template-id appears in an integral
16801 constant-expression, the contents of the argument list do
16802 not. */
16803 saved_ice_p = parser->integral_constant_expression_p;
16804 parser->integral_constant_expression_p = false;
16805 saved_non_ice_p = parser->non_integral_constant_expression_p;
16806 parser->non_integral_constant_expression_p = false;
16807
16808 /* Parse the arguments. */
16809 do
16810 {
16811 tree argument;
16812
16813 if (n_args)
16814 /* Consume the comma. */
16815 cp_lexer_consume_token (parser->lexer);
16816
16817 /* Parse the template-argument. */
16818 argument = cp_parser_template_argument (parser);
16819
16820 /* If the next token is an ellipsis, we're expanding a template
16821 argument pack. */
16822 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16823 {
16824 if (argument == error_mark_node)
16825 {
16826 cp_token *token = cp_lexer_peek_token (parser->lexer);
16827 error_at (token->location,
16828 "expected parameter pack before %<...%>");
16829 }
16830 /* Consume the `...' token. */
16831 cp_lexer_consume_token (parser->lexer);
16832
16833 /* Make the argument into a TYPE_PACK_EXPANSION or
16834 EXPR_PACK_EXPANSION. */
16835 argument = make_pack_expansion (argument);
16836 }
16837
16838 if (n_args == alloced)
16839 {
16840 alloced *= 2;
16841
16842 if (arg_ary == fixed_args)
16843 {
16844 arg_ary = XNEWVEC (tree, alloced);
16845 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16846 }
16847 else
16848 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16849 }
16850 arg_ary[n_args++] = argument;
16851 }
16852 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16853
16854 vec = make_tree_vec (n_args);
16855
16856 while (n_args--)
16857 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16858
16859 if (arg_ary != fixed_args)
16860 free (arg_ary);
16861 parser->non_integral_constant_expression_p = saved_non_ice_p;
16862 parser->integral_constant_expression_p = saved_ice_p;
16863 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16864 if (CHECKING_P)
16865 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16866 return vec;
16867 }
16868
16869 /* Parse a template-argument.
16870
16871 template-argument:
16872 assignment-expression
16873 type-id
16874 id-expression
16875
16876 The representation is that of an assignment-expression, type-id, or
16877 id-expression -- except that the qualified id-expression is
16878 evaluated, so that the value returned is either a DECL or an
16879 OVERLOAD.
16880
16881 Although the standard says "assignment-expression", it forbids
16882 throw-expressions or assignments in the template argument.
16883 Therefore, we use "conditional-expression" instead. */
16884
16885 static tree
16886 cp_parser_template_argument (cp_parser* parser)
16887 {
16888 tree argument;
16889 bool template_p;
16890 bool address_p;
16891 bool maybe_type_id = false;
16892 cp_token *token = NULL, *argument_start_token = NULL;
16893 location_t loc = 0;
16894 cp_id_kind idk;
16895
16896 /* There's really no way to know what we're looking at, so we just
16897 try each alternative in order.
16898
16899 [temp.arg]
16900
16901 In a template-argument, an ambiguity between a type-id and an
16902 expression is resolved to a type-id, regardless of the form of
16903 the corresponding template-parameter.
16904
16905 Therefore, we try a type-id first. */
16906 cp_parser_parse_tentatively (parser);
16907 argument = cp_parser_template_type_arg (parser);
16908 /* If there was no error parsing the type-id but the next token is a
16909 '>>', our behavior depends on which dialect of C++ we're
16910 parsing. In C++98, we probably found a typo for '> >'. But there
16911 are type-id which are also valid expressions. For instance:
16912
16913 struct X { int operator >> (int); };
16914 template <int V> struct Foo {};
16915 Foo<X () >> 5> r;
16916
16917 Here 'X()' is a valid type-id of a function type, but the user just
16918 wanted to write the expression "X() >> 5". Thus, we remember that we
16919 found a valid type-id, but we still try to parse the argument as an
16920 expression to see what happens.
16921
16922 In C++0x, the '>>' will be considered two separate '>'
16923 tokens. */
16924 if (!cp_parser_error_occurred (parser)
16925 && cxx_dialect == cxx98
16926 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16927 {
16928 maybe_type_id = true;
16929 cp_parser_abort_tentative_parse (parser);
16930 }
16931 else
16932 {
16933 /* If the next token isn't a `,' or a `>', then this argument wasn't
16934 really finished. This means that the argument is not a valid
16935 type-id. */
16936 if (!cp_parser_next_token_ends_template_argument_p (parser))
16937 cp_parser_error (parser, "expected template-argument");
16938 /* If that worked, we're done. */
16939 if (cp_parser_parse_definitely (parser))
16940 return argument;
16941 }
16942 /* We're still not sure what the argument will be. */
16943 cp_parser_parse_tentatively (parser);
16944 /* Try a template. */
16945 argument_start_token = cp_lexer_peek_token (parser->lexer);
16946 argument = cp_parser_id_expression (parser,
16947 /*template_keyword_p=*/false,
16948 /*check_dependency_p=*/true,
16949 &template_p,
16950 /*declarator_p=*/false,
16951 /*optional_p=*/false);
16952 /* If the next token isn't a `,' or a `>', then this argument wasn't
16953 really finished. */
16954 if (!cp_parser_next_token_ends_template_argument_p (parser))
16955 cp_parser_error (parser, "expected template-argument");
16956 if (!cp_parser_error_occurred (parser))
16957 {
16958 /* Figure out what is being referred to. If the id-expression
16959 was for a class template specialization, then we will have a
16960 TYPE_DECL at this point. There is no need to do name lookup
16961 at this point in that case. */
16962 if (TREE_CODE (argument) != TYPE_DECL)
16963 argument = cp_parser_lookup_name (parser, argument,
16964 none_type,
16965 /*is_template=*/template_p,
16966 /*is_namespace=*/false,
16967 /*check_dependency=*/true,
16968 /*ambiguous_decls=*/NULL,
16969 argument_start_token->location);
16970 /* Handle a constrained-type-specifier for a non-type template
16971 parameter. */
16972 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16973 argument = decl;
16974 else if (TREE_CODE (argument) != TEMPLATE_DECL
16975 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16976 cp_parser_error (parser, "expected template-name");
16977 }
16978 if (cp_parser_parse_definitely (parser))
16979 {
16980 if (TREE_DEPRECATED (argument))
16981 warn_deprecated_use (argument, NULL_TREE);
16982 return argument;
16983 }
16984 /* It must be a non-type argument. In C++17 any constant-expression is
16985 allowed. */
16986 if (cxx_dialect > cxx14)
16987 goto general_expr;
16988
16989 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16990
16991 -- an integral constant-expression of integral or enumeration
16992 type; or
16993
16994 -- the name of a non-type template-parameter; or
16995
16996 -- the name of an object or function with external linkage...
16997
16998 -- the address of an object or function with external linkage...
16999
17000 -- a pointer to member... */
17001 /* Look for a non-type template parameter. */
17002 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17003 {
17004 cp_parser_parse_tentatively (parser);
17005 argument = cp_parser_primary_expression (parser,
17006 /*address_p=*/false,
17007 /*cast_p=*/false,
17008 /*template_arg_p=*/true,
17009 &idk);
17010 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
17011 || !cp_parser_next_token_ends_template_argument_p (parser))
17012 cp_parser_simulate_error (parser);
17013 if (cp_parser_parse_definitely (parser))
17014 return argument;
17015 }
17016
17017 /* If the next token is "&", the argument must be the address of an
17018 object or function with external linkage. */
17019 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
17020 if (address_p)
17021 {
17022 loc = cp_lexer_peek_token (parser->lexer)->location;
17023 cp_lexer_consume_token (parser->lexer);
17024 }
17025 /* See if we might have an id-expression. */
17026 token = cp_lexer_peek_token (parser->lexer);
17027 if (token->type == CPP_NAME
17028 || token->keyword == RID_OPERATOR
17029 || token->type == CPP_SCOPE
17030 || token->type == CPP_TEMPLATE_ID
17031 || token->type == CPP_NESTED_NAME_SPECIFIER)
17032 {
17033 cp_parser_parse_tentatively (parser);
17034 argument = cp_parser_primary_expression (parser,
17035 address_p,
17036 /*cast_p=*/false,
17037 /*template_arg_p=*/true,
17038 &idk);
17039 if (cp_parser_error_occurred (parser)
17040 || !cp_parser_next_token_ends_template_argument_p (parser))
17041 cp_parser_abort_tentative_parse (parser);
17042 else
17043 {
17044 tree probe;
17045
17046 if (INDIRECT_REF_P (argument))
17047 {
17048 /* Strip the dereference temporarily. */
17049 gcc_assert (REFERENCE_REF_P (argument));
17050 argument = TREE_OPERAND (argument, 0);
17051 }
17052
17053 /* If we're in a template, we represent a qualified-id referring
17054 to a static data member as a SCOPE_REF even if the scope isn't
17055 dependent so that we can check access control later. */
17056 probe = argument;
17057 if (TREE_CODE (probe) == SCOPE_REF)
17058 probe = TREE_OPERAND (probe, 1);
17059 if (VAR_P (probe))
17060 {
17061 /* A variable without external linkage might still be a
17062 valid constant-expression, so no error is issued here
17063 if the external-linkage check fails. */
17064 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
17065 cp_parser_simulate_error (parser);
17066 }
17067 else if (is_overloaded_fn (argument))
17068 /* All overloaded functions are allowed; if the external
17069 linkage test does not pass, an error will be issued
17070 later. */
17071 ;
17072 else if (address_p
17073 && (TREE_CODE (argument) == OFFSET_REF
17074 || TREE_CODE (argument) == SCOPE_REF))
17075 /* A pointer-to-member. */
17076 ;
17077 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
17078 ;
17079 else
17080 cp_parser_simulate_error (parser);
17081
17082 if (cp_parser_parse_definitely (parser))
17083 {
17084 if (address_p)
17085 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
17086 tf_warning_or_error);
17087 else
17088 argument = convert_from_reference (argument);
17089 return argument;
17090 }
17091 }
17092 }
17093 /* If the argument started with "&", there are no other valid
17094 alternatives at this point. */
17095 if (address_p)
17096 {
17097 cp_parser_error (parser, "invalid non-type template argument");
17098 return error_mark_node;
17099 }
17100
17101 general_expr:
17102 /* If the argument wasn't successfully parsed as a type-id followed
17103 by '>>', the argument can only be a constant expression now.
17104 Otherwise, we try parsing the constant-expression tentatively,
17105 because the argument could really be a type-id. */
17106 if (maybe_type_id)
17107 cp_parser_parse_tentatively (parser);
17108
17109 if (cxx_dialect <= cxx14)
17110 argument = cp_parser_constant_expression (parser);
17111 else
17112 {
17113 /* In C++20, we can encounter a braced-init-list. */
17114 if (cxx_dialect >= cxx2a
17115 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17116 {
17117 bool expr_non_constant_p;
17118 return cp_parser_braced_list (parser, &expr_non_constant_p);
17119 }
17120
17121 /* With C++17 generalized non-type template arguments we need to handle
17122 lvalue constant expressions, too. */
17123 argument = cp_parser_assignment_expression (parser);
17124 require_potential_constant_expression (argument);
17125 }
17126
17127 if (!maybe_type_id)
17128 return argument;
17129 if (!cp_parser_next_token_ends_template_argument_p (parser))
17130 cp_parser_error (parser, "expected template-argument");
17131 if (cp_parser_parse_definitely (parser))
17132 return argument;
17133 /* We did our best to parse the argument as a non type-id, but that
17134 was the only alternative that matched (albeit with a '>' after
17135 it). We can assume it's just a typo from the user, and a
17136 diagnostic will then be issued. */
17137 return cp_parser_template_type_arg (parser);
17138 }
17139
17140 /* Parse an explicit-instantiation.
17141
17142 explicit-instantiation:
17143 template declaration
17144
17145 Although the standard says `declaration', what it really means is:
17146
17147 explicit-instantiation:
17148 template decl-specifier-seq [opt] declarator [opt] ;
17149
17150 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17151 supposed to be allowed. A defect report has been filed about this
17152 issue.
17153
17154 GNU Extension:
17155
17156 explicit-instantiation:
17157 storage-class-specifier template
17158 decl-specifier-seq [opt] declarator [opt] ;
17159 function-specifier template
17160 decl-specifier-seq [opt] declarator [opt] ; */
17161
17162 static void
17163 cp_parser_explicit_instantiation (cp_parser* parser)
17164 {
17165 int declares_class_or_enum;
17166 cp_decl_specifier_seq decl_specifiers;
17167 tree extension_specifier = NULL_TREE;
17168
17169 timevar_push (TV_TEMPLATE_INST);
17170
17171 /* Look for an (optional) storage-class-specifier or
17172 function-specifier. */
17173 if (cp_parser_allow_gnu_extensions_p (parser))
17174 {
17175 extension_specifier
17176 = cp_parser_storage_class_specifier_opt (parser);
17177 if (!extension_specifier)
17178 extension_specifier
17179 = cp_parser_function_specifier_opt (parser,
17180 /*decl_specs=*/NULL);
17181 }
17182
17183 /* Look for the `template' keyword. */
17184 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17185 /* Let the front end know that we are processing an explicit
17186 instantiation. */
17187 begin_explicit_instantiation ();
17188 /* [temp.explicit] says that we are supposed to ignore access
17189 control while processing explicit instantiation directives. */
17190 push_deferring_access_checks (dk_no_check);
17191 /* Parse a decl-specifier-seq. */
17192 cp_parser_decl_specifier_seq (parser,
17193 CP_PARSER_FLAGS_OPTIONAL,
17194 &decl_specifiers,
17195 &declares_class_or_enum);
17196 /* If there was exactly one decl-specifier, and it declared a class,
17197 and there's no declarator, then we have an explicit type
17198 instantiation. */
17199 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
17200 {
17201 tree type;
17202
17203 type = check_tag_decl (&decl_specifiers,
17204 /*explicit_type_instantiation_p=*/true);
17205 /* Turn access control back on for names used during
17206 template instantiation. */
17207 pop_deferring_access_checks ();
17208 if (type)
17209 do_type_instantiation (type, extension_specifier,
17210 /*complain=*/tf_error);
17211 }
17212 else
17213 {
17214 cp_declarator *declarator;
17215 tree decl;
17216
17217 /* Parse the declarator. */
17218 declarator
17219 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17220 CP_PARSER_FLAGS_NONE,
17221 /*ctor_dtor_or_conv_p=*/NULL,
17222 /*parenthesized_p=*/NULL,
17223 /*member_p=*/false,
17224 /*friend_p=*/false,
17225 /*static_p=*/false);
17226 if (declares_class_or_enum & 2)
17227 cp_parser_check_for_definition_in_return_type (declarator,
17228 decl_specifiers.type,
17229 decl_specifiers.locations[ds_type_spec]);
17230 if (declarator != cp_error_declarator)
17231 {
17232 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
17233 permerror (decl_specifiers.locations[ds_inline],
17234 "explicit instantiation shall not use"
17235 " %<inline%> specifier");
17236 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
17237 permerror (decl_specifiers.locations[ds_constexpr],
17238 "explicit instantiation shall not use"
17239 " %<constexpr%> specifier");
17240
17241 decl = grokdeclarator (declarator, &decl_specifiers,
17242 NORMAL, 0, &decl_specifiers.attributes);
17243 /* Turn access control back on for names used during
17244 template instantiation. */
17245 pop_deferring_access_checks ();
17246 /* Do the explicit instantiation. */
17247 do_decl_instantiation (decl, extension_specifier);
17248 }
17249 else
17250 {
17251 pop_deferring_access_checks ();
17252 /* Skip the body of the explicit instantiation. */
17253 cp_parser_skip_to_end_of_statement (parser);
17254 }
17255 }
17256 /* We're done with the instantiation. */
17257 end_explicit_instantiation ();
17258
17259 cp_parser_consume_semicolon_at_end_of_statement (parser);
17260
17261 timevar_pop (TV_TEMPLATE_INST);
17262 }
17263
17264 /* Parse an explicit-specialization.
17265
17266 explicit-specialization:
17267 template < > declaration
17268
17269 Although the standard says `declaration', what it really means is:
17270
17271 explicit-specialization:
17272 template <> decl-specifier [opt] init-declarator [opt] ;
17273 template <> function-definition
17274 template <> explicit-specialization
17275 template <> template-declaration */
17276
17277 static void
17278 cp_parser_explicit_specialization (cp_parser* parser)
17279 {
17280 bool need_lang_pop;
17281 cp_token *token = cp_lexer_peek_token (parser->lexer);
17282
17283 /* Look for the `template' keyword. */
17284 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17285 /* Look for the `<'. */
17286 cp_parser_require (parser, CPP_LESS, RT_LESS);
17287 /* Look for the `>'. */
17288 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17289 /* We have processed another parameter list. */
17290 ++parser->num_template_parameter_lists;
17291 /* [temp]
17292
17293 A template ... explicit specialization ... shall not have C
17294 linkage. */
17295 if (current_lang_name == lang_name_c)
17296 {
17297 error_at (token->location, "template specialization with C linkage");
17298 maybe_show_extern_c_location ();
17299 /* Give it C++ linkage to avoid confusing other parts of the
17300 front end. */
17301 push_lang_context (lang_name_cplusplus);
17302 need_lang_pop = true;
17303 }
17304 else
17305 need_lang_pop = false;
17306 /* Let the front end know that we are beginning a specialization. */
17307 if (!begin_specialization ())
17308 {
17309 end_specialization ();
17310 return;
17311 }
17312
17313 /* If the next keyword is `template', we need to figure out whether
17314 or not we're looking a template-declaration. */
17315 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17316 {
17317 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17318 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
17319 cp_parser_template_declaration_after_export (parser,
17320 /*member_p=*/false);
17321 else
17322 cp_parser_explicit_specialization (parser);
17323 }
17324 else
17325 /* Parse the dependent declaration. */
17326 cp_parser_single_declaration (parser,
17327 /*checks=*/NULL,
17328 /*member_p=*/false,
17329 /*explicit_specialization_p=*/true,
17330 /*friend_p=*/NULL);
17331 /* We're done with the specialization. */
17332 end_specialization ();
17333 /* For the erroneous case of a template with C linkage, we pushed an
17334 implicit C++ linkage scope; exit that scope now. */
17335 if (need_lang_pop)
17336 pop_lang_context ();
17337 /* We're done with this parameter list. */
17338 --parser->num_template_parameter_lists;
17339 }
17340
17341 /* Parse a type-specifier.
17342
17343 type-specifier:
17344 simple-type-specifier
17345 class-specifier
17346 enum-specifier
17347 elaborated-type-specifier
17348 cv-qualifier
17349
17350 GNU Extension:
17351
17352 type-specifier:
17353 __complex__
17354
17355 Returns a representation of the type-specifier. For a
17356 class-specifier, enum-specifier, or elaborated-type-specifier, a
17357 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17358
17359 The parser flags FLAGS is used to control type-specifier parsing.
17360
17361 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17362 in a decl-specifier-seq.
17363
17364 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17365 class-specifier, enum-specifier, or elaborated-type-specifier, then
17366 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17367 if a type is declared; 2 if it is defined. Otherwise, it is set to
17368 zero.
17369
17370 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17371 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17372 is set to FALSE. */
17373
17374 static tree
17375 cp_parser_type_specifier (cp_parser* parser,
17376 cp_parser_flags flags,
17377 cp_decl_specifier_seq *decl_specs,
17378 bool is_declaration,
17379 int* declares_class_or_enum,
17380 bool* is_cv_qualifier)
17381 {
17382 tree type_spec = NULL_TREE;
17383 cp_token *token;
17384 enum rid keyword;
17385 cp_decl_spec ds = ds_last;
17386
17387 /* Assume this type-specifier does not declare a new type. */
17388 if (declares_class_or_enum)
17389 *declares_class_or_enum = 0;
17390 /* And that it does not specify a cv-qualifier. */
17391 if (is_cv_qualifier)
17392 *is_cv_qualifier = false;
17393 /* Peek at the next token. */
17394 token = cp_lexer_peek_token (parser->lexer);
17395
17396 /* If we're looking at a keyword, we can use that to guide the
17397 production we choose. */
17398 keyword = token->keyword;
17399 switch (keyword)
17400 {
17401 case RID_ENUM:
17402 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17403 goto elaborated_type_specifier;
17404
17405 /* Look for the enum-specifier. */
17406 type_spec = cp_parser_enum_specifier (parser);
17407 /* If that worked, we're done. */
17408 if (type_spec)
17409 {
17410 if (declares_class_or_enum)
17411 *declares_class_or_enum = 2;
17412 if (decl_specs)
17413 cp_parser_set_decl_spec_type (decl_specs,
17414 type_spec,
17415 token,
17416 /*type_definition_p=*/true);
17417 return type_spec;
17418 }
17419 else
17420 goto elaborated_type_specifier;
17421
17422 /* Any of these indicate either a class-specifier, or an
17423 elaborated-type-specifier. */
17424 case RID_CLASS:
17425 case RID_STRUCT:
17426 case RID_UNION:
17427 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17428 goto elaborated_type_specifier;
17429
17430 /* Parse tentatively so that we can back up if we don't find a
17431 class-specifier. */
17432 cp_parser_parse_tentatively (parser);
17433 /* Look for the class-specifier. */
17434 type_spec = cp_parser_class_specifier (parser);
17435 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17436 /* If that worked, we're done. */
17437 if (cp_parser_parse_definitely (parser))
17438 {
17439 if (declares_class_or_enum)
17440 *declares_class_or_enum = 2;
17441 if (decl_specs)
17442 cp_parser_set_decl_spec_type (decl_specs,
17443 type_spec,
17444 token,
17445 /*type_definition_p=*/true);
17446 return type_spec;
17447 }
17448
17449 /* Fall through. */
17450 elaborated_type_specifier:
17451 /* We're declaring (not defining) a class or enum. */
17452 if (declares_class_or_enum)
17453 *declares_class_or_enum = 1;
17454
17455 /* Fall through. */
17456 case RID_TYPENAME:
17457 /* Look for an elaborated-type-specifier. */
17458 type_spec
17459 = (cp_parser_elaborated_type_specifier
17460 (parser,
17461 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17462 is_declaration));
17463 if (decl_specs)
17464 cp_parser_set_decl_spec_type (decl_specs,
17465 type_spec,
17466 token,
17467 /*type_definition_p=*/false);
17468 return type_spec;
17469
17470 case RID_CONST:
17471 ds = ds_const;
17472 if (is_cv_qualifier)
17473 *is_cv_qualifier = true;
17474 break;
17475
17476 case RID_VOLATILE:
17477 ds = ds_volatile;
17478 if (is_cv_qualifier)
17479 *is_cv_qualifier = true;
17480 break;
17481
17482 case RID_RESTRICT:
17483 ds = ds_restrict;
17484 if (is_cv_qualifier)
17485 *is_cv_qualifier = true;
17486 break;
17487
17488 case RID_COMPLEX:
17489 /* The `__complex__' keyword is a GNU extension. */
17490 ds = ds_complex;
17491 break;
17492
17493 default:
17494 break;
17495 }
17496
17497 /* Handle simple keywords. */
17498 if (ds != ds_last)
17499 {
17500 if (decl_specs)
17501 {
17502 set_and_check_decl_spec_loc (decl_specs, ds, token);
17503 decl_specs->any_specifiers_p = true;
17504 }
17505 return cp_lexer_consume_token (parser->lexer)->u.value;
17506 }
17507
17508 /* If we do not already have a type-specifier, assume we are looking
17509 at a simple-type-specifier. */
17510 type_spec = cp_parser_simple_type_specifier (parser,
17511 decl_specs,
17512 flags);
17513
17514 /* If we didn't find a type-specifier, and a type-specifier was not
17515 optional in this context, issue an error message. */
17516 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17517 {
17518 cp_parser_error (parser, "expected type specifier");
17519 return error_mark_node;
17520 }
17521
17522 return type_spec;
17523 }
17524
17525 /* Parse a simple-type-specifier.
17526
17527 simple-type-specifier:
17528 :: [opt] nested-name-specifier [opt] type-name
17529 :: [opt] nested-name-specifier template template-id
17530 char
17531 wchar_t
17532 bool
17533 short
17534 int
17535 long
17536 signed
17537 unsigned
17538 float
17539 double
17540 void
17541
17542 C++11 Extension:
17543
17544 simple-type-specifier:
17545 auto
17546 decltype ( expression )
17547 char16_t
17548 char32_t
17549 __underlying_type ( type-id )
17550
17551 C++17 extension:
17552
17553 nested-name-specifier(opt) template-name
17554
17555 GNU Extension:
17556
17557 simple-type-specifier:
17558 __int128
17559 __typeof__ unary-expression
17560 __typeof__ ( type-id )
17561 __typeof__ ( type-id ) { initializer-list , [opt] }
17562
17563 Concepts Extension:
17564
17565 simple-type-specifier:
17566 constrained-type-specifier
17567
17568 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17569 appropriately updated. */
17570
17571 static tree
17572 cp_parser_simple_type_specifier (cp_parser* parser,
17573 cp_decl_specifier_seq *decl_specs,
17574 cp_parser_flags flags)
17575 {
17576 tree type = NULL_TREE;
17577 cp_token *token;
17578 int idx;
17579
17580 /* Peek at the next token. */
17581 token = cp_lexer_peek_token (parser->lexer);
17582
17583 /* If we're looking at a keyword, things are easy. */
17584 switch (token->keyword)
17585 {
17586 case RID_CHAR:
17587 if (decl_specs)
17588 decl_specs->explicit_char_p = true;
17589 type = char_type_node;
17590 break;
17591 case RID_CHAR8:
17592 type = char8_type_node;
17593 break;
17594 case RID_CHAR16:
17595 type = char16_type_node;
17596 break;
17597 case RID_CHAR32:
17598 type = char32_type_node;
17599 break;
17600 case RID_WCHAR:
17601 type = wchar_type_node;
17602 break;
17603 case RID_BOOL:
17604 type = boolean_type_node;
17605 break;
17606 case RID_SHORT:
17607 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17608 type = short_integer_type_node;
17609 break;
17610 case RID_INT:
17611 if (decl_specs)
17612 decl_specs->explicit_int_p = true;
17613 type = integer_type_node;
17614 break;
17615 case RID_INT_N_0:
17616 case RID_INT_N_1:
17617 case RID_INT_N_2:
17618 case RID_INT_N_3:
17619 idx = token->keyword - RID_INT_N_0;
17620 if (! int_n_enabled_p [idx])
17621 break;
17622 if (decl_specs)
17623 {
17624 decl_specs->explicit_intN_p = true;
17625 decl_specs->int_n_idx = idx;
17626 /* Check if the alternate "__intN__" form has been used instead of
17627 "__intN". */
17628 if (strncmp (IDENTIFIER_POINTER (token->u.value)
17629 + (IDENTIFIER_LENGTH (token->u.value) - 2),
17630 "__", 2) == 0)
17631 decl_specs->int_n_alt = true;
17632 }
17633 type = int_n_trees [idx].signed_type;
17634 break;
17635 case RID_LONG:
17636 if (decl_specs)
17637 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17638 type = long_integer_type_node;
17639 break;
17640 case RID_SIGNED:
17641 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17642 type = integer_type_node;
17643 break;
17644 case RID_UNSIGNED:
17645 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17646 type = unsigned_type_node;
17647 break;
17648 case RID_FLOAT:
17649 type = float_type_node;
17650 break;
17651 case RID_DOUBLE:
17652 type = double_type_node;
17653 break;
17654 case RID_VOID:
17655 type = void_type_node;
17656 break;
17657
17658 case RID_AUTO:
17659 maybe_warn_cpp0x (CPP0X_AUTO);
17660 if (parser->auto_is_implicit_function_template_parm_p)
17661 {
17662 /* The 'auto' might be the placeholder return type for a function decl
17663 with trailing return type. */
17664 bool have_trailing_return_fn_decl = false;
17665
17666 cp_parser_parse_tentatively (parser);
17667 cp_lexer_consume_token (parser->lexer);
17668 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17669 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17670 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17671 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17672 {
17673 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17674 {
17675 cp_lexer_consume_token (parser->lexer);
17676 cp_parser_skip_to_closing_parenthesis (parser,
17677 /*recovering*/false,
17678 /*or_comma*/false,
17679 /*consume_paren*/true);
17680 continue;
17681 }
17682
17683 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17684 {
17685 have_trailing_return_fn_decl = true;
17686 break;
17687 }
17688
17689 cp_lexer_consume_token (parser->lexer);
17690 }
17691 cp_parser_abort_tentative_parse (parser);
17692
17693 if (have_trailing_return_fn_decl)
17694 {
17695 type = make_auto ();
17696 break;
17697 }
17698
17699 if (cxx_dialect >= cxx14)
17700 {
17701 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17702 type = TREE_TYPE (type);
17703 }
17704 else
17705 type = error_mark_node;
17706
17707 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17708 {
17709 if (cxx_dialect < cxx14)
17710 error_at (token->location,
17711 "use of %<auto%> in lambda parameter declaration "
17712 "only available with "
17713 "%<-std=c++14%> or %<-std=gnu++14%>");
17714 }
17715 else if (cxx_dialect < cxx14)
17716 error_at (token->location,
17717 "use of %<auto%> in parameter declaration "
17718 "only available with "
17719 "%<-std=c++14%> or %<-std=gnu++14%>");
17720 else if (!flag_concepts)
17721 pedwarn (token->location, 0,
17722 "use of %<auto%> in parameter declaration "
17723 "only available with %<-fconcepts%>");
17724 }
17725 else
17726 type = make_auto ();
17727 break;
17728
17729 case RID_DECLTYPE:
17730 /* Since DR 743, decltype can either be a simple-type-specifier by
17731 itself or begin a nested-name-specifier. Parsing it will replace
17732 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17733 handling below decide what to do. */
17734 cp_parser_decltype (parser);
17735 cp_lexer_set_token_position (parser->lexer, token);
17736 break;
17737
17738 case RID_TYPEOF:
17739 /* Consume the `typeof' token. */
17740 cp_lexer_consume_token (parser->lexer);
17741 /* Parse the operand to `typeof'. */
17742 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17743 /* If it is not already a TYPE, take its type. */
17744 if (!TYPE_P (type))
17745 type = finish_typeof (type);
17746
17747 if (decl_specs)
17748 cp_parser_set_decl_spec_type (decl_specs, type,
17749 token,
17750 /*type_definition_p=*/false);
17751
17752 return type;
17753
17754 case RID_UNDERLYING_TYPE:
17755 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17756 if (decl_specs)
17757 cp_parser_set_decl_spec_type (decl_specs, type,
17758 token,
17759 /*type_definition_p=*/false);
17760
17761 return type;
17762
17763 case RID_BASES:
17764 case RID_DIRECT_BASES:
17765 type = cp_parser_trait_expr (parser, token->keyword);
17766 if (decl_specs)
17767 cp_parser_set_decl_spec_type (decl_specs, type,
17768 token,
17769 /*type_definition_p=*/false);
17770 return type;
17771 default:
17772 break;
17773 }
17774
17775 /* If token is an already-parsed decltype not followed by ::,
17776 it's a simple-type-specifier. */
17777 if (token->type == CPP_DECLTYPE
17778 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17779 {
17780 type = saved_checks_value (token->u.tree_check_value);
17781 if (decl_specs)
17782 {
17783 cp_parser_set_decl_spec_type (decl_specs, type,
17784 token,
17785 /*type_definition_p=*/false);
17786 /* Remember that we are handling a decltype in order to
17787 implement the resolution of DR 1510 when the argument
17788 isn't instantiation dependent. */
17789 decl_specs->decltype_p = true;
17790 }
17791 cp_lexer_consume_token (parser->lexer);
17792 return type;
17793 }
17794
17795 /* If the type-specifier was for a built-in type, we're done. */
17796 if (type)
17797 {
17798 /* Record the type. */
17799 if (decl_specs
17800 && (token->keyword != RID_SIGNED
17801 && token->keyword != RID_UNSIGNED
17802 && token->keyword != RID_SHORT
17803 && token->keyword != RID_LONG))
17804 cp_parser_set_decl_spec_type (decl_specs,
17805 type,
17806 token,
17807 /*type_definition_p=*/false);
17808 if (decl_specs)
17809 decl_specs->any_specifiers_p = true;
17810
17811 /* Consume the token. */
17812 cp_lexer_consume_token (parser->lexer);
17813
17814 if (type == error_mark_node)
17815 return error_mark_node;
17816
17817 /* There is no valid C++ program where a non-template type is
17818 followed by a "<". That usually indicates that the user thought
17819 that the type was a template. */
17820 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17821 token->location);
17822
17823 return TYPE_NAME (type);
17824 }
17825
17826 /* The type-specifier must be a user-defined type. */
17827 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17828 {
17829 bool qualified_p;
17830 bool global_p;
17831 const bool typename_p = (cxx_dialect >= cxx2a
17832 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
17833
17834 /* Don't gobble tokens or issue error messages if this is an
17835 optional type-specifier. */
17836 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17837 cp_parser_parse_tentatively (parser);
17838
17839 token = cp_lexer_peek_token (parser->lexer);
17840
17841 /* Look for the optional `::' operator. */
17842 global_p
17843 = (cp_parser_global_scope_opt (parser,
17844 /*current_scope_valid_p=*/false)
17845 != NULL_TREE);
17846 /* Look for the nested-name specifier. */
17847 qualified_p
17848 = (cp_parser_nested_name_specifier_opt (parser,
17849 /*typename_keyword_p=*/false,
17850 /*check_dependency_p=*/true,
17851 /*type_p=*/false,
17852 /*is_declaration=*/false)
17853 != NULL_TREE);
17854 /* If we have seen a nested-name-specifier, and the next token
17855 is `template', then we are using the template-id production. */
17856 if (parser->scope
17857 && cp_parser_optional_template_keyword (parser))
17858 {
17859 /* Look for the template-id. */
17860 type = cp_parser_template_id (parser,
17861 /*template_keyword_p=*/true,
17862 /*check_dependency_p=*/true,
17863 none_type,
17864 /*is_declaration=*/false);
17865 /* If the template-id did not name a type, we are out of
17866 luck. */
17867 if (TREE_CODE (type) != TYPE_DECL)
17868 {
17869 /* ...unless we pretend we have seen 'typename'. */
17870 if (typename_p)
17871 type = cp_parser_make_typename_type (parser, type,
17872 token->location);
17873 else
17874 {
17875 cp_parser_error (parser, "expected template-id for type");
17876 type = error_mark_node;
17877 }
17878 }
17879 }
17880
17881 /* Otherwise, look for a type-name. */
17882 if (!type)
17883 {
17884 if (cxx_dialect >= cxx17)
17885 cp_parser_parse_tentatively (parser);
17886
17887 type = cp_parser_type_name (parser, (qualified_p && typename_p));
17888
17889 if (cxx_dialect >= cxx17 && !cp_parser_parse_definitely (parser))
17890 type = NULL_TREE;
17891 }
17892
17893 if (!type && cxx_dialect >= cxx17)
17894 {
17895 /* Try class template argument deduction. */
17896 tree name = cp_parser_identifier (parser);
17897 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17898 && parser->scope != error_mark_node)
17899 {
17900 tree tmpl = cp_parser_lookup_name (parser, name,
17901 none_type,
17902 /*is_template=*/false,
17903 /*is_namespace=*/false,
17904 /*check_dependency=*/true,
17905 /*ambiguous_decls=*/NULL,
17906 token->location);
17907 if (tmpl && tmpl != error_mark_node
17908 && (DECL_CLASS_TEMPLATE_P (tmpl)
17909 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17910 type = make_template_placeholder (tmpl);
17911 else
17912 {
17913 type = error_mark_node;
17914 if (!cp_parser_simulate_error (parser))
17915 cp_parser_name_lookup_error (parser, name, tmpl,
17916 NLE_TYPE, token->location);
17917 }
17918 }
17919 else
17920 type = error_mark_node;
17921 }
17922
17923 /* If it didn't work out, we don't have a TYPE. */
17924 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17925 && !cp_parser_parse_definitely (parser))
17926 type = NULL_TREE;
17927
17928 /* Keep track of all name-lookups performed in class scopes. */
17929 if (type
17930 && !global_p
17931 && !qualified_p
17932 && TREE_CODE (type) == TYPE_DECL
17933 && identifier_p (DECL_NAME (type)))
17934 maybe_note_name_used_in_class (DECL_NAME (type), type);
17935
17936 if (type && decl_specs)
17937 cp_parser_set_decl_spec_type (decl_specs, type,
17938 token,
17939 /*type_definition_p=*/false);
17940 }
17941
17942 /* If we didn't get a type-name, issue an error message. */
17943 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17944 {
17945 cp_parser_error (parser, "expected type-name");
17946 return error_mark_node;
17947 }
17948
17949 if (type && type != error_mark_node)
17950 {
17951 /* See if TYPE is an Objective-C type, and if so, parse and
17952 accept any protocol references following it. Do this before
17953 the cp_parser_check_for_invalid_template_id() call, because
17954 Objective-C types can be followed by '<...>' which would
17955 enclose protocol names rather than template arguments, and so
17956 everything is fine. */
17957 if (c_dialect_objc () && !parser->scope
17958 && (objc_is_id (type) || objc_is_class_name (type)))
17959 {
17960 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17961 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17962
17963 /* Clobber the "unqualified" type previously entered into
17964 DECL_SPECS with the new, improved protocol-qualified version. */
17965 if (decl_specs)
17966 decl_specs->type = qual_type;
17967
17968 return qual_type;
17969 }
17970
17971 /* There is no valid C++ program where a non-template type is
17972 followed by a "<". That usually indicates that the user
17973 thought that the type was a template. */
17974 cp_parser_check_for_invalid_template_id (parser, type,
17975 none_type,
17976 token->location);
17977 }
17978
17979 return type;
17980 }
17981
17982 /* Parse a type-name.
17983
17984 type-name:
17985 class-name
17986 enum-name
17987 typedef-name
17988 simple-template-id [in c++0x]
17989
17990 enum-name:
17991 identifier
17992
17993 typedef-name:
17994 identifier
17995
17996 Concepts:
17997
17998 type-name:
17999 concept-name
18000 partial-concept-id
18001
18002 concept-name:
18003 identifier
18004
18005 Returns a TYPE_DECL for the type. */
18006
18007 static tree
18008 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
18009 {
18010 tree type_decl;
18011
18012 /* We can't know yet whether it is a class-name or not. */
18013 cp_parser_parse_tentatively (parser);
18014 /* Try a class-name. */
18015 type_decl = cp_parser_class_name (parser,
18016 typename_keyword_p,
18017 /*template_keyword_p=*/false,
18018 none_type,
18019 /*check_dependency_p=*/true,
18020 /*class_head_p=*/false,
18021 /*is_declaration=*/false);
18022 /* If it's not a class-name, keep looking. */
18023 if (!cp_parser_parse_definitely (parser))
18024 {
18025 if (cxx_dialect < cxx11)
18026 /* It must be a typedef-name or an enum-name. */
18027 return cp_parser_nonclass_name (parser);
18028
18029 cp_parser_parse_tentatively (parser);
18030 /* It is either a simple-template-id representing an
18031 instantiation of an alias template... */
18032 type_decl = cp_parser_template_id (parser,
18033 /*template_keyword_p=*/false,
18034 /*check_dependency_p=*/true,
18035 none_type,
18036 /*is_declaration=*/false);
18037 /* Note that this must be an instantiation of an alias template
18038 because [temp.names]/6 says:
18039
18040 A template-id that names an alias template specialization
18041 is a type-name.
18042
18043 Whereas [temp.names]/7 says:
18044
18045 A simple-template-id that names a class template
18046 specialization is a class-name.
18047
18048 With concepts, this could also be a partial-concept-id that
18049 declares a non-type template parameter. */
18050 if (type_decl != NULL_TREE
18051 && TREE_CODE (type_decl) == TYPE_DECL
18052 && TYPE_DECL_ALIAS_P (type_decl))
18053 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
18054 else if (is_constrained_parameter (type_decl))
18055 /* Don't do anything. */ ;
18056 else
18057 cp_parser_simulate_error (parser);
18058
18059 if (!cp_parser_parse_definitely (parser))
18060 /* ... Or a typedef-name or an enum-name. */
18061 return cp_parser_nonclass_name (parser);
18062 }
18063
18064 return type_decl;
18065 }
18066
18067 /* Check if DECL and ARGS can form a constrained-type-specifier.
18068 If ARGS is non-null, we try to form a concept check of the
18069 form DECL<?, ARGS> where ? is a wildcard that matches any
18070 kind of template argument. If ARGS is NULL, then we try to
18071 form a concept check of the form DECL<?>. */
18072
18073 static tree
18074 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
18075 tree decl, tree args)
18076 {
18077 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
18078
18079 /* If we a constrained-type-specifier cannot be deduced. */
18080 if (parser->prevent_constrained_type_specifiers)
18081 return NULL_TREE;
18082
18083 /* A constrained type specifier can only be found in an
18084 overload set or as a reference to a template declaration.
18085
18086 FIXME: This might be masking a bug. It's possible that
18087 that the deduction below is causing template specializations
18088 to be formed with the wildcard as an argument. */
18089 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
18090 return NULL_TREE;
18091
18092 /* Try to build a call expression that evaluates the
18093 concept. This can fail if the overload set refers
18094 only to non-templates. */
18095 tree placeholder = build_nt (WILDCARD_DECL);
18096 tree check = build_concept_check (decl, placeholder, args);
18097 if (check == error_mark_node)
18098 return NULL_TREE;
18099
18100 /* Deduce the checked constraint and the prototype parameter.
18101
18102 FIXME: In certain cases, failure to deduce should be a
18103 diagnosable error. */
18104 tree conc;
18105 tree proto;
18106 if (!deduce_constrained_parameter (check, conc, proto))
18107 return NULL_TREE;
18108
18109 /* In template parameter scope, this results in a constrained
18110 parameter. Return a descriptor of that parm. */
18111 if (processing_template_parmlist)
18112 return build_constrained_parameter (conc, proto, args);
18113
18114 /* In a parameter-declaration-clause, constrained-type
18115 specifiers result in invented template parameters. */
18116 if (parser->auto_is_implicit_function_template_parm_p)
18117 {
18118 tree x = build_constrained_parameter (conc, proto, args);
18119 return synthesize_implicit_template_parm (parser, x);
18120 }
18121 else
18122 {
18123 /* Otherwise, we're in a context where the constrained
18124 type name is deduced and the constraint applies
18125 after deduction. */
18126 return make_constrained_auto (conc, args);
18127 }
18128
18129 return NULL_TREE;
18130 }
18131
18132 /* If DECL refers to a concept, return a TYPE_DECL representing
18133 the result of using the constrained type specifier in the
18134 current context. DECL refers to a concept if
18135
18136 - it is an overload set containing a function concept taking a single
18137 type argument, or
18138
18139 - it is a variable concept taking a single type argument. */
18140
18141 static tree
18142 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
18143 {
18144 if (flag_concepts
18145 && (TREE_CODE (decl) == OVERLOAD
18146 || BASELINK_P (decl)
18147 || variable_concept_p (decl)))
18148 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
18149 else
18150 return NULL_TREE;
18151 }
18152
18153 /* Check if DECL and ARGS form a partial-concept-id. If so,
18154 assign ID to the resulting constrained placeholder.
18155
18156 Returns true if the partial-concept-id designates a placeholder
18157 and false otherwise. Note that *id is set to NULL_TREE in
18158 this case. */
18159
18160 static tree
18161 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
18162 {
18163 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
18164 }
18165
18166 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18167 or a concept-name.
18168
18169 enum-name:
18170 identifier
18171
18172 typedef-name:
18173 identifier
18174
18175 concept-name:
18176 identifier
18177
18178 Returns a TYPE_DECL for the type. */
18179
18180 static tree
18181 cp_parser_nonclass_name (cp_parser* parser)
18182 {
18183 tree type_decl;
18184 tree identifier;
18185
18186 cp_token *token = cp_lexer_peek_token (parser->lexer);
18187 identifier = cp_parser_identifier (parser);
18188 if (identifier == error_mark_node)
18189 return error_mark_node;
18190
18191 /* Look up the type-name. */
18192 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
18193
18194 type_decl = strip_using_decl (type_decl);
18195
18196 /* If we found an overload set, then it may refer to a concept-name. */
18197 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
18198 type_decl = decl;
18199
18200 if (TREE_CODE (type_decl) != TYPE_DECL
18201 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
18202 {
18203 /* See if this is an Objective-C type. */
18204 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18205 tree type = objc_get_protocol_qualified_type (identifier, protos);
18206 if (type)
18207 type_decl = TYPE_NAME (type);
18208 }
18209
18210 /* Issue an error if we did not find a type-name. */
18211 if (TREE_CODE (type_decl) != TYPE_DECL
18212 /* In Objective-C, we have the complication that class names are
18213 normally type names and start declarations (eg, the
18214 "NSObject" in "NSObject *object;"), but can be used in an
18215 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18216 is an expression. So, a classname followed by a dot is not a
18217 valid type-name. */
18218 || (objc_is_class_name (TREE_TYPE (type_decl))
18219 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
18220 {
18221 if (!cp_parser_simulate_error (parser))
18222 cp_parser_name_lookup_error (parser, identifier, type_decl,
18223 NLE_TYPE, token->location);
18224 return error_mark_node;
18225 }
18226 /* Remember that the name was used in the definition of the
18227 current class so that we can check later to see if the
18228 meaning would have been different after the class was
18229 entirely defined. */
18230 else if (type_decl != error_mark_node
18231 && !parser->scope)
18232 maybe_note_name_used_in_class (identifier, type_decl);
18233
18234 return type_decl;
18235 }
18236
18237 /* Parse an elaborated-type-specifier. Note that the grammar given
18238 here incorporates the resolution to DR68.
18239
18240 elaborated-type-specifier:
18241 class-key :: [opt] nested-name-specifier [opt] identifier
18242 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18243 enum-key :: [opt] nested-name-specifier [opt] identifier
18244 typename :: [opt] nested-name-specifier identifier
18245 typename :: [opt] nested-name-specifier template [opt]
18246 template-id
18247
18248 GNU extension:
18249
18250 elaborated-type-specifier:
18251 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18252 class-key attributes :: [opt] nested-name-specifier [opt]
18253 template [opt] template-id
18254 enum attributes :: [opt] nested-name-specifier [opt] identifier
18255
18256 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18257 declared `friend'. If IS_DECLARATION is TRUE, then this
18258 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18259 something is being declared.
18260
18261 Returns the TYPE specified. */
18262
18263 static tree
18264 cp_parser_elaborated_type_specifier (cp_parser* parser,
18265 bool is_friend,
18266 bool is_declaration)
18267 {
18268 enum tag_types tag_type;
18269 tree identifier;
18270 tree type = NULL_TREE;
18271 tree attributes = NULL_TREE;
18272 tree globalscope;
18273 cp_token *token = NULL;
18274
18275 /* See if we're looking at the `enum' keyword. */
18276 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
18277 {
18278 /* Consume the `enum' token. */
18279 cp_lexer_consume_token (parser->lexer);
18280 /* Remember that it's an enumeration type. */
18281 tag_type = enum_type;
18282 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18283 enums) is used here. */
18284 cp_token *token = cp_lexer_peek_token (parser->lexer);
18285 if (cp_parser_is_keyword (token, RID_CLASS)
18286 || cp_parser_is_keyword (token, RID_STRUCT))
18287 {
18288 gcc_rich_location richloc (token->location);
18289 richloc.add_range (input_location);
18290 richloc.add_fixit_remove ();
18291 pedwarn (&richloc, 0, "elaborated-type-specifier for "
18292 "a scoped enum must not use the %qD keyword",
18293 token->u.value);
18294 /* Consume the `struct' or `class' and parse it anyway. */
18295 cp_lexer_consume_token (parser->lexer);
18296 }
18297 /* Parse the attributes. */
18298 attributes = cp_parser_attributes_opt (parser);
18299 }
18300 /* Or, it might be `typename'. */
18301 else if (cp_lexer_next_token_is_keyword (parser->lexer,
18302 RID_TYPENAME))
18303 {
18304 /* Consume the `typename' token. */
18305 cp_lexer_consume_token (parser->lexer);
18306 /* Remember that it's a `typename' type. */
18307 tag_type = typename_type;
18308 }
18309 /* Otherwise it must be a class-key. */
18310 else
18311 {
18312 tag_type = cp_parser_class_key (parser);
18313 if (tag_type == none_type)
18314 return error_mark_node;
18315 /* Parse the attributes. */
18316 attributes = cp_parser_attributes_opt (parser);
18317 }
18318
18319 /* Look for the `::' operator. */
18320 globalscope = cp_parser_global_scope_opt (parser,
18321 /*current_scope_valid_p=*/false);
18322 /* Look for the nested-name-specifier. */
18323 tree nested_name_specifier;
18324 if (tag_type == typename_type && !globalscope)
18325 {
18326 nested_name_specifier
18327 = cp_parser_nested_name_specifier (parser,
18328 /*typename_keyword_p=*/true,
18329 /*check_dependency_p=*/true,
18330 /*type_p=*/true,
18331 is_declaration);
18332 if (!nested_name_specifier)
18333 return error_mark_node;
18334 }
18335 else
18336 /* Even though `typename' is not present, the proposed resolution
18337 to Core Issue 180 says that in `class A<T>::B', `B' should be
18338 considered a type-name, even if `A<T>' is dependent. */
18339 nested_name_specifier
18340 = cp_parser_nested_name_specifier_opt (parser,
18341 /*typename_keyword_p=*/true,
18342 /*check_dependency_p=*/true,
18343 /*type_p=*/true,
18344 is_declaration);
18345 /* For everything but enumeration types, consider a template-id.
18346 For an enumeration type, consider only a plain identifier. */
18347 if (tag_type != enum_type)
18348 {
18349 bool template_p = false;
18350 tree decl;
18351
18352 /* Allow the `template' keyword. */
18353 template_p = cp_parser_optional_template_keyword (parser);
18354 /* If we didn't see `template', we don't know if there's a
18355 template-id or not. */
18356 if (!template_p)
18357 cp_parser_parse_tentatively (parser);
18358 /* The `template' keyword must follow a nested-name-specifier. */
18359 else if (!nested_name_specifier)
18360 {
18361 cp_parser_error (parser, "%<template%> must follow a nested-"
18362 "name-specifier");
18363 return error_mark_node;
18364 }
18365
18366 /* Parse the template-id. */
18367 token = cp_lexer_peek_token (parser->lexer);
18368 decl = cp_parser_template_id (parser, template_p,
18369 /*check_dependency_p=*/true,
18370 tag_type,
18371 is_declaration);
18372 /* If we didn't find a template-id, look for an ordinary
18373 identifier. */
18374 if (!template_p && !cp_parser_parse_definitely (parser))
18375 ;
18376 /* We can get here when cp_parser_template_id, called by
18377 cp_parser_class_name with tag_type == none_type, succeeds
18378 and caches a BASELINK. Then, when called again here,
18379 instead of failing and returning an error_mark_node
18380 returns it (see template/typename17.C in C++11).
18381 ??? Could we diagnose this earlier? */
18382 else if (tag_type == typename_type && BASELINK_P (decl))
18383 {
18384 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18385 type = error_mark_node;
18386 }
18387 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18388 in effect, then we must assume that, upon instantiation, the
18389 template will correspond to a class. */
18390 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18391 && tag_type == typename_type)
18392 type = make_typename_type (parser->scope, decl,
18393 typename_type,
18394 /*complain=*/tf_error);
18395 /* If the `typename' keyword is in effect and DECL is not a type
18396 decl, then type is non existent. */
18397 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18398 ;
18399 else if (TREE_CODE (decl) == TYPE_DECL)
18400 {
18401 type = check_elaborated_type_specifier (tag_type, decl,
18402 /*allow_template_p=*/true);
18403
18404 /* If the next token is a semicolon, this must be a specialization,
18405 instantiation, or friend declaration. Check the scope while we
18406 still know whether or not we had a nested-name-specifier. */
18407 if (type != error_mark_node
18408 && !nested_name_specifier && !is_friend
18409 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18410 check_unqualified_spec_or_inst (type, token->location);
18411 }
18412 else if (decl == error_mark_node)
18413 type = error_mark_node;
18414 }
18415
18416 if (!type)
18417 {
18418 token = cp_lexer_peek_token (parser->lexer);
18419 identifier = cp_parser_identifier (parser);
18420
18421 if (identifier == error_mark_node)
18422 {
18423 parser->scope = NULL_TREE;
18424 return error_mark_node;
18425 }
18426
18427 /* For a `typename', we needn't call xref_tag. */
18428 if (tag_type == typename_type
18429 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18430 return cp_parser_make_typename_type (parser, identifier,
18431 token->location);
18432
18433 /* Template parameter lists apply only if we are not within a
18434 function parameter list. */
18435 bool template_parm_lists_apply
18436 = parser->num_template_parameter_lists;
18437 if (template_parm_lists_apply)
18438 for (cp_binding_level *s = current_binding_level;
18439 s && s->kind != sk_template_parms;
18440 s = s->level_chain)
18441 if (s->kind == sk_function_parms)
18442 template_parm_lists_apply = false;
18443
18444 /* Look up a qualified name in the usual way. */
18445 if (parser->scope)
18446 {
18447 tree decl;
18448 tree ambiguous_decls;
18449
18450 decl = cp_parser_lookup_name (parser, identifier,
18451 tag_type,
18452 /*is_template=*/false,
18453 /*is_namespace=*/false,
18454 /*check_dependency=*/true,
18455 &ambiguous_decls,
18456 token->location);
18457
18458 /* If the lookup was ambiguous, an error will already have been
18459 issued. */
18460 if (ambiguous_decls)
18461 return error_mark_node;
18462
18463 /* If we are parsing friend declaration, DECL may be a
18464 TEMPLATE_DECL tree node here. However, we need to check
18465 whether this TEMPLATE_DECL results in valid code. Consider
18466 the following example:
18467
18468 namespace N {
18469 template <class T> class C {};
18470 }
18471 class X {
18472 template <class T> friend class N::C; // #1, valid code
18473 };
18474 template <class T> class Y {
18475 friend class N::C; // #2, invalid code
18476 };
18477
18478 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18479 name lookup of `N::C'. We see that friend declaration must
18480 be template for the code to be valid. Note that
18481 processing_template_decl does not work here since it is
18482 always 1 for the above two cases. */
18483
18484 decl = (cp_parser_maybe_treat_template_as_class
18485 (decl, /*tag_name_p=*/is_friend
18486 && template_parm_lists_apply));
18487
18488 if (TREE_CODE (decl) != TYPE_DECL)
18489 {
18490 cp_parser_diagnose_invalid_type_name (parser,
18491 identifier,
18492 token->location);
18493 return error_mark_node;
18494 }
18495
18496 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18497 {
18498 bool allow_template = (template_parm_lists_apply
18499 || DECL_SELF_REFERENCE_P (decl));
18500 type = check_elaborated_type_specifier (tag_type, decl,
18501 allow_template);
18502
18503 if (type == error_mark_node)
18504 return error_mark_node;
18505 }
18506
18507 /* Forward declarations of nested types, such as
18508
18509 class C1::C2;
18510 class C1::C2::C3;
18511
18512 are invalid unless all components preceding the final '::'
18513 are complete. If all enclosing types are complete, these
18514 declarations become merely pointless.
18515
18516 Invalid forward declarations of nested types are errors
18517 caught elsewhere in parsing. Those that are pointless arrive
18518 here. */
18519
18520 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18521 && !is_friend && !processing_explicit_instantiation)
18522 warning (0, "declaration %qD does not declare anything", decl);
18523
18524 type = TREE_TYPE (decl);
18525 }
18526 else
18527 {
18528 /* An elaborated-type-specifier sometimes introduces a new type and
18529 sometimes names an existing type. Normally, the rule is that it
18530 introduces a new type only if there is not an existing type of
18531 the same name already in scope. For example, given:
18532
18533 struct S {};
18534 void f() { struct S s; }
18535
18536 the `struct S' in the body of `f' is the same `struct S' as in
18537 the global scope; the existing definition is used. However, if
18538 there were no global declaration, this would introduce a new
18539 local class named `S'.
18540
18541 An exception to this rule applies to the following code:
18542
18543 namespace N { struct S; }
18544
18545 Here, the elaborated-type-specifier names a new type
18546 unconditionally; even if there is already an `S' in the
18547 containing scope this declaration names a new type.
18548 This exception only applies if the elaborated-type-specifier
18549 forms the complete declaration:
18550
18551 [class.name]
18552
18553 A declaration consisting solely of `class-key identifier ;' is
18554 either a redeclaration of the name in the current scope or a
18555 forward declaration of the identifier as a class name. It
18556 introduces the name into the current scope.
18557
18558 We are in this situation precisely when the next token is a `;'.
18559
18560 An exception to the exception is that a `friend' declaration does
18561 *not* name a new type; i.e., given:
18562
18563 struct S { friend struct T; };
18564
18565 `T' is not a new type in the scope of `S'.
18566
18567 Also, `new struct S' or `sizeof (struct S)' never results in the
18568 definition of a new type; a new type can only be declared in a
18569 declaration context. */
18570
18571 tag_scope ts;
18572 bool template_p;
18573
18574 if (is_friend)
18575 /* Friends have special name lookup rules. */
18576 ts = ts_within_enclosing_non_class;
18577 else if (is_declaration
18578 && cp_lexer_next_token_is (parser->lexer,
18579 CPP_SEMICOLON))
18580 /* This is a `class-key identifier ;' */
18581 ts = ts_current;
18582 else
18583 ts = ts_global;
18584
18585 template_p =
18586 (template_parm_lists_apply
18587 && (cp_parser_next_token_starts_class_definition_p (parser)
18588 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18589 /* An unqualified name was used to reference this type, so
18590 there were no qualifying templates. */
18591 if (template_parm_lists_apply
18592 && !cp_parser_check_template_parameters (parser,
18593 /*num_templates=*/0,
18594 /*template_id*/false,
18595 token->location,
18596 /*declarator=*/NULL))
18597 return error_mark_node;
18598 type = xref_tag (tag_type, identifier, ts, template_p);
18599 }
18600 }
18601
18602 if (type == error_mark_node)
18603 return error_mark_node;
18604
18605 /* Allow attributes on forward declarations of classes. */
18606 if (attributes)
18607 {
18608 if (TREE_CODE (type) == TYPENAME_TYPE)
18609 warning (OPT_Wattributes,
18610 "attributes ignored on uninstantiated type");
18611 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18612 && ! processing_explicit_instantiation)
18613 warning (OPT_Wattributes,
18614 "attributes ignored on template instantiation");
18615 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18616 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18617 else
18618 warning (OPT_Wattributes,
18619 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18620 }
18621
18622 if (tag_type != enum_type)
18623 {
18624 /* Indicate whether this class was declared as a `class' or as a
18625 `struct'. */
18626 if (CLASS_TYPE_P (type))
18627 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18628 cp_parser_check_class_key (tag_type, type);
18629 }
18630
18631 /* A "<" cannot follow an elaborated type specifier. If that
18632 happens, the user was probably trying to form a template-id. */
18633 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18634 token->location);
18635
18636 return type;
18637 }
18638
18639 /* Parse an enum-specifier.
18640
18641 enum-specifier:
18642 enum-head { enumerator-list [opt] }
18643 enum-head { enumerator-list , } [C++0x]
18644
18645 enum-head:
18646 enum-key identifier [opt] enum-base [opt]
18647 enum-key nested-name-specifier identifier enum-base [opt]
18648
18649 enum-key:
18650 enum
18651 enum class [C++0x]
18652 enum struct [C++0x]
18653
18654 enum-base: [C++0x]
18655 : type-specifier-seq
18656
18657 opaque-enum-specifier:
18658 enum-key identifier enum-base [opt] ;
18659
18660 GNU Extensions:
18661 enum-key attributes[opt] identifier [opt] enum-base [opt]
18662 { enumerator-list [opt] }attributes[opt]
18663 enum-key attributes[opt] identifier [opt] enum-base [opt]
18664 { enumerator-list, }attributes[opt] [C++0x]
18665
18666 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18667 if the token stream isn't an enum-specifier after all. */
18668
18669 static tree
18670 cp_parser_enum_specifier (cp_parser* parser)
18671 {
18672 tree identifier;
18673 tree type = NULL_TREE;
18674 tree prev_scope;
18675 tree nested_name_specifier = NULL_TREE;
18676 tree attributes;
18677 bool scoped_enum_p = false;
18678 bool has_underlying_type = false;
18679 bool nested_being_defined = false;
18680 bool new_value_list = false;
18681 bool is_new_type = false;
18682 bool is_unnamed = false;
18683 tree underlying_type = NULL_TREE;
18684 cp_token *type_start_token = NULL;
18685 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18686
18687 parser->colon_corrects_to_scope_p = false;
18688
18689 /* Parse tentatively so that we can back up if we don't find a
18690 enum-specifier. */
18691 cp_parser_parse_tentatively (parser);
18692
18693 /* Caller guarantees that the current token is 'enum', an identifier
18694 possibly follows, and the token after that is an opening brace.
18695 If we don't have an identifier, fabricate an anonymous name for
18696 the enumeration being defined. */
18697 cp_lexer_consume_token (parser->lexer);
18698
18699 /* Parse the "class" or "struct", which indicates a scoped
18700 enumeration type in C++0x. */
18701 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18702 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18703 {
18704 if (cxx_dialect < cxx11)
18705 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18706
18707 /* Consume the `struct' or `class' token. */
18708 cp_lexer_consume_token (parser->lexer);
18709
18710 scoped_enum_p = true;
18711 }
18712
18713 attributes = cp_parser_attributes_opt (parser);
18714
18715 /* Clear the qualification. */
18716 parser->scope = NULL_TREE;
18717 parser->qualifying_scope = NULL_TREE;
18718 parser->object_scope = NULL_TREE;
18719
18720 /* Figure out in what scope the declaration is being placed. */
18721 prev_scope = current_scope ();
18722
18723 type_start_token = cp_lexer_peek_token (parser->lexer);
18724
18725 push_deferring_access_checks (dk_no_check);
18726 nested_name_specifier
18727 = cp_parser_nested_name_specifier_opt (parser,
18728 /*typename_keyword_p=*/true,
18729 /*check_dependency_p=*/false,
18730 /*type_p=*/false,
18731 /*is_declaration=*/false);
18732
18733 if (nested_name_specifier)
18734 {
18735 tree name;
18736
18737 identifier = cp_parser_identifier (parser);
18738 name = cp_parser_lookup_name (parser, identifier,
18739 enum_type,
18740 /*is_template=*/false,
18741 /*is_namespace=*/false,
18742 /*check_dependency=*/true,
18743 /*ambiguous_decls=*/NULL,
18744 input_location);
18745 if (name && name != error_mark_node)
18746 {
18747 type = TREE_TYPE (name);
18748 if (TREE_CODE (type) == TYPENAME_TYPE)
18749 {
18750 /* Are template enums allowed in ISO? */
18751 if (template_parm_scope_p ())
18752 pedwarn (type_start_token->location, OPT_Wpedantic,
18753 "%qD is an enumeration template", name);
18754 /* ignore a typename reference, for it will be solved by name
18755 in start_enum. */
18756 type = NULL_TREE;
18757 }
18758 }
18759 else if (nested_name_specifier == error_mark_node)
18760 /* We already issued an error. */;
18761 else
18762 {
18763 error_at (type_start_token->location,
18764 "%qD does not name an enumeration in %qT",
18765 identifier, nested_name_specifier);
18766 nested_name_specifier = error_mark_node;
18767 }
18768 }
18769 else
18770 {
18771 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18772 identifier = cp_parser_identifier (parser);
18773 else
18774 {
18775 identifier = make_anon_name ();
18776 is_unnamed = true;
18777 if (scoped_enum_p)
18778 error_at (type_start_token->location,
18779 "unnamed scoped enum is not allowed");
18780 }
18781 }
18782 pop_deferring_access_checks ();
18783
18784 /* Check for the `:' that denotes a specified underlying type in C++0x.
18785 Note that a ':' could also indicate a bitfield width, however. */
18786 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18787 {
18788 cp_decl_specifier_seq type_specifiers;
18789
18790 /* Consume the `:'. */
18791 cp_lexer_consume_token (parser->lexer);
18792
18793 /* Parse the type-specifier-seq. */
18794 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
18795 /*is_declaration=*/false,
18796 /*is_trailing_return=*/false,
18797 &type_specifiers);
18798
18799 /* At this point this is surely not elaborated type specifier. */
18800 if (!cp_parser_parse_definitely (parser))
18801 return NULL_TREE;
18802
18803 if (cxx_dialect < cxx11)
18804 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18805
18806 has_underlying_type = true;
18807
18808 /* If that didn't work, stop. */
18809 if (type_specifiers.type != error_mark_node)
18810 {
18811 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18812 /*initialized=*/0, NULL);
18813 if (underlying_type == error_mark_node
18814 || check_for_bare_parameter_packs (underlying_type))
18815 underlying_type = NULL_TREE;
18816 }
18817 }
18818
18819 /* Look for the `{' but don't consume it yet. */
18820 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18821 {
18822 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18823 {
18824 cp_parser_error (parser, "expected %<{%>");
18825 if (has_underlying_type)
18826 {
18827 type = NULL_TREE;
18828 goto out;
18829 }
18830 }
18831 /* An opaque-enum-specifier must have a ';' here. */
18832 if ((scoped_enum_p || underlying_type)
18833 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18834 {
18835 cp_parser_error (parser, "expected %<;%> or %<{%>");
18836 if (has_underlying_type)
18837 {
18838 type = NULL_TREE;
18839 goto out;
18840 }
18841 }
18842 }
18843
18844 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18845 return NULL_TREE;
18846
18847 if (nested_name_specifier)
18848 {
18849 if (CLASS_TYPE_P (nested_name_specifier))
18850 {
18851 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18852 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18853 push_scope (nested_name_specifier);
18854 }
18855 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18856 {
18857 push_nested_namespace (nested_name_specifier);
18858 }
18859 }
18860
18861 /* Issue an error message if type-definitions are forbidden here. */
18862 if (!cp_parser_check_type_definition (parser))
18863 type = error_mark_node;
18864 else
18865 /* Create the new type. We do this before consuming the opening
18866 brace so the enum will be recorded as being on the line of its
18867 tag (or the 'enum' keyword, if there is no tag). */
18868 type = start_enum (identifier, type, underlying_type,
18869 attributes, scoped_enum_p, &is_new_type);
18870
18871 /* If the next token is not '{' it is an opaque-enum-specifier or an
18872 elaborated-type-specifier. */
18873 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18874 {
18875 timevar_push (TV_PARSE_ENUM);
18876 if (nested_name_specifier
18877 && nested_name_specifier != error_mark_node)
18878 {
18879 /* The following catches invalid code such as:
18880 enum class S<int>::E { A, B, C }; */
18881 if (!processing_specialization
18882 && CLASS_TYPE_P (nested_name_specifier)
18883 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18884 error_at (type_start_token->location, "cannot add an enumerator "
18885 "list to a template instantiation");
18886
18887 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18888 {
18889 error_at (type_start_token->location,
18890 "%<%T::%E%> has not been declared",
18891 TYPE_CONTEXT (nested_name_specifier),
18892 nested_name_specifier);
18893 type = error_mark_node;
18894 }
18895 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18896 && !CLASS_TYPE_P (nested_name_specifier))
18897 {
18898 error_at (type_start_token->location, "nested name specifier "
18899 "%qT for enum declaration does not name a class "
18900 "or namespace", nested_name_specifier);
18901 type = error_mark_node;
18902 }
18903 /* If that scope does not contain the scope in which the
18904 class was originally declared, the program is invalid. */
18905 else if (prev_scope && !is_ancestor (prev_scope,
18906 nested_name_specifier))
18907 {
18908 if (at_namespace_scope_p ())
18909 error_at (type_start_token->location,
18910 "declaration of %qD in namespace %qD which does not "
18911 "enclose %qD",
18912 type, prev_scope, nested_name_specifier);
18913 else
18914 error_at (type_start_token->location,
18915 "declaration of %qD in %qD which does not "
18916 "enclose %qD",
18917 type, prev_scope, nested_name_specifier);
18918 type = error_mark_node;
18919 }
18920 /* If that scope is the scope where the declaration is being placed
18921 the program is invalid. */
18922 else if (CLASS_TYPE_P (nested_name_specifier)
18923 && CLASS_TYPE_P (prev_scope)
18924 && same_type_p (nested_name_specifier, prev_scope))
18925 {
18926 permerror (type_start_token->location,
18927 "extra qualification not allowed");
18928 nested_name_specifier = NULL_TREE;
18929 }
18930 }
18931
18932 if (scoped_enum_p)
18933 begin_scope (sk_scoped_enum, type);
18934
18935 /* Consume the opening brace. */
18936 matching_braces braces;
18937 braces.consume_open (parser);
18938
18939 if (type == error_mark_node)
18940 ; /* Nothing to add */
18941 else if (OPAQUE_ENUM_P (type)
18942 || (cxx_dialect > cxx98 && processing_specialization))
18943 {
18944 new_value_list = true;
18945 SET_OPAQUE_ENUM_P (type, false);
18946 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18947 }
18948 else
18949 {
18950 error_at (type_start_token->location,
18951 "multiple definition of %q#T", type);
18952 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18953 "previous definition here");
18954 type = error_mark_node;
18955 }
18956
18957 if (type == error_mark_node)
18958 cp_parser_skip_to_end_of_block_or_statement (parser);
18959 /* If the next token is not '}', then there are some enumerators. */
18960 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18961 {
18962 if (is_unnamed && !scoped_enum_p)
18963 pedwarn (type_start_token->location, OPT_Wpedantic,
18964 "ISO C++ forbids empty unnamed enum");
18965 }
18966 else
18967 cp_parser_enumerator_list (parser, type);
18968
18969 /* Consume the final '}'. */
18970 braces.require_close (parser);
18971
18972 if (scoped_enum_p)
18973 finish_scope ();
18974 timevar_pop (TV_PARSE_ENUM);
18975 }
18976 else
18977 {
18978 /* If a ';' follows, then it is an opaque-enum-specifier
18979 and additional restrictions apply. */
18980 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18981 {
18982 if (is_unnamed)
18983 error_at (type_start_token->location,
18984 "opaque-enum-specifier without name");
18985 else if (nested_name_specifier)
18986 error_at (type_start_token->location,
18987 "opaque-enum-specifier must use a simple identifier");
18988 }
18989 }
18990
18991 /* Look for trailing attributes to apply to this enumeration, and
18992 apply them if appropriate. */
18993 if (cp_parser_allow_gnu_extensions_p (parser))
18994 {
18995 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18996 cplus_decl_attributes (&type,
18997 trailing_attr,
18998 (int) ATTR_FLAG_TYPE_IN_PLACE);
18999 }
19000
19001 /* Finish up the enumeration. */
19002 if (type != error_mark_node)
19003 {
19004 if (new_value_list)
19005 finish_enum_value_list (type);
19006 if (is_new_type)
19007 finish_enum (type);
19008 }
19009
19010 if (nested_name_specifier)
19011 {
19012 if (CLASS_TYPE_P (nested_name_specifier))
19013 {
19014 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
19015 pop_scope (nested_name_specifier);
19016 }
19017 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
19018 {
19019 pop_nested_namespace (nested_name_specifier);
19020 }
19021 }
19022 out:
19023 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19024 return type;
19025 }
19026
19027 /* Parse an enumerator-list. The enumerators all have the indicated
19028 TYPE.
19029
19030 enumerator-list:
19031 enumerator-definition
19032 enumerator-list , enumerator-definition */
19033
19034 static void
19035 cp_parser_enumerator_list (cp_parser* parser, tree type)
19036 {
19037 while (true)
19038 {
19039 /* Parse an enumerator-definition. */
19040 cp_parser_enumerator_definition (parser, type);
19041
19042 /* If the next token is not a ',', we've reached the end of
19043 the list. */
19044 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19045 break;
19046 /* Otherwise, consume the `,' and keep going. */
19047 cp_lexer_consume_token (parser->lexer);
19048 /* If the next token is a `}', there is a trailing comma. */
19049 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19050 {
19051 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
19052 pedwarn (input_location, OPT_Wpedantic,
19053 "comma at end of enumerator list");
19054 break;
19055 }
19056 }
19057 }
19058
19059 /* Parse an enumerator-definition. The enumerator has the indicated
19060 TYPE.
19061
19062 enumerator-definition:
19063 enumerator
19064 enumerator = constant-expression
19065
19066 enumerator:
19067 identifier
19068
19069 GNU Extensions:
19070
19071 enumerator-definition:
19072 enumerator attributes [opt]
19073 enumerator attributes [opt] = constant-expression */
19074
19075 static void
19076 cp_parser_enumerator_definition (cp_parser* parser, tree type)
19077 {
19078 tree identifier;
19079 tree value;
19080 location_t loc;
19081
19082 /* Save the input location because we are interested in the location
19083 of the identifier and not the location of the explicit value. */
19084 loc = cp_lexer_peek_token (parser->lexer)->location;
19085
19086 /* Look for the identifier. */
19087 identifier = cp_parser_identifier (parser);
19088 if (identifier == error_mark_node)
19089 return;
19090
19091 /* Parse any specified attributes. */
19092 tree attrs = cp_parser_attributes_opt (parser);
19093
19094 /* If the next token is an '=', then there is an explicit value. */
19095 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19096 {
19097 /* Consume the `=' token. */
19098 cp_lexer_consume_token (parser->lexer);
19099 /* Parse the value. */
19100 value = cp_parser_constant_expression (parser);
19101 }
19102 else
19103 value = NULL_TREE;
19104
19105 /* If we are processing a template, make sure the initializer of the
19106 enumerator doesn't contain any bare template parameter pack. */
19107 if (check_for_bare_parameter_packs (value))
19108 value = error_mark_node;
19109
19110 /* Create the enumerator. */
19111 build_enumerator (identifier, value, type, attrs, loc);
19112 }
19113
19114 /* Parse a namespace-name.
19115
19116 namespace-name:
19117 original-namespace-name
19118 namespace-alias
19119
19120 Returns the NAMESPACE_DECL for the namespace. */
19121
19122 static tree
19123 cp_parser_namespace_name (cp_parser* parser)
19124 {
19125 tree identifier;
19126 tree namespace_decl;
19127
19128 cp_token *token = cp_lexer_peek_token (parser->lexer);
19129
19130 /* Get the name of the namespace. */
19131 identifier = cp_parser_identifier (parser);
19132 if (identifier == error_mark_node)
19133 return error_mark_node;
19134
19135 /* Look up the identifier in the currently active scope. Look only
19136 for namespaces, due to:
19137
19138 [basic.lookup.udir]
19139
19140 When looking up a namespace-name in a using-directive or alias
19141 definition, only namespace names are considered.
19142
19143 And:
19144
19145 [basic.lookup.qual]
19146
19147 During the lookup of a name preceding the :: scope resolution
19148 operator, object, function, and enumerator names are ignored.
19149
19150 (Note that cp_parser_qualifying_entity only calls this
19151 function if the token after the name is the scope resolution
19152 operator.) */
19153 namespace_decl = cp_parser_lookup_name (parser, identifier,
19154 none_type,
19155 /*is_template=*/false,
19156 /*is_namespace=*/true,
19157 /*check_dependency=*/true,
19158 /*ambiguous_decls=*/NULL,
19159 token->location);
19160 /* If it's not a namespace, issue an error. */
19161 if (namespace_decl == error_mark_node
19162 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
19163 {
19164 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19165 {
19166 auto_diagnostic_group d;
19167 name_hint hint;
19168 if (namespace_decl == error_mark_node
19169 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
19170 hint = suggest_alternative_in_explicit_scope (token->location,
19171 identifier,
19172 parser->scope);
19173 if (const char *suggestion = hint.suggestion ())
19174 {
19175 gcc_rich_location richloc (token->location);
19176 richloc.add_fixit_replace (suggestion);
19177 error_at (&richloc,
19178 "%qD is not a namespace-name; did you mean %qs?",
19179 identifier, suggestion);
19180 }
19181 else
19182 error_at (token->location, "%qD is not a namespace-name",
19183 identifier);
19184 }
19185 else
19186 cp_parser_error (parser, "expected namespace-name");
19187 namespace_decl = error_mark_node;
19188 }
19189
19190 return namespace_decl;
19191 }
19192
19193 /* Parse a namespace-definition.
19194
19195 namespace-definition:
19196 named-namespace-definition
19197 unnamed-namespace-definition
19198
19199 named-namespace-definition:
19200 original-namespace-definition
19201 extension-namespace-definition
19202
19203 original-namespace-definition:
19204 namespace identifier { namespace-body }
19205
19206 extension-namespace-definition:
19207 namespace original-namespace-name { namespace-body }
19208
19209 unnamed-namespace-definition:
19210 namespace { namespace-body } */
19211
19212 static void
19213 cp_parser_namespace_definition (cp_parser* parser)
19214 {
19215 tree identifier;
19216 int nested_definition_count = 0;
19217
19218 cp_ensure_no_omp_declare_simd (parser);
19219 cp_ensure_no_oacc_routine (parser);
19220
19221 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
19222 const bool topmost_inline_p = is_inline;
19223
19224 if (is_inline)
19225 {
19226 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
19227 cp_lexer_consume_token (parser->lexer);
19228 }
19229
19230 /* Look for the `namespace' keyword. */
19231 cp_token* token
19232 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19233
19234 /* Parse any specified attributes before the identifier. */
19235 tree attribs = cp_parser_attributes_opt (parser);
19236
19237 for (;;)
19238 {
19239 identifier = NULL_TREE;
19240
19241 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
19242 RID_INLINE);
19243 if (nested_inline_p && nested_definition_count != 0)
19244 {
19245 if (cxx_dialect < cxx2a)
19246 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
19247 OPT_Wpedantic, "nested inline namespace definitions only "
19248 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
19249 cp_lexer_consume_token (parser->lexer);
19250 }
19251
19252 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19253 {
19254 identifier = cp_parser_identifier (parser);
19255
19256 if (cp_next_tokens_can_be_std_attribute_p (parser))
19257 pedwarn (input_location, OPT_Wpedantic,
19258 "standard attributes on namespaces must precede "
19259 "the namespace name");
19260
19261 /* Parse any attributes specified after the identifier. */
19262 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
19263 }
19264
19265 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19266 {
19267 /* Don't forget that the innermost namespace might have been
19268 marked as inline. Use |= because we cannot overwrite
19269 IS_INLINE in case the outermost namespace is inline, but
19270 there are no nested inlines. */
19271 is_inline |= nested_inline_p;
19272 break;
19273 }
19274
19275 if (!nested_definition_count && cxx_dialect < cxx17)
19276 pedwarn (input_location, OPT_Wpedantic,
19277 "nested namespace definitions only available with "
19278 "%<-std=c++17%> or %<-std=gnu++17%>");
19279
19280 /* Nested namespace names can create new namespaces (unlike
19281 other qualified-ids). */
19282 if (int count = (identifier
19283 ? push_namespace (identifier, nested_inline_p)
19284 : 0))
19285 nested_definition_count += count;
19286 else
19287 cp_parser_error (parser, "nested namespace name required");
19288 cp_lexer_consume_token (parser->lexer);
19289 }
19290
19291 if (nested_definition_count && !identifier)
19292 cp_parser_error (parser, "namespace name required");
19293
19294 if (nested_definition_count && attribs)
19295 error_at (token->location,
19296 "a nested namespace definition cannot have attributes");
19297 if (nested_definition_count && topmost_inline_p)
19298 error_at (token->location,
19299 "a nested namespace definition cannot be inline");
19300
19301 /* Start the namespace. */
19302 nested_definition_count += push_namespace (identifier, is_inline);
19303
19304 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
19305
19306 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
19307
19308 /* Look for the `{' to validate starting the namespace. */
19309 matching_braces braces;
19310 if (braces.require_open (parser))
19311 {
19312 /* Parse the body of the namespace. */
19313 cp_parser_namespace_body (parser);
19314
19315 /* Look for the final `}'. */
19316 braces.require_close (parser);
19317 }
19318
19319 if (has_visibility)
19320 pop_visibility (1);
19321
19322 /* Pop the nested namespace definitions. */
19323 while (nested_definition_count--)
19324 pop_namespace ();
19325 }
19326
19327 /* Parse a namespace-body.
19328
19329 namespace-body:
19330 declaration-seq [opt] */
19331
19332 static void
19333 cp_parser_namespace_body (cp_parser* parser)
19334 {
19335 cp_parser_declaration_seq_opt (parser);
19336 }
19337
19338 /* Parse a namespace-alias-definition.
19339
19340 namespace-alias-definition:
19341 namespace identifier = qualified-namespace-specifier ; */
19342
19343 static void
19344 cp_parser_namespace_alias_definition (cp_parser* parser)
19345 {
19346 tree identifier;
19347 tree namespace_specifier;
19348
19349 cp_token *token = cp_lexer_peek_token (parser->lexer);
19350
19351 /* Look for the `namespace' keyword. */
19352 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19353 /* Look for the identifier. */
19354 identifier = cp_parser_identifier (parser);
19355 if (identifier == error_mark_node)
19356 return;
19357 /* Look for the `=' token. */
19358 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
19359 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19360 {
19361 error_at (token->location, "%<namespace%> definition is not allowed here");
19362 /* Skip the definition. */
19363 cp_lexer_consume_token (parser->lexer);
19364 if (cp_parser_skip_to_closing_brace (parser))
19365 cp_lexer_consume_token (parser->lexer);
19366 return;
19367 }
19368 cp_parser_require (parser, CPP_EQ, RT_EQ);
19369 /* Look for the qualified-namespace-specifier. */
19370 namespace_specifier
19371 = cp_parser_qualified_namespace_specifier (parser);
19372 /* Look for the `;' token. */
19373 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19374
19375 /* Register the alias in the symbol table. */
19376 do_namespace_alias (identifier, namespace_specifier);
19377 }
19378
19379 /* Parse a qualified-namespace-specifier.
19380
19381 qualified-namespace-specifier:
19382 :: [opt] nested-name-specifier [opt] namespace-name
19383
19384 Returns a NAMESPACE_DECL corresponding to the specified
19385 namespace. */
19386
19387 static tree
19388 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19389 {
19390 /* Look for the optional `::'. */
19391 cp_parser_global_scope_opt (parser,
19392 /*current_scope_valid_p=*/false);
19393
19394 /* Look for the optional nested-name-specifier. */
19395 cp_parser_nested_name_specifier_opt (parser,
19396 /*typename_keyword_p=*/false,
19397 /*check_dependency_p=*/true,
19398 /*type_p=*/false,
19399 /*is_declaration=*/true);
19400
19401 return cp_parser_namespace_name (parser);
19402 }
19403
19404 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19405 access declaration.
19406
19407 using-declaration:
19408 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19409 using :: unqualified-id ;
19410
19411 access-declaration:
19412 qualified-id ;
19413
19414 */
19415
19416 static bool
19417 cp_parser_using_declaration (cp_parser* parser,
19418 bool access_declaration_p)
19419 {
19420 cp_token *token;
19421 bool typename_p = false;
19422 bool global_scope_p;
19423 tree decl;
19424 tree identifier;
19425 tree qscope;
19426 int oldcount = errorcount;
19427 cp_token *diag_token = NULL;
19428
19429 if (access_declaration_p)
19430 {
19431 diag_token = cp_lexer_peek_token (parser->lexer);
19432 cp_parser_parse_tentatively (parser);
19433 }
19434 else
19435 {
19436 /* Look for the `using' keyword. */
19437 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19438
19439 again:
19440 /* Peek at the next token. */
19441 token = cp_lexer_peek_token (parser->lexer);
19442 /* See if it's `typename'. */
19443 if (token->keyword == RID_TYPENAME)
19444 {
19445 /* Remember that we've seen it. */
19446 typename_p = true;
19447 /* Consume the `typename' token. */
19448 cp_lexer_consume_token (parser->lexer);
19449 }
19450 }
19451
19452 /* Look for the optional global scope qualification. */
19453 global_scope_p
19454 = (cp_parser_global_scope_opt (parser,
19455 /*current_scope_valid_p=*/false)
19456 != NULL_TREE);
19457
19458 /* If we saw `typename', or didn't see `::', then there must be a
19459 nested-name-specifier present. */
19460 if (typename_p || !global_scope_p)
19461 {
19462 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19463 /*check_dependency_p=*/true,
19464 /*type_p=*/false,
19465 /*is_declaration=*/true);
19466 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19467 {
19468 cp_parser_skip_to_end_of_block_or_statement (parser);
19469 return false;
19470 }
19471 }
19472 /* Otherwise, we could be in either of the two productions. In that
19473 case, treat the nested-name-specifier as optional. */
19474 else
19475 qscope = cp_parser_nested_name_specifier_opt (parser,
19476 /*typename_keyword_p=*/false,
19477 /*check_dependency_p=*/true,
19478 /*type_p=*/false,
19479 /*is_declaration=*/true);
19480 if (!qscope)
19481 qscope = global_namespace;
19482 else if (UNSCOPED_ENUM_P (qscope)
19483 && !TYPE_FUNCTION_SCOPE_P (qscope))
19484 qscope = CP_TYPE_CONTEXT (qscope);
19485
19486 if (access_declaration_p && cp_parser_error_occurred (parser))
19487 /* Something has already gone wrong; there's no need to parse
19488 further. Since an error has occurred, the return value of
19489 cp_parser_parse_definitely will be false, as required. */
19490 return cp_parser_parse_definitely (parser);
19491
19492 token = cp_lexer_peek_token (parser->lexer);
19493 /* Parse the unqualified-id. */
19494 identifier = cp_parser_unqualified_id (parser,
19495 /*template_keyword_p=*/false,
19496 /*check_dependency_p=*/true,
19497 /*declarator_p=*/true,
19498 /*optional_p=*/false);
19499
19500 if (access_declaration_p)
19501 {
19502 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19503 cp_parser_simulate_error (parser);
19504 if (!cp_parser_parse_definitely (parser))
19505 return false;
19506 }
19507 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19508 {
19509 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19510 if (cxx_dialect < cxx17
19511 && !in_system_header_at (ell->location))
19512 pedwarn (ell->location, 0,
19513 "pack expansion in using-declaration only available "
19514 "with %<-std=c++17%> or %<-std=gnu++17%>");
19515 qscope = make_pack_expansion (qscope);
19516 }
19517
19518 /* The function we call to handle a using-declaration is different
19519 depending on what scope we are in. */
19520 if (qscope == error_mark_node || identifier == error_mark_node)
19521 ;
19522 else if (!identifier_p (identifier)
19523 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19524 /* [namespace.udecl]
19525
19526 A using declaration shall not name a template-id. */
19527 error_at (token->location,
19528 "a template-id may not appear in a using-declaration");
19529 else
19530 {
19531 if (at_class_scope_p ())
19532 {
19533 /* Create the USING_DECL. */
19534 decl = do_class_using_decl (qscope, identifier);
19535
19536 if (decl && typename_p)
19537 USING_DECL_TYPENAME_P (decl) = 1;
19538
19539 if (check_for_bare_parameter_packs (decl))
19540 {
19541 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19542 return false;
19543 }
19544 else
19545 /* Add it to the list of members in this class. */
19546 finish_member_declaration (decl);
19547 }
19548 else
19549 finish_nonmember_using_decl (qscope, identifier);
19550 }
19551
19552 if (!access_declaration_p
19553 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19554 {
19555 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19556 if (cxx_dialect < cxx17)
19557 pedwarn (comma->location, 0,
19558 "comma-separated list in using-declaration only available "
19559 "with %<-std=c++17%> or %<-std=gnu++17%>");
19560 goto again;
19561 }
19562
19563 /* Look for the final `;'. */
19564 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19565
19566 if (access_declaration_p && errorcount == oldcount)
19567 warning_at (diag_token->location, OPT_Wdeprecated,
19568 "access declarations are deprecated "
19569 "in favour of using-declarations; "
19570 "suggestion: add the %<using%> keyword");
19571
19572 return true;
19573 }
19574
19575 /* Parse an alias-declaration.
19576
19577 alias-declaration:
19578 using identifier attribute-specifier-seq [opt] = type-id */
19579
19580 static tree
19581 cp_parser_alias_declaration (cp_parser* parser)
19582 {
19583 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19584 location_t id_location, type_location;
19585 cp_declarator *declarator;
19586 cp_decl_specifier_seq decl_specs;
19587 bool member_p;
19588 const char *saved_message = NULL;
19589
19590 /* Look for the `using' keyword. */
19591 cp_token *using_token
19592 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19593 if (using_token == NULL)
19594 return error_mark_node;
19595
19596 id_location = cp_lexer_peek_token (parser->lexer)->location;
19597 id = cp_parser_identifier (parser);
19598 if (id == error_mark_node)
19599 return error_mark_node;
19600
19601 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19602 attributes = cp_parser_attributes_opt (parser);
19603 if (attributes == error_mark_node)
19604 return error_mark_node;
19605
19606 cp_parser_require (parser, CPP_EQ, RT_EQ);
19607
19608 if (cp_parser_error_occurred (parser))
19609 return error_mark_node;
19610
19611 cp_parser_commit_to_tentative_parse (parser);
19612
19613 /* Now we are going to parse the type-id of the declaration. */
19614
19615 /*
19616 [dcl.type]/3 says:
19617
19618 "A type-specifier-seq shall not define a class or enumeration
19619 unless it appears in the type-id of an alias-declaration (7.1.3) that
19620 is not the declaration of a template-declaration."
19621
19622 In other words, if we currently are in an alias template, the
19623 type-id should not define a type.
19624
19625 So let's set parser->type_definition_forbidden_message in that
19626 case; cp_parser_check_type_definition (called by
19627 cp_parser_class_specifier) will then emit an error if a type is
19628 defined in the type-id. */
19629 if (parser->num_template_parameter_lists)
19630 {
19631 saved_message = parser->type_definition_forbidden_message;
19632 parser->type_definition_forbidden_message =
19633 G_("types may not be defined in alias template declarations");
19634 }
19635
19636 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
19637 &type_location);
19638
19639 /* Restore the error message if need be. */
19640 if (parser->num_template_parameter_lists)
19641 parser->type_definition_forbidden_message = saved_message;
19642
19643 if (type == error_mark_node
19644 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19645 {
19646 cp_parser_skip_to_end_of_block_or_statement (parser);
19647 return error_mark_node;
19648 }
19649
19650 /* A typedef-name can also be introduced by an alias-declaration. The
19651 identifier following the using keyword becomes a typedef-name. It has
19652 the same semantics as if it were introduced by the typedef
19653 specifier. In particular, it does not define a new type and it shall
19654 not appear in the type-id. */
19655
19656 clear_decl_specs (&decl_specs);
19657 decl_specs.type = type;
19658 if (attributes != NULL_TREE)
19659 {
19660 decl_specs.attributes = attributes;
19661 set_and_check_decl_spec_loc (&decl_specs,
19662 ds_attribute,
19663 attrs_token);
19664 }
19665 set_and_check_decl_spec_loc (&decl_specs,
19666 ds_typedef,
19667 using_token);
19668 set_and_check_decl_spec_loc (&decl_specs,
19669 ds_alias,
19670 using_token);
19671 decl_specs.locations[ds_type_spec] = type_location;
19672
19673 if (parser->num_template_parameter_lists
19674 && !cp_parser_check_template_parameters (parser,
19675 /*num_templates=*/0,
19676 /*template_id*/false,
19677 id_location,
19678 /*declarator=*/NULL))
19679 return error_mark_node;
19680
19681 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
19682
19683 member_p = at_class_scope_p ();
19684 if (member_p)
19685 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19686 NULL_TREE, attributes);
19687 else
19688 decl = start_decl (declarator, &decl_specs, 0,
19689 attributes, NULL_TREE, &pushed_scope);
19690 if (decl == error_mark_node)
19691 return decl;
19692
19693 // Attach constraints to the alias declaration.
19694 if (flag_concepts && current_template_parms)
19695 {
19696 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19697 tree constr = build_constraints (reqs, NULL_TREE);
19698 set_constraints (decl, constr);
19699 }
19700
19701 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19702
19703 if (pushed_scope)
19704 pop_scope (pushed_scope);
19705
19706 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19707 added into the symbol table; otherwise, return the TYPE_DECL. */
19708 if (DECL_LANG_SPECIFIC (decl)
19709 && DECL_TEMPLATE_INFO (decl)
19710 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19711 {
19712 decl = DECL_TI_TEMPLATE (decl);
19713 if (member_p)
19714 check_member_template (decl);
19715 }
19716
19717 return decl;
19718 }
19719
19720 /* Parse a using-directive.
19721
19722 using-directive:
19723 using namespace :: [opt] nested-name-specifier [opt]
19724 namespace-name ; */
19725
19726 static void
19727 cp_parser_using_directive (cp_parser* parser)
19728 {
19729 tree namespace_decl;
19730 tree attribs;
19731
19732 /* Look for the `using' keyword. */
19733 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19734 /* And the `namespace' keyword. */
19735 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19736 /* Look for the optional `::' operator. */
19737 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19738 /* And the optional nested-name-specifier. */
19739 cp_parser_nested_name_specifier_opt (parser,
19740 /*typename_keyword_p=*/false,
19741 /*check_dependency_p=*/true,
19742 /*type_p=*/false,
19743 /*is_declaration=*/true);
19744 /* Get the namespace being used. */
19745 namespace_decl = cp_parser_namespace_name (parser);
19746 /* And any specified attributes. */
19747 attribs = cp_parser_attributes_opt (parser);
19748
19749 /* Update the symbol table. */
19750 finish_using_directive (namespace_decl, attribs);
19751
19752 /* Look for the final `;'. */
19753 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19754 }
19755
19756 /* Parse an asm-definition.
19757
19758 asm-qualifier:
19759 volatile
19760 inline
19761 goto
19762
19763 asm-qualifier-list:
19764 asm-qualifier
19765 asm-qualifier-list asm-qualifier
19766
19767 asm-definition:
19768 asm ( string-literal ) ;
19769
19770 GNU Extension:
19771
19772 asm-definition:
19773 asm asm-qualifier-list [opt] ( string-literal ) ;
19774 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19775 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19776 : asm-operand-list [opt] ) ;
19777 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19778 : asm-operand-list [opt]
19779 : asm-clobber-list [opt] ) ;
19780 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19781 : asm-clobber-list [opt]
19782 : asm-goto-list ) ;
19783
19784 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19785 contains goto, and is the only allowed form in that case. No duplicates are
19786 allowed in an asm-qualifier-list. */
19787
19788 static void
19789 cp_parser_asm_definition (cp_parser* parser)
19790 {
19791 tree string;
19792 tree outputs = NULL_TREE;
19793 tree inputs = NULL_TREE;
19794 tree clobbers = NULL_TREE;
19795 tree labels = NULL_TREE;
19796 tree asm_stmt;
19797 bool extended_p = false;
19798 bool invalid_inputs_p = false;
19799 bool invalid_outputs_p = false;
19800 required_token missing = RT_NONE;
19801
19802 /* Look for the `asm' keyword. */
19803 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19804
19805 if (parser->in_function_body
19806 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19807 {
19808 error ("%<asm%> in %<constexpr%> function");
19809 cp_function_chain->invalid_constexpr = true;
19810 }
19811
19812 /* Handle the asm-qualifier-list. */
19813 location_t volatile_loc = UNKNOWN_LOCATION;
19814 location_t inline_loc = UNKNOWN_LOCATION;
19815 location_t goto_loc = UNKNOWN_LOCATION;
19816 location_t first_loc = UNKNOWN_LOCATION;
19817
19818 if (cp_parser_allow_gnu_extensions_p (parser))
19819 for (;;)
19820 {
19821 cp_token *token = cp_lexer_peek_token (parser->lexer);
19822 location_t loc = token->location;
19823 switch (cp_lexer_peek_token (parser->lexer)->keyword)
19824 {
19825 case RID_VOLATILE:
19826 if (volatile_loc)
19827 {
19828 error_at (loc, "duplicate %<asm%> qualifier %qT",
19829 token->u.value);
19830 inform (volatile_loc, "first seen here");
19831 }
19832 else
19833 {
19834 if (!parser->in_function_body)
19835 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
19836 "outside of function body", token->u.value);
19837 volatile_loc = loc;
19838 }
19839 cp_lexer_consume_token (parser->lexer);
19840 continue;
19841
19842 case RID_INLINE:
19843 if (inline_loc)
19844 {
19845 error_at (loc, "duplicate %<asm%> qualifier %qT",
19846 token->u.value);
19847 inform (inline_loc, "first seen here");
19848 }
19849 else
19850 inline_loc = loc;
19851 if (!first_loc)
19852 first_loc = loc;
19853 cp_lexer_consume_token (parser->lexer);
19854 continue;
19855
19856 case RID_GOTO:
19857 if (goto_loc)
19858 {
19859 error_at (loc, "duplicate %<asm%> qualifier %qT",
19860 token->u.value);
19861 inform (goto_loc, "first seen here");
19862 }
19863 else
19864 goto_loc = loc;
19865 if (!first_loc)
19866 first_loc = loc;
19867 cp_lexer_consume_token (parser->lexer);
19868 continue;
19869
19870 case RID_CONST:
19871 case RID_RESTRICT:
19872 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
19873 cp_lexer_consume_token (parser->lexer);
19874 continue;
19875
19876 default:
19877 break;
19878 }
19879 break;
19880 }
19881
19882 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
19883 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
19884 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
19885
19886 if (!parser->in_function_body && (inline_p || goto_p))
19887 {
19888 error_at (first_loc, "%<asm%> qualifier outside of function body");
19889 inline_p = goto_p = false;
19890 }
19891
19892 /* Look for the opening `('. */
19893 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19894 return;
19895 /* Look for the string. */
19896 string = cp_parser_string_literal (parser, false, false);
19897 if (string == error_mark_node)
19898 {
19899 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19900 /*consume_paren=*/true);
19901 return;
19902 }
19903
19904 /* If we're allowing GNU extensions, check for the extended assembly
19905 syntax. Unfortunately, the `:' tokens need not be separated by
19906 a space in C, and so, for compatibility, we tolerate that here
19907 too. Doing that means that we have to treat the `::' operator as
19908 two `:' tokens. */
19909 if (cp_parser_allow_gnu_extensions_p (parser)
19910 && parser->in_function_body
19911 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19912 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19913 {
19914 bool inputs_p = false;
19915 bool clobbers_p = false;
19916 bool labels_p = false;
19917
19918 /* The extended syntax was used. */
19919 extended_p = true;
19920
19921 /* Look for outputs. */
19922 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19923 {
19924 /* Consume the `:'. */
19925 cp_lexer_consume_token (parser->lexer);
19926 /* Parse the output-operands. */
19927 if (cp_lexer_next_token_is_not (parser->lexer,
19928 CPP_COLON)
19929 && cp_lexer_next_token_is_not (parser->lexer,
19930 CPP_SCOPE)
19931 && cp_lexer_next_token_is_not (parser->lexer,
19932 CPP_CLOSE_PAREN)
19933 && !goto_p)
19934 {
19935 outputs = cp_parser_asm_operand_list (parser);
19936 if (outputs == error_mark_node)
19937 invalid_outputs_p = true;
19938 }
19939 }
19940 /* If the next token is `::', there are no outputs, and the
19941 next token is the beginning of the inputs. */
19942 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19943 /* The inputs are coming next. */
19944 inputs_p = true;
19945
19946 /* Look for inputs. */
19947 if (inputs_p
19948 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19949 {
19950 /* Consume the `:' or `::'. */
19951 cp_lexer_consume_token (parser->lexer);
19952 /* Parse the output-operands. */
19953 if (cp_lexer_next_token_is_not (parser->lexer,
19954 CPP_COLON)
19955 && cp_lexer_next_token_is_not (parser->lexer,
19956 CPP_SCOPE)
19957 && cp_lexer_next_token_is_not (parser->lexer,
19958 CPP_CLOSE_PAREN))
19959 {
19960 inputs = cp_parser_asm_operand_list (parser);
19961 if (inputs == error_mark_node)
19962 invalid_inputs_p = true;
19963 }
19964 }
19965 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19966 /* The clobbers are coming next. */
19967 clobbers_p = true;
19968
19969 /* Look for clobbers. */
19970 if (clobbers_p
19971 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19972 {
19973 clobbers_p = true;
19974 /* Consume the `:' or `::'. */
19975 cp_lexer_consume_token (parser->lexer);
19976 /* Parse the clobbers. */
19977 if (cp_lexer_next_token_is_not (parser->lexer,
19978 CPP_COLON)
19979 && cp_lexer_next_token_is_not (parser->lexer,
19980 CPP_CLOSE_PAREN))
19981 clobbers = cp_parser_asm_clobber_list (parser);
19982 }
19983 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19984 /* The labels are coming next. */
19985 labels_p = true;
19986
19987 /* Look for labels. */
19988 if (labels_p
19989 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19990 {
19991 labels_p = true;
19992 /* Consume the `:' or `::'. */
19993 cp_lexer_consume_token (parser->lexer);
19994 /* Parse the labels. */
19995 labels = cp_parser_asm_label_list (parser);
19996 }
19997
19998 if (goto_p && !labels_p)
19999 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
20000 }
20001 else if (goto_p)
20002 missing = RT_COLON_SCOPE;
20003
20004 /* Look for the closing `)'. */
20005 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
20006 missing ? missing : RT_CLOSE_PAREN))
20007 cp_parser_skip_to_closing_parenthesis (parser, true, false,
20008 /*consume_paren=*/true);
20009 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20010
20011 if (!invalid_inputs_p && !invalid_outputs_p)
20012 {
20013 /* Create the ASM_EXPR. */
20014 if (parser->in_function_body)
20015 {
20016 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
20017 inputs, clobbers, labels, inline_p);
20018 /* If the extended syntax was not used, mark the ASM_EXPR. */
20019 if (!extended_p)
20020 {
20021 tree temp = asm_stmt;
20022 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
20023 temp = TREE_OPERAND (temp, 0);
20024
20025 ASM_INPUT_P (temp) = 1;
20026 }
20027 }
20028 else
20029 symtab->finalize_toplevel_asm (string);
20030 }
20031 }
20032
20033 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
20034 type that comes from the decl-specifier-seq. */
20035
20036 static tree
20037 strip_declarator_types (tree type, cp_declarator *declarator)
20038 {
20039 for (cp_declarator *d = declarator; d;)
20040 switch (d->kind)
20041 {
20042 case cdk_id:
20043 case cdk_decomp:
20044 case cdk_error:
20045 d = NULL;
20046 break;
20047
20048 default:
20049 if (TYPE_PTRMEMFUNC_P (type))
20050 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
20051 type = TREE_TYPE (type);
20052 d = d->declarator;
20053 break;
20054 }
20055
20056 return type;
20057 }
20058
20059 /* Declarators [gram.dcl.decl] */
20060
20061 /* Parse an init-declarator.
20062
20063 init-declarator:
20064 declarator initializer [opt]
20065
20066 GNU Extension:
20067
20068 init-declarator:
20069 declarator asm-specification [opt] attributes [opt] initializer [opt]
20070
20071 function-definition:
20072 decl-specifier-seq [opt] declarator ctor-initializer [opt]
20073 function-body
20074 decl-specifier-seq [opt] declarator function-try-block
20075
20076 GNU Extension:
20077
20078 function-definition:
20079 __extension__ function-definition
20080
20081 TM Extension:
20082
20083 function-definition:
20084 decl-specifier-seq [opt] declarator function-transaction-block
20085
20086 The parser flags FLAGS is used to control type-specifier parsing.
20087
20088 The DECL_SPECIFIERS apply to this declarator. Returns a
20089 representation of the entity declared. If MEMBER_P is TRUE, then
20090 this declarator appears in a class scope. The new DECL created by
20091 this declarator is returned.
20092
20093 The CHECKS are access checks that should be performed once we know
20094 what entity is being declared (and, therefore, what classes have
20095 befriended it).
20096
20097 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20098 for a function-definition here as well. If the declarator is a
20099 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20100 be TRUE upon return. By that point, the function-definition will
20101 have been completely parsed.
20102
20103 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20104 is FALSE.
20105
20106 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20107 parsed declaration if it is an uninitialized single declarator not followed
20108 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20109 if present, will not be consumed. If returned, this declarator will be
20110 created with SD_INITIALIZED but will not call cp_finish_decl.
20111
20112 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20113 and there is an initializer, the pointed location_t is set to the
20114 location of the '=' or `(', or '{' in C++11 token introducing the
20115 initializer. */
20116
20117 static tree
20118 cp_parser_init_declarator (cp_parser* parser,
20119 cp_parser_flags flags,
20120 cp_decl_specifier_seq *decl_specifiers,
20121 vec<deferred_access_check, va_gc> *checks,
20122 bool function_definition_allowed_p,
20123 bool member_p,
20124 int declares_class_or_enum,
20125 bool* function_definition_p,
20126 tree* maybe_range_for_decl,
20127 location_t* init_loc,
20128 tree* auto_result)
20129 {
20130 cp_token *token = NULL, *asm_spec_start_token = NULL,
20131 *attributes_start_token = NULL;
20132 cp_declarator *declarator;
20133 tree prefix_attributes;
20134 tree attributes = NULL;
20135 tree asm_specification;
20136 tree initializer;
20137 tree decl = NULL_TREE;
20138 tree scope;
20139 int is_initialized;
20140 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20141 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20142 "(...)". */
20143 enum cpp_ttype initialization_kind;
20144 bool is_direct_init = false;
20145 bool is_non_constant_init;
20146 int ctor_dtor_or_conv_p;
20147 bool friend_p = cp_parser_friend_p (decl_specifiers);
20148 tree pushed_scope = NULL_TREE;
20149 bool range_for_decl_p = false;
20150 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20151 location_t tmp_init_loc = UNKNOWN_LOCATION;
20152
20153 /* Gather the attributes that were provided with the
20154 decl-specifiers. */
20155 prefix_attributes = decl_specifiers->attributes;
20156
20157 /* Assume that this is not the declarator for a function
20158 definition. */
20159 if (function_definition_p)
20160 *function_definition_p = false;
20161
20162 /* Default arguments are only permitted for function parameters. */
20163 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
20164 parser->default_arg_ok_p = false;
20165
20166 /* Defer access checks while parsing the declarator; we cannot know
20167 what names are accessible until we know what is being
20168 declared. */
20169 resume_deferring_access_checks ();
20170
20171 token = cp_lexer_peek_token (parser->lexer);
20172
20173 /* Parse the declarator. */
20174 declarator
20175 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20176 flags, &ctor_dtor_or_conv_p,
20177 /*parenthesized_p=*/NULL,
20178 member_p, friend_p, /*static_p=*/false);
20179 /* Gather up the deferred checks. */
20180 stop_deferring_access_checks ();
20181
20182 parser->default_arg_ok_p = saved_default_arg_ok_p;
20183
20184 /* If the DECLARATOR was erroneous, there's no need to go
20185 further. */
20186 if (declarator == cp_error_declarator)
20187 return error_mark_node;
20188
20189 /* Check that the number of template-parameter-lists is OK. */
20190 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
20191 token->location))
20192 return error_mark_node;
20193
20194 if (declares_class_or_enum & 2)
20195 cp_parser_check_for_definition_in_return_type (declarator,
20196 decl_specifiers->type,
20197 decl_specifiers->locations[ds_type_spec]);
20198
20199 /* Figure out what scope the entity declared by the DECLARATOR is
20200 located in. `grokdeclarator' sometimes changes the scope, so
20201 we compute it now. */
20202 scope = get_scope_of_declarator (declarator);
20203
20204 /* Perform any lookups in the declared type which were thought to be
20205 dependent, but are not in the scope of the declarator. */
20206 decl_specifiers->type
20207 = maybe_update_decl_type (decl_specifiers->type, scope);
20208
20209 /* If we're allowing GNU extensions, look for an
20210 asm-specification. */
20211 if (cp_parser_allow_gnu_extensions_p (parser))
20212 {
20213 /* Look for an asm-specification. */
20214 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
20215 asm_specification = cp_parser_asm_specification_opt (parser);
20216 }
20217 else
20218 asm_specification = NULL_TREE;
20219
20220 /* Look for attributes. */
20221 attributes_start_token = cp_lexer_peek_token (parser->lexer);
20222 attributes = cp_parser_attributes_opt (parser);
20223
20224 /* Peek at the next token. */
20225 token = cp_lexer_peek_token (parser->lexer);
20226
20227 bool bogus_implicit_tmpl = false;
20228
20229 if (function_declarator_p (declarator))
20230 {
20231 /* Handle C++17 deduction guides. */
20232 if (!decl_specifiers->type
20233 && ctor_dtor_or_conv_p <= 0
20234 && cxx_dialect >= cxx17)
20235 {
20236 cp_declarator *id = get_id_declarator (declarator);
20237 tree name = id->u.id.unqualified_name;
20238 parser->scope = id->u.id.qualifying_scope;
20239 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
20240 if (tmpl
20241 && (DECL_CLASS_TEMPLATE_P (tmpl)
20242 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
20243 {
20244 id->u.id.unqualified_name = dguide_name (tmpl);
20245 id->u.id.sfk = sfk_deduction_guide;
20246 ctor_dtor_or_conv_p = 1;
20247 }
20248 }
20249
20250 /* Check to see if the token indicates the start of a
20251 function-definition. */
20252 if (cp_parser_token_starts_function_definition_p (token))
20253 {
20254 if (!function_definition_allowed_p)
20255 {
20256 /* If a function-definition should not appear here, issue an
20257 error message. */
20258 cp_parser_error (parser,
20259 "a function-definition is not allowed here");
20260 return error_mark_node;
20261 }
20262
20263 location_t func_brace_location
20264 = cp_lexer_peek_token (parser->lexer)->location;
20265
20266 /* Neither attributes nor an asm-specification are allowed
20267 on a function-definition. */
20268 if (asm_specification)
20269 error_at (asm_spec_start_token->location,
20270 "an %<asm%> specification is not allowed "
20271 "on a function-definition");
20272 if (attributes)
20273 error_at (attributes_start_token->location,
20274 "attributes are not allowed "
20275 "on a function-definition");
20276 /* This is a function-definition. */
20277 *function_definition_p = true;
20278
20279 /* Parse the function definition. */
20280 if (member_p)
20281 decl = cp_parser_save_member_function_body (parser,
20282 decl_specifiers,
20283 declarator,
20284 prefix_attributes);
20285 else
20286 decl =
20287 (cp_parser_function_definition_from_specifiers_and_declarator
20288 (parser, decl_specifiers, prefix_attributes, declarator));
20289
20290 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
20291 {
20292 /* This is where the prologue starts... */
20293 DECL_STRUCT_FUNCTION (decl)->function_start_locus
20294 = func_brace_location;
20295 }
20296
20297 return decl;
20298 }
20299 }
20300 else if (parser->fully_implicit_function_template_p)
20301 {
20302 /* A non-template declaration involving a function parameter list
20303 containing an implicit template parameter will be made into a
20304 template. If the resulting declaration is not going to be an
20305 actual function then finish the template scope here to prevent it.
20306 An error message will be issued once we have a decl to talk about.
20307
20308 FIXME probably we should do type deduction rather than create an
20309 implicit template, but the standard currently doesn't allow it. */
20310 bogus_implicit_tmpl = true;
20311 finish_fully_implicit_template (parser, NULL_TREE);
20312 }
20313
20314 /* [dcl.dcl]
20315
20316 Only in function declarations for constructors, destructors, type
20317 conversions, and deduction guides can the decl-specifier-seq be omitted.
20318
20319 We explicitly postpone this check past the point where we handle
20320 function-definitions because we tolerate function-definitions
20321 that are missing their return types in some modes. */
20322 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
20323 {
20324 cp_parser_error (parser,
20325 "expected constructor, destructor, or type conversion");
20326 return error_mark_node;
20327 }
20328
20329 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20330 if (token->type == CPP_EQ
20331 || token->type == CPP_OPEN_PAREN
20332 || token->type == CPP_OPEN_BRACE)
20333 {
20334 is_initialized = SD_INITIALIZED;
20335 initialization_kind = token->type;
20336 if (maybe_range_for_decl)
20337 *maybe_range_for_decl = error_mark_node;
20338 tmp_init_loc = token->location;
20339 if (init_loc && *init_loc == UNKNOWN_LOCATION)
20340 *init_loc = tmp_init_loc;
20341
20342 if (token->type == CPP_EQ
20343 && function_declarator_p (declarator))
20344 {
20345 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
20346 if (t2->keyword == RID_DEFAULT)
20347 is_initialized = SD_DEFAULTED;
20348 else if (t2->keyword == RID_DELETE)
20349 is_initialized = SD_DELETED;
20350 }
20351 }
20352 else
20353 {
20354 /* If the init-declarator isn't initialized and isn't followed by a
20355 `,' or `;', it's not a valid init-declarator. */
20356 if (token->type != CPP_COMMA
20357 && token->type != CPP_SEMICOLON)
20358 {
20359 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
20360 range_for_decl_p = true;
20361 else
20362 {
20363 if (!maybe_range_for_decl)
20364 cp_parser_error (parser, "expected initializer");
20365 return error_mark_node;
20366 }
20367 }
20368 is_initialized = SD_UNINITIALIZED;
20369 initialization_kind = CPP_EOF;
20370 }
20371
20372 /* Because start_decl has side-effects, we should only call it if we
20373 know we're going ahead. By this point, we know that we cannot
20374 possibly be looking at any other construct. */
20375 cp_parser_commit_to_tentative_parse (parser);
20376
20377 /* Enter the newly declared entry in the symbol table. If we're
20378 processing a declaration in a class-specifier, we wait until
20379 after processing the initializer. */
20380 if (!member_p)
20381 {
20382 if (parser->in_unbraced_linkage_specification_p)
20383 decl_specifiers->storage_class = sc_extern;
20384 decl = start_decl (declarator, decl_specifiers,
20385 range_for_decl_p? SD_INITIALIZED : is_initialized,
20386 attributes, prefix_attributes, &pushed_scope);
20387 cp_finalize_omp_declare_simd (parser, decl);
20388 cp_finalize_oacc_routine (parser, decl, false);
20389 /* Adjust location of decl if declarator->id_loc is more appropriate:
20390 set, and decl wasn't merged with another decl, in which case its
20391 location would be different from input_location, and more accurate. */
20392 if (DECL_P (decl)
20393 && declarator->id_loc != UNKNOWN_LOCATION
20394 && DECL_SOURCE_LOCATION (decl) == input_location)
20395 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
20396 }
20397 else if (scope)
20398 /* Enter the SCOPE. That way unqualified names appearing in the
20399 initializer will be looked up in SCOPE. */
20400 pushed_scope = push_scope (scope);
20401
20402 /* Perform deferred access control checks, now that we know in which
20403 SCOPE the declared entity resides. */
20404 if (!member_p && decl)
20405 {
20406 tree saved_current_function_decl = NULL_TREE;
20407
20408 /* If the entity being declared is a function, pretend that we
20409 are in its scope. If it is a `friend', it may have access to
20410 things that would not otherwise be accessible. */
20411 if (TREE_CODE (decl) == FUNCTION_DECL)
20412 {
20413 saved_current_function_decl = current_function_decl;
20414 current_function_decl = decl;
20415 }
20416
20417 /* Perform access checks for template parameters. */
20418 cp_parser_perform_template_parameter_access_checks (checks);
20419
20420 /* Perform the access control checks for the declarator and the
20421 decl-specifiers. */
20422 perform_deferred_access_checks (tf_warning_or_error);
20423
20424 /* Restore the saved value. */
20425 if (TREE_CODE (decl) == FUNCTION_DECL)
20426 current_function_decl = saved_current_function_decl;
20427 }
20428
20429 /* Parse the initializer. */
20430 initializer = NULL_TREE;
20431 is_direct_init = false;
20432 is_non_constant_init = true;
20433 if (is_initialized)
20434 {
20435 if (function_declarator_p (declarator))
20436 {
20437 if (initialization_kind == CPP_EQ)
20438 initializer = cp_parser_pure_specifier (parser);
20439 else
20440 {
20441 /* If the declaration was erroneous, we don't really
20442 know what the user intended, so just silently
20443 consume the initializer. */
20444 if (decl != error_mark_node)
20445 error_at (tmp_init_loc, "initializer provided for function");
20446 cp_parser_skip_to_closing_parenthesis (parser,
20447 /*recovering=*/true,
20448 /*or_comma=*/false,
20449 /*consume_paren=*/true);
20450 }
20451 }
20452 else
20453 {
20454 /* We want to record the extra mangling scope for in-class
20455 initializers of class members and initializers of static data
20456 member templates. The former involves deferring
20457 parsing of the initializer until end of class as with default
20458 arguments. So right here we only handle the latter. */
20459 if (!member_p && processing_template_decl && decl != error_mark_node)
20460 start_lambda_scope (decl);
20461 initializer = cp_parser_initializer (parser,
20462 &is_direct_init,
20463 &is_non_constant_init);
20464 if (!member_p && processing_template_decl && decl != error_mark_node)
20465 finish_lambda_scope ();
20466 if (initializer == error_mark_node)
20467 cp_parser_skip_to_end_of_statement (parser);
20468 }
20469 }
20470
20471 /* The old parser allows attributes to appear after a parenthesized
20472 initializer. Mark Mitchell proposed removing this functionality
20473 on the GCC mailing lists on 2002-08-13. This parser accepts the
20474 attributes -- but ignores them. Made a permerror in GCC 8. */
20475 if (cp_parser_allow_gnu_extensions_p (parser)
20476 && initialization_kind == CPP_OPEN_PAREN
20477 && cp_parser_attributes_opt (parser)
20478 && permerror (input_location,
20479 "attributes after parenthesized initializer ignored"))
20480 {
20481 static bool hint;
20482 if (flag_permissive && !hint)
20483 {
20484 hint = true;
20485 inform (input_location,
20486 "this flexibility is deprecated and will be removed");
20487 }
20488 }
20489
20490 /* And now complain about a non-function implicit template. */
20491 if (bogus_implicit_tmpl && decl != error_mark_node)
20492 error_at (DECL_SOURCE_LOCATION (decl),
20493 "non-function %qD declared as implicit template", decl);
20494
20495 /* For an in-class declaration, use `grokfield' to create the
20496 declaration. */
20497 if (member_p)
20498 {
20499 if (pushed_scope)
20500 {
20501 pop_scope (pushed_scope);
20502 pushed_scope = NULL_TREE;
20503 }
20504 decl = grokfield (declarator, decl_specifiers,
20505 initializer, !is_non_constant_init,
20506 /*asmspec=*/NULL_TREE,
20507 attr_chainon (attributes, prefix_attributes));
20508 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20509 cp_parser_save_default_args (parser, decl);
20510 cp_finalize_omp_declare_simd (parser, decl);
20511 cp_finalize_oacc_routine (parser, decl, false);
20512 }
20513
20514 /* Finish processing the declaration. But, skip member
20515 declarations. */
20516 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20517 {
20518 cp_finish_decl (decl,
20519 initializer, !is_non_constant_init,
20520 asm_specification,
20521 /* If the initializer is in parentheses, then this is
20522 a direct-initialization, which means that an
20523 `explicit' constructor is OK. Otherwise, an
20524 `explicit' constructor cannot be used. */
20525 ((is_direct_init || !is_initialized)
20526 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20527 }
20528 else if ((cxx_dialect != cxx98) && friend_p
20529 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20530 /* Core issue #226 (C++0x only): A default template-argument
20531 shall not be specified in a friend class template
20532 declaration. */
20533 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20534 /*is_partial=*/false, /*is_friend_decl=*/1);
20535
20536 if (!friend_p && pushed_scope)
20537 pop_scope (pushed_scope);
20538
20539 if (function_declarator_p (declarator)
20540 && parser->fully_implicit_function_template_p)
20541 {
20542 if (member_p)
20543 decl = finish_fully_implicit_template (parser, decl);
20544 else
20545 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20546 }
20547
20548 if (auto_result && is_initialized && decl_specifiers->type
20549 && type_uses_auto (decl_specifiers->type))
20550 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20551
20552 return decl;
20553 }
20554
20555 /* Parse a declarator.
20556
20557 declarator:
20558 direct-declarator
20559 ptr-operator declarator
20560
20561 abstract-declarator:
20562 ptr-operator abstract-declarator [opt]
20563 direct-abstract-declarator
20564
20565 GNU Extensions:
20566
20567 declarator:
20568 attributes [opt] direct-declarator
20569 attributes [opt] ptr-operator declarator
20570
20571 abstract-declarator:
20572 attributes [opt] ptr-operator abstract-declarator [opt]
20573 attributes [opt] direct-abstract-declarator
20574
20575 The parser flags FLAGS is used to control type-specifier parsing.
20576
20577 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20578 detect constructors, destructors, deduction guides, or conversion operators.
20579 It is set to -1 if the declarator is a name, and +1 if it is a
20580 function. Otherwise it is set to zero. Usually you just want to
20581 test for >0, but internally the negative value is used.
20582
20583 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20584 a decl-specifier-seq unless it declares a constructor, destructor,
20585 or conversion. It might seem that we could check this condition in
20586 semantic analysis, rather than parsing, but that makes it difficult
20587 to handle something like `f()'. We want to notice that there are
20588 no decl-specifiers, and therefore realize that this is an
20589 expression, not a declaration.)
20590
20591 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20592 the declarator is a direct-declarator of the form "(...)".
20593
20594 MEMBER_P is true iff this declarator is a member-declarator.
20595
20596 FRIEND_P is true iff this declarator is a friend.
20597
20598 STATIC_P is true iff the keyword static was seen. */
20599
20600 static cp_declarator *
20601 cp_parser_declarator (cp_parser* parser,
20602 cp_parser_declarator_kind dcl_kind,
20603 cp_parser_flags flags,
20604 int* ctor_dtor_or_conv_p,
20605 bool* parenthesized_p,
20606 bool member_p, bool friend_p, bool static_p)
20607 {
20608 cp_declarator *declarator;
20609 enum tree_code code;
20610 cp_cv_quals cv_quals;
20611 tree class_type;
20612 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20613
20614 /* Assume this is not a constructor, destructor, or type-conversion
20615 operator. */
20616 if (ctor_dtor_or_conv_p)
20617 *ctor_dtor_or_conv_p = 0;
20618
20619 if (cp_parser_allow_gnu_extensions_p (parser))
20620 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20621
20622 /* Check for the ptr-operator production. */
20623 cp_parser_parse_tentatively (parser);
20624 /* Parse the ptr-operator. */
20625 code = cp_parser_ptr_operator (parser,
20626 &class_type,
20627 &cv_quals,
20628 &std_attributes);
20629
20630 /* If that worked, then we have a ptr-operator. */
20631 if (cp_parser_parse_definitely (parser))
20632 {
20633 /* If a ptr-operator was found, then this declarator was not
20634 parenthesized. */
20635 if (parenthesized_p)
20636 *parenthesized_p = true;
20637 /* The dependent declarator is optional if we are parsing an
20638 abstract-declarator. */
20639 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20640 cp_parser_parse_tentatively (parser);
20641
20642 /* Parse the dependent declarator. */
20643 declarator = cp_parser_declarator (parser, dcl_kind,
20644 CP_PARSER_FLAGS_NONE,
20645 /*ctor_dtor_or_conv_p=*/NULL,
20646 /*parenthesized_p=*/NULL,
20647 /*member_p=*/false,
20648 friend_p, /*static_p=*/false);
20649
20650 /* If we are parsing an abstract-declarator, we must handle the
20651 case where the dependent declarator is absent. */
20652 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20653 && !cp_parser_parse_definitely (parser))
20654 declarator = NULL;
20655
20656 declarator = cp_parser_make_indirect_declarator
20657 (code, class_type, cv_quals, declarator, std_attributes);
20658 }
20659 /* Everything else is a direct-declarator. */
20660 else
20661 {
20662 if (parenthesized_p)
20663 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20664 CPP_OPEN_PAREN);
20665 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20666 flags, ctor_dtor_or_conv_p,
20667 member_p, friend_p, static_p);
20668 }
20669
20670 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20671 declarator->attributes = gnu_attributes;
20672 return declarator;
20673 }
20674
20675 /* Parse a direct-declarator or direct-abstract-declarator.
20676
20677 direct-declarator:
20678 declarator-id
20679 direct-declarator ( parameter-declaration-clause )
20680 cv-qualifier-seq [opt]
20681 ref-qualifier [opt]
20682 exception-specification [opt]
20683 direct-declarator [ constant-expression [opt] ]
20684 ( declarator )
20685
20686 direct-abstract-declarator:
20687 direct-abstract-declarator [opt]
20688 ( parameter-declaration-clause )
20689 cv-qualifier-seq [opt]
20690 ref-qualifier [opt]
20691 exception-specification [opt]
20692 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20693 ( abstract-declarator )
20694
20695 Returns a representation of the declarator. DCL_KIND is
20696 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20697 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20698 we are parsing a direct-declarator. It is
20699 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20700 of ambiguity we prefer an abstract declarator, as per
20701 [dcl.ambig.res].
20702 The parser flags FLAGS is used to control type-specifier parsing.
20703 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
20704 as for cp_parser_declarator. */
20705
20706 static cp_declarator *
20707 cp_parser_direct_declarator (cp_parser* parser,
20708 cp_parser_declarator_kind dcl_kind,
20709 cp_parser_flags flags,
20710 int* ctor_dtor_or_conv_p,
20711 bool member_p, bool friend_p, bool static_p)
20712 {
20713 cp_token *token;
20714 cp_declarator *declarator = NULL;
20715 tree scope = NULL_TREE;
20716 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20717 bool saved_in_declarator_p = parser->in_declarator_p;
20718 bool first = true;
20719 tree pushed_scope = NULL_TREE;
20720 cp_token *open_paren = NULL, *close_paren = NULL;
20721
20722 while (true)
20723 {
20724 /* Peek at the next token. */
20725 token = cp_lexer_peek_token (parser->lexer);
20726 if (token->type == CPP_OPEN_PAREN)
20727 {
20728 /* This is either a parameter-declaration-clause, or a
20729 parenthesized declarator. When we know we are parsing a
20730 named declarator, it must be a parenthesized declarator
20731 if FIRST is true. For instance, `(int)' is a
20732 parameter-declaration-clause, with an omitted
20733 direct-abstract-declarator. But `((*))', is a
20734 parenthesized abstract declarator. Finally, when T is a
20735 template parameter `(T)' is a
20736 parameter-declaration-clause, and not a parenthesized
20737 named declarator.
20738
20739 We first try and parse a parameter-declaration-clause,
20740 and then try a nested declarator (if FIRST is true).
20741
20742 It is not an error for it not to be a
20743 parameter-declaration-clause, even when FIRST is
20744 false. Consider,
20745
20746 int i (int);
20747 int i (3);
20748
20749 The first is the declaration of a function while the
20750 second is the definition of a variable, including its
20751 initializer.
20752
20753 Having seen only the parenthesis, we cannot know which of
20754 these two alternatives should be selected. Even more
20755 complex are examples like:
20756
20757 int i (int (a));
20758 int i (int (3));
20759
20760 The former is a function-declaration; the latter is a
20761 variable initialization.
20762
20763 Thus again, we try a parameter-declaration-clause, and if
20764 that fails, we back out and return. */
20765
20766 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20767 {
20768 tree params;
20769 bool is_declarator = false;
20770
20771 open_paren = NULL;
20772
20773 /* In a member-declarator, the only valid interpretation
20774 of a parenthesis is the start of a
20775 parameter-declaration-clause. (It is invalid to
20776 initialize a static data member with a parenthesized
20777 initializer; only the "=" form of initialization is
20778 permitted.) */
20779 if (!member_p)
20780 cp_parser_parse_tentatively (parser);
20781
20782 /* Consume the `('. */
20783 matching_parens parens;
20784 parens.consume_open (parser);
20785 if (first)
20786 {
20787 /* If this is going to be an abstract declarator, we're
20788 in a declarator and we can't have default args. */
20789 parser->default_arg_ok_p = false;
20790 parser->in_declarator_p = true;
20791 }
20792
20793 begin_scope (sk_function_parms, NULL_TREE);
20794
20795 /* Parse the parameter-declaration-clause. */
20796 params
20797 = cp_parser_parameter_declaration_clause (parser, flags);
20798
20799 /* Consume the `)'. */
20800 parens.require_close (parser);
20801
20802 /* If all went well, parse the cv-qualifier-seq,
20803 ref-qualifier and the exception-specification. */
20804 if (member_p || cp_parser_parse_definitely (parser))
20805 {
20806 cp_cv_quals cv_quals;
20807 cp_virt_specifiers virt_specifiers;
20808 cp_ref_qualifier ref_qual;
20809 tree exception_specification;
20810 tree late_return;
20811 tree attrs;
20812 bool memfn = (member_p || (pushed_scope
20813 && CLASS_TYPE_P (pushed_scope)));
20814 unsigned char local_variables_forbidden_p
20815 = parser->local_variables_forbidden_p;
20816 /* 'this' is not allowed in static member functions. */
20817 if (static_p || friend_p)
20818 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
20819
20820 is_declarator = true;
20821
20822 if (ctor_dtor_or_conv_p)
20823 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20824 first = false;
20825
20826 /* Parse the cv-qualifier-seq. */
20827 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20828 /* Parse the ref-qualifier. */
20829 ref_qual = cp_parser_ref_qualifier_opt (parser);
20830 /* Parse the tx-qualifier. */
20831 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20832 /* And the exception-specification. */
20833 exception_specification
20834 = cp_parser_exception_specification_opt (parser, friend_p);
20835
20836 attrs = cp_parser_std_attribute_spec_seq (parser);
20837
20838 /* In here, we handle cases where attribute is used after
20839 the function declaration. For example:
20840 void func (int x) __attribute__((vector(..))); */
20841 tree gnu_attrs = NULL_TREE;
20842 tree requires_clause = NULL_TREE;
20843 late_return = (cp_parser_late_return_type_opt
20844 (parser, declarator, requires_clause,
20845 memfn ? cv_quals : -1));
20846
20847 /* Parse the virt-specifier-seq. */
20848 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20849
20850 /* Create the function-declarator. */
20851 declarator = make_call_declarator (declarator,
20852 params,
20853 cv_quals,
20854 virt_specifiers,
20855 ref_qual,
20856 tx_qual,
20857 exception_specification,
20858 late_return,
20859 requires_clause);
20860 declarator->std_attributes = attrs;
20861 declarator->attributes = gnu_attrs;
20862 /* Any subsequent parameter lists are to do with
20863 return type, so are not those of the declared
20864 function. */
20865 parser->default_arg_ok_p = false;
20866
20867 /* Restore the state of local_variables_forbidden_p. */
20868 parser->local_variables_forbidden_p
20869 = local_variables_forbidden_p;
20870 }
20871
20872 /* Remove the function parms from scope. */
20873 pop_bindings_and_leave_scope ();
20874
20875 if (is_declarator)
20876 /* Repeat the main loop. */
20877 continue;
20878 }
20879
20880 /* If this is the first, we can try a parenthesized
20881 declarator. */
20882 if (first)
20883 {
20884 bool saved_in_type_id_in_expr_p;
20885
20886 parser->default_arg_ok_p = saved_default_arg_ok_p;
20887 parser->in_declarator_p = saved_in_declarator_p;
20888
20889 open_paren = token;
20890 /* Consume the `('. */
20891 matching_parens parens;
20892 parens.consume_open (parser);
20893 /* Parse the nested declarator. */
20894 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20895 parser->in_type_id_in_expr_p = true;
20896 declarator
20897 = cp_parser_declarator (parser, dcl_kind, flags,
20898 ctor_dtor_or_conv_p,
20899 /*parenthesized_p=*/NULL,
20900 member_p, friend_p,
20901 /*static_p=*/false);
20902 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20903 first = false;
20904 /* Expect a `)'. */
20905 close_paren = cp_lexer_peek_token (parser->lexer);
20906 if (!parens.require_close (parser))
20907 declarator = cp_error_declarator;
20908 if (declarator == cp_error_declarator)
20909 break;
20910
20911 goto handle_declarator;
20912 }
20913 /* Otherwise, we must be done. */
20914 else
20915 break;
20916 }
20917 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20918 && token->type == CPP_OPEN_SQUARE
20919 && !cp_next_tokens_can_be_attribute_p (parser))
20920 {
20921 /* Parse an array-declarator. */
20922 tree bounds, attrs;
20923
20924 if (ctor_dtor_or_conv_p)
20925 *ctor_dtor_or_conv_p = 0;
20926
20927 open_paren = NULL;
20928 first = false;
20929 parser->default_arg_ok_p = false;
20930 parser->in_declarator_p = true;
20931 /* Consume the `['. */
20932 cp_lexer_consume_token (parser->lexer);
20933 /* Peek at the next token. */
20934 token = cp_lexer_peek_token (parser->lexer);
20935 /* If the next token is `]', then there is no
20936 constant-expression. */
20937 if (token->type != CPP_CLOSE_SQUARE)
20938 {
20939 bool non_constant_p;
20940 bounds
20941 = cp_parser_constant_expression (parser,
20942 /*allow_non_constant=*/true,
20943 &non_constant_p);
20944 if (!non_constant_p)
20945 /* OK */;
20946 else if (error_operand_p (bounds))
20947 /* Already gave an error. */;
20948 else if (!parser->in_function_body
20949 || current_binding_level->kind == sk_function_parms)
20950 {
20951 /* Normally, the array bound must be an integral constant
20952 expression. However, as an extension, we allow VLAs
20953 in function scopes as long as they aren't part of a
20954 parameter declaration. */
20955 cp_parser_error (parser,
20956 "array bound is not an integer constant");
20957 bounds = error_mark_node;
20958 }
20959 else if (processing_template_decl
20960 && !type_dependent_expression_p (bounds))
20961 {
20962 /* Remember this wasn't a constant-expression. */
20963 bounds = build_nop (TREE_TYPE (bounds), bounds);
20964 TREE_SIDE_EFFECTS (bounds) = 1;
20965 }
20966 }
20967 else
20968 bounds = NULL_TREE;
20969 /* Look for the closing `]'. */
20970 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20971 {
20972 declarator = cp_error_declarator;
20973 break;
20974 }
20975
20976 attrs = cp_parser_std_attribute_spec_seq (parser);
20977 declarator = make_array_declarator (declarator, bounds);
20978 declarator->std_attributes = attrs;
20979 }
20980 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20981 {
20982 {
20983 tree qualifying_scope;
20984 tree unqualified_name;
20985 tree attrs;
20986 special_function_kind sfk;
20987 bool abstract_ok;
20988 bool pack_expansion_p = false;
20989 cp_token *declarator_id_start_token;
20990
20991 /* Parse a declarator-id */
20992 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20993 if (abstract_ok)
20994 {
20995 cp_parser_parse_tentatively (parser);
20996
20997 /* If we see an ellipsis, we should be looking at a
20998 parameter pack. */
20999 if (token->type == CPP_ELLIPSIS)
21000 {
21001 /* Consume the `...' */
21002 cp_lexer_consume_token (parser->lexer);
21003
21004 pack_expansion_p = true;
21005 }
21006 }
21007
21008 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
21009 unqualified_name
21010 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
21011 qualifying_scope = parser->scope;
21012 if (abstract_ok)
21013 {
21014 bool okay = false;
21015
21016 if (!unqualified_name && pack_expansion_p)
21017 {
21018 /* Check whether an error occurred. */
21019 okay = !cp_parser_error_occurred (parser);
21020
21021 /* We already consumed the ellipsis to mark a
21022 parameter pack, but we have no way to report it,
21023 so abort the tentative parse. We will be exiting
21024 immediately anyway. */
21025 cp_parser_abort_tentative_parse (parser);
21026 }
21027 else
21028 okay = cp_parser_parse_definitely (parser);
21029
21030 if (!okay)
21031 unqualified_name = error_mark_node;
21032 else if (unqualified_name
21033 && (qualifying_scope
21034 || (!identifier_p (unqualified_name))))
21035 {
21036 cp_parser_error (parser, "expected unqualified-id");
21037 unqualified_name = error_mark_node;
21038 }
21039 }
21040
21041 if (!unqualified_name)
21042 return NULL;
21043 if (unqualified_name == error_mark_node)
21044 {
21045 declarator = cp_error_declarator;
21046 pack_expansion_p = false;
21047 declarator->parameter_pack_p = false;
21048 break;
21049 }
21050
21051 attrs = cp_parser_std_attribute_spec_seq (parser);
21052
21053 if (qualifying_scope && at_namespace_scope_p ()
21054 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
21055 {
21056 /* In the declaration of a member of a template class
21057 outside of the class itself, the SCOPE will sometimes
21058 be a TYPENAME_TYPE. For example, given:
21059
21060 template <typename T>
21061 int S<T>::R::i = 3;
21062
21063 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
21064 this context, we must resolve S<T>::R to an ordinary
21065 type, rather than a typename type.
21066
21067 The reason we normally avoid resolving TYPENAME_TYPEs
21068 is that a specialization of `S' might render
21069 `S<T>::R' not a type. However, if `S' is
21070 specialized, then this `i' will not be used, so there
21071 is no harm in resolving the types here. */
21072 tree type;
21073
21074 /* Resolve the TYPENAME_TYPE. */
21075 type = resolve_typename_type (qualifying_scope,
21076 /*only_current_p=*/false);
21077 /* If that failed, the declarator is invalid. */
21078 if (TREE_CODE (type) == TYPENAME_TYPE)
21079 {
21080 if (typedef_variant_p (type))
21081 error_at (declarator_id_start_token->location,
21082 "cannot define member of dependent typedef "
21083 "%qT", type);
21084 else
21085 error_at (declarator_id_start_token->location,
21086 "%<%T::%E%> is not a type",
21087 TYPE_CONTEXT (qualifying_scope),
21088 TYPE_IDENTIFIER (qualifying_scope));
21089 }
21090 qualifying_scope = type;
21091 }
21092
21093 sfk = sfk_none;
21094
21095 if (unqualified_name)
21096 {
21097 tree class_type;
21098
21099 if (qualifying_scope
21100 && CLASS_TYPE_P (qualifying_scope))
21101 class_type = qualifying_scope;
21102 else
21103 class_type = current_class_type;
21104
21105 if (TREE_CODE (unqualified_name) == TYPE_DECL)
21106 {
21107 tree name_type = TREE_TYPE (unqualified_name);
21108
21109 if (!class_type || !same_type_p (name_type, class_type))
21110 {
21111 /* We do not attempt to print the declarator
21112 here because we do not have enough
21113 information about its original syntactic
21114 form. */
21115 cp_parser_error (parser, "invalid declarator");
21116 declarator = cp_error_declarator;
21117 break;
21118 }
21119 else if (qualifying_scope
21120 && CLASSTYPE_USE_TEMPLATE (name_type))
21121 {
21122 error_at (declarator_id_start_token->location,
21123 "invalid use of constructor as a template");
21124 inform (declarator_id_start_token->location,
21125 "use %<%T::%D%> instead of %<%T::%D%> to "
21126 "name the constructor in a qualified name",
21127 class_type,
21128 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
21129 class_type, name_type);
21130 declarator = cp_error_declarator;
21131 break;
21132 }
21133 unqualified_name = constructor_name (class_type);
21134 }
21135
21136 if (class_type)
21137 {
21138 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
21139 sfk = sfk_destructor;
21140 else if (identifier_p (unqualified_name)
21141 && IDENTIFIER_CONV_OP_P (unqualified_name))
21142 sfk = sfk_conversion;
21143 else if (/* There's no way to declare a constructor
21144 for an unnamed type, even if the type
21145 got a name for linkage purposes. */
21146 !TYPE_WAS_UNNAMED (class_type)
21147 /* Handle correctly (c++/19200):
21148
21149 struct S {
21150 struct T{};
21151 friend void S(T);
21152 };
21153
21154 and also:
21155
21156 namespace N {
21157 void S();
21158 }
21159
21160 struct S {
21161 friend void N::S();
21162 }; */
21163 && (!friend_p || class_type == qualifying_scope)
21164 && constructor_name_p (unqualified_name,
21165 class_type))
21166 sfk = sfk_constructor;
21167 else if (is_overloaded_fn (unqualified_name)
21168 && DECL_CONSTRUCTOR_P (get_first_fn
21169 (unqualified_name)))
21170 sfk = sfk_constructor;
21171
21172 if (ctor_dtor_or_conv_p && sfk != sfk_none)
21173 *ctor_dtor_or_conv_p = -1;
21174 }
21175 }
21176 declarator = make_id_declarator (qualifying_scope,
21177 unqualified_name,
21178 sfk, token->location);
21179 declarator->std_attributes = attrs;
21180 declarator->parameter_pack_p = pack_expansion_p;
21181
21182 if (pack_expansion_p)
21183 maybe_warn_variadic_templates ();
21184
21185 /* We're looking for this case in [temp.res]:
21186 A qualified-id is assumed to name a type if [...]
21187 - it is a decl-specifier of the decl-specifier-seq of a
21188 parameter-declaration in a declarator of a function or
21189 function template declaration, ... */
21190 if (cxx_dialect >= cxx2a
21191 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
21192 && declarator->kind == cdk_id
21193 && !at_class_scope_p ()
21194 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21195 {
21196 /* ...whose declarator-id is qualified. If it isn't, never
21197 assume the parameters to refer to types. */
21198 if (qualifying_scope == NULL_TREE)
21199 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21200 else
21201 {
21202 /* Now we have something like
21203 template <typename T> int C::x(S::p);
21204 which can be a function template declaration or a
21205 variable template definition. If name lookup for
21206 the declarator-id C::x finds one or more function
21207 templates, assume S::p to name a type. Otherwise,
21208 don't. */
21209 tree decl
21210 = cp_parser_lookup_name_simple (parser, unqualified_name,
21211 token->location);
21212 if (!is_overloaded_fn (decl)
21213 /* Allow
21214 template<typename T>
21215 A<T>::A(T::type) { } */
21216 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
21217 && constructor_name_p (unqualified_name,
21218 qualifying_scope)))
21219 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21220 }
21221 }
21222 }
21223
21224 handle_declarator:;
21225 scope = get_scope_of_declarator (declarator);
21226 if (scope)
21227 {
21228 /* Any names that appear after the declarator-id for a
21229 member are looked up in the containing scope. */
21230 if (at_function_scope_p ())
21231 {
21232 /* But declarations with qualified-ids can't appear in a
21233 function. */
21234 cp_parser_error (parser, "qualified-id in declaration");
21235 declarator = cp_error_declarator;
21236 break;
21237 }
21238 pushed_scope = push_scope (scope);
21239 }
21240 parser->in_declarator_p = true;
21241 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
21242 || (declarator && declarator->kind == cdk_id))
21243 /* Default args are only allowed on function
21244 declarations. */
21245 parser->default_arg_ok_p = saved_default_arg_ok_p;
21246 else
21247 parser->default_arg_ok_p = false;
21248
21249 first = false;
21250 }
21251 /* We're done. */
21252 else
21253 break;
21254 }
21255
21256 /* For an abstract declarator, we might wind up with nothing at this
21257 point. That's an error; the declarator is not optional. */
21258 if (!declarator)
21259 cp_parser_error (parser, "expected declarator");
21260 else if (open_paren)
21261 {
21262 /* Record overly parenthesized declarator so we can give a
21263 diagnostic about confusing decl/expr disambiguation. */
21264 if (declarator->kind == cdk_array)
21265 {
21266 /* If the open and close parens are on different lines, this
21267 is probably a formatting thing, so ignore. */
21268 expanded_location open = expand_location (open_paren->location);
21269 expanded_location close = expand_location (close_paren->location);
21270 if (open.line != close.line || open.file != close.file)
21271 open_paren = NULL;
21272 }
21273 if (open_paren)
21274 declarator->parenthesized = open_paren->location;
21275 }
21276
21277 /* If we entered a scope, we must exit it now. */
21278 if (pushed_scope)
21279 pop_scope (pushed_scope);
21280
21281 parser->default_arg_ok_p = saved_default_arg_ok_p;
21282 parser->in_declarator_p = saved_in_declarator_p;
21283
21284 return declarator;
21285 }
21286
21287 /* Parse a ptr-operator.
21288
21289 ptr-operator:
21290 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21291 * cv-qualifier-seq [opt]
21292 &
21293 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21294 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21295
21296 GNU Extension:
21297
21298 ptr-operator:
21299 & cv-qualifier-seq [opt]
21300
21301 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21302 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21303 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21304 filled in with the TYPE containing the member. *CV_QUALS is
21305 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21306 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21307 Note that the tree codes returned by this function have nothing
21308 to do with the types of trees that will be eventually be created
21309 to represent the pointer or reference type being parsed. They are
21310 just constants with suggestive names. */
21311 static enum tree_code
21312 cp_parser_ptr_operator (cp_parser* parser,
21313 tree* type,
21314 cp_cv_quals *cv_quals,
21315 tree *attributes)
21316 {
21317 enum tree_code code = ERROR_MARK;
21318 cp_token *token;
21319 tree attrs = NULL_TREE;
21320
21321 /* Assume that it's not a pointer-to-member. */
21322 *type = NULL_TREE;
21323 /* And that there are no cv-qualifiers. */
21324 *cv_quals = TYPE_UNQUALIFIED;
21325
21326 /* Peek at the next token. */
21327 token = cp_lexer_peek_token (parser->lexer);
21328
21329 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21330 if (token->type == CPP_MULT)
21331 code = INDIRECT_REF;
21332 else if (token->type == CPP_AND)
21333 code = ADDR_EXPR;
21334 else if ((cxx_dialect != cxx98) &&
21335 token->type == CPP_AND_AND) /* C++0x only */
21336 code = NON_LVALUE_EXPR;
21337
21338 if (code != ERROR_MARK)
21339 {
21340 /* Consume the `*', `&' or `&&'. */
21341 cp_lexer_consume_token (parser->lexer);
21342
21343 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21344 `&', if we are allowing GNU extensions. (The only qualifier
21345 that can legally appear after `&' is `restrict', but that is
21346 enforced during semantic analysis. */
21347 if (code == INDIRECT_REF
21348 || cp_parser_allow_gnu_extensions_p (parser))
21349 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21350
21351 attrs = cp_parser_std_attribute_spec_seq (parser);
21352 if (attributes != NULL)
21353 *attributes = attrs;
21354 }
21355 else
21356 {
21357 /* Try the pointer-to-member case. */
21358 cp_parser_parse_tentatively (parser);
21359 /* Look for the optional `::' operator. */
21360 cp_parser_global_scope_opt (parser,
21361 /*current_scope_valid_p=*/false);
21362 /* Look for the nested-name specifier. */
21363 token = cp_lexer_peek_token (parser->lexer);
21364 cp_parser_nested_name_specifier (parser,
21365 /*typename_keyword_p=*/false,
21366 /*check_dependency_p=*/true,
21367 /*type_p=*/false,
21368 /*is_declaration=*/false);
21369 /* If we found it, and the next token is a `*', then we are
21370 indeed looking at a pointer-to-member operator. */
21371 if (!cp_parser_error_occurred (parser)
21372 && cp_parser_require (parser, CPP_MULT, RT_MULT))
21373 {
21374 /* Indicate that the `*' operator was used. */
21375 code = INDIRECT_REF;
21376
21377 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21378 error_at (token->location, "%qD is a namespace", parser->scope);
21379 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
21380 error_at (token->location, "cannot form pointer to member of "
21381 "non-class %q#T", parser->scope);
21382 else
21383 {
21384 /* The type of which the member is a member is given by the
21385 current SCOPE. */
21386 *type = parser->scope;
21387 /* The next name will not be qualified. */
21388 parser->scope = NULL_TREE;
21389 parser->qualifying_scope = NULL_TREE;
21390 parser->object_scope = NULL_TREE;
21391 /* Look for optional c++11 attributes. */
21392 attrs = cp_parser_std_attribute_spec_seq (parser);
21393 if (attributes != NULL)
21394 *attributes = attrs;
21395 /* Look for the optional cv-qualifier-seq. */
21396 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21397 }
21398 }
21399 /* If that didn't work we don't have a ptr-operator. */
21400 if (!cp_parser_parse_definitely (parser))
21401 cp_parser_error (parser, "expected ptr-operator");
21402 }
21403
21404 return code;
21405 }
21406
21407 /* Parse an (optional) cv-qualifier-seq.
21408
21409 cv-qualifier-seq:
21410 cv-qualifier cv-qualifier-seq [opt]
21411
21412 cv-qualifier:
21413 const
21414 volatile
21415
21416 GNU Extension:
21417
21418 cv-qualifier:
21419 __restrict__
21420
21421 Returns a bitmask representing the cv-qualifiers. */
21422
21423 static cp_cv_quals
21424 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
21425 {
21426 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
21427
21428 while (true)
21429 {
21430 cp_token *token;
21431 cp_cv_quals cv_qualifier;
21432
21433 /* Peek at the next token. */
21434 token = cp_lexer_peek_token (parser->lexer);
21435 /* See if it's a cv-qualifier. */
21436 switch (token->keyword)
21437 {
21438 case RID_CONST:
21439 cv_qualifier = TYPE_QUAL_CONST;
21440 break;
21441
21442 case RID_VOLATILE:
21443 cv_qualifier = TYPE_QUAL_VOLATILE;
21444 break;
21445
21446 case RID_RESTRICT:
21447 cv_qualifier = TYPE_QUAL_RESTRICT;
21448 break;
21449
21450 default:
21451 cv_qualifier = TYPE_UNQUALIFIED;
21452 break;
21453 }
21454
21455 if (!cv_qualifier)
21456 break;
21457
21458 if (cv_quals & cv_qualifier)
21459 {
21460 gcc_rich_location richloc (token->location);
21461 richloc.add_fixit_remove ();
21462 error_at (&richloc, "duplicate cv-qualifier");
21463 cp_lexer_purge_token (parser->lexer);
21464 }
21465 else
21466 {
21467 cp_lexer_consume_token (parser->lexer);
21468 cv_quals |= cv_qualifier;
21469 }
21470 }
21471
21472 return cv_quals;
21473 }
21474
21475 /* Parse an (optional) ref-qualifier
21476
21477 ref-qualifier:
21478 &
21479 &&
21480
21481 Returns cp_ref_qualifier representing ref-qualifier. */
21482
21483 static cp_ref_qualifier
21484 cp_parser_ref_qualifier_opt (cp_parser* parser)
21485 {
21486 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
21487
21488 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21489 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
21490 return ref_qual;
21491
21492 while (true)
21493 {
21494 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
21495 cp_token *token = cp_lexer_peek_token (parser->lexer);
21496
21497 switch (token->type)
21498 {
21499 case CPP_AND:
21500 curr_ref_qual = REF_QUAL_LVALUE;
21501 break;
21502
21503 case CPP_AND_AND:
21504 curr_ref_qual = REF_QUAL_RVALUE;
21505 break;
21506
21507 default:
21508 curr_ref_qual = REF_QUAL_NONE;
21509 break;
21510 }
21511
21512 if (!curr_ref_qual)
21513 break;
21514 else if (ref_qual)
21515 {
21516 error_at (token->location, "multiple ref-qualifiers");
21517 cp_lexer_purge_token (parser->lexer);
21518 }
21519 else
21520 {
21521 ref_qual = curr_ref_qual;
21522 cp_lexer_consume_token (parser->lexer);
21523 }
21524 }
21525
21526 return ref_qual;
21527 }
21528
21529 /* Parse an optional tx-qualifier.
21530
21531 tx-qualifier:
21532 transaction_safe
21533 transaction_safe_dynamic */
21534
21535 static tree
21536 cp_parser_tx_qualifier_opt (cp_parser *parser)
21537 {
21538 cp_token *token = cp_lexer_peek_token (parser->lexer);
21539 if (token->type == CPP_NAME)
21540 {
21541 tree name = token->u.value;
21542 const char *p = IDENTIFIER_POINTER (name);
21543 const int len = strlen ("transaction_safe");
21544 if (!strncmp (p, "transaction_safe", len))
21545 {
21546 p += len;
21547 if (*p == '\0'
21548 || !strcmp (p, "_dynamic"))
21549 {
21550 cp_lexer_consume_token (parser->lexer);
21551 if (!flag_tm)
21552 {
21553 error ("%qE requires %<-fgnu-tm%>", name);
21554 return NULL_TREE;
21555 }
21556 else
21557 return name;
21558 }
21559 }
21560 }
21561 return NULL_TREE;
21562 }
21563
21564 /* Parse an (optional) virt-specifier-seq.
21565
21566 virt-specifier-seq:
21567 virt-specifier virt-specifier-seq [opt]
21568
21569 virt-specifier:
21570 override
21571 final
21572
21573 Returns a bitmask representing the virt-specifiers. */
21574
21575 static cp_virt_specifiers
21576 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21577 {
21578 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21579
21580 while (true)
21581 {
21582 cp_token *token;
21583 cp_virt_specifiers virt_specifier;
21584
21585 /* Peek at the next token. */
21586 token = cp_lexer_peek_token (parser->lexer);
21587 /* See if it's a virt-specifier-qualifier. */
21588 if (token->type != CPP_NAME)
21589 break;
21590 if (id_equal (token->u.value, "override"))
21591 {
21592 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21593 virt_specifier = VIRT_SPEC_OVERRIDE;
21594 }
21595 else if (id_equal (token->u.value, "final"))
21596 {
21597 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21598 virt_specifier = VIRT_SPEC_FINAL;
21599 }
21600 else if (id_equal (token->u.value, "__final"))
21601 {
21602 virt_specifier = VIRT_SPEC_FINAL;
21603 }
21604 else
21605 break;
21606
21607 if (virt_specifiers & virt_specifier)
21608 {
21609 gcc_rich_location richloc (token->location);
21610 richloc.add_fixit_remove ();
21611 error_at (&richloc, "duplicate virt-specifier");
21612 cp_lexer_purge_token (parser->lexer);
21613 }
21614 else
21615 {
21616 cp_lexer_consume_token (parser->lexer);
21617 virt_specifiers |= virt_specifier;
21618 }
21619 }
21620 return virt_specifiers;
21621 }
21622
21623 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21624 is in scope even though it isn't real. */
21625
21626 void
21627 inject_this_parameter (tree ctype, cp_cv_quals quals)
21628 {
21629 tree this_parm;
21630
21631 if (current_class_ptr)
21632 {
21633 /* We don't clear this between NSDMIs. Is it already what we want? */
21634 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21635 if (DECL_P (current_class_ptr)
21636 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21637 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21638 && cp_type_quals (type) == quals)
21639 return;
21640 }
21641
21642 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21643 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21644 current_class_ptr = NULL_TREE;
21645 current_class_ref
21646 = cp_build_fold_indirect_ref (this_parm);
21647 current_class_ptr = this_parm;
21648 }
21649
21650 /* Return true iff our current scope is a non-static data member
21651 initializer. */
21652
21653 bool
21654 parsing_nsdmi (void)
21655 {
21656 /* We recognize NSDMI context by the context-less 'this' pointer set up
21657 by the function above. */
21658 if (current_class_ptr
21659 && TREE_CODE (current_class_ptr) == PARM_DECL
21660 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21661 return true;
21662 return false;
21663 }
21664
21665 /* Parse a late-specified return type, if any. This is not a separate
21666 non-terminal, but part of a function declarator, which looks like
21667
21668 -> trailing-type-specifier-seq abstract-declarator(opt)
21669
21670 Returns the type indicated by the type-id.
21671
21672 In addition to this, parse any queued up #pragma omp declare simd
21673 clauses, and #pragma acc routine clauses.
21674
21675 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21676 function. */
21677
21678 static tree
21679 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21680 tree& requires_clause, cp_cv_quals quals)
21681 {
21682 cp_token *token;
21683 tree type = NULL_TREE;
21684 bool declare_simd_p = (parser->omp_declare_simd
21685 && declarator
21686 && declarator->kind == cdk_id);
21687
21688 bool oacc_routine_p = (parser->oacc_routine
21689 && declarator
21690 && declarator->kind == cdk_id);
21691
21692 /* Peek at the next token. */
21693 token = cp_lexer_peek_token (parser->lexer);
21694 /* A late-specified return type is indicated by an initial '->'. */
21695 if (token->type != CPP_DEREF
21696 && token->keyword != RID_REQUIRES
21697 && !(token->type == CPP_NAME
21698 && token->u.value == ridpointers[RID_REQUIRES])
21699 && !(declare_simd_p || oacc_routine_p))
21700 return NULL_TREE;
21701
21702 tree save_ccp = current_class_ptr;
21703 tree save_ccr = current_class_ref;
21704 if (quals >= 0)
21705 {
21706 /* DR 1207: 'this' is in scope in the trailing return type. */
21707 inject_this_parameter (current_class_type, quals);
21708 }
21709
21710 if (token->type == CPP_DEREF)
21711 {
21712 /* Consume the ->. */
21713 cp_lexer_consume_token (parser->lexer);
21714
21715 type = cp_parser_trailing_type_id (parser);
21716 }
21717
21718 /* Function declarations may be followed by a trailing
21719 requires-clause. */
21720 requires_clause = cp_parser_requires_clause_opt (parser);
21721
21722 if (declare_simd_p)
21723 declarator->attributes
21724 = cp_parser_late_parsing_omp_declare_simd (parser,
21725 declarator->attributes);
21726 if (oacc_routine_p)
21727 declarator->attributes
21728 = cp_parser_late_parsing_oacc_routine (parser,
21729 declarator->attributes);
21730
21731 if (quals >= 0)
21732 {
21733 current_class_ptr = save_ccp;
21734 current_class_ref = save_ccr;
21735 }
21736
21737 return type;
21738 }
21739
21740 /* Parse a declarator-id.
21741
21742 declarator-id:
21743 id-expression
21744 :: [opt] nested-name-specifier [opt] type-name
21745
21746 In the `id-expression' case, the value returned is as for
21747 cp_parser_id_expression if the id-expression was an unqualified-id.
21748 If the id-expression was a qualified-id, then a SCOPE_REF is
21749 returned. The first operand is the scope (either a NAMESPACE_DECL
21750 or TREE_TYPE), but the second is still just a representation of an
21751 unqualified-id. */
21752
21753 static tree
21754 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21755 {
21756 tree id;
21757 /* The expression must be an id-expression. Assume that qualified
21758 names are the names of types so that:
21759
21760 template <class T>
21761 int S<T>::R::i = 3;
21762
21763 will work; we must treat `S<T>::R' as the name of a type.
21764 Similarly, assume that qualified names are templates, where
21765 required, so that:
21766
21767 template <class T>
21768 int S<T>::R<T>::i = 3;
21769
21770 will work, too. */
21771 id = cp_parser_id_expression (parser,
21772 /*template_keyword_p=*/false,
21773 /*check_dependency_p=*/false,
21774 /*template_p=*/NULL,
21775 /*declarator_p=*/true,
21776 optional_p);
21777 if (id && BASELINK_P (id))
21778 id = BASELINK_FUNCTIONS (id);
21779 return id;
21780 }
21781
21782 /* Parse a type-id.
21783
21784 type-id:
21785 type-specifier-seq abstract-declarator [opt]
21786
21787 The parser flags FLAGS is used to control type-specifier parsing.
21788
21789 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
21790
21791 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21792 i.e. we've just seen "->".
21793
21794 Returns the TYPE specified. */
21795
21796 static tree
21797 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
21798 bool is_template_arg, bool is_trailing_return,
21799 location_t *type_location)
21800 {
21801 cp_decl_specifier_seq type_specifier_seq;
21802 cp_declarator *abstract_declarator;
21803
21804 /* Parse the type-specifier-seq. */
21805 cp_parser_type_specifier_seq (parser, flags,
21806 /*is_declaration=*/false,
21807 is_trailing_return,
21808 &type_specifier_seq);
21809 if (type_location)
21810 *type_location = type_specifier_seq.locations[ds_type_spec];
21811
21812 if (is_template_arg && type_specifier_seq.type
21813 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21814 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21815 /* A bare template name as a template argument is a template template
21816 argument, not a placeholder, so fail parsing it as a type argument. */
21817 {
21818 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21819 cp_parser_simulate_error (parser);
21820 return error_mark_node;
21821 }
21822 if (type_specifier_seq.type == error_mark_node)
21823 return error_mark_node;
21824
21825 /* There might or might not be an abstract declarator. */
21826 cp_parser_parse_tentatively (parser);
21827 /* Look for the declarator. */
21828 abstract_declarator
21829 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
21830 CP_PARSER_FLAGS_NONE, NULL,
21831 /*parenthesized_p=*/NULL,
21832 /*member_p=*/false,
21833 /*friend_p=*/false,
21834 /*static_p=*/false);
21835 /* Check to see if there really was a declarator. */
21836 if (!cp_parser_parse_definitely (parser))
21837 abstract_declarator = NULL;
21838
21839 if (type_specifier_seq.type
21840 /* The concepts TS allows 'auto' as a type-id. */
21841 && (!flag_concepts || parser->in_type_id_in_expr_p)
21842 /* None of the valid uses of 'auto' in C++14 involve the type-id
21843 nonterminal, but it is valid in a trailing-return-type. */
21844 && !(cxx_dialect >= cxx14 && is_trailing_return))
21845 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21846 {
21847 /* A type-id with type 'auto' is only ok if the abstract declarator
21848 is a function declarator with a late-specified return type.
21849
21850 A type-id with 'auto' is also valid in a trailing-return-type
21851 in a compound-requirement. */
21852 if (abstract_declarator
21853 && abstract_declarator->kind == cdk_function
21854 && abstract_declarator->u.function.late_return_type)
21855 /* OK */;
21856 else if (parser->in_result_type_constraint_p)
21857 /* OK */;
21858 else
21859 {
21860 location_t loc = type_specifier_seq.locations[ds_type_spec];
21861 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21862 {
21863 error_at (loc, "missing template arguments after %qT",
21864 auto_node);
21865 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21866 tmpl);
21867 }
21868 else
21869 error_at (loc, "invalid use of %qT", auto_node);
21870 return error_mark_node;
21871 }
21872 }
21873
21874 return groktypename (&type_specifier_seq, abstract_declarator,
21875 is_template_arg);
21876 }
21877
21878 /* Wrapper for cp_parser_type_id_1. */
21879
21880 static tree
21881 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
21882 location_t *type_location)
21883 {
21884 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
21885 }
21886
21887 /* Wrapper for cp_parser_type_id_1. */
21888
21889 static tree
21890 cp_parser_template_type_arg (cp_parser *parser)
21891 {
21892 tree r;
21893 const char *saved_message = parser->type_definition_forbidden_message;
21894 parser->type_definition_forbidden_message
21895 = G_("types may not be defined in template arguments");
21896 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
21897 parser->type_definition_forbidden_message = saved_message;
21898 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21899 {
21900 error ("invalid use of %<auto%> in template argument");
21901 r = error_mark_node;
21902 }
21903 return r;
21904 }
21905
21906 /* Wrapper for cp_parser_type_id_1. */
21907
21908 static tree
21909 cp_parser_trailing_type_id (cp_parser *parser)
21910 {
21911 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
21912 false, true, NULL);
21913 }
21914
21915 /* Parse a type-specifier-seq.
21916
21917 type-specifier-seq:
21918 type-specifier type-specifier-seq [opt]
21919
21920 GNU extension:
21921
21922 type-specifier-seq:
21923 attributes type-specifier-seq [opt]
21924
21925 The parser flags FLAGS is used to control type-specifier parsing.
21926
21927 If IS_DECLARATION is true, we are at the start of a "condition" or
21928 exception-declaration, so we might be followed by a declarator-id.
21929
21930 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21931 i.e. we've just seen "->".
21932
21933 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21934
21935 static void
21936 cp_parser_type_specifier_seq (cp_parser* parser,
21937 cp_parser_flags flags,
21938 bool is_declaration,
21939 bool is_trailing_return,
21940 cp_decl_specifier_seq *type_specifier_seq)
21941 {
21942 bool seen_type_specifier = false;
21943 cp_token *start_token = NULL;
21944
21945 /* Clear the TYPE_SPECIFIER_SEQ. */
21946 clear_decl_specs (type_specifier_seq);
21947
21948 flags |= CP_PARSER_FLAGS_OPTIONAL;
21949 /* In the context of a trailing return type, enum E { } is an
21950 elaborated-type-specifier followed by a function-body, not an
21951 enum-specifier. */
21952 if (is_trailing_return)
21953 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21954
21955 /* Parse the type-specifiers and attributes. */
21956 while (true)
21957 {
21958 tree type_specifier;
21959 bool is_cv_qualifier;
21960
21961 /* Check for attributes first. */
21962 if (cp_next_tokens_can_be_attribute_p (parser))
21963 {
21964 type_specifier_seq->attributes
21965 = attr_chainon (type_specifier_seq->attributes,
21966 cp_parser_attributes_opt (parser));
21967 continue;
21968 }
21969
21970 /* record the token of the beginning of the type specifier seq,
21971 for error reporting purposes*/
21972 if (!start_token)
21973 start_token = cp_lexer_peek_token (parser->lexer);
21974
21975 /* Look for the type-specifier. */
21976 type_specifier = cp_parser_type_specifier (parser,
21977 flags,
21978 type_specifier_seq,
21979 /*is_declaration=*/false,
21980 NULL,
21981 &is_cv_qualifier);
21982 if (!type_specifier)
21983 {
21984 /* If the first type-specifier could not be found, this is not a
21985 type-specifier-seq at all. */
21986 if (!seen_type_specifier)
21987 {
21988 /* Set in_declarator_p to avoid skipping to the semicolon. */
21989 int in_decl = parser->in_declarator_p;
21990 parser->in_declarator_p = true;
21991
21992 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21993 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21994 cp_parser_error (parser, "expected type-specifier");
21995
21996 parser->in_declarator_p = in_decl;
21997
21998 type_specifier_seq->type = error_mark_node;
21999 return;
22000 }
22001 /* If subsequent type-specifiers could not be found, the
22002 type-specifier-seq is complete. */
22003 break;
22004 }
22005
22006 seen_type_specifier = true;
22007 /* The standard says that a condition can be:
22008
22009 type-specifier-seq declarator = assignment-expression
22010
22011 However, given:
22012
22013 struct S {};
22014 if (int S = ...)
22015
22016 we should treat the "S" as a declarator, not as a
22017 type-specifier. The standard doesn't say that explicitly for
22018 type-specifier-seq, but it does say that for
22019 decl-specifier-seq in an ordinary declaration. Perhaps it
22020 would be clearer just to allow a decl-specifier-seq here, and
22021 then add a semantic restriction that if any decl-specifiers
22022 that are not type-specifiers appear, the program is invalid. */
22023 if (is_declaration && !is_cv_qualifier)
22024 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
22025 }
22026 }
22027
22028 /* Return whether the function currently being declared has an associated
22029 template parameter list. */
22030
22031 static bool
22032 function_being_declared_is_template_p (cp_parser* parser)
22033 {
22034 if (!current_template_parms || processing_template_parmlist)
22035 return false;
22036
22037 if (parser->implicit_template_scope)
22038 return true;
22039
22040 if (at_class_scope_p ()
22041 && TYPE_BEING_DEFINED (current_class_type))
22042 return parser->num_template_parameter_lists != 0;
22043
22044 return ((int) parser->num_template_parameter_lists > template_class_depth
22045 (current_class_type));
22046 }
22047
22048 /* Parse a parameter-declaration-clause.
22049
22050 parameter-declaration-clause:
22051 parameter-declaration-list [opt] ... [opt]
22052 parameter-declaration-list , ...
22053
22054 The parser flags FLAGS is used to control type-specifier parsing.
22055
22056 Returns a representation for the parameter declarations. A return
22057 value of NULL indicates a parameter-declaration-clause consisting
22058 only of an ellipsis. */
22059
22060 static tree
22061 cp_parser_parameter_declaration_clause (cp_parser* parser,
22062 cp_parser_flags flags)
22063 {
22064 tree parameters;
22065 cp_token *token;
22066 bool ellipsis_p;
22067
22068 temp_override<bool> cleanup
22069 (parser->auto_is_implicit_function_template_parm_p);
22070
22071 if (!processing_specialization
22072 && !processing_template_parmlist
22073 && !processing_explicit_instantiation
22074 /* default_arg_ok_p tracks whether this is a parameter-clause for an
22075 actual function or a random abstract declarator. */
22076 && parser->default_arg_ok_p)
22077 if (!current_function_decl
22078 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
22079 parser->auto_is_implicit_function_template_parm_p = true;
22080
22081 /* Peek at the next token. */
22082 token = cp_lexer_peek_token (parser->lexer);
22083 /* Check for trivial parameter-declaration-clauses. */
22084 if (token->type == CPP_ELLIPSIS)
22085 {
22086 /* Consume the `...' token. */
22087 cp_lexer_consume_token (parser->lexer);
22088 return NULL_TREE;
22089 }
22090 else if (token->type == CPP_CLOSE_PAREN)
22091 /* There are no parameters. */
22092 return void_list_node;
22093 /* Check for `(void)', too, which is a special case. */
22094 else if (token->keyword == RID_VOID
22095 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22096 == CPP_CLOSE_PAREN))
22097 {
22098 /* Consume the `void' token. */
22099 cp_lexer_consume_token (parser->lexer);
22100 /* There are no parameters. */
22101 return void_list_node;
22102 }
22103
22104 /* Parse the parameter-declaration-list. */
22105 parameters = cp_parser_parameter_declaration_list (parser, flags);
22106 /* If a parse error occurred while parsing the
22107 parameter-declaration-list, then the entire
22108 parameter-declaration-clause is erroneous. */
22109 if (parameters == error_mark_node)
22110 return NULL_TREE;
22111
22112 /* Peek at the next token. */
22113 token = cp_lexer_peek_token (parser->lexer);
22114 /* If it's a `,', the clause should terminate with an ellipsis. */
22115 if (token->type == CPP_COMMA)
22116 {
22117 /* Consume the `,'. */
22118 cp_lexer_consume_token (parser->lexer);
22119 /* Expect an ellipsis. */
22120 ellipsis_p
22121 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
22122 }
22123 /* It might also be `...' if the optional trailing `,' was
22124 omitted. */
22125 else if (token->type == CPP_ELLIPSIS)
22126 {
22127 /* Consume the `...' token. */
22128 cp_lexer_consume_token (parser->lexer);
22129 /* And remember that we saw it. */
22130 ellipsis_p = true;
22131 }
22132 else
22133 ellipsis_p = false;
22134
22135 /* Finish the parameter list. */
22136 if (!ellipsis_p)
22137 parameters = chainon (parameters, void_list_node);
22138
22139 return parameters;
22140 }
22141
22142 /* Parse a parameter-declaration-list.
22143
22144 parameter-declaration-list:
22145 parameter-declaration
22146 parameter-declaration-list , parameter-declaration
22147
22148 The parser flags FLAGS is used to control type-specifier parsing.
22149
22150 Returns a representation of the parameter-declaration-list, as for
22151 cp_parser_parameter_declaration_clause. However, the
22152 `void_list_node' is never appended to the list. */
22153
22154 static tree
22155 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
22156 {
22157 tree parameters = NULL_TREE;
22158 tree *tail = &parameters;
22159 bool saved_in_unbraced_linkage_specification_p;
22160 int index = 0;
22161
22162 /* The special considerations that apply to a function within an
22163 unbraced linkage specifications do not apply to the parameters
22164 to the function. */
22165 saved_in_unbraced_linkage_specification_p
22166 = parser->in_unbraced_linkage_specification_p;
22167 parser->in_unbraced_linkage_specification_p = false;
22168
22169 /* Look for more parameters. */
22170 while (true)
22171 {
22172 cp_parameter_declarator *parameter;
22173 tree decl = error_mark_node;
22174 bool parenthesized_p = false;
22175
22176 /* Parse the parameter. */
22177 parameter
22178 = cp_parser_parameter_declaration (parser, flags,
22179 /*template_parm_p=*/false,
22180 &parenthesized_p);
22181
22182 /* We don't know yet if the enclosing context is deprecated, so wait
22183 and warn in grokparms if appropriate. */
22184 deprecated_state = DEPRECATED_SUPPRESS;
22185
22186 if (parameter)
22187 {
22188 decl = grokdeclarator (parameter->declarator,
22189 &parameter->decl_specifiers,
22190 PARM,
22191 parameter->default_argument != NULL_TREE,
22192 &parameter->decl_specifiers.attributes);
22193 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
22194 DECL_SOURCE_LOCATION (decl) = parameter->loc;
22195 }
22196
22197 deprecated_state = DEPRECATED_NORMAL;
22198
22199 /* If a parse error occurred parsing the parameter declaration,
22200 then the entire parameter-declaration-list is erroneous. */
22201 if (decl == error_mark_node)
22202 {
22203 parameters = error_mark_node;
22204 break;
22205 }
22206
22207 if (parameter->decl_specifiers.attributes)
22208 cplus_decl_attributes (&decl,
22209 parameter->decl_specifiers.attributes,
22210 0);
22211 if (DECL_NAME (decl))
22212 decl = pushdecl (decl);
22213
22214 if (decl != error_mark_node)
22215 {
22216 retrofit_lang_decl (decl);
22217 DECL_PARM_INDEX (decl) = ++index;
22218 DECL_PARM_LEVEL (decl) = function_parm_depth ();
22219 }
22220
22221 /* Add the new parameter to the list. */
22222 *tail = build_tree_list (parameter->default_argument, decl);
22223 tail = &TREE_CHAIN (*tail);
22224
22225 /* Peek at the next token. */
22226 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
22227 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
22228 /* These are for Objective-C++ */
22229 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22230 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22231 /* The parameter-declaration-list is complete. */
22232 break;
22233 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22234 {
22235 cp_token *token;
22236
22237 /* Peek at the next token. */
22238 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22239 /* If it's an ellipsis, then the list is complete. */
22240 if (token->type == CPP_ELLIPSIS)
22241 break;
22242 /* Otherwise, there must be more parameters. Consume the
22243 `,'. */
22244 cp_lexer_consume_token (parser->lexer);
22245 /* When parsing something like:
22246
22247 int i(float f, double d)
22248
22249 we can tell after seeing the declaration for "f" that we
22250 are not looking at an initialization of a variable "i",
22251 but rather at the declaration of a function "i".
22252
22253 Due to the fact that the parsing of template arguments
22254 (as specified to a template-id) requires backtracking we
22255 cannot use this technique when inside a template argument
22256 list. */
22257 if (!parser->in_template_argument_list_p
22258 && !parser->in_type_id_in_expr_p
22259 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22260 /* However, a parameter-declaration of the form
22261 "float(f)" (which is a valid declaration of a
22262 parameter "f") can also be interpreted as an
22263 expression (the conversion of "f" to "float"). */
22264 && !parenthesized_p)
22265 cp_parser_commit_to_tentative_parse (parser);
22266 }
22267 else
22268 {
22269 cp_parser_error (parser, "expected %<,%> or %<...%>");
22270 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
22271 cp_parser_skip_to_closing_parenthesis (parser,
22272 /*recovering=*/true,
22273 /*or_comma=*/false,
22274 /*consume_paren=*/false);
22275 break;
22276 }
22277 }
22278
22279 parser->in_unbraced_linkage_specification_p
22280 = saved_in_unbraced_linkage_specification_p;
22281
22282 /* Reset implicit_template_scope if we are about to leave the function
22283 parameter list that introduced it. Note that for out-of-line member
22284 definitions, there will be one or more class scopes before we get to
22285 the template parameter scope. */
22286
22287 if (cp_binding_level *its = parser->implicit_template_scope)
22288 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
22289 {
22290 while (maybe_its->kind == sk_class)
22291 maybe_its = maybe_its->level_chain;
22292 if (maybe_its == its)
22293 {
22294 parser->implicit_template_parms = 0;
22295 parser->implicit_template_scope = 0;
22296 }
22297 }
22298
22299 return parameters;
22300 }
22301
22302 /* Parse a parameter declaration.
22303
22304 parameter-declaration:
22305 decl-specifier-seq ... [opt] declarator
22306 decl-specifier-seq declarator = assignment-expression
22307 decl-specifier-seq ... [opt] abstract-declarator [opt]
22308 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22309
22310 The parser flags FLAGS is used to control type-specifier parsing.
22311
22312 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22313 declares a template parameter. (In that case, a non-nested `>'
22314 token encountered during the parsing of the assignment-expression
22315 is not interpreted as a greater-than operator.)
22316
22317 Returns a representation of the parameter, or NULL if an error
22318 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22319 true iff the declarator is of the form "(p)". */
22320
22321 static cp_parameter_declarator *
22322 cp_parser_parameter_declaration (cp_parser *parser,
22323 cp_parser_flags flags,
22324 bool template_parm_p,
22325 bool *parenthesized_p)
22326 {
22327 int declares_class_or_enum;
22328 cp_decl_specifier_seq decl_specifiers;
22329 cp_declarator *declarator;
22330 tree default_argument;
22331 cp_token *token = NULL, *declarator_token_start = NULL;
22332 const char *saved_message;
22333 bool template_parameter_pack_p = false;
22334
22335 /* In a template parameter, `>' is not an operator.
22336
22337 [temp.param]
22338
22339 When parsing a default template-argument for a non-type
22340 template-parameter, the first non-nested `>' is taken as the end
22341 of the template parameter-list rather than a greater-than
22342 operator. */
22343
22344 /* Type definitions may not appear in parameter types. */
22345 saved_message = parser->type_definition_forbidden_message;
22346 parser->type_definition_forbidden_message
22347 = G_("types may not be defined in parameter types");
22348
22349 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
22350 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22351 (current_template_parms)) : 0);
22352
22353 /* Parse the declaration-specifiers. */
22354 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22355 cp_parser_decl_specifier_seq (parser,
22356 flags,
22357 &decl_specifiers,
22358 &declares_class_or_enum);
22359
22360 /* Complain about missing 'typename' or other invalid type names. */
22361 if (!decl_specifiers.any_type_specifiers_p
22362 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22363 decl_specifiers.type = error_mark_node;
22364
22365 /* If an error occurred, there's no reason to attempt to parse the
22366 rest of the declaration. */
22367 if (cp_parser_error_occurred (parser))
22368 {
22369 parser->type_definition_forbidden_message = saved_message;
22370 return NULL;
22371 }
22372
22373 /* Peek at the next token. */
22374 token = cp_lexer_peek_token (parser->lexer);
22375
22376 /* If the next token is a `)', `,', `=', `>', or `...', then there
22377 is no declarator. However, when variadic templates are enabled,
22378 there may be a declarator following `...'. */
22379 if (token->type == CPP_CLOSE_PAREN
22380 || token->type == CPP_COMMA
22381 || token->type == CPP_EQ
22382 || token->type == CPP_GREATER)
22383 {
22384 declarator = NULL;
22385 if (parenthesized_p)
22386 *parenthesized_p = false;
22387 }
22388 /* Otherwise, there should be a declarator. */
22389 else
22390 {
22391 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22392 parser->default_arg_ok_p = false;
22393
22394 /* After seeing a decl-specifier-seq, if the next token is not a
22395 "(", there is no possibility that the code is a valid
22396 expression. Therefore, if parsing tentatively, we commit at
22397 this point. */
22398 if (!parser->in_template_argument_list_p
22399 /* In an expression context, having seen:
22400
22401 (int((char ...
22402
22403 we cannot be sure whether we are looking at a
22404 function-type (taking a "char" as a parameter) or a cast
22405 of some object of type "char" to "int". */
22406 && !parser->in_type_id_in_expr_p
22407 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22408 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22409 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
22410 cp_parser_commit_to_tentative_parse (parser);
22411 /* Parse the declarator. */
22412 declarator_token_start = token;
22413 declarator = cp_parser_declarator (parser,
22414 CP_PARSER_DECLARATOR_EITHER,
22415 CP_PARSER_FLAGS_NONE,
22416 /*ctor_dtor_or_conv_p=*/NULL,
22417 parenthesized_p,
22418 /*member_p=*/false,
22419 /*friend_p=*/false,
22420 /*static_p=*/false);
22421 parser->default_arg_ok_p = saved_default_arg_ok_p;
22422 /* After the declarator, allow more attributes. */
22423 decl_specifiers.attributes
22424 = attr_chainon (decl_specifiers.attributes,
22425 cp_parser_attributes_opt (parser));
22426
22427 /* If the declarator is a template parameter pack, remember that and
22428 clear the flag in the declarator itself so we don't get errors
22429 from grokdeclarator. */
22430 if (template_parm_p && declarator && declarator->parameter_pack_p)
22431 {
22432 declarator->parameter_pack_p = false;
22433 template_parameter_pack_p = true;
22434 }
22435 }
22436
22437 /* If the next token is an ellipsis, and we have not seen a declarator
22438 name, and if either the type of the declarator contains parameter
22439 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22440 for, eg, abbreviated integral type names), then we actually have a
22441 parameter pack expansion expression. Otherwise, leave the ellipsis
22442 for a C-style variadic function. */
22443 token = cp_lexer_peek_token (parser->lexer);
22444
22445 /* If a function parameter pack was specified and an implicit template
22446 parameter was introduced during cp_parser_parameter_declaration,
22447 change any implicit parameters introduced into packs. */
22448 if (parser->implicit_template_parms
22449 && ((token->type == CPP_ELLIPSIS
22450 && declarator_can_be_parameter_pack (declarator))
22451 || (declarator && declarator->parameter_pack_p)))
22452 {
22453 int latest_template_parm_idx = TREE_VEC_LENGTH
22454 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
22455
22456 if (latest_template_parm_idx != template_parm_idx)
22457 decl_specifiers.type = convert_generic_types_to_packs
22458 (decl_specifiers.type,
22459 template_parm_idx, latest_template_parm_idx);
22460 }
22461
22462 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22463 {
22464 tree type = decl_specifiers.type;
22465
22466 if (type && DECL_P (type))
22467 type = TREE_TYPE (type);
22468
22469 if (((type
22470 && TREE_CODE (type) != TYPE_PACK_EXPANSION
22471 && (template_parm_p || uses_parameter_packs (type)))
22472 || (!type && template_parm_p))
22473 && declarator_can_be_parameter_pack (declarator))
22474 {
22475 /* Consume the `...'. */
22476 cp_lexer_consume_token (parser->lexer);
22477 maybe_warn_variadic_templates ();
22478
22479 /* Build a pack expansion type */
22480 if (template_parm_p)
22481 template_parameter_pack_p = true;
22482 else if (declarator)
22483 declarator->parameter_pack_p = true;
22484 else
22485 decl_specifiers.type = make_pack_expansion (type);
22486 }
22487 }
22488
22489 /* The restriction on defining new types applies only to the type
22490 of the parameter, not to the default argument. */
22491 parser->type_definition_forbidden_message = saved_message;
22492
22493 /* If the next token is `=', then process a default argument. */
22494 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22495 {
22496 tree type = decl_specifiers.type;
22497 token = cp_lexer_peek_token (parser->lexer);
22498 /* If we are defining a class, then the tokens that make up the
22499 default argument must be saved and processed later. */
22500 if (!template_parm_p && at_class_scope_p ()
22501 && TYPE_BEING_DEFINED (current_class_type)
22502 && !LAMBDA_TYPE_P (current_class_type))
22503 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
22504
22505 // A constrained-type-specifier may declare a type template-parameter.
22506 else if (declares_constrained_type_template_parameter (type))
22507 default_argument
22508 = cp_parser_default_type_template_argument (parser);
22509
22510 // A constrained-type-specifier may declare a template-template-parameter.
22511 else if (declares_constrained_template_template_parameter (type))
22512 default_argument
22513 = cp_parser_default_template_template_argument (parser);
22514
22515 /* Outside of a class definition, we can just parse the
22516 assignment-expression. */
22517 else
22518 default_argument
22519 = cp_parser_default_argument (parser, template_parm_p);
22520
22521 if (!parser->default_arg_ok_p)
22522 {
22523 permerror (token->location,
22524 "default arguments are only "
22525 "permitted for function parameters");
22526 }
22527 else if ((declarator && declarator->parameter_pack_p)
22528 || template_parameter_pack_p
22529 || (decl_specifiers.type
22530 && PACK_EXPANSION_P (decl_specifiers.type)))
22531 {
22532 /* Find the name of the parameter pack. */
22533 cp_declarator *id_declarator = declarator;
22534 while (id_declarator && id_declarator->kind != cdk_id)
22535 id_declarator = id_declarator->declarator;
22536
22537 if (id_declarator && id_declarator->kind == cdk_id)
22538 error_at (declarator_token_start->location,
22539 template_parm_p
22540 ? G_("template parameter pack %qD "
22541 "cannot have a default argument")
22542 : G_("parameter pack %qD cannot have "
22543 "a default argument"),
22544 id_declarator->u.id.unqualified_name);
22545 else
22546 error_at (declarator_token_start->location,
22547 template_parm_p
22548 ? G_("template parameter pack cannot have "
22549 "a default argument")
22550 : G_("parameter pack cannot have a "
22551 "default argument"));
22552
22553 default_argument = NULL_TREE;
22554 }
22555 }
22556 else
22557 default_argument = NULL_TREE;
22558
22559 if (default_argument)
22560 STRIP_ANY_LOCATION_WRAPPER (default_argument);
22561
22562 /* Generate a location for the parameter, ranging from the start of the
22563 initial token to the end of the final token (using input_location for
22564 the latter, set up by cp_lexer_set_source_position_from_token when
22565 consuming tokens).
22566
22567 If we have a identifier, then use it for the caret location, e.g.
22568
22569 extern int callee (int one, int (*two)(int, int), float three);
22570 ~~~~~~^~~~~~~~~~~~~~
22571
22572 otherwise, reuse the start location for the caret location e.g.:
22573
22574 extern int callee (int one, int (*)(int, int), float three);
22575 ^~~~~~~~~~~~~~~~~
22576
22577 */
22578 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22579 ? declarator->id_loc
22580 : decl_spec_token_start->location);
22581 location_t param_loc = make_location (caret_loc,
22582 decl_spec_token_start->location,
22583 input_location);
22584
22585 return make_parameter_declarator (&decl_specifiers,
22586 declarator,
22587 default_argument,
22588 param_loc,
22589 template_parameter_pack_p);
22590 }
22591
22592 /* Parse a default argument and return it.
22593
22594 TEMPLATE_PARM_P is true if this is a default argument for a
22595 non-type template parameter. */
22596 static tree
22597 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22598 {
22599 tree default_argument = NULL_TREE;
22600 bool saved_greater_than_is_operator_p;
22601 unsigned char saved_local_variables_forbidden_p;
22602 bool non_constant_p, is_direct_init;
22603
22604 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22605 set correctly. */
22606 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22607 parser->greater_than_is_operator_p = !template_parm_p;
22608 /* Local variable names (and the `this' keyword) may not
22609 appear in a default argument. */
22610 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22611 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
22612 /* Parse the assignment-expression. */
22613 if (template_parm_p)
22614 push_deferring_access_checks (dk_no_deferred);
22615 tree saved_class_ptr = NULL_TREE;
22616 tree saved_class_ref = NULL_TREE;
22617 /* The "this" pointer is not valid in a default argument. */
22618 if (cfun)
22619 {
22620 saved_class_ptr = current_class_ptr;
22621 cp_function_chain->x_current_class_ptr = NULL_TREE;
22622 saved_class_ref = current_class_ref;
22623 cp_function_chain->x_current_class_ref = NULL_TREE;
22624 }
22625 default_argument
22626 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22627 /* Restore the "this" pointer. */
22628 if (cfun)
22629 {
22630 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22631 cp_function_chain->x_current_class_ref = saved_class_ref;
22632 }
22633 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22634 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22635 if (template_parm_p)
22636 pop_deferring_access_checks ();
22637 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22638 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22639
22640 return default_argument;
22641 }
22642
22643 /* Parse a function-body.
22644
22645 function-body:
22646 compound_statement */
22647
22648 static void
22649 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22650 {
22651 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22652 ? BCS_TRY_BLOCK : BCS_NORMAL),
22653 true);
22654 }
22655
22656 /* Parse a ctor-initializer-opt followed by a function-body. Return
22657 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22658 is true we are parsing a function-try-block. */
22659
22660 static void
22661 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22662 bool in_function_try_block)
22663 {
22664 tree body, list;
22665 const bool check_body_p
22666 = (DECL_CONSTRUCTOR_P (current_function_decl)
22667 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
22668 tree last = NULL;
22669
22670 if (in_function_try_block
22671 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
22672 && cxx_dialect < cxx2a)
22673 {
22674 if (DECL_CONSTRUCTOR_P (current_function_decl))
22675 pedwarn (input_location, 0,
22676 "function-try-block body of %<constexpr%> constructor only "
22677 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22678 else
22679 pedwarn (input_location, 0,
22680 "function-try-block body of %<constexpr%> function only "
22681 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22682 }
22683
22684 /* Begin the function body. */
22685 body = begin_function_body ();
22686 /* Parse the optional ctor-initializer. */
22687 cp_parser_ctor_initializer_opt (parser);
22688
22689 /* If we're parsing a constexpr constructor definition, we need
22690 to check that the constructor body is indeed empty. However,
22691 before we get to cp_parser_function_body lot of junk has been
22692 generated, so we can't just check that we have an empty block.
22693 Rather we take a snapshot of the outermost block, and check whether
22694 cp_parser_function_body changed its state. */
22695 if (check_body_p)
22696 {
22697 list = cur_stmt_list;
22698 if (STATEMENT_LIST_TAIL (list))
22699 last = STATEMENT_LIST_TAIL (list)->stmt;
22700 }
22701 /* Parse the function-body. */
22702 cp_parser_function_body (parser, in_function_try_block);
22703 if (check_body_p)
22704 check_constexpr_ctor_body (last, list, /*complain=*/true);
22705 /* Finish the function body. */
22706 finish_function_body (body);
22707 }
22708
22709 /* Parse an initializer.
22710
22711 initializer:
22712 = initializer-clause
22713 ( expression-list )
22714
22715 Returns an expression representing the initializer. If no
22716 initializer is present, NULL_TREE is returned.
22717
22718 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22719 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22720 set to TRUE if there is no initializer present. If there is an
22721 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22722 is set to true; otherwise it is set to false. */
22723
22724 static tree
22725 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22726 bool* non_constant_p, bool subexpression_p)
22727 {
22728 cp_token *token;
22729 tree init;
22730
22731 /* Peek at the next token. */
22732 token = cp_lexer_peek_token (parser->lexer);
22733
22734 /* Let our caller know whether or not this initializer was
22735 parenthesized. */
22736 *is_direct_init = (token->type != CPP_EQ);
22737 /* Assume that the initializer is constant. */
22738 *non_constant_p = false;
22739
22740 if (token->type == CPP_EQ)
22741 {
22742 /* Consume the `='. */
22743 cp_lexer_consume_token (parser->lexer);
22744 /* Parse the initializer-clause. */
22745 init = cp_parser_initializer_clause (parser, non_constant_p);
22746 }
22747 else if (token->type == CPP_OPEN_PAREN)
22748 {
22749 vec<tree, va_gc> *vec;
22750 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22751 /*cast_p=*/false,
22752 /*allow_expansion_p=*/true,
22753 non_constant_p);
22754 if (vec == NULL)
22755 return error_mark_node;
22756 init = build_tree_list_vec (vec);
22757 release_tree_vector (vec);
22758 }
22759 else if (token->type == CPP_OPEN_BRACE)
22760 {
22761 cp_lexer_set_source_position (parser->lexer);
22762 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22763 init = cp_parser_braced_list (parser, non_constant_p);
22764 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22765 }
22766 else
22767 {
22768 /* Anything else is an error. */
22769 cp_parser_error (parser, "expected initializer");
22770 init = error_mark_node;
22771 }
22772
22773 if (!subexpression_p && check_for_bare_parameter_packs (init))
22774 init = error_mark_node;
22775
22776 return init;
22777 }
22778
22779 /* Parse an initializer-clause.
22780
22781 initializer-clause:
22782 assignment-expression
22783 braced-init-list
22784
22785 Returns an expression representing the initializer.
22786
22787 If the `assignment-expression' production is used the value
22788 returned is simply a representation for the expression.
22789
22790 Otherwise, calls cp_parser_braced_list. */
22791
22792 static cp_expr
22793 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22794 {
22795 cp_expr initializer;
22796
22797 /* Assume the expression is constant. */
22798 *non_constant_p = false;
22799
22800 /* If it is not a `{', then we are looking at an
22801 assignment-expression. */
22802 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22803 {
22804 initializer
22805 = cp_parser_constant_expression (parser,
22806 /*allow_non_constant_p=*/true,
22807 non_constant_p);
22808 }
22809 else
22810 initializer = cp_parser_braced_list (parser, non_constant_p);
22811
22812 return initializer;
22813 }
22814
22815 /* Parse a brace-enclosed initializer list.
22816
22817 braced-init-list:
22818 { initializer-list , [opt] }
22819 { designated-initializer-list , [opt] }
22820 { }
22821
22822 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22823 the elements of the initializer-list (or NULL, if the last
22824 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22825 NULL_TREE. There is no way to detect whether or not the optional
22826 trailing `,' was provided. NON_CONSTANT_P is as for
22827 cp_parser_initializer. */
22828
22829 static cp_expr
22830 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22831 {
22832 tree initializer;
22833 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22834
22835 /* Consume the `{' token. */
22836 matching_braces braces;
22837 braces.require_open (parser);
22838 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22839 initializer = make_node (CONSTRUCTOR);
22840 /* If it's not a `}', then there is a non-trivial initializer. */
22841 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22842 {
22843 bool designated;
22844 /* Parse the initializer list. */
22845 CONSTRUCTOR_ELTS (initializer)
22846 = cp_parser_initializer_list (parser, non_constant_p, &designated);
22847 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
22848 /* A trailing `,' token is allowed. */
22849 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22850 cp_lexer_consume_token (parser->lexer);
22851 }
22852 else
22853 *non_constant_p = false;
22854 /* Now, there should be a trailing `}'. */
22855 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22856 braces.require_close (parser);
22857 TREE_TYPE (initializer) = init_list_type_node;
22858
22859 cp_expr result (initializer);
22860 /* Build a location of the form:
22861 { ... }
22862 ^~~~~~~
22863 with caret==start at the open brace, finish at the close brace. */
22864 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22865 result.set_location (combined_loc);
22866 return result;
22867 }
22868
22869 /* Consume tokens up to, and including, the next non-nested closing `]'.
22870 Returns true iff we found a closing `]'. */
22871
22872 static bool
22873 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22874 {
22875 unsigned square_depth = 0;
22876
22877 while (true)
22878 {
22879 cp_token * token = cp_lexer_peek_token (parser->lexer);
22880
22881 switch (token->type)
22882 {
22883 case CPP_PRAGMA_EOL:
22884 if (!parser->lexer->in_pragma)
22885 break;
22886 /* FALLTHRU */
22887 case CPP_EOF:
22888 /* If we've run out of tokens, then there is no closing `]'. */
22889 return false;
22890
22891 case CPP_OPEN_SQUARE:
22892 ++square_depth;
22893 break;
22894
22895 case CPP_CLOSE_SQUARE:
22896 if (!square_depth--)
22897 {
22898 cp_lexer_consume_token (parser->lexer);
22899 return true;
22900 }
22901 break;
22902
22903 default:
22904 break;
22905 }
22906
22907 /* Consume the token. */
22908 cp_lexer_consume_token (parser->lexer);
22909 }
22910 }
22911
22912 /* Return true if we are looking at an array-designator, false otherwise. */
22913
22914 static bool
22915 cp_parser_array_designator_p (cp_parser *parser)
22916 {
22917 /* Consume the `['. */
22918 cp_lexer_consume_token (parser->lexer);
22919
22920 cp_lexer_save_tokens (parser->lexer);
22921
22922 /* Skip tokens until the next token is a closing square bracket.
22923 If we find the closing `]', and the next token is a `=', then
22924 we are looking at an array designator. */
22925 bool array_designator_p
22926 = (cp_parser_skip_to_closing_square_bracket (parser)
22927 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22928
22929 /* Roll back the tokens we skipped. */
22930 cp_lexer_rollback_tokens (parser->lexer);
22931
22932 return array_designator_p;
22933 }
22934
22935 /* Parse an initializer-list.
22936
22937 initializer-list:
22938 initializer-clause ... [opt]
22939 initializer-list , initializer-clause ... [opt]
22940
22941 C++2A Extension:
22942
22943 designated-initializer-list:
22944 designated-initializer-clause
22945 designated-initializer-list , designated-initializer-clause
22946
22947 designated-initializer-clause:
22948 designator brace-or-equal-initializer
22949
22950 designator:
22951 . identifier
22952
22953 GNU Extension:
22954
22955 initializer-list:
22956 designation initializer-clause ...[opt]
22957 initializer-list , designation initializer-clause ...[opt]
22958
22959 designation:
22960 . identifier =
22961 identifier :
22962 [ constant-expression ] =
22963
22964 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22965 for the initializer. If the INDEX of the elt is non-NULL, it is the
22966 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22967 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
22968 are any designators. */
22969
22970 static vec<constructor_elt, va_gc> *
22971 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
22972 bool *designated)
22973 {
22974 vec<constructor_elt, va_gc> *v = NULL;
22975 bool first_p = true;
22976 tree first_designator = NULL_TREE;
22977
22978 /* Assume all of the expressions are constant. */
22979 *non_constant_p = false;
22980
22981 /* Parse the rest of the list. */
22982 while (true)
22983 {
22984 cp_token *token;
22985 tree designator;
22986 tree initializer;
22987 bool clause_non_constant_p;
22988 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22989
22990 /* Handle the C++2A syntax, '. id ='. */
22991 if ((cxx_dialect >= cxx2a
22992 || cp_parser_allow_gnu_extensions_p (parser))
22993 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22994 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22995 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22996 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22997 == CPP_OPEN_BRACE)))
22998 {
22999 if (cxx_dialect < cxx2a)
23000 pedwarn (loc, OPT_Wpedantic,
23001 "C++ designated initializers only available with "
23002 "%<-std=c++2a%> or %<-std=gnu++2a%>");
23003 /* Consume the `.'. */
23004 cp_lexer_consume_token (parser->lexer);
23005 /* Consume the identifier. */
23006 designator = cp_lexer_consume_token (parser->lexer)->u.value;
23007 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23008 /* Consume the `='. */
23009 cp_lexer_consume_token (parser->lexer);
23010 }
23011 /* Also, if the next token is an identifier and the following one is a
23012 colon, we are looking at the GNU designated-initializer
23013 syntax. */
23014 else if (cp_parser_allow_gnu_extensions_p (parser)
23015 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
23016 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23017 == CPP_COLON))
23018 {
23019 /* Warn the user that they are using an extension. */
23020 pedwarn (loc, OPT_Wpedantic,
23021 "ISO C++ does not allow GNU designated initializers");
23022 /* Consume the identifier. */
23023 designator = cp_lexer_consume_token (parser->lexer)->u.value;
23024 /* Consume the `:'. */
23025 cp_lexer_consume_token (parser->lexer);
23026 }
23027 /* Also handle C99 array designators, '[ const ] ='. */
23028 else if (cp_parser_allow_gnu_extensions_p (parser)
23029 && !c_dialect_objc ()
23030 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23031 {
23032 /* In C++11, [ could start a lambda-introducer. */
23033 bool non_const = false;
23034
23035 cp_parser_parse_tentatively (parser);
23036
23037 if (!cp_parser_array_designator_p (parser))
23038 {
23039 cp_parser_simulate_error (parser);
23040 designator = NULL_TREE;
23041 }
23042 else
23043 {
23044 designator = cp_parser_constant_expression (parser, true,
23045 &non_const);
23046 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23047 cp_parser_require (parser, CPP_EQ, RT_EQ);
23048 }
23049
23050 if (!cp_parser_parse_definitely (parser))
23051 designator = NULL_TREE;
23052 else if (non_const
23053 && (!require_potential_rvalue_constant_expression
23054 (designator)))
23055 designator = NULL_TREE;
23056 if (designator)
23057 /* Warn the user that they are using an extension. */
23058 pedwarn (loc, OPT_Wpedantic,
23059 "ISO C++ does not allow C99 designated initializers");
23060 }
23061 else
23062 designator = NULL_TREE;
23063
23064 if (first_p)
23065 {
23066 first_designator = designator;
23067 first_p = false;
23068 }
23069 else if (cxx_dialect >= cxx2a
23070 && first_designator != error_mark_node
23071 && (!first_designator != !designator))
23072 {
23073 error_at (loc, "either all initializer clauses should be designated "
23074 "or none of them should be");
23075 first_designator = error_mark_node;
23076 }
23077 else if (cxx_dialect < cxx2a && !first_designator)
23078 first_designator = designator;
23079
23080 /* Parse the initializer. */
23081 initializer = cp_parser_initializer_clause (parser,
23082 &clause_non_constant_p);
23083 /* If any clause is non-constant, so is the entire initializer. */
23084 if (clause_non_constant_p)
23085 *non_constant_p = true;
23086
23087 /* If we have an ellipsis, this is an initializer pack
23088 expansion. */
23089 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23090 {
23091 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23092
23093 /* Consume the `...'. */
23094 cp_lexer_consume_token (parser->lexer);
23095
23096 if (designator && cxx_dialect >= cxx2a)
23097 error_at (loc,
23098 "%<...%> not allowed in designated initializer list");
23099
23100 /* Turn the initializer into an initializer expansion. */
23101 initializer = make_pack_expansion (initializer);
23102 }
23103
23104 /* Add it to the vector. */
23105 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
23106
23107 /* If the next token is not a comma, we have reached the end of
23108 the list. */
23109 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23110 break;
23111
23112 /* Peek at the next token. */
23113 token = cp_lexer_peek_nth_token (parser->lexer, 2);
23114 /* If the next token is a `}', then we're still done. An
23115 initializer-clause can have a trailing `,' after the
23116 initializer-list and before the closing `}'. */
23117 if (token->type == CPP_CLOSE_BRACE)
23118 break;
23119
23120 /* Consume the `,' token. */
23121 cp_lexer_consume_token (parser->lexer);
23122 }
23123
23124 /* The same identifier shall not appear in multiple designators
23125 of a designated-initializer-list. */
23126 if (first_designator)
23127 {
23128 unsigned int i;
23129 tree designator, val;
23130 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23131 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23132 {
23133 if (IDENTIFIER_MARKED (designator))
23134 {
23135 error_at (cp_expr_loc_or_loc (val, input_location),
23136 "%<.%s%> designator used multiple times in "
23137 "the same initializer list",
23138 IDENTIFIER_POINTER (designator));
23139 (*v)[i].index = error_mark_node;
23140 }
23141 else
23142 IDENTIFIER_MARKED (designator) = 1;
23143 }
23144 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23145 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23146 IDENTIFIER_MARKED (designator) = 0;
23147 }
23148
23149 *designated = first_designator != NULL_TREE;
23150 return v;
23151 }
23152
23153 /* Classes [gram.class] */
23154
23155 /* Parse a class-name.
23156
23157 class-name:
23158 identifier
23159 template-id
23160
23161 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23162 to indicate that names looked up in dependent types should be
23163 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23164 keyword has been used to indicate that the name that appears next
23165 is a template. TAG_TYPE indicates the explicit tag given before
23166 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23167 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23168 is the class being defined in a class-head. If ENUM_OK is TRUE,
23169 enum-names are also accepted.
23170
23171 Returns the TYPE_DECL representing the class. */
23172
23173 static tree
23174 cp_parser_class_name (cp_parser *parser,
23175 bool typename_keyword_p,
23176 bool template_keyword_p,
23177 enum tag_types tag_type,
23178 bool check_dependency_p,
23179 bool class_head_p,
23180 bool is_declaration,
23181 bool enum_ok)
23182 {
23183 tree decl;
23184 tree scope;
23185 bool typename_p;
23186 cp_token *token;
23187 tree identifier = NULL_TREE;
23188
23189 /* All class-names start with an identifier. */
23190 token = cp_lexer_peek_token (parser->lexer);
23191 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
23192 {
23193 cp_parser_error (parser, "expected class-name");
23194 return error_mark_node;
23195 }
23196
23197 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23198 to a template-id, so we save it here. */
23199 scope = parser->scope;
23200 if (scope == error_mark_node)
23201 return error_mark_node;
23202
23203 /* Any name names a type if we're following the `typename' keyword
23204 in a qualified name where the enclosing scope is type-dependent. */
23205 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
23206 && dependent_type_p (scope));
23207 /* Handle the common case (an identifier, but not a template-id)
23208 efficiently. */
23209 if (token->type == CPP_NAME
23210 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
23211 {
23212 cp_token *identifier_token;
23213 bool ambiguous_p;
23214
23215 /* Look for the identifier. */
23216 identifier_token = cp_lexer_peek_token (parser->lexer);
23217 ambiguous_p = identifier_token->error_reported;
23218 identifier = cp_parser_identifier (parser);
23219 /* If the next token isn't an identifier, we are certainly not
23220 looking at a class-name. */
23221 if (identifier == error_mark_node)
23222 decl = error_mark_node;
23223 /* If we know this is a type-name, there's no need to look it
23224 up. */
23225 else if (typename_p)
23226 decl = identifier;
23227 else
23228 {
23229 tree ambiguous_decls;
23230 /* If we already know that this lookup is ambiguous, then
23231 we've already issued an error message; there's no reason
23232 to check again. */
23233 if (ambiguous_p)
23234 {
23235 cp_parser_simulate_error (parser);
23236 return error_mark_node;
23237 }
23238 /* If the next token is a `::', then the name must be a type
23239 name.
23240
23241 [basic.lookup.qual]
23242
23243 During the lookup for a name preceding the :: scope
23244 resolution operator, object, function, and enumerator
23245 names are ignored. */
23246 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23247 tag_type = scope_type;
23248 /* Look up the name. */
23249 decl = cp_parser_lookup_name (parser, identifier,
23250 tag_type,
23251 /*is_template=*/false,
23252 /*is_namespace=*/false,
23253 check_dependency_p,
23254 &ambiguous_decls,
23255 identifier_token->location);
23256 if (ambiguous_decls)
23257 {
23258 if (cp_parser_parsing_tentatively (parser))
23259 cp_parser_simulate_error (parser);
23260 return error_mark_node;
23261 }
23262 }
23263 }
23264 else
23265 {
23266 /* Try a template-id. */
23267 decl = cp_parser_template_id (parser, template_keyword_p,
23268 check_dependency_p,
23269 tag_type,
23270 is_declaration);
23271 if (decl == error_mark_node)
23272 return error_mark_node;
23273 }
23274
23275 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
23276
23277 /* If this is a typename, create a TYPENAME_TYPE. */
23278 if (typename_p
23279 && decl != error_mark_node
23280 && !is_overloaded_fn (decl))
23281 {
23282 decl = make_typename_type (scope, decl, typename_type,
23283 /*complain=*/tf_error);
23284 if (decl != error_mark_node)
23285 decl = TYPE_NAME (decl);
23286 }
23287
23288 decl = strip_using_decl (decl);
23289
23290 /* Check to see that it is really the name of a class. */
23291 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
23292 && identifier_p (TREE_OPERAND (decl, 0))
23293 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23294 /* Situations like this:
23295
23296 template <typename T> struct A {
23297 typename T::template X<int>::I i;
23298 };
23299
23300 are problematic. Is `T::template X<int>' a class-name? The
23301 standard does not seem to be definitive, but there is no other
23302 valid interpretation of the following `::'. Therefore, those
23303 names are considered class-names. */
23304 {
23305 decl = make_typename_type (scope, decl, tag_type, tf_error);
23306 if (decl != error_mark_node)
23307 decl = TYPE_NAME (decl);
23308 }
23309 else if (TREE_CODE (decl) != TYPE_DECL
23310 || TREE_TYPE (decl) == error_mark_node
23311 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
23312 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
23313 /* In Objective-C 2.0, a classname followed by '.' starts a
23314 dot-syntax expression, and it's not a type-name. */
23315 || (c_dialect_objc ()
23316 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
23317 && objc_is_class_name (decl)))
23318 decl = error_mark_node;
23319
23320 if (decl == error_mark_node)
23321 cp_parser_error (parser, "expected class-name");
23322 else if (identifier && !parser->scope)
23323 maybe_note_name_used_in_class (identifier, decl);
23324
23325 return decl;
23326 }
23327
23328 /* Make sure that any member-function parameters are in scope.
23329 For instance, a function's noexcept-specifier can use the function's
23330 parameters:
23331
23332 struct S {
23333 void fn (int p) noexcept(noexcept(p));
23334 };
23335
23336 so we need to make sure name lookup can find them. This is used
23337 when we delay parsing of the noexcept-specifier. */
23338
23339 static void
23340 inject_parm_decls (tree decl)
23341 {
23342 begin_scope (sk_function_parms, decl);
23343 tree args = DECL_ARGUMENTS (decl);
23344
23345 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
23346 }
23347
23348 /* Undo the effects of inject_parm_decls. */
23349
23350 static void
23351 pop_injected_parms (void)
23352 {
23353 pop_bindings_and_leave_scope ();
23354 }
23355
23356 /* Parse a class-specifier.
23357
23358 class-specifier:
23359 class-head { member-specification [opt] }
23360
23361 Returns the TREE_TYPE representing the class. */
23362
23363 static tree
23364 cp_parser_class_specifier_1 (cp_parser* parser)
23365 {
23366 tree type;
23367 tree attributes = NULL_TREE;
23368 bool nested_name_specifier_p;
23369 unsigned saved_num_template_parameter_lists;
23370 bool saved_in_function_body;
23371 unsigned char in_statement;
23372 bool in_switch_statement_p;
23373 bool saved_in_unbraced_linkage_specification_p;
23374 tree old_scope = NULL_TREE;
23375 tree scope = NULL_TREE;
23376 cp_token *closing_brace;
23377
23378 push_deferring_access_checks (dk_no_deferred);
23379
23380 /* Parse the class-head. */
23381 type = cp_parser_class_head (parser,
23382 &nested_name_specifier_p);
23383 /* If the class-head was a semantic disaster, skip the entire body
23384 of the class. */
23385 if (!type)
23386 {
23387 cp_parser_skip_to_end_of_block_or_statement (parser);
23388 pop_deferring_access_checks ();
23389 return error_mark_node;
23390 }
23391
23392 /* Look for the `{'. */
23393 matching_braces braces;
23394 if (!braces.require_open (parser))
23395 {
23396 pop_deferring_access_checks ();
23397 return error_mark_node;
23398 }
23399
23400 cp_ensure_no_omp_declare_simd (parser);
23401 cp_ensure_no_oacc_routine (parser);
23402
23403 /* Issue an error message if type-definitions are forbidden here. */
23404 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
23405 /* Remember that we are defining one more class. */
23406 ++parser->num_classes_being_defined;
23407 /* Inside the class, surrounding template-parameter-lists do not
23408 apply. */
23409 saved_num_template_parameter_lists
23410 = parser->num_template_parameter_lists;
23411 parser->num_template_parameter_lists = 0;
23412 /* We are not in a function body. */
23413 saved_in_function_body = parser->in_function_body;
23414 parser->in_function_body = false;
23415 /* Or in a loop. */
23416 in_statement = parser->in_statement;
23417 parser->in_statement = 0;
23418 /* Or in a switch. */
23419 in_switch_statement_p = parser->in_switch_statement_p;
23420 parser->in_switch_statement_p = false;
23421 /* We are not immediately inside an extern "lang" block. */
23422 saved_in_unbraced_linkage_specification_p
23423 = parser->in_unbraced_linkage_specification_p;
23424 parser->in_unbraced_linkage_specification_p = false;
23425
23426 // Associate constraints with the type.
23427 if (flag_concepts)
23428 type = associate_classtype_constraints (type);
23429
23430 /* Start the class. */
23431 if (nested_name_specifier_p)
23432 {
23433 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
23434 old_scope = push_inner_scope (scope);
23435 }
23436 type = begin_class_definition (type);
23437
23438 if (type == error_mark_node)
23439 /* If the type is erroneous, skip the entire body of the class. */
23440 cp_parser_skip_to_closing_brace (parser);
23441 else
23442 /* Parse the member-specification. */
23443 cp_parser_member_specification_opt (parser);
23444
23445 /* Look for the trailing `}'. */
23446 closing_brace = braces.require_close (parser);
23447 /* Look for trailing attributes to apply to this class. */
23448 if (cp_parser_allow_gnu_extensions_p (parser))
23449 attributes = cp_parser_gnu_attributes_opt (parser);
23450 if (type != error_mark_node)
23451 type = finish_struct (type, attributes);
23452 if (nested_name_specifier_p)
23453 pop_inner_scope (old_scope, scope);
23454
23455 /* We've finished a type definition. Check for the common syntax
23456 error of forgetting a semicolon after the definition. We need to
23457 be careful, as we can't just check for not-a-semicolon and be done
23458 with it; the user might have typed:
23459
23460 class X { } c = ...;
23461 class X { } *p = ...;
23462
23463 and so forth. Instead, enumerate all the possible tokens that
23464 might follow this production; if we don't see one of them, then
23465 complain and silently insert the semicolon. */
23466 {
23467 cp_token *token = cp_lexer_peek_token (parser->lexer);
23468 bool want_semicolon = true;
23469
23470 if (cp_next_tokens_can_be_std_attribute_p (parser))
23471 /* Don't try to parse c++11 attributes here. As per the
23472 grammar, that should be a task for
23473 cp_parser_decl_specifier_seq. */
23474 want_semicolon = false;
23475
23476 switch (token->type)
23477 {
23478 case CPP_NAME:
23479 case CPP_SEMICOLON:
23480 case CPP_MULT:
23481 case CPP_AND:
23482 case CPP_OPEN_PAREN:
23483 case CPP_CLOSE_PAREN:
23484 case CPP_COMMA:
23485 want_semicolon = false;
23486 break;
23487
23488 /* While it's legal for type qualifiers and storage class
23489 specifiers to follow type definitions in the grammar, only
23490 compiler testsuites contain code like that. Assume that if
23491 we see such code, then what we're really seeing is a case
23492 like:
23493
23494 class X { }
23495 const <type> var = ...;
23496
23497 or
23498
23499 class Y { }
23500 static <type> func (...) ...
23501
23502 i.e. the qualifier or specifier applies to the next
23503 declaration. To do so, however, we need to look ahead one
23504 more token to see if *that* token is a type specifier.
23505
23506 This code could be improved to handle:
23507
23508 class Z { }
23509 static const <type> var = ...; */
23510 case CPP_KEYWORD:
23511 if (keyword_is_decl_specifier (token->keyword))
23512 {
23513 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
23514
23515 /* Handling user-defined types here would be nice, but very
23516 tricky. */
23517 want_semicolon
23518 = (lookahead->type == CPP_KEYWORD
23519 && keyword_begins_type_specifier (lookahead->keyword));
23520 }
23521 break;
23522 default:
23523 break;
23524 }
23525
23526 /* If we don't have a type, then something is very wrong and we
23527 shouldn't try to do anything clever. Likewise for not seeing the
23528 closing brace. */
23529 if (closing_brace && TYPE_P (type) && want_semicolon)
23530 {
23531 /* Locate the closing brace. */
23532 cp_token_position prev
23533 = cp_lexer_previous_token_position (parser->lexer);
23534 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
23535 location_t loc = prev_token->location;
23536
23537 /* We want to suggest insertion of a ';' immediately *after* the
23538 closing brace, so, if we can, offset the location by 1 column. */
23539 location_t next_loc = loc;
23540 if (!linemap_location_from_macro_expansion_p (line_table, loc))
23541 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
23542
23543 rich_location richloc (line_table, next_loc);
23544
23545 /* If we successfully offset the location, suggest the fix-it. */
23546 if (next_loc != loc)
23547 richloc.add_fixit_insert_before (next_loc, ";");
23548
23549 if (CLASSTYPE_DECLARED_CLASS (type))
23550 error_at (&richloc,
23551 "expected %<;%> after class definition");
23552 else if (TREE_CODE (type) == RECORD_TYPE)
23553 error_at (&richloc,
23554 "expected %<;%> after struct definition");
23555 else if (TREE_CODE (type) == UNION_TYPE)
23556 error_at (&richloc,
23557 "expected %<;%> after union definition");
23558 else
23559 gcc_unreachable ();
23560
23561 /* Unget one token and smash it to look as though we encountered
23562 a semicolon in the input stream. */
23563 cp_lexer_set_token_position (parser->lexer, prev);
23564 token = cp_lexer_peek_token (parser->lexer);
23565 token->type = CPP_SEMICOLON;
23566 token->keyword = RID_MAX;
23567 }
23568 }
23569
23570 /* If this class is not itself within the scope of another class,
23571 then we need to parse the bodies of all of the queued function
23572 definitions. Note that the queued functions defined in a class
23573 are not always processed immediately following the
23574 class-specifier for that class. Consider:
23575
23576 struct A {
23577 struct B { void f() { sizeof (A); } };
23578 };
23579
23580 If `f' were processed before the processing of `A' were
23581 completed, there would be no way to compute the size of `A'.
23582 Note that the nesting we are interested in here is lexical --
23583 not the semantic nesting given by TYPE_CONTEXT. In particular,
23584 for:
23585
23586 struct A { struct B; };
23587 struct A::B { void f() { } };
23588
23589 there is no need to delay the parsing of `A::B::f'. */
23590 if (--parser->num_classes_being_defined == 0)
23591 {
23592 tree decl;
23593 tree class_type = NULL_TREE;
23594 tree pushed_scope = NULL_TREE;
23595 unsigned ix;
23596 cp_default_arg_entry *e;
23597 tree save_ccp, save_ccr;
23598
23599 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
23600 {
23601 /* Skip default arguments, NSDMIs, etc, in order to improve
23602 error recovery (c++/71169, c++/71832). */
23603 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23604 vec_safe_truncate (unparsed_nsdmis, 0);
23605 vec_safe_truncate (unparsed_classes, 0);
23606 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23607 }
23608
23609 /* In a first pass, parse default arguments to the functions.
23610 Then, in a second pass, parse the bodies of the functions.
23611 This two-phased approach handles cases like:
23612
23613 struct S {
23614 void f() { g(); }
23615 void g(int i = 3);
23616 };
23617
23618 */
23619 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
23620 {
23621 decl = e->decl;
23622 /* If there are default arguments that have not yet been processed,
23623 take care of them now. */
23624 if (class_type != e->class_type)
23625 {
23626 if (pushed_scope)
23627 pop_scope (pushed_scope);
23628 class_type = e->class_type;
23629 pushed_scope = push_scope (class_type);
23630 }
23631 /* Make sure that any template parameters are in scope. */
23632 maybe_begin_member_template_processing (decl);
23633 /* Parse the default argument expressions. */
23634 cp_parser_late_parsing_default_args (parser, decl);
23635 /* Remove any template parameters from the symbol table. */
23636 maybe_end_member_template_processing ();
23637 }
23638 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23639 /* Now parse any NSDMIs. */
23640 save_ccp = current_class_ptr;
23641 save_ccr = current_class_ref;
23642 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23643 {
23644 if (class_type != DECL_CONTEXT (decl))
23645 {
23646 if (pushed_scope)
23647 pop_scope (pushed_scope);
23648 class_type = DECL_CONTEXT (decl);
23649 pushed_scope = push_scope (class_type);
23650 }
23651 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23652 cp_parser_late_parsing_nsdmi (parser, decl);
23653 }
23654 vec_safe_truncate (unparsed_nsdmis, 0);
23655 current_class_ptr = save_ccp;
23656 current_class_ref = save_ccr;
23657 if (pushed_scope)
23658 pop_scope (pushed_scope);
23659
23660 /* Now do some post-NSDMI bookkeeping. */
23661 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23662 after_nsdmi_defaulted_late_checks (class_type);
23663 vec_safe_truncate (unparsed_classes, 0);
23664 after_nsdmi_defaulted_late_checks (type);
23665
23666 /* If there are noexcept-specifiers that have not yet been processed,
23667 take care of them now. */
23668 class_type = NULL_TREE;
23669 pushed_scope = NULL_TREE;
23670 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
23671 {
23672 tree ctx = DECL_CONTEXT (decl);
23673 if (class_type != ctx)
23674 {
23675 if (pushed_scope)
23676 pop_scope (pushed_scope);
23677 class_type = ctx;
23678 pushed_scope = push_scope (class_type);
23679 }
23680
23681 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
23682 spec = TREE_PURPOSE (spec);
23683
23684 /* Make sure that any template parameters are in scope. */
23685 maybe_begin_member_template_processing (decl);
23686
23687 /* Make sure that any member-function parameters are in scope. */
23688 inject_parm_decls (decl);
23689
23690 /* 'this' is not allowed in static member functions. */
23691 unsigned char local_variables_forbidden_p
23692 = parser->local_variables_forbidden_p;
23693 if (DECL_THIS_STATIC (decl))
23694 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
23695
23696 /* Now we can parse the noexcept-specifier. */
23697 spec = cp_parser_late_noexcept_specifier (parser, spec);
23698
23699 if (spec != error_mark_node)
23700 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
23701
23702 /* Restore the state of local_variables_forbidden_p. */
23703 parser->local_variables_forbidden_p = local_variables_forbidden_p;
23704
23705 /* The finish_struct call above performed various override checking,
23706 but it skipped unparsed noexcept-specifier operands. Now that we
23707 have resolved them, check again. */
23708 noexcept_override_late_checks (type, decl);
23709
23710 /* Remove any member-function parameters from the symbol table. */
23711 pop_injected_parms ();
23712
23713 /* Remove any template parameters from the symbol table. */
23714 maybe_end_member_template_processing ();
23715 }
23716 vec_safe_truncate (unparsed_noexcepts, 0);
23717 if (pushed_scope)
23718 pop_scope (pushed_scope);
23719
23720 /* Now parse the body of the functions. */
23721 if (flag_openmp)
23722 {
23723 /* OpenMP UDRs need to be parsed before all other functions. */
23724 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23725 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23726 cp_parser_late_parsing_for_member (parser, decl);
23727 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23728 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23729 cp_parser_late_parsing_for_member (parser, decl);
23730 }
23731 else
23732 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23733 cp_parser_late_parsing_for_member (parser, decl);
23734 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23735 }
23736 else
23737 vec_safe_push (unparsed_classes, type);
23738
23739 /* Put back any saved access checks. */
23740 pop_deferring_access_checks ();
23741
23742 /* Restore saved state. */
23743 parser->in_switch_statement_p = in_switch_statement_p;
23744 parser->in_statement = in_statement;
23745 parser->in_function_body = saved_in_function_body;
23746 parser->num_template_parameter_lists
23747 = saved_num_template_parameter_lists;
23748 parser->in_unbraced_linkage_specification_p
23749 = saved_in_unbraced_linkage_specification_p;
23750
23751 return type;
23752 }
23753
23754 static tree
23755 cp_parser_class_specifier (cp_parser* parser)
23756 {
23757 tree ret;
23758 timevar_push (TV_PARSE_STRUCT);
23759 ret = cp_parser_class_specifier_1 (parser);
23760 timevar_pop (TV_PARSE_STRUCT);
23761 return ret;
23762 }
23763
23764 /* Parse a class-head.
23765
23766 class-head:
23767 class-key identifier [opt] base-clause [opt]
23768 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23769 class-key nested-name-specifier [opt] template-id
23770 base-clause [opt]
23771
23772 class-virt-specifier:
23773 final
23774
23775 GNU Extensions:
23776 class-key attributes identifier [opt] base-clause [opt]
23777 class-key attributes nested-name-specifier identifier base-clause [opt]
23778 class-key attributes nested-name-specifier [opt] template-id
23779 base-clause [opt]
23780
23781 Upon return BASES is initialized to the list of base classes (or
23782 NULL, if there are none) in the same form returned by
23783 cp_parser_base_clause.
23784
23785 Returns the TYPE of the indicated class. Sets
23786 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23787 involving a nested-name-specifier was used, and FALSE otherwise.
23788
23789 Returns error_mark_node if this is not a class-head.
23790
23791 Returns NULL_TREE if the class-head is syntactically valid, but
23792 semantically invalid in a way that means we should skip the entire
23793 body of the class. */
23794
23795 static tree
23796 cp_parser_class_head (cp_parser* parser,
23797 bool* nested_name_specifier_p)
23798 {
23799 tree nested_name_specifier;
23800 enum tag_types class_key;
23801 tree id = NULL_TREE;
23802 tree type = NULL_TREE;
23803 tree attributes;
23804 tree bases;
23805 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23806 bool template_id_p = false;
23807 bool qualified_p = false;
23808 bool invalid_nested_name_p = false;
23809 bool invalid_explicit_specialization_p = false;
23810 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23811 tree pushed_scope = NULL_TREE;
23812 unsigned num_templates;
23813 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23814 /* Assume no nested-name-specifier will be present. */
23815 *nested_name_specifier_p = false;
23816 /* Assume no template parameter lists will be used in defining the
23817 type. */
23818 num_templates = 0;
23819 parser->colon_corrects_to_scope_p = false;
23820
23821 /* Look for the class-key. */
23822 class_key = cp_parser_class_key (parser);
23823 if (class_key == none_type)
23824 return error_mark_node;
23825
23826 location_t class_head_start_location = input_location;
23827
23828 /* Parse the attributes. */
23829 attributes = cp_parser_attributes_opt (parser);
23830
23831 /* If the next token is `::', that is invalid -- but sometimes
23832 people do try to write:
23833
23834 struct ::S {};
23835
23836 Handle this gracefully by accepting the extra qualifier, and then
23837 issuing an error about it later if this really is a
23838 class-head. If it turns out just to be an elaborated type
23839 specifier, remain silent. */
23840 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23841 qualified_p = true;
23842
23843 push_deferring_access_checks (dk_no_check);
23844
23845 /* Determine the name of the class. Begin by looking for an
23846 optional nested-name-specifier. */
23847 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23848 nested_name_specifier
23849 = cp_parser_nested_name_specifier_opt (parser,
23850 /*typename_keyword_p=*/false,
23851 /*check_dependency_p=*/false,
23852 /*type_p=*/true,
23853 /*is_declaration=*/false);
23854 /* If there was a nested-name-specifier, then there *must* be an
23855 identifier. */
23856
23857 cp_token *bad_template_keyword = NULL;
23858
23859 if (nested_name_specifier)
23860 {
23861 type_start_token = cp_lexer_peek_token (parser->lexer);
23862 /* Although the grammar says `identifier', it really means
23863 `class-name' or `template-name'. You are only allowed to
23864 define a class that has already been declared with this
23865 syntax.
23866
23867 The proposed resolution for Core Issue 180 says that wherever
23868 you see `class T::X' you should treat `X' as a type-name.
23869
23870 It is OK to define an inaccessible class; for example:
23871
23872 class A { class B; };
23873 class A::B {};
23874
23875 We do not know if we will see a class-name, or a
23876 template-name. We look for a class-name first, in case the
23877 class-name is a template-id; if we looked for the
23878 template-name first we would stop after the template-name. */
23879 cp_parser_parse_tentatively (parser);
23880 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23881 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23882 type = cp_parser_class_name (parser,
23883 /*typename_keyword_p=*/false,
23884 /*template_keyword_p=*/false,
23885 class_type,
23886 /*check_dependency_p=*/false,
23887 /*class_head_p=*/true,
23888 /*is_declaration=*/false);
23889 /* If that didn't work, ignore the nested-name-specifier. */
23890 if (!cp_parser_parse_definitely (parser))
23891 {
23892 invalid_nested_name_p = true;
23893 type_start_token = cp_lexer_peek_token (parser->lexer);
23894 id = cp_parser_identifier (parser);
23895 if (id == error_mark_node)
23896 id = NULL_TREE;
23897 }
23898 /* If we could not find a corresponding TYPE, treat this
23899 declaration like an unqualified declaration. */
23900 if (type == error_mark_node)
23901 nested_name_specifier = NULL_TREE;
23902 /* Otherwise, count the number of templates used in TYPE and its
23903 containing scopes. */
23904 else
23905 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23906 }
23907 /* Otherwise, the identifier is optional. */
23908 else
23909 {
23910 /* We don't know whether what comes next is a template-id,
23911 an identifier, or nothing at all. */
23912 cp_parser_parse_tentatively (parser);
23913 /* Check for a template-id. */
23914 type_start_token = cp_lexer_peek_token (parser->lexer);
23915 id = cp_parser_template_id (parser,
23916 /*template_keyword_p=*/false,
23917 /*check_dependency_p=*/true,
23918 class_key,
23919 /*is_declaration=*/true);
23920 /* If that didn't work, it could still be an identifier. */
23921 if (!cp_parser_parse_definitely (parser))
23922 {
23923 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23924 {
23925 type_start_token = cp_lexer_peek_token (parser->lexer);
23926 id = cp_parser_identifier (parser);
23927 }
23928 else
23929 id = NULL_TREE;
23930 }
23931 else
23932 {
23933 template_id_p = true;
23934 ++num_templates;
23935 }
23936 }
23937
23938 pop_deferring_access_checks ();
23939
23940 if (id)
23941 {
23942 cp_parser_check_for_invalid_template_id (parser, id,
23943 class_key,
23944 type_start_token->location);
23945 }
23946 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23947
23948 /* If it's not a `:' or a `{' then we can't really be looking at a
23949 class-head, since a class-head only appears as part of a
23950 class-specifier. We have to detect this situation before calling
23951 xref_tag, since that has irreversible side-effects. */
23952 if (!cp_parser_next_token_starts_class_definition_p (parser))
23953 {
23954 cp_parser_error (parser, "expected %<{%> or %<:%>");
23955 type = error_mark_node;
23956 goto out;
23957 }
23958
23959 /* At this point, we're going ahead with the class-specifier, even
23960 if some other problem occurs. */
23961 cp_parser_commit_to_tentative_parse (parser);
23962 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23963 {
23964 cp_parser_error (parser,
23965 "cannot specify %<override%> for a class");
23966 type = error_mark_node;
23967 goto out;
23968 }
23969 /* Issue the error about the overly-qualified name now. */
23970 if (qualified_p)
23971 {
23972 cp_parser_error (parser,
23973 "global qualification of class name is invalid");
23974 type = error_mark_node;
23975 goto out;
23976 }
23977 else if (invalid_nested_name_p)
23978 {
23979 cp_parser_error (parser,
23980 "qualified name does not name a class");
23981 type = error_mark_node;
23982 goto out;
23983 }
23984 else if (nested_name_specifier)
23985 {
23986 tree scope;
23987
23988 if (bad_template_keyword)
23989 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23990 keyword template shall not appear at the top level. */
23991 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23992 "keyword %<template%> not allowed in class-head-name");
23993
23994 /* Reject typedef-names in class heads. */
23995 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23996 {
23997 error_at (type_start_token->location,
23998 "invalid class name in declaration of %qD",
23999 type);
24000 type = NULL_TREE;
24001 goto done;
24002 }
24003
24004 /* Figure out in what scope the declaration is being placed. */
24005 scope = current_scope ();
24006 /* If that scope does not contain the scope in which the
24007 class was originally declared, the program is invalid. */
24008 if (scope && !is_ancestor (scope, nested_name_specifier))
24009 {
24010 if (at_namespace_scope_p ())
24011 error_at (type_start_token->location,
24012 "declaration of %qD in namespace %qD which does not "
24013 "enclose %qD",
24014 type, scope, nested_name_specifier);
24015 else
24016 error_at (type_start_token->location,
24017 "declaration of %qD in %qD which does not enclose %qD",
24018 type, scope, nested_name_specifier);
24019 type = NULL_TREE;
24020 goto done;
24021 }
24022 /* [dcl.meaning]
24023
24024 A declarator-id shall not be qualified except for the
24025 definition of a ... nested class outside of its class
24026 ... [or] the definition or explicit instantiation of a
24027 class member of a namespace outside of its namespace. */
24028 if (scope == nested_name_specifier)
24029 {
24030 permerror (nested_name_specifier_token_start->location,
24031 "extra qualification not allowed");
24032 nested_name_specifier = NULL_TREE;
24033 num_templates = 0;
24034 }
24035 }
24036 /* An explicit-specialization must be preceded by "template <>". If
24037 it is not, try to recover gracefully. */
24038 if (at_namespace_scope_p ()
24039 && parser->num_template_parameter_lists == 0
24040 && !processing_template_parmlist
24041 && template_id_p)
24042 {
24043 /* Build a location of this form:
24044 struct typename <ARGS>
24045 ^~~~~~~~~~~~~~~~~~~~~~
24046 with caret==start at the start token, and
24047 finishing at the end of the type. */
24048 location_t reported_loc
24049 = make_location (class_head_start_location,
24050 class_head_start_location,
24051 get_finish (type_start_token->location));
24052 rich_location richloc (line_table, reported_loc);
24053 richloc.add_fixit_insert_before (class_head_start_location,
24054 "template <> ");
24055 error_at (&richloc,
24056 "an explicit specialization must be preceded by"
24057 " %<template <>%>");
24058 invalid_explicit_specialization_p = true;
24059 /* Take the same action that would have been taken by
24060 cp_parser_explicit_specialization. */
24061 ++parser->num_template_parameter_lists;
24062 begin_specialization ();
24063 }
24064 /* There must be no "return" statements between this point and the
24065 end of this function; set "type "to the correct return value and
24066 use "goto done;" to return. */
24067 /* Make sure that the right number of template parameters were
24068 present. */
24069 if (!cp_parser_check_template_parameters (parser, num_templates,
24070 template_id_p,
24071 type_start_token->location,
24072 /*declarator=*/NULL))
24073 {
24074 /* If something went wrong, there is no point in even trying to
24075 process the class-definition. */
24076 type = NULL_TREE;
24077 goto done;
24078 }
24079
24080 /* Look up the type. */
24081 if (template_id_p)
24082 {
24083 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
24084 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
24085 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
24086 {
24087 error_at (type_start_token->location,
24088 "function template %qD redeclared as a class template", id);
24089 type = error_mark_node;
24090 }
24091 else
24092 {
24093 type = TREE_TYPE (id);
24094 type = maybe_process_partial_specialization (type);
24095
24096 /* Check the scope while we still know whether or not we had a
24097 nested-name-specifier. */
24098 if (type != error_mark_node)
24099 check_unqualified_spec_or_inst (type, type_start_token->location);
24100 }
24101 if (nested_name_specifier)
24102 pushed_scope = push_scope (nested_name_specifier);
24103 }
24104 else if (nested_name_specifier)
24105 {
24106 tree class_type;
24107
24108 /* Given:
24109
24110 template <typename T> struct S { struct T };
24111 template <typename T> struct S<T>::T { };
24112
24113 we will get a TYPENAME_TYPE when processing the definition of
24114 `S::T'. We need to resolve it to the actual type before we
24115 try to define it. */
24116 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
24117 {
24118 class_type = resolve_typename_type (TREE_TYPE (type),
24119 /*only_current_p=*/false);
24120 if (TREE_CODE (class_type) != TYPENAME_TYPE)
24121 type = TYPE_NAME (class_type);
24122 else
24123 {
24124 cp_parser_error (parser, "could not resolve typename type");
24125 type = error_mark_node;
24126 }
24127 }
24128
24129 if (maybe_process_partial_specialization (TREE_TYPE (type))
24130 == error_mark_node)
24131 {
24132 type = NULL_TREE;
24133 goto done;
24134 }
24135
24136 class_type = current_class_type;
24137 /* Enter the scope indicated by the nested-name-specifier. */
24138 pushed_scope = push_scope (nested_name_specifier);
24139 /* Get the canonical version of this type. */
24140 type = TYPE_MAIN_DECL (TREE_TYPE (type));
24141 /* Call push_template_decl if it seems like we should be defining a
24142 template either from the template headers or the type we're
24143 defining, so that we diagnose both extra and missing headers. */
24144 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
24145 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
24146 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
24147 {
24148 type = push_template_decl (type);
24149 if (type == error_mark_node)
24150 {
24151 type = NULL_TREE;
24152 goto done;
24153 }
24154 }
24155
24156 type = TREE_TYPE (type);
24157 *nested_name_specifier_p = true;
24158 }
24159 else /* The name is not a nested name. */
24160 {
24161 /* If the class was unnamed, create a dummy name. */
24162 if (!id)
24163 id = make_anon_name ();
24164 tag_scope tag_scope = (parser->in_type_id_in_expr_p
24165 ? ts_within_enclosing_non_class
24166 : ts_current);
24167 type = xref_tag (class_key, id, tag_scope,
24168 parser->num_template_parameter_lists);
24169 }
24170
24171 /* Indicate whether this class was declared as a `class' or as a
24172 `struct'. */
24173 if (TREE_CODE (type) == RECORD_TYPE)
24174 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
24175 cp_parser_check_class_key (class_key, type);
24176
24177 /* If this type was already complete, and we see another definition,
24178 that's an error. Likewise if the type is already being defined:
24179 this can happen, eg, when it's defined from within an expression
24180 (c++/84605). */
24181 if (type != error_mark_node
24182 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
24183 {
24184 error_at (type_start_token->location, "redefinition of %q#T",
24185 type);
24186 inform (location_of (type), "previous definition of %q#T",
24187 type);
24188 type = NULL_TREE;
24189 goto done;
24190 }
24191 else if (type == error_mark_node)
24192 type = NULL_TREE;
24193
24194 if (type)
24195 {
24196 /* Apply attributes now, before any use of the class as a template
24197 argument in its base list. */
24198 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
24199 fixup_attribute_variants (type);
24200 }
24201
24202 /* We will have entered the scope containing the class; the names of
24203 base classes should be looked up in that context. For example:
24204
24205 struct A { struct B {}; struct C; };
24206 struct A::C : B {};
24207
24208 is valid. */
24209
24210 /* Get the list of base-classes, if there is one. */
24211 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24212 {
24213 /* PR59482: enter the class scope so that base-specifiers are looked
24214 up correctly. */
24215 if (type)
24216 pushclass (type);
24217 bases = cp_parser_base_clause (parser);
24218 /* PR59482: get out of the previously pushed class scope so that the
24219 subsequent pops pop the right thing. */
24220 if (type)
24221 popclass ();
24222 }
24223 else
24224 bases = NULL_TREE;
24225
24226 /* If we're really defining a class, process the base classes.
24227 If they're invalid, fail. */
24228 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24229 xref_basetypes (type, bases);
24230
24231 done:
24232 /* Leave the scope given by the nested-name-specifier. We will
24233 enter the class scope itself while processing the members. */
24234 if (pushed_scope)
24235 pop_scope (pushed_scope);
24236
24237 if (invalid_explicit_specialization_p)
24238 {
24239 end_specialization ();
24240 --parser->num_template_parameter_lists;
24241 }
24242
24243 if (type)
24244 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
24245 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
24246 CLASSTYPE_FINAL (type) = 1;
24247 out:
24248 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24249 return type;
24250 }
24251
24252 /* Parse a class-key.
24253
24254 class-key:
24255 class
24256 struct
24257 union
24258
24259 Returns the kind of class-key specified, or none_type to indicate
24260 error. */
24261
24262 static enum tag_types
24263 cp_parser_class_key (cp_parser* parser)
24264 {
24265 cp_token *token;
24266 enum tag_types tag_type;
24267
24268 /* Look for the class-key. */
24269 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
24270 if (!token)
24271 return none_type;
24272
24273 /* Check to see if the TOKEN is a class-key. */
24274 tag_type = cp_parser_token_is_class_key (token);
24275 if (!tag_type)
24276 cp_parser_error (parser, "expected class-key");
24277 return tag_type;
24278 }
24279
24280 /* Parse a type-parameter-key.
24281
24282 type-parameter-key:
24283 class
24284 typename
24285 */
24286
24287 static void
24288 cp_parser_type_parameter_key (cp_parser* parser)
24289 {
24290 /* Look for the type-parameter-key. */
24291 enum tag_types tag_type = none_type;
24292 cp_token *token = cp_lexer_peek_token (parser->lexer);
24293 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
24294 {
24295 cp_lexer_consume_token (parser->lexer);
24296 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
24297 /* typename is not allowed in a template template parameter
24298 by the standard until C++17. */
24299 pedwarn (token->location, OPT_Wpedantic,
24300 "ISO C++ forbids typename key in template template parameter;"
24301 " use %<-std=c++17%> or %<-std=gnu++17%>");
24302 }
24303 else
24304 cp_parser_error (parser, "expected %<class%> or %<typename%>");
24305
24306 return;
24307 }
24308
24309 /* Parse an (optional) member-specification.
24310
24311 member-specification:
24312 member-declaration member-specification [opt]
24313 access-specifier : member-specification [opt] */
24314
24315 static void
24316 cp_parser_member_specification_opt (cp_parser* parser)
24317 {
24318 while (true)
24319 {
24320 cp_token *token;
24321 enum rid keyword;
24322
24323 /* Peek at the next token. */
24324 token = cp_lexer_peek_token (parser->lexer);
24325 /* If it's a `}', or EOF then we've seen all the members. */
24326 if (token->type == CPP_CLOSE_BRACE
24327 || token->type == CPP_EOF
24328 || token->type == CPP_PRAGMA_EOL)
24329 break;
24330
24331 /* See if this token is a keyword. */
24332 keyword = token->keyword;
24333 switch (keyword)
24334 {
24335 case RID_PUBLIC:
24336 case RID_PROTECTED:
24337 case RID_PRIVATE:
24338 /* Consume the access-specifier. */
24339 cp_lexer_consume_token (parser->lexer);
24340 /* Remember which access-specifier is active. */
24341 current_access_specifier = token->u.value;
24342 /* Look for the `:'. */
24343 cp_parser_require (parser, CPP_COLON, RT_COLON);
24344 break;
24345
24346 default:
24347 /* Accept #pragmas at class scope. */
24348 if (token->type == CPP_PRAGMA)
24349 {
24350 cp_parser_pragma (parser, pragma_member, NULL);
24351 break;
24352 }
24353
24354 /* Otherwise, the next construction must be a
24355 member-declaration. */
24356 cp_parser_member_declaration (parser);
24357 }
24358 }
24359 }
24360
24361 /* Parse a member-declaration.
24362
24363 member-declaration:
24364 decl-specifier-seq [opt] member-declarator-list [opt] ;
24365 function-definition ; [opt]
24366 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24367 using-declaration
24368 template-declaration
24369 alias-declaration
24370
24371 member-declarator-list:
24372 member-declarator
24373 member-declarator-list , member-declarator
24374
24375 member-declarator:
24376 declarator pure-specifier [opt]
24377 declarator constant-initializer [opt]
24378 identifier [opt] : constant-expression
24379
24380 GNU Extensions:
24381
24382 member-declaration:
24383 __extension__ member-declaration
24384
24385 member-declarator:
24386 declarator attributes [opt] pure-specifier [opt]
24387 declarator attributes [opt] constant-initializer [opt]
24388 identifier [opt] attributes [opt] : constant-expression
24389
24390 C++0x Extensions:
24391
24392 member-declaration:
24393 static_assert-declaration */
24394
24395 static void
24396 cp_parser_member_declaration (cp_parser* parser)
24397 {
24398 cp_decl_specifier_seq decl_specifiers;
24399 tree prefix_attributes;
24400 tree decl;
24401 int declares_class_or_enum;
24402 bool friend_p;
24403 cp_token *token = NULL;
24404 cp_token *decl_spec_token_start = NULL;
24405 cp_token *initializer_token_start = NULL;
24406 int saved_pedantic;
24407 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24408
24409 /* Check for the `__extension__' keyword. */
24410 if (cp_parser_extension_opt (parser, &saved_pedantic))
24411 {
24412 /* Recurse. */
24413 cp_parser_member_declaration (parser);
24414 /* Restore the old value of the PEDANTIC flag. */
24415 pedantic = saved_pedantic;
24416
24417 return;
24418 }
24419
24420 /* Check for a template-declaration. */
24421 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24422 {
24423 /* An explicit specialization here is an error condition, and we
24424 expect the specialization handler to detect and report this. */
24425 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
24426 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
24427 cp_parser_explicit_specialization (parser);
24428 else
24429 cp_parser_template_declaration (parser, /*member_p=*/true);
24430
24431 return;
24432 }
24433 /* Check for a template introduction. */
24434 else if (cp_parser_template_declaration_after_export (parser, true))
24435 return;
24436
24437 /* Check for a using-declaration. */
24438 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24439 {
24440 if (cxx_dialect < cxx11)
24441 {
24442 /* Parse the using-declaration. */
24443 cp_parser_using_declaration (parser,
24444 /*access_declaration_p=*/false);
24445 return;
24446 }
24447 else
24448 {
24449 tree decl;
24450 bool alias_decl_expected;
24451 cp_parser_parse_tentatively (parser);
24452 decl = cp_parser_alias_declaration (parser);
24453 /* Note that if we actually see the '=' token after the
24454 identifier, cp_parser_alias_declaration commits the
24455 tentative parse. In that case, we really expect an
24456 alias-declaration. Otherwise, we expect a using
24457 declaration. */
24458 alias_decl_expected =
24459 !cp_parser_uncommitted_to_tentative_parse_p (parser);
24460 cp_parser_parse_definitely (parser);
24461
24462 if (alias_decl_expected)
24463 finish_member_declaration (decl);
24464 else
24465 cp_parser_using_declaration (parser,
24466 /*access_declaration_p=*/false);
24467 return;
24468 }
24469 }
24470
24471 /* Check for @defs. */
24472 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
24473 {
24474 tree ivar, member;
24475 tree ivar_chains = cp_parser_objc_defs_expression (parser);
24476 ivar = ivar_chains;
24477 while (ivar)
24478 {
24479 member = ivar;
24480 ivar = TREE_CHAIN (member);
24481 TREE_CHAIN (member) = NULL_TREE;
24482 finish_member_declaration (member);
24483 }
24484 return;
24485 }
24486
24487 /* If the next token is `static_assert' we have a static assertion. */
24488 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
24489 {
24490 cp_parser_static_assert (parser, /*member_p=*/true);
24491 return;
24492 }
24493
24494 parser->colon_corrects_to_scope_p = false;
24495
24496 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
24497 goto out;
24498
24499 /* Parse the decl-specifier-seq. */
24500 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24501 cp_parser_decl_specifier_seq (parser,
24502 (CP_PARSER_FLAGS_OPTIONAL
24503 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
24504 &decl_specifiers,
24505 &declares_class_or_enum);
24506 /* Check for an invalid type-name. */
24507 if (!decl_specifiers.any_type_specifiers_p
24508 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24509 goto out;
24510 /* If there is no declarator, then the decl-specifier-seq should
24511 specify a type. */
24512 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24513 {
24514 /* If there was no decl-specifier-seq, and the next token is a
24515 `;', then we have something like:
24516
24517 struct S { ; };
24518
24519 [class.mem]
24520
24521 Each member-declaration shall declare at least one member
24522 name of the class. */
24523 if (!decl_specifiers.any_specifiers_p)
24524 {
24525 cp_token *token = cp_lexer_peek_token (parser->lexer);
24526 if (!in_system_header_at (token->location))
24527 {
24528 gcc_rich_location richloc (token->location);
24529 richloc.add_fixit_remove ();
24530 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
24531 }
24532 }
24533 else
24534 {
24535 tree type;
24536
24537 /* See if this declaration is a friend. */
24538 friend_p = cp_parser_friend_p (&decl_specifiers);
24539 /* If there were decl-specifiers, check to see if there was
24540 a class-declaration. */
24541 type = check_tag_decl (&decl_specifiers,
24542 /*explicit_type_instantiation_p=*/false);
24543 /* Nested classes have already been added to the class, but
24544 a `friend' needs to be explicitly registered. */
24545 if (friend_p)
24546 {
24547 /* If the `friend' keyword was present, the friend must
24548 be introduced with a class-key. */
24549 if (!declares_class_or_enum && cxx_dialect < cxx11)
24550 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
24551 "in C++03 a class-key must be used "
24552 "when declaring a friend");
24553 /* In this case:
24554
24555 template <typename T> struct A {
24556 friend struct A<T>::B;
24557 };
24558
24559 A<T>::B will be represented by a TYPENAME_TYPE, and
24560 therefore not recognized by check_tag_decl. */
24561 if (!type)
24562 {
24563 type = decl_specifiers.type;
24564 if (type && TREE_CODE (type) == TYPE_DECL)
24565 type = TREE_TYPE (type);
24566 }
24567 if (!type || !TYPE_P (type))
24568 error_at (decl_spec_token_start->location,
24569 "friend declaration does not name a class or "
24570 "function");
24571 else
24572 make_friend_class (current_class_type, type,
24573 /*complain=*/true);
24574 }
24575 /* If there is no TYPE, an error message will already have
24576 been issued. */
24577 else if (!type || type == error_mark_node)
24578 ;
24579 /* An anonymous aggregate has to be handled specially; such
24580 a declaration really declares a data member (with a
24581 particular type), as opposed to a nested class. */
24582 else if (ANON_AGGR_TYPE_P (type))
24583 {
24584 /* C++11 9.5/6. */
24585 if (decl_specifiers.storage_class != sc_none)
24586 error_at (decl_spec_token_start->location,
24587 "a storage class on an anonymous aggregate "
24588 "in class scope is not allowed");
24589
24590 /* Remove constructors and such from TYPE, now that we
24591 know it is an anonymous aggregate. */
24592 fixup_anonymous_aggr (type);
24593 /* And make the corresponding data member. */
24594 decl = build_decl (decl_spec_token_start->location,
24595 FIELD_DECL, NULL_TREE, type);
24596 /* Add it to the class. */
24597 finish_member_declaration (decl);
24598 }
24599 else
24600 cp_parser_check_access_in_redeclaration
24601 (TYPE_NAME (type),
24602 decl_spec_token_start->location);
24603 }
24604 }
24605 else
24606 {
24607 bool assume_semicolon = false;
24608
24609 /* Clear attributes from the decl_specifiers but keep them
24610 around as prefix attributes that apply them to the entity
24611 being declared. */
24612 prefix_attributes = decl_specifiers.attributes;
24613 decl_specifiers.attributes = NULL_TREE;
24614
24615 /* See if these declarations will be friends. */
24616 friend_p = cp_parser_friend_p (&decl_specifiers);
24617
24618 /* Keep going until we hit the `;' at the end of the
24619 declaration. */
24620 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24621 {
24622 tree attributes = NULL_TREE;
24623 tree first_attribute;
24624 tree initializer;
24625 bool named_bitfld = false;
24626
24627 /* Peek at the next token. */
24628 token = cp_lexer_peek_token (parser->lexer);
24629
24630 /* The following code wants to know early if it is a bit-field
24631 or some other declaration. Attributes can appear before
24632 the `:' token. Skip over them without consuming any tokens
24633 to peek if they are followed by `:'. */
24634 if (cp_next_tokens_can_be_attribute_p (parser)
24635 || (token->type == CPP_NAME
24636 && cp_nth_tokens_can_be_attribute_p (parser, 2)
24637 && (named_bitfld = true)))
24638 {
24639 size_t n
24640 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
24641 token = cp_lexer_peek_nth_token (parser->lexer, n);
24642 }
24643
24644 /* Check for a bitfield declaration. */
24645 if (token->type == CPP_COLON
24646 || (token->type == CPP_NAME
24647 && token == cp_lexer_peek_token (parser->lexer)
24648 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
24649 && (named_bitfld = true)))
24650 {
24651 tree identifier;
24652 tree width;
24653 tree late_attributes = NULL_TREE;
24654 location_t id_location
24655 = cp_lexer_peek_token (parser->lexer)->location;
24656
24657 if (named_bitfld)
24658 identifier = cp_parser_identifier (parser);
24659 else
24660 identifier = NULL_TREE;
24661
24662 /* Look for attributes that apply to the bitfield. */
24663 attributes = cp_parser_attributes_opt (parser);
24664
24665 /* Consume the `:' token. */
24666 cp_lexer_consume_token (parser->lexer);
24667
24668 /* Get the width of the bitfield. */
24669 width = cp_parser_constant_expression (parser, false, NULL,
24670 cxx_dialect >= cxx11);
24671
24672 /* In C++2A and as extension for C++11 and above we allow
24673 default member initializers for bit-fields. */
24674 initializer = NULL_TREE;
24675 if (cxx_dialect >= cxx11
24676 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
24677 || cp_lexer_next_token_is (parser->lexer,
24678 CPP_OPEN_BRACE)))
24679 {
24680 location_t loc
24681 = cp_lexer_peek_token (parser->lexer)->location;
24682 if (cxx_dialect < cxx2a
24683 && !in_system_header_at (loc)
24684 && identifier != NULL_TREE)
24685 pedwarn (loc, 0,
24686 "default member initializers for bit-fields "
24687 "only available with %<-std=c++2a%> or "
24688 "%<-std=gnu++2a%>");
24689
24690 initializer = cp_parser_save_nsdmi (parser);
24691 if (identifier == NULL_TREE)
24692 {
24693 error_at (loc, "default member initializer for "
24694 "unnamed bit-field");
24695 initializer = NULL_TREE;
24696 }
24697 }
24698 else
24699 {
24700 /* Look for attributes that apply to the bitfield after
24701 the `:' token and width. This is where GCC used to
24702 parse attributes in the past, pedwarn if there is
24703 a std attribute. */
24704 if (cp_next_tokens_can_be_std_attribute_p (parser))
24705 pedwarn (input_location, OPT_Wpedantic,
24706 "ISO C++ allows bit-field attributes only "
24707 "before the %<:%> token");
24708
24709 late_attributes = cp_parser_attributes_opt (parser);
24710 }
24711
24712 attributes = attr_chainon (attributes, late_attributes);
24713
24714 /* Remember which attributes are prefix attributes and
24715 which are not. */
24716 first_attribute = attributes;
24717 /* Combine the attributes. */
24718 attributes = attr_chainon (prefix_attributes, attributes);
24719
24720 /* Create the bitfield declaration. */
24721 decl = grokbitfield (identifier
24722 ? make_id_declarator (NULL_TREE,
24723 identifier,
24724 sfk_none,
24725 id_location)
24726 : NULL,
24727 &decl_specifiers,
24728 width, initializer,
24729 attributes);
24730 }
24731 else
24732 {
24733 cp_declarator *declarator;
24734 tree asm_specification;
24735 int ctor_dtor_or_conv_p;
24736 bool static_p = (decl_specifiers.storage_class == sc_static);
24737
24738 /* Parse the declarator. */
24739 declarator
24740 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24741 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24742 &ctor_dtor_or_conv_p,
24743 /*parenthesized_p=*/NULL,
24744 /*member_p=*/true,
24745 friend_p, static_p);
24746
24747 /* If something went wrong parsing the declarator, make sure
24748 that we at least consume some tokens. */
24749 if (declarator == cp_error_declarator)
24750 {
24751 /* Skip to the end of the statement. */
24752 cp_parser_skip_to_end_of_statement (parser);
24753 /* If the next token is not a semicolon, that is
24754 probably because we just skipped over the body of
24755 a function. So, we consume a semicolon if
24756 present, but do not issue an error message if it
24757 is not present. */
24758 if (cp_lexer_next_token_is (parser->lexer,
24759 CPP_SEMICOLON))
24760 cp_lexer_consume_token (parser->lexer);
24761 goto out;
24762 }
24763
24764 if (declares_class_or_enum & 2)
24765 cp_parser_check_for_definition_in_return_type
24766 (declarator, decl_specifiers.type,
24767 decl_specifiers.locations[ds_type_spec]);
24768
24769 /* Look for an asm-specification. */
24770 asm_specification = cp_parser_asm_specification_opt (parser);
24771 /* Look for attributes that apply to the declaration. */
24772 attributes = cp_parser_attributes_opt (parser);
24773 /* Remember which attributes are prefix attributes and
24774 which are not. */
24775 first_attribute = attributes;
24776 /* Combine the attributes. */
24777 attributes = attr_chainon (prefix_attributes, attributes);
24778
24779 /* If it's an `=', then we have a constant-initializer or a
24780 pure-specifier. It is not correct to parse the
24781 initializer before registering the member declaration
24782 since the member declaration should be in scope while
24783 its initializer is processed. However, the rest of the
24784 front end does not yet provide an interface that allows
24785 us to handle this correctly. */
24786 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24787 {
24788 /* In [class.mem]:
24789
24790 A pure-specifier shall be used only in the declaration of
24791 a virtual function.
24792
24793 A member-declarator can contain a constant-initializer
24794 only if it declares a static member of integral or
24795 enumeration type.
24796
24797 Therefore, if the DECLARATOR is for a function, we look
24798 for a pure-specifier; otherwise, we look for a
24799 constant-initializer. When we call `grokfield', it will
24800 perform more stringent semantics checks. */
24801 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24802 if (function_declarator_p (declarator)
24803 || (decl_specifiers.type
24804 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24805 && declarator->kind == cdk_id
24806 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24807 == FUNCTION_TYPE)))
24808 initializer = cp_parser_pure_specifier (parser);
24809 else if (decl_specifiers.storage_class != sc_static)
24810 initializer = cp_parser_save_nsdmi (parser);
24811 else if (cxx_dialect >= cxx11)
24812 {
24813 bool nonconst;
24814 /* Don't require a constant rvalue in C++11, since we
24815 might want a reference constant. We'll enforce
24816 constancy later. */
24817 cp_lexer_consume_token (parser->lexer);
24818 /* Parse the initializer. */
24819 initializer = cp_parser_initializer_clause (parser,
24820 &nonconst);
24821 }
24822 else
24823 /* Parse the initializer. */
24824 initializer = cp_parser_constant_initializer (parser);
24825 }
24826 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24827 && !function_declarator_p (declarator))
24828 {
24829 bool x;
24830 if (decl_specifiers.storage_class != sc_static)
24831 initializer = cp_parser_save_nsdmi (parser);
24832 else
24833 initializer = cp_parser_initializer (parser, &x, &x);
24834 }
24835 /* Otherwise, there is no initializer. */
24836 else
24837 initializer = NULL_TREE;
24838
24839 /* See if we are probably looking at a function
24840 definition. We are certainly not looking at a
24841 member-declarator. Calling `grokfield' has
24842 side-effects, so we must not do it unless we are sure
24843 that we are looking at a member-declarator. */
24844 if (cp_parser_token_starts_function_definition_p
24845 (cp_lexer_peek_token (parser->lexer)))
24846 {
24847 /* The grammar does not allow a pure-specifier to be
24848 used when a member function is defined. (It is
24849 possible that this fact is an oversight in the
24850 standard, since a pure function may be defined
24851 outside of the class-specifier. */
24852 if (initializer && initializer_token_start)
24853 error_at (initializer_token_start->location,
24854 "pure-specifier on function-definition");
24855 decl = cp_parser_save_member_function_body (parser,
24856 &decl_specifiers,
24857 declarator,
24858 attributes);
24859 if (parser->fully_implicit_function_template_p)
24860 decl = finish_fully_implicit_template (parser, decl);
24861 /* If the member was not a friend, declare it here. */
24862 if (!friend_p)
24863 finish_member_declaration (decl);
24864 /* Peek at the next token. */
24865 token = cp_lexer_peek_token (parser->lexer);
24866 /* If the next token is a semicolon, consume it. */
24867 if (token->type == CPP_SEMICOLON)
24868 {
24869 location_t semicolon_loc
24870 = cp_lexer_consume_token (parser->lexer)->location;
24871 gcc_rich_location richloc (semicolon_loc);
24872 richloc.add_fixit_remove ();
24873 warning_at (&richloc, OPT_Wextra_semi,
24874 "extra %<;%> after in-class "
24875 "function definition");
24876 }
24877 goto out;
24878 }
24879 else
24880 if (declarator->kind == cdk_function)
24881 declarator->id_loc = token->location;
24882 /* Create the declaration. */
24883 decl = grokfield (declarator, &decl_specifiers,
24884 initializer, /*init_const_expr_p=*/true,
24885 asm_specification, attributes);
24886 if (parser->fully_implicit_function_template_p)
24887 {
24888 if (friend_p)
24889 finish_fully_implicit_template (parser, 0);
24890 else
24891 decl = finish_fully_implicit_template (parser, decl);
24892 }
24893 }
24894
24895 cp_finalize_omp_declare_simd (parser, decl);
24896 cp_finalize_oacc_routine (parser, decl, false);
24897
24898 /* Reset PREFIX_ATTRIBUTES. */
24899 if (attributes != error_mark_node)
24900 {
24901 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24902 attributes = TREE_CHAIN (attributes);
24903 if (attributes)
24904 TREE_CHAIN (attributes) = NULL_TREE;
24905 }
24906
24907 /* If there is any qualification still in effect, clear it
24908 now; we will be starting fresh with the next declarator. */
24909 parser->scope = NULL_TREE;
24910 parser->qualifying_scope = NULL_TREE;
24911 parser->object_scope = NULL_TREE;
24912 /* If it's a `,', then there are more declarators. */
24913 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24914 {
24915 cp_lexer_consume_token (parser->lexer);
24916 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24917 {
24918 cp_token *token = cp_lexer_previous_token (parser->lexer);
24919 gcc_rich_location richloc (token->location);
24920 richloc.add_fixit_remove ();
24921 error_at (&richloc, "stray %<,%> at end of "
24922 "member declaration");
24923 }
24924 }
24925 /* If the next token isn't a `;', then we have a parse error. */
24926 else if (cp_lexer_next_token_is_not (parser->lexer,
24927 CPP_SEMICOLON))
24928 {
24929 /* The next token might be a ways away from where the
24930 actual semicolon is missing. Find the previous token
24931 and use that for our error position. */
24932 cp_token *token = cp_lexer_previous_token (parser->lexer);
24933 gcc_rich_location richloc (token->location);
24934 richloc.add_fixit_insert_after (";");
24935 error_at (&richloc, "expected %<;%> at end of "
24936 "member declaration");
24937
24938 /* Assume that the user meant to provide a semicolon. If
24939 we were to cp_parser_skip_to_end_of_statement, we might
24940 skip to a semicolon inside a member function definition
24941 and issue nonsensical error messages. */
24942 assume_semicolon = true;
24943 }
24944
24945 if (decl)
24946 {
24947 /* Add DECL to the list of members. */
24948 if (!friend_p
24949 /* Explicitly include, eg, NSDMIs, for better error
24950 recovery (c++/58650). */
24951 || !DECL_DECLARES_FUNCTION_P (decl))
24952 finish_member_declaration (decl);
24953
24954 if (TREE_CODE (decl) == FUNCTION_DECL)
24955 cp_parser_save_default_args (parser, decl);
24956 else if (TREE_CODE (decl) == FIELD_DECL
24957 && DECL_INITIAL (decl))
24958 /* Add DECL to the queue of NSDMI to be parsed later. */
24959 vec_safe_push (unparsed_nsdmis, decl);
24960 }
24961
24962 if (assume_semicolon)
24963 goto out;
24964 }
24965 }
24966
24967 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24968 out:
24969 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24970 }
24971
24972 /* Parse a pure-specifier.
24973
24974 pure-specifier:
24975 = 0
24976
24977 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24978 Otherwise, ERROR_MARK_NODE is returned. */
24979
24980 static tree
24981 cp_parser_pure_specifier (cp_parser* parser)
24982 {
24983 cp_token *token;
24984
24985 /* Look for the `=' token. */
24986 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24987 return error_mark_node;
24988 /* Look for the `0' token. */
24989 token = cp_lexer_peek_token (parser->lexer);
24990
24991 if (token->type == CPP_EOF
24992 || token->type == CPP_PRAGMA_EOL)
24993 return error_mark_node;
24994
24995 cp_lexer_consume_token (parser->lexer);
24996
24997 /* Accept = default or = delete in c++0x mode. */
24998 if (token->keyword == RID_DEFAULT
24999 || token->keyword == RID_DELETE)
25000 {
25001 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
25002 return token->u.value;
25003 }
25004
25005 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
25006 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
25007 {
25008 cp_parser_error (parser,
25009 "invalid pure specifier (only %<= 0%> is allowed)");
25010 cp_parser_skip_to_end_of_statement (parser);
25011 return error_mark_node;
25012 }
25013 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
25014 {
25015 error_at (token->location, "templates may not be %<virtual%>");
25016 return error_mark_node;
25017 }
25018
25019 return integer_zero_node;
25020 }
25021
25022 /* Parse a constant-initializer.
25023
25024 constant-initializer:
25025 = constant-expression
25026
25027 Returns a representation of the constant-expression. */
25028
25029 static tree
25030 cp_parser_constant_initializer (cp_parser* parser)
25031 {
25032 /* Look for the `=' token. */
25033 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25034 return error_mark_node;
25035
25036 /* It is invalid to write:
25037
25038 struct S { static const int i = { 7 }; };
25039
25040 */
25041 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25042 {
25043 cp_parser_error (parser,
25044 "a brace-enclosed initializer is not allowed here");
25045 /* Consume the opening brace. */
25046 matching_braces braces;
25047 braces.consume_open (parser);
25048 /* Skip the initializer. */
25049 cp_parser_skip_to_closing_brace (parser);
25050 /* Look for the trailing `}'. */
25051 braces.require_close (parser);
25052
25053 return error_mark_node;
25054 }
25055
25056 return cp_parser_constant_expression (parser);
25057 }
25058
25059 /* Derived classes [gram.class.derived] */
25060
25061 /* Parse a base-clause.
25062
25063 base-clause:
25064 : base-specifier-list
25065
25066 base-specifier-list:
25067 base-specifier ... [opt]
25068 base-specifier-list , base-specifier ... [opt]
25069
25070 Returns a TREE_LIST representing the base-classes, in the order in
25071 which they were declared. The representation of each node is as
25072 described by cp_parser_base_specifier.
25073
25074 In the case that no bases are specified, this function will return
25075 NULL_TREE, not ERROR_MARK_NODE. */
25076
25077 static tree
25078 cp_parser_base_clause (cp_parser* parser)
25079 {
25080 tree bases = NULL_TREE;
25081
25082 /* Look for the `:' that begins the list. */
25083 cp_parser_require (parser, CPP_COLON, RT_COLON);
25084
25085 /* Scan the base-specifier-list. */
25086 while (true)
25087 {
25088 cp_token *token;
25089 tree base;
25090 bool pack_expansion_p = false;
25091
25092 /* Look for the base-specifier. */
25093 base = cp_parser_base_specifier (parser);
25094 /* Look for the (optional) ellipsis. */
25095 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25096 {
25097 /* Consume the `...'. */
25098 cp_lexer_consume_token (parser->lexer);
25099
25100 pack_expansion_p = true;
25101 }
25102
25103 /* Add BASE to the front of the list. */
25104 if (base && base != error_mark_node)
25105 {
25106 if (pack_expansion_p)
25107 /* Make this a pack expansion type. */
25108 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
25109
25110 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
25111 {
25112 TREE_CHAIN (base) = bases;
25113 bases = base;
25114 }
25115 }
25116 /* Peek at the next token. */
25117 token = cp_lexer_peek_token (parser->lexer);
25118 /* If it's not a comma, then the list is complete. */
25119 if (token->type != CPP_COMMA)
25120 break;
25121 /* Consume the `,'. */
25122 cp_lexer_consume_token (parser->lexer);
25123 }
25124
25125 /* PARSER->SCOPE may still be non-NULL at this point, if the last
25126 base class had a qualified name. However, the next name that
25127 appears is certainly not qualified. */
25128 parser->scope = NULL_TREE;
25129 parser->qualifying_scope = NULL_TREE;
25130 parser->object_scope = NULL_TREE;
25131
25132 return nreverse (bases);
25133 }
25134
25135 /* Parse a base-specifier.
25136
25137 base-specifier:
25138 :: [opt] nested-name-specifier [opt] class-name
25139 virtual access-specifier [opt] :: [opt] nested-name-specifier
25140 [opt] class-name
25141 access-specifier virtual [opt] :: [opt] nested-name-specifier
25142 [opt] class-name
25143
25144 Returns a TREE_LIST. The TREE_PURPOSE will be one of
25145 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
25146 indicate the specifiers provided. The TREE_VALUE will be a TYPE
25147 (or the ERROR_MARK_NODE) indicating the type that was specified. */
25148
25149 static tree
25150 cp_parser_base_specifier (cp_parser* parser)
25151 {
25152 cp_token *token;
25153 bool done = false;
25154 bool virtual_p = false;
25155 bool duplicate_virtual_error_issued_p = false;
25156 bool duplicate_access_error_issued_p = false;
25157 bool class_scope_p, template_p;
25158 tree access = access_default_node;
25159 tree type;
25160
25161 /* Process the optional `virtual' and `access-specifier'. */
25162 while (!done)
25163 {
25164 /* Peek at the next token. */
25165 token = cp_lexer_peek_token (parser->lexer);
25166 /* Process `virtual'. */
25167 switch (token->keyword)
25168 {
25169 case RID_VIRTUAL:
25170 /* If `virtual' appears more than once, issue an error. */
25171 if (virtual_p && !duplicate_virtual_error_issued_p)
25172 {
25173 cp_parser_error (parser,
25174 "%<virtual%> specified more than once in base-specifier");
25175 duplicate_virtual_error_issued_p = true;
25176 }
25177
25178 virtual_p = true;
25179
25180 /* Consume the `virtual' token. */
25181 cp_lexer_consume_token (parser->lexer);
25182
25183 break;
25184
25185 case RID_PUBLIC:
25186 case RID_PROTECTED:
25187 case RID_PRIVATE:
25188 /* If more than one access specifier appears, issue an
25189 error. */
25190 if (access != access_default_node
25191 && !duplicate_access_error_issued_p)
25192 {
25193 cp_parser_error (parser,
25194 "more than one access specifier in base-specifier");
25195 duplicate_access_error_issued_p = true;
25196 }
25197
25198 access = ridpointers[(int) token->keyword];
25199
25200 /* Consume the access-specifier. */
25201 cp_lexer_consume_token (parser->lexer);
25202
25203 break;
25204
25205 default:
25206 done = true;
25207 break;
25208 }
25209 }
25210 /* It is not uncommon to see programs mechanically, erroneously, use
25211 the 'typename' keyword to denote (dependent) qualified types
25212 as base classes. */
25213 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25214 {
25215 token = cp_lexer_peek_token (parser->lexer);
25216 if (!processing_template_decl)
25217 error_at (token->location,
25218 "keyword %<typename%> not allowed outside of templates");
25219 else
25220 error_at (token->location,
25221 "keyword %<typename%> not allowed in this context "
25222 "(the base class is implicitly a type)");
25223 cp_lexer_consume_token (parser->lexer);
25224 }
25225
25226 /* Look for the optional `::' operator. */
25227 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
25228 /* Look for the nested-name-specifier. The simplest way to
25229 implement:
25230
25231 [temp.res]
25232
25233 The keyword `typename' is not permitted in a base-specifier or
25234 mem-initializer; in these contexts a qualified name that
25235 depends on a template-parameter is implicitly assumed to be a
25236 type name.
25237
25238 is to pretend that we have seen the `typename' keyword at this
25239 point. */
25240 cp_parser_nested_name_specifier_opt (parser,
25241 /*typename_keyword_p=*/true,
25242 /*check_dependency_p=*/true,
25243 /*type_p=*/true,
25244 /*is_declaration=*/true);
25245 /* If the base class is given by a qualified name, assume that names
25246 we see are type names or templates, as appropriate. */
25247 class_scope_p = (parser->scope && TYPE_P (parser->scope));
25248 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
25249
25250 if (!parser->scope
25251 && cp_lexer_next_token_is_decltype (parser->lexer))
25252 /* DR 950 allows decltype as a base-specifier. */
25253 type = cp_parser_decltype (parser);
25254 else
25255 {
25256 /* Otherwise, look for the class-name. */
25257 type = cp_parser_class_name (parser,
25258 class_scope_p,
25259 template_p,
25260 typename_type,
25261 /*check_dependency_p=*/true,
25262 /*class_head_p=*/false,
25263 /*is_declaration=*/true);
25264 type = TREE_TYPE (type);
25265 }
25266
25267 if (type == error_mark_node)
25268 return error_mark_node;
25269
25270 return finish_base_specifier (type, access, virtual_p);
25271 }
25272
25273 /* Exception handling [gram.exception] */
25274
25275 /* Save the tokens that make up the noexcept-specifier for a member-function.
25276 Returns a DEFERRED_PARSE. */
25277
25278 static tree
25279 cp_parser_save_noexcept (cp_parser *parser)
25280 {
25281 cp_token *first = parser->lexer->next_token;
25282 /* We want everything up to, including, the final ')'. */
25283 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
25284 cp_token *last = parser->lexer->next_token;
25285
25286 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
25287 to carry the information we will need. */
25288 tree expr = make_node (DEFERRED_PARSE);
25289 /* Save away the noexcept-specifier; we will process it when the
25290 class is complete. */
25291 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
25292 expr = build_tree_list (expr, NULL_TREE);
25293 return expr;
25294 }
25295
25296 /* Used for late processing of noexcept-specifiers of member-functions.
25297 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
25298 we saved for later; parse it now. */
25299
25300 static tree
25301 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
25302 {
25303 /* Make sure we've gotten something that hasn't been parsed yet. */
25304 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
25305
25306 push_unparsed_function_queues (parser);
25307
25308 /* Push the saved tokens for the noexcept-specifier onto the parser's
25309 lexer stack. */
25310 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
25311 cp_parser_push_lexer_for_tokens (parser, tokens);
25312
25313 /* Parse the cached noexcept-specifier. */
25314 tree parsed_arg
25315 = cp_parser_noexcept_specification_opt (parser,
25316 /*require_constexpr=*/true,
25317 /*consumed_expr=*/NULL,
25318 /*return_cond=*/false,
25319 /*friend_p=*/false);
25320
25321 /* Revert to the main lexer. */
25322 cp_parser_pop_lexer (parser);
25323
25324 /* Restore the queue. */
25325 pop_unparsed_function_queues (parser);
25326
25327 /* And we're done. */
25328 return parsed_arg;
25329 }
25330
25331 /* Perform late checking of overriding function with respect to their
25332 noexcept-specifiers. TYPE is the class and FNDECL is the function
25333 that potentially overrides some virtual function with the same
25334 signature. */
25335
25336 static void
25337 noexcept_override_late_checks (tree type, tree fndecl)
25338 {
25339 tree binfo = TYPE_BINFO (type);
25340 tree base_binfo;
25341
25342 if (DECL_STATIC_FUNCTION_P (fndecl))
25343 return;
25344
25345 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
25346 {
25347 tree basetype = BINFO_TYPE (base_binfo);
25348
25349 if (!TYPE_POLYMORPHIC_P (basetype))
25350 continue;
25351
25352 tree fn = look_for_overrides_here (basetype, fndecl);
25353 if (fn)
25354 maybe_check_overriding_exception_spec (fndecl, fn);
25355 }
25356 }
25357
25358 /* Parse an (optional) noexcept-specification.
25359
25360 noexcept-specification:
25361 noexcept ( constant-expression ) [opt]
25362
25363 If no noexcept-specification is present, returns NULL_TREE.
25364 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25365 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25366 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25367 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25368 in which case a boolean condition is returned instead. If FRIEND_P is true,
25369 the function with this noexcept-specification had the `friend' specifier. */
25370
25371 static tree
25372 cp_parser_noexcept_specification_opt (cp_parser* parser,
25373 bool require_constexpr,
25374 bool* consumed_expr,
25375 bool return_cond,
25376 bool friend_p)
25377 {
25378 cp_token *token;
25379 const char *saved_message;
25380
25381 /* Peek at the next token. */
25382 token = cp_lexer_peek_token (parser->lexer);
25383
25384 /* Is it a noexcept-specification? */
25385 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
25386 {
25387 tree expr;
25388
25389 /* [class.mem]/6 says that a noexcept-specifer (within the
25390 member-specification of the class) is a complete-class context of
25391 a class. So, if the noexcept-specifier has the optional expression,
25392 just save the tokens, and reparse this after we're done with the
25393 class. */
25394 const bool literal_p
25395 = ((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
25396 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
25397 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN));
25398
25399 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
25400 /* No need to delay parsing for a number literal or true/false. */
25401 && !literal_p
25402 && at_class_scope_p ()
25403 /* Don't delay parsing for friend member functions. */
25404 && !friend_p
25405 && TYPE_BEING_DEFINED (current_class_type)
25406 && !LAMBDA_TYPE_P (current_class_type))
25407 return cp_parser_save_noexcept (parser);
25408
25409 cp_lexer_consume_token (parser->lexer);
25410
25411 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
25412 {
25413 matching_parens parens;
25414 parens.consume_open (parser);
25415
25416 tree save_ccp = current_class_ptr;
25417 tree save_ccr = current_class_ref;
25418
25419 if (current_class_type)
25420 inject_this_parameter (current_class_type, TYPE_UNQUALIFIED);
25421
25422 if (require_constexpr)
25423 {
25424 /* Types may not be defined in an exception-specification. */
25425 saved_message = parser->type_definition_forbidden_message;
25426 parser->type_definition_forbidden_message
25427 = G_("types may not be defined in an exception-specification");
25428
25429 bool non_constant_p;
25430 expr
25431 = cp_parser_constant_expression (parser,
25432 /*allow_non_constant=*/true,
25433 &non_constant_p);
25434 if (non_constant_p
25435 && !require_potential_rvalue_constant_expression (expr))
25436 {
25437 expr = NULL_TREE;
25438 return_cond = true;
25439 }
25440
25441 /* Restore the saved message. */
25442 parser->type_definition_forbidden_message = saved_message;
25443 }
25444 else
25445 {
25446 expr = cp_parser_expression (parser);
25447 *consumed_expr = true;
25448 }
25449
25450 parens.require_close (parser);
25451
25452 current_class_ptr = save_ccp;
25453 current_class_ref = save_ccr;
25454 }
25455 else
25456 {
25457 expr = boolean_true_node;
25458 if (!require_constexpr)
25459 *consumed_expr = false;
25460 }
25461
25462 /* We cannot build a noexcept-spec right away because this will check
25463 that expr is a constexpr. */
25464 if (!return_cond)
25465 return build_noexcept_spec (expr, tf_warning_or_error);
25466 else
25467 return expr;
25468 }
25469 else
25470 return NULL_TREE;
25471 }
25472
25473 /* Parse an (optional) exception-specification.
25474
25475 exception-specification:
25476 throw ( type-id-list [opt] )
25477
25478 Returns a TREE_LIST representing the exception-specification. The
25479 TREE_VALUE of each node is a type. If FRIEND_P is true, the function
25480 with this noexcept-specification had the `friend' specifier. */
25481
25482 static tree
25483 cp_parser_exception_specification_opt (cp_parser* parser, bool friend_p)
25484 {
25485 cp_token *token;
25486 tree type_id_list;
25487 const char *saved_message;
25488
25489 /* Peek at the next token. */
25490 token = cp_lexer_peek_token (parser->lexer);
25491
25492 /* Is it a noexcept-specification? */
25493 type_id_list
25494 = cp_parser_noexcept_specification_opt (parser,
25495 /*require_constexpr=*/true,
25496 /*consumed_expr=*/NULL,
25497 /*return_cond=*/false,
25498 friend_p);
25499 if (type_id_list != NULL_TREE)
25500 return type_id_list;
25501
25502 /* If it's not `throw', then there's no exception-specification. */
25503 if (!cp_parser_is_keyword (token, RID_THROW))
25504 return NULL_TREE;
25505
25506 location_t loc = token->location;
25507
25508 /* Consume the `throw'. */
25509 cp_lexer_consume_token (parser->lexer);
25510
25511 /* Look for the `('. */
25512 matching_parens parens;
25513 parens.require_open (parser);
25514
25515 /* Peek at the next token. */
25516 token = cp_lexer_peek_token (parser->lexer);
25517 /* If it's not a `)', then there is a type-id-list. */
25518 if (token->type != CPP_CLOSE_PAREN)
25519 {
25520 /* Types may not be defined in an exception-specification. */
25521 saved_message = parser->type_definition_forbidden_message;
25522 parser->type_definition_forbidden_message
25523 = G_("types may not be defined in an exception-specification");
25524 /* Parse the type-id-list. */
25525 type_id_list = cp_parser_type_id_list (parser);
25526 /* Restore the saved message. */
25527 parser->type_definition_forbidden_message = saved_message;
25528
25529 if (cxx_dialect >= cxx17)
25530 {
25531 error_at (loc, "ISO C++17 does not allow dynamic exception "
25532 "specifications");
25533 type_id_list = NULL_TREE;
25534 }
25535 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
25536 warning_at (loc, OPT_Wdeprecated,
25537 "dynamic exception specifications are deprecated in "
25538 "C++11");
25539 }
25540 /* In C++17, throw() is equivalent to noexcept (true). throw()
25541 is deprecated in C++11 and above as well, but is still widely used,
25542 so don't warn about it yet. */
25543 else if (cxx_dialect >= cxx17)
25544 type_id_list = noexcept_true_spec;
25545 else
25546 type_id_list = empty_except_spec;
25547
25548 /* Look for the `)'. */
25549 parens.require_close (parser);
25550
25551 return type_id_list;
25552 }
25553
25554 /* Parse an (optional) type-id-list.
25555
25556 type-id-list:
25557 type-id ... [opt]
25558 type-id-list , type-id ... [opt]
25559
25560 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25561 in the order that the types were presented. */
25562
25563 static tree
25564 cp_parser_type_id_list (cp_parser* parser)
25565 {
25566 tree types = NULL_TREE;
25567
25568 while (true)
25569 {
25570 cp_token *token;
25571 tree type;
25572
25573 token = cp_lexer_peek_token (parser->lexer);
25574
25575 /* Get the next type-id. */
25576 type = cp_parser_type_id (parser);
25577 /* Check for invalid 'auto'. */
25578 if (flag_concepts && type_uses_auto (type))
25579 {
25580 error_at (token->location,
25581 "invalid use of %<auto%> in exception-specification");
25582 type = error_mark_node;
25583 }
25584 /* Parse the optional ellipsis. */
25585 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25586 {
25587 /* Consume the `...'. */
25588 cp_lexer_consume_token (parser->lexer);
25589
25590 /* Turn the type into a pack expansion expression. */
25591 type = make_pack_expansion (type);
25592 }
25593 /* Add it to the list. */
25594 types = add_exception_specifier (types, type, /*complain=*/1);
25595 /* Peek at the next token. */
25596 token = cp_lexer_peek_token (parser->lexer);
25597 /* If it is not a `,', we are done. */
25598 if (token->type != CPP_COMMA)
25599 break;
25600 /* Consume the `,'. */
25601 cp_lexer_consume_token (parser->lexer);
25602 }
25603
25604 return nreverse (types);
25605 }
25606
25607 /* Parse a try-block.
25608
25609 try-block:
25610 try compound-statement handler-seq */
25611
25612 static tree
25613 cp_parser_try_block (cp_parser* parser)
25614 {
25615 tree try_block;
25616
25617 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
25618 if (parser->in_function_body
25619 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25620 && cxx_dialect < cxx2a)
25621 pedwarn (input_location, 0,
25622 "%<try%> in %<constexpr%> function only "
25623 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
25624
25625 try_block = begin_try_block ();
25626 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
25627 finish_try_block (try_block);
25628 cp_parser_handler_seq (parser);
25629 finish_handler_sequence (try_block);
25630
25631 return try_block;
25632 }
25633
25634 /* Parse a function-try-block.
25635
25636 function-try-block:
25637 try ctor-initializer [opt] function-body handler-seq */
25638
25639 static void
25640 cp_parser_function_try_block (cp_parser* parser)
25641 {
25642 tree compound_stmt;
25643 tree try_block;
25644
25645 /* Look for the `try' keyword. */
25646 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
25647 return;
25648 /* Let the rest of the front end know where we are. */
25649 try_block = begin_function_try_block (&compound_stmt);
25650 /* Parse the function-body. */
25651 cp_parser_ctor_initializer_opt_and_function_body
25652 (parser, /*in_function_try_block=*/true);
25653 /* We're done with the `try' part. */
25654 finish_function_try_block (try_block);
25655 /* Parse the handlers. */
25656 cp_parser_handler_seq (parser);
25657 /* We're done with the handlers. */
25658 finish_function_handler_sequence (try_block, compound_stmt);
25659 }
25660
25661 /* Parse a handler-seq.
25662
25663 handler-seq:
25664 handler handler-seq [opt] */
25665
25666 static void
25667 cp_parser_handler_seq (cp_parser* parser)
25668 {
25669 while (true)
25670 {
25671 cp_token *token;
25672
25673 /* Parse the handler. */
25674 cp_parser_handler (parser);
25675 /* Peek at the next token. */
25676 token = cp_lexer_peek_token (parser->lexer);
25677 /* If it's not `catch' then there are no more handlers. */
25678 if (!cp_parser_is_keyword (token, RID_CATCH))
25679 break;
25680 }
25681 }
25682
25683 /* Parse a handler.
25684
25685 handler:
25686 catch ( exception-declaration ) compound-statement */
25687
25688 static void
25689 cp_parser_handler (cp_parser* parser)
25690 {
25691 tree handler;
25692 tree declaration;
25693
25694 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
25695 handler = begin_handler ();
25696 matching_parens parens;
25697 parens.require_open (parser);
25698 declaration = cp_parser_exception_declaration (parser);
25699 finish_handler_parms (declaration, handler);
25700 parens.require_close (parser);
25701 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
25702 finish_handler (handler);
25703 }
25704
25705 /* Parse an exception-declaration.
25706
25707 exception-declaration:
25708 type-specifier-seq declarator
25709 type-specifier-seq abstract-declarator
25710 type-specifier-seq
25711 ...
25712
25713 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25714 ellipsis variant is used. */
25715
25716 static tree
25717 cp_parser_exception_declaration (cp_parser* parser)
25718 {
25719 cp_decl_specifier_seq type_specifiers;
25720 cp_declarator *declarator;
25721 const char *saved_message;
25722
25723 /* If it's an ellipsis, it's easy to handle. */
25724 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25725 {
25726 /* Consume the `...' token. */
25727 cp_lexer_consume_token (parser->lexer);
25728 return NULL_TREE;
25729 }
25730
25731 /* Types may not be defined in exception-declarations. */
25732 saved_message = parser->type_definition_forbidden_message;
25733 parser->type_definition_forbidden_message
25734 = G_("types may not be defined in exception-declarations");
25735
25736 /* Parse the type-specifier-seq. */
25737 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
25738 /*is_declaration=*/true,
25739 /*is_trailing_return=*/false,
25740 &type_specifiers);
25741 /* If it's a `)', then there is no declarator. */
25742 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25743 declarator = NULL;
25744 else
25745 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
25746 CP_PARSER_FLAGS_NONE,
25747 /*ctor_dtor_or_conv_p=*/NULL,
25748 /*parenthesized_p=*/NULL,
25749 /*member_p=*/false,
25750 /*friend_p=*/false,
25751 /*static_p=*/false);
25752
25753 /* Restore the saved message. */
25754 parser->type_definition_forbidden_message = saved_message;
25755
25756 if (!type_specifiers.any_specifiers_p)
25757 return error_mark_node;
25758
25759 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
25760 }
25761
25762 /* Parse a throw-expression.
25763
25764 throw-expression:
25765 throw assignment-expression [opt]
25766
25767 Returns a THROW_EXPR representing the throw-expression. */
25768
25769 static tree
25770 cp_parser_throw_expression (cp_parser* parser)
25771 {
25772 tree expression;
25773 cp_token* token;
25774
25775 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
25776 token = cp_lexer_peek_token (parser->lexer);
25777 /* Figure out whether or not there is an assignment-expression
25778 following the "throw" keyword. */
25779 if (token->type == CPP_COMMA
25780 || token->type == CPP_SEMICOLON
25781 || token->type == CPP_CLOSE_PAREN
25782 || token->type == CPP_CLOSE_SQUARE
25783 || token->type == CPP_CLOSE_BRACE
25784 || token->type == CPP_COLON)
25785 expression = NULL_TREE;
25786 else
25787 expression = cp_parser_assignment_expression (parser);
25788
25789 return build_throw (expression);
25790 }
25791
25792 /* GNU Extensions */
25793
25794 /* Parse an (optional) asm-specification.
25795
25796 asm-specification:
25797 asm ( string-literal )
25798
25799 If the asm-specification is present, returns a STRING_CST
25800 corresponding to the string-literal. Otherwise, returns
25801 NULL_TREE. */
25802
25803 static tree
25804 cp_parser_asm_specification_opt (cp_parser* parser)
25805 {
25806 cp_token *token;
25807 tree asm_specification;
25808
25809 /* Peek at the next token. */
25810 token = cp_lexer_peek_token (parser->lexer);
25811 /* If the next token isn't the `asm' keyword, then there's no
25812 asm-specification. */
25813 if (!cp_parser_is_keyword (token, RID_ASM))
25814 return NULL_TREE;
25815
25816 /* Consume the `asm' token. */
25817 cp_lexer_consume_token (parser->lexer);
25818 /* Look for the `('. */
25819 matching_parens parens;
25820 parens.require_open (parser);
25821
25822 /* Look for the string-literal. */
25823 asm_specification = cp_parser_string_literal (parser, false, false);
25824
25825 /* Look for the `)'. */
25826 parens.require_close (parser);
25827
25828 return asm_specification;
25829 }
25830
25831 /* Parse an asm-operand-list.
25832
25833 asm-operand-list:
25834 asm-operand
25835 asm-operand-list , asm-operand
25836
25837 asm-operand:
25838 string-literal ( expression )
25839 [ string-literal ] string-literal ( expression )
25840
25841 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25842 each node is the expression. The TREE_PURPOSE is itself a
25843 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25844 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25845 is a STRING_CST for the string literal before the parenthesis. Returns
25846 ERROR_MARK_NODE if any of the operands are invalid. */
25847
25848 static tree
25849 cp_parser_asm_operand_list (cp_parser* parser)
25850 {
25851 tree asm_operands = NULL_TREE;
25852 bool invalid_operands = false;
25853
25854 while (true)
25855 {
25856 tree string_literal;
25857 tree expression;
25858 tree name;
25859
25860 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25861 {
25862 /* Consume the `[' token. */
25863 cp_lexer_consume_token (parser->lexer);
25864 /* Read the operand name. */
25865 name = cp_parser_identifier (parser);
25866 if (name != error_mark_node)
25867 name = build_string (IDENTIFIER_LENGTH (name),
25868 IDENTIFIER_POINTER (name));
25869 /* Look for the closing `]'. */
25870 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25871 }
25872 else
25873 name = NULL_TREE;
25874 /* Look for the string-literal. */
25875 string_literal = cp_parser_string_literal (parser, false, false);
25876
25877 /* Look for the `('. */
25878 matching_parens parens;
25879 parens.require_open (parser);
25880 /* Parse the expression. */
25881 expression = cp_parser_expression (parser);
25882 /* Look for the `)'. */
25883 parens.require_close (parser);
25884
25885 if (name == error_mark_node
25886 || string_literal == error_mark_node
25887 || expression == error_mark_node)
25888 invalid_operands = true;
25889
25890 /* Add this operand to the list. */
25891 asm_operands = tree_cons (build_tree_list (name, string_literal),
25892 expression,
25893 asm_operands);
25894 /* If the next token is not a `,', there are no more
25895 operands. */
25896 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25897 break;
25898 /* Consume the `,'. */
25899 cp_lexer_consume_token (parser->lexer);
25900 }
25901
25902 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25903 }
25904
25905 /* Parse an asm-clobber-list.
25906
25907 asm-clobber-list:
25908 string-literal
25909 asm-clobber-list , string-literal
25910
25911 Returns a TREE_LIST, indicating the clobbers in the order that they
25912 appeared. The TREE_VALUE of each node is a STRING_CST. */
25913
25914 static tree
25915 cp_parser_asm_clobber_list (cp_parser* parser)
25916 {
25917 tree clobbers = NULL_TREE;
25918
25919 while (true)
25920 {
25921 tree string_literal;
25922
25923 /* Look for the string literal. */
25924 string_literal = cp_parser_string_literal (parser, false, false);
25925 /* Add it to the list. */
25926 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25927 /* If the next token is not a `,', then the list is
25928 complete. */
25929 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25930 break;
25931 /* Consume the `,' token. */
25932 cp_lexer_consume_token (parser->lexer);
25933 }
25934
25935 return clobbers;
25936 }
25937
25938 /* Parse an asm-label-list.
25939
25940 asm-label-list:
25941 identifier
25942 asm-label-list , identifier
25943
25944 Returns a TREE_LIST, indicating the labels in the order that they
25945 appeared. The TREE_VALUE of each node is a label. */
25946
25947 static tree
25948 cp_parser_asm_label_list (cp_parser* parser)
25949 {
25950 tree labels = NULL_TREE;
25951
25952 while (true)
25953 {
25954 tree identifier, label, name;
25955
25956 /* Look for the identifier. */
25957 identifier = cp_parser_identifier (parser);
25958 if (!error_operand_p (identifier))
25959 {
25960 label = lookup_label (identifier);
25961 if (TREE_CODE (label) == LABEL_DECL)
25962 {
25963 TREE_USED (label) = 1;
25964 check_goto (label);
25965 name = build_string (IDENTIFIER_LENGTH (identifier),
25966 IDENTIFIER_POINTER (identifier));
25967 labels = tree_cons (name, label, labels);
25968 }
25969 }
25970 /* If the next token is not a `,', then the list is
25971 complete. */
25972 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25973 break;
25974 /* Consume the `,' token. */
25975 cp_lexer_consume_token (parser->lexer);
25976 }
25977
25978 return nreverse (labels);
25979 }
25980
25981 /* Return TRUE iff the next tokens in the stream are possibly the
25982 beginning of a GNU extension attribute. */
25983
25984 static bool
25985 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25986 {
25987 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25988 }
25989
25990 /* Return TRUE iff the next tokens in the stream are possibly the
25991 beginning of a standard C++-11 attribute specifier. */
25992
25993 static bool
25994 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25995 {
25996 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25997 }
25998
25999 /* Return TRUE iff the next Nth tokens in the stream are possibly the
26000 beginning of a standard C++-11 attribute specifier. */
26001
26002 static bool
26003 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
26004 {
26005 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
26006
26007 return (cxx_dialect >= cxx11
26008 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
26009 || (token->type == CPP_OPEN_SQUARE
26010 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
26011 && token->type == CPP_OPEN_SQUARE)));
26012 }
26013
26014 /* Return TRUE iff the next Nth tokens in the stream are possibly the
26015 beginning of a GNU extension attribute. */
26016
26017 static bool
26018 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
26019 {
26020 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
26021
26022 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
26023 }
26024
26025 /* Return true iff the next tokens can be the beginning of either a
26026 GNU attribute list, or a standard C++11 attribute sequence. */
26027
26028 static bool
26029 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
26030 {
26031 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
26032 || cp_next_tokens_can_be_std_attribute_p (parser));
26033 }
26034
26035 /* Return true iff the next Nth tokens can be the beginning of either
26036 a GNU attribute list, or a standard C++11 attribute sequence. */
26037
26038 static bool
26039 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
26040 {
26041 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
26042 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
26043 }
26044
26045 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
26046 of GNU attributes, or return NULL. */
26047
26048 static tree
26049 cp_parser_attributes_opt (cp_parser *parser)
26050 {
26051 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
26052 return cp_parser_gnu_attributes_opt (parser);
26053 return cp_parser_std_attribute_spec_seq (parser);
26054 }
26055
26056 /* Parse an (optional) series of attributes.
26057
26058 attributes:
26059 attributes attribute
26060
26061 attribute:
26062 __attribute__ (( attribute-list [opt] ))
26063
26064 The return value is as for cp_parser_gnu_attribute_list. */
26065
26066 static tree
26067 cp_parser_gnu_attributes_opt (cp_parser* parser)
26068 {
26069 tree attributes = NULL_TREE;
26070
26071 temp_override<bool> cleanup
26072 (parser->auto_is_implicit_function_template_parm_p, false);
26073
26074 while (true)
26075 {
26076 cp_token *token;
26077 tree attribute_list;
26078 bool ok = true;
26079
26080 /* Peek at the next token. */
26081 token = cp_lexer_peek_token (parser->lexer);
26082 /* If it's not `__attribute__', then we're done. */
26083 if (token->keyword != RID_ATTRIBUTE)
26084 break;
26085
26086 /* Consume the `__attribute__' keyword. */
26087 cp_lexer_consume_token (parser->lexer);
26088 /* Look for the two `(' tokens. */
26089 matching_parens outer_parens;
26090 if (!outer_parens.require_open (parser))
26091 ok = false;
26092 matching_parens inner_parens;
26093 if (!inner_parens.require_open (parser))
26094 ok = false;
26095
26096 /* Peek at the next token. */
26097 token = cp_lexer_peek_token (parser->lexer);
26098 if (token->type != CPP_CLOSE_PAREN)
26099 /* Parse the attribute-list. */
26100 attribute_list = cp_parser_gnu_attribute_list (parser);
26101 else
26102 /* If the next token is a `)', then there is no attribute
26103 list. */
26104 attribute_list = NULL;
26105
26106 /* Look for the two `)' tokens. */
26107 if (!inner_parens.require_close (parser))
26108 ok = false;
26109 if (!outer_parens.require_close (parser))
26110 ok = false;
26111 if (!ok)
26112 cp_parser_skip_to_end_of_statement (parser);
26113
26114 /* Add these new attributes to the list. */
26115 attributes = attr_chainon (attributes, attribute_list);
26116 }
26117
26118 return attributes;
26119 }
26120
26121 /* Parse a GNU attribute-list.
26122
26123 attribute-list:
26124 attribute
26125 attribute-list , attribute
26126
26127 attribute:
26128 identifier
26129 identifier ( identifier )
26130 identifier ( identifier , expression-list )
26131 identifier ( expression-list )
26132
26133 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
26134 to an attribute. The TREE_PURPOSE of each node is the identifier
26135 indicating which attribute is in use. The TREE_VALUE represents
26136 the arguments, if any. */
26137
26138 static tree
26139 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
26140 {
26141 tree attribute_list = NULL_TREE;
26142 bool save_translate_strings_p = parser->translate_strings_p;
26143
26144 /* Don't create wrapper nodes within attributes: the
26145 handlers don't know how to handle them. */
26146 auto_suppress_location_wrappers sentinel;
26147
26148 parser->translate_strings_p = false;
26149 while (true)
26150 {
26151 cp_token *token;
26152 tree identifier;
26153 tree attribute;
26154
26155 /* Look for the identifier. We also allow keywords here; for
26156 example `__attribute__ ((const))' is legal. */
26157 token = cp_lexer_peek_token (parser->lexer);
26158 if (token->type == CPP_NAME
26159 || token->type == CPP_KEYWORD)
26160 {
26161 tree arguments = NULL_TREE;
26162
26163 /* Consume the token, but save it since we need it for the
26164 SIMD enabled function parsing. */
26165 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
26166
26167 /* Save away the identifier that indicates which attribute
26168 this is. */
26169 identifier = (token->type == CPP_KEYWORD)
26170 /* For keywords, use the canonical spelling, not the
26171 parsed identifier. */
26172 ? ridpointers[(int) token->keyword]
26173 : id_token->u.value;
26174
26175 identifier = canonicalize_attr_name (identifier);
26176 attribute = build_tree_list (identifier, NULL_TREE);
26177
26178 /* Peek at the next token. */
26179 token = cp_lexer_peek_token (parser->lexer);
26180 /* If it's an `(', then parse the attribute arguments. */
26181 if (token->type == CPP_OPEN_PAREN)
26182 {
26183 vec<tree, va_gc> *vec;
26184 int attr_flag = (attribute_takes_identifier_p (identifier)
26185 ? id_attr : normal_attr);
26186 vec = cp_parser_parenthesized_expression_list
26187 (parser, attr_flag, /*cast_p=*/false,
26188 /*allow_expansion_p=*/false,
26189 /*non_constant_p=*/NULL);
26190 if (vec == NULL)
26191 arguments = error_mark_node;
26192 else
26193 {
26194 arguments = build_tree_list_vec (vec);
26195 release_tree_vector (vec);
26196 }
26197 /* Save the arguments away. */
26198 TREE_VALUE (attribute) = arguments;
26199 }
26200
26201 if (arguments != error_mark_node)
26202 {
26203 /* Add this attribute to the list. */
26204 TREE_CHAIN (attribute) = attribute_list;
26205 attribute_list = attribute;
26206 }
26207
26208 token = cp_lexer_peek_token (parser->lexer);
26209 }
26210 /* Unless EXACTLY_ONE is set look for more attributes.
26211 If the next token isn't a `,', we're done. */
26212 if (exactly_one || token->type != CPP_COMMA)
26213 break;
26214
26215 /* Consume the comma and keep going. */
26216 cp_lexer_consume_token (parser->lexer);
26217 }
26218 parser->translate_strings_p = save_translate_strings_p;
26219
26220 /* We built up the list in reverse order. */
26221 return nreverse (attribute_list);
26222 }
26223
26224 /* Parse a standard C++11 attribute.
26225
26226 The returned representation is a TREE_LIST which TREE_PURPOSE is
26227 the scoped name of the attribute, and the TREE_VALUE is its
26228 arguments list.
26229
26230 Note that the scoped name of the attribute is itself a TREE_LIST
26231 which TREE_PURPOSE is the namespace of the attribute, and
26232 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
26233 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
26234 and which TREE_PURPOSE is directly the attribute name.
26235
26236 Clients of the attribute code should use get_attribute_namespace
26237 and get_attribute_name to get the actual namespace and name of
26238 attributes, regardless of their being GNU or C++11 attributes.
26239
26240 attribute:
26241 attribute-token attribute-argument-clause [opt]
26242
26243 attribute-token:
26244 identifier
26245 attribute-scoped-token
26246
26247 attribute-scoped-token:
26248 attribute-namespace :: identifier
26249
26250 attribute-namespace:
26251 identifier
26252
26253 attribute-argument-clause:
26254 ( balanced-token-seq )
26255
26256 balanced-token-seq:
26257 balanced-token [opt]
26258 balanced-token-seq balanced-token
26259
26260 balanced-token:
26261 ( balanced-token-seq )
26262 [ balanced-token-seq ]
26263 { balanced-token-seq }. */
26264
26265 static tree
26266 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
26267 {
26268 tree attribute, attr_id = NULL_TREE, arguments;
26269 cp_token *token;
26270
26271 temp_override<bool> cleanup
26272 (parser->auto_is_implicit_function_template_parm_p, false);
26273
26274 /* First, parse name of the attribute, a.k.a attribute-token. */
26275
26276 token = cp_lexer_peek_token (parser->lexer);
26277 if (token->type == CPP_NAME)
26278 attr_id = token->u.value;
26279 else if (token->type == CPP_KEYWORD)
26280 attr_id = ridpointers[(int) token->keyword];
26281 else if (token->flags & NAMED_OP)
26282 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26283
26284 if (attr_id == NULL_TREE)
26285 return NULL_TREE;
26286
26287 cp_lexer_consume_token (parser->lexer);
26288
26289 token = cp_lexer_peek_token (parser->lexer);
26290 if (token->type == CPP_SCOPE)
26291 {
26292 /* We are seeing a scoped attribute token. */
26293
26294 cp_lexer_consume_token (parser->lexer);
26295 if (attr_ns)
26296 error_at (token->location, "attribute using prefix used together "
26297 "with scoped attribute token");
26298 attr_ns = attr_id;
26299
26300 token = cp_lexer_consume_token (parser->lexer);
26301 if (token->type == CPP_NAME)
26302 attr_id = token->u.value;
26303 else if (token->type == CPP_KEYWORD)
26304 attr_id = ridpointers[(int) token->keyword];
26305 else if (token->flags & NAMED_OP)
26306 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26307 else
26308 {
26309 error_at (token->location,
26310 "expected an identifier for the attribute name");
26311 return error_mark_node;
26312 }
26313
26314 attr_ns = canonicalize_attr_name (attr_ns);
26315 attr_id = canonicalize_attr_name (attr_id);
26316 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26317 NULL_TREE);
26318 token = cp_lexer_peek_token (parser->lexer);
26319 }
26320 else if (attr_ns)
26321 {
26322 attr_ns = canonicalize_attr_name (attr_ns);
26323 attr_id = canonicalize_attr_name (attr_id);
26324 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26325 NULL_TREE);
26326 }
26327 else
26328 {
26329 attr_id = canonicalize_attr_name (attr_id);
26330 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
26331 NULL_TREE);
26332 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
26333 but no longer: we have to be able to tell [[noreturn]] and
26334 __attribute__((noreturn)) apart. */
26335 /* C++14 deprecated attribute is equivalent to GNU's. */
26336 if (is_attribute_p ("deprecated", attr_id))
26337 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26338 /* C++17 fallthrough attribute is equivalent to GNU's. */
26339 else if (is_attribute_p ("fallthrough", attr_id))
26340 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26341 /* Transactional Memory TS optimize_for_synchronized attribute is
26342 equivalent to GNU transaction_callable. */
26343 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
26344 TREE_PURPOSE (attribute)
26345 = get_identifier ("transaction_callable");
26346 /* Transactional Memory attributes are GNU attributes. */
26347 else if (tm_attr_to_mask (attr_id))
26348 TREE_PURPOSE (attribute) = attr_id;
26349 }
26350
26351 /* Now parse the optional argument clause of the attribute. */
26352
26353 if (token->type != CPP_OPEN_PAREN)
26354 return attribute;
26355
26356 {
26357 vec<tree, va_gc> *vec;
26358 int attr_flag = normal_attr;
26359
26360 /* Maybe we don't expect to see any arguments for this attribute. */
26361 const attribute_spec *as
26362 = lookup_attribute_spec (TREE_PURPOSE (attribute));
26363 if (as && as->max_length == 0)
26364 {
26365 error_at (token->location, "%qE attribute does not take any arguments",
26366 attr_id);
26367 cp_parser_skip_to_closing_parenthesis (parser,
26368 /*recovering=*/true,
26369 /*or_comma=*/false,
26370 /*consume_paren=*/true);
26371 return error_mark_node;
26372 }
26373
26374 if (attr_ns == gnu_identifier
26375 && attribute_takes_identifier_p (attr_id))
26376 /* A GNU attribute that takes an identifier in parameter. */
26377 attr_flag = id_attr;
26378
26379 vec = cp_parser_parenthesized_expression_list
26380 (parser, attr_flag, /*cast_p=*/false,
26381 /*allow_expansion_p=*/true,
26382 /*non_constant_p=*/NULL);
26383 if (vec == NULL)
26384 arguments = error_mark_node;
26385 else
26386 {
26387 arguments = build_tree_list_vec (vec);
26388 release_tree_vector (vec);
26389 }
26390
26391 if (arguments == error_mark_node)
26392 attribute = error_mark_node;
26393 else
26394 TREE_VALUE (attribute) = arguments;
26395 }
26396
26397 return attribute;
26398 }
26399
26400 /* Check that the attribute ATTRIBUTE appears at most once in the
26401 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
26402 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
26403 isn't implemented yet in GCC. */
26404
26405 static void
26406 cp_parser_check_std_attribute (tree attributes, tree attribute)
26407 {
26408 if (attributes)
26409 {
26410 tree name = get_attribute_name (attribute);
26411 if (is_attribute_p ("noreturn", name)
26412 && lookup_attribute ("noreturn", attributes))
26413 error ("attribute %<noreturn%> can appear at most once "
26414 "in an attribute-list");
26415 else if (is_attribute_p ("deprecated", name)
26416 && lookup_attribute ("deprecated", attributes))
26417 error ("attribute %<deprecated%> can appear at most once "
26418 "in an attribute-list");
26419 }
26420 }
26421
26422 /* Parse a list of standard C++-11 attributes.
26423
26424 attribute-list:
26425 attribute [opt]
26426 attribute-list , attribute[opt]
26427 attribute ...
26428 attribute-list , attribute ...
26429 */
26430
26431 static tree
26432 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
26433 {
26434 tree attributes = NULL_TREE, attribute = NULL_TREE;
26435 cp_token *token = NULL;
26436
26437 while (true)
26438 {
26439 attribute = cp_parser_std_attribute (parser, attr_ns);
26440 if (attribute == error_mark_node)
26441 break;
26442 if (attribute != NULL_TREE)
26443 {
26444 cp_parser_check_std_attribute (attributes, attribute);
26445 TREE_CHAIN (attribute) = attributes;
26446 attributes = attribute;
26447 }
26448 token = cp_lexer_peek_token (parser->lexer);
26449 if (token->type == CPP_ELLIPSIS)
26450 {
26451 cp_lexer_consume_token (parser->lexer);
26452 if (attribute == NULL_TREE)
26453 error_at (token->location,
26454 "expected attribute before %<...%>");
26455 else
26456 {
26457 tree pack = make_pack_expansion (TREE_VALUE (attribute));
26458 if (pack == error_mark_node)
26459 return error_mark_node;
26460 TREE_VALUE (attribute) = pack;
26461 }
26462 token = cp_lexer_peek_token (parser->lexer);
26463 }
26464 if (token->type != CPP_COMMA)
26465 break;
26466 cp_lexer_consume_token (parser->lexer);
26467 }
26468 attributes = nreverse (attributes);
26469 return attributes;
26470 }
26471
26472 /* Parse a standard C++-11 attribute specifier.
26473
26474 attribute-specifier:
26475 [ [ attribute-using-prefix [opt] attribute-list ] ]
26476 alignment-specifier
26477
26478 attribute-using-prefix:
26479 using attribute-namespace :
26480
26481 alignment-specifier:
26482 alignas ( type-id ... [opt] )
26483 alignas ( alignment-expression ... [opt] ). */
26484
26485 static tree
26486 cp_parser_std_attribute_spec (cp_parser *parser)
26487 {
26488 tree attributes = NULL_TREE;
26489 cp_token *token = cp_lexer_peek_token (parser->lexer);
26490
26491 if (token->type == CPP_OPEN_SQUARE
26492 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
26493 {
26494 tree attr_ns = NULL_TREE;
26495
26496 cp_lexer_consume_token (parser->lexer);
26497 cp_lexer_consume_token (parser->lexer);
26498
26499 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26500 {
26501 token = cp_lexer_peek_nth_token (parser->lexer, 2);
26502 if (token->type == CPP_NAME)
26503 attr_ns = token->u.value;
26504 else if (token->type == CPP_KEYWORD)
26505 attr_ns = ridpointers[(int) token->keyword];
26506 else if (token->flags & NAMED_OP)
26507 attr_ns = get_identifier (cpp_type2name (token->type,
26508 token->flags));
26509 if (attr_ns
26510 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
26511 {
26512 if (cxx_dialect < cxx17
26513 && !in_system_header_at (input_location))
26514 pedwarn (input_location, 0,
26515 "attribute using prefix only available "
26516 "with %<-std=c++17%> or %<-std=gnu++17%>");
26517
26518 cp_lexer_consume_token (parser->lexer);
26519 cp_lexer_consume_token (parser->lexer);
26520 cp_lexer_consume_token (parser->lexer);
26521 }
26522 else
26523 attr_ns = NULL_TREE;
26524 }
26525
26526 attributes = cp_parser_std_attribute_list (parser, attr_ns);
26527
26528 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
26529 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
26530 cp_parser_skip_to_end_of_statement (parser);
26531 else
26532 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26533 when we are sure that we have actually parsed them. */
26534 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26535 }
26536 else
26537 {
26538 tree alignas_expr;
26539
26540 /* Look for an alignment-specifier. */
26541
26542 token = cp_lexer_peek_token (parser->lexer);
26543
26544 if (token->type != CPP_KEYWORD
26545 || token->keyword != RID_ALIGNAS)
26546 return NULL_TREE;
26547
26548 cp_lexer_consume_token (parser->lexer);
26549 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26550
26551 matching_parens parens;
26552 if (!parens.require_open (parser))
26553 return error_mark_node;
26554
26555 cp_parser_parse_tentatively (parser);
26556 alignas_expr = cp_parser_type_id (parser);
26557
26558 if (!cp_parser_parse_definitely (parser))
26559 {
26560 alignas_expr = cp_parser_assignment_expression (parser);
26561 if (alignas_expr == error_mark_node)
26562 cp_parser_skip_to_end_of_statement (parser);
26563 if (alignas_expr == NULL_TREE
26564 || alignas_expr == error_mark_node)
26565 return alignas_expr;
26566 }
26567
26568 alignas_expr = cxx_alignas_expr (alignas_expr);
26569 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
26570
26571 /* Handle alignas (pack...). */
26572 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26573 {
26574 cp_lexer_consume_token (parser->lexer);
26575 alignas_expr = make_pack_expansion (alignas_expr);
26576 }
26577
26578 /* Something went wrong, so don't build the attribute. */
26579 if (alignas_expr == error_mark_node)
26580 return error_mark_node;
26581
26582 /* Missing ')' means the code cannot possibly be valid; go ahead
26583 and commit to make sure we issue a hard error. */
26584 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
26585 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26586 cp_parser_commit_to_tentative_parse (parser);
26587
26588 if (!parens.require_close (parser))
26589 return error_mark_node;
26590
26591 /* Build the C++-11 representation of an 'aligned'
26592 attribute. */
26593 attributes
26594 = build_tree_list (build_tree_list (gnu_identifier,
26595 aligned_identifier), alignas_expr);
26596 }
26597
26598 return attributes;
26599 }
26600
26601 /* Parse a standard C++-11 attribute-specifier-seq.
26602
26603 attribute-specifier-seq:
26604 attribute-specifier-seq [opt] attribute-specifier
26605 */
26606
26607 static tree
26608 cp_parser_std_attribute_spec_seq (cp_parser *parser)
26609 {
26610 tree attr_specs = NULL_TREE;
26611 tree attr_last = NULL_TREE;
26612
26613 /* Don't create wrapper nodes within attributes: the
26614 handlers don't know how to handle them. */
26615 auto_suppress_location_wrappers sentinel;
26616
26617 while (true)
26618 {
26619 tree attr_spec = cp_parser_std_attribute_spec (parser);
26620 if (attr_spec == NULL_TREE)
26621 break;
26622 if (attr_spec == error_mark_node)
26623 return error_mark_node;
26624
26625 if (attr_last)
26626 TREE_CHAIN (attr_last) = attr_spec;
26627 else
26628 attr_specs = attr_last = attr_spec;
26629 attr_last = tree_last (attr_last);
26630 }
26631
26632 return attr_specs;
26633 }
26634
26635 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26636 return index of the first token after balanced-token, or N on failure. */
26637
26638 static size_t
26639 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
26640 {
26641 size_t orig_n = n;
26642 int nparens = 0, nbraces = 0, nsquares = 0;
26643 do
26644 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
26645 {
26646 case CPP_PRAGMA_EOL:
26647 if (!parser->lexer->in_pragma)
26648 break;
26649 /* FALLTHRU */
26650 case CPP_EOF:
26651 /* Ran out of tokens. */
26652 return orig_n;
26653 case CPP_OPEN_PAREN:
26654 ++nparens;
26655 break;
26656 case CPP_OPEN_BRACE:
26657 ++nbraces;
26658 break;
26659 case CPP_OPEN_SQUARE:
26660 ++nsquares;
26661 break;
26662 case CPP_CLOSE_PAREN:
26663 --nparens;
26664 break;
26665 case CPP_CLOSE_BRACE:
26666 --nbraces;
26667 break;
26668 case CPP_CLOSE_SQUARE:
26669 --nsquares;
26670 break;
26671 default:
26672 break;
26673 }
26674 while (nparens || nbraces || nsquares);
26675 return n;
26676 }
26677
26678 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26679 return index of the first token after the GNU attribute tokens, or N on
26680 failure. */
26681
26682 static size_t
26683 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
26684 {
26685 while (true)
26686 {
26687 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
26688 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
26689 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
26690 break;
26691
26692 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
26693 if (n2 == n + 2)
26694 break;
26695 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
26696 break;
26697 n = n2 + 1;
26698 }
26699 return n;
26700 }
26701
26702 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26703 next token), return index of the first token after the standard C++11
26704 attribute tokens, or N on failure. */
26705
26706 static size_t
26707 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
26708 {
26709 while (true)
26710 {
26711 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
26712 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
26713 {
26714 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26715 if (n2 == n + 1)
26716 break;
26717 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
26718 break;
26719 n = n2 + 1;
26720 }
26721 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
26722 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
26723 {
26724 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26725 if (n2 == n + 1)
26726 break;
26727 n = n2;
26728 }
26729 else
26730 break;
26731 }
26732 return n;
26733 }
26734
26735 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26736 as the next token), return index of the first token after the attribute
26737 tokens, or N on failure. */
26738
26739 static size_t
26740 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
26741 {
26742 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
26743 return cp_parser_skip_gnu_attributes_opt (parser, n);
26744 return cp_parser_skip_std_attribute_spec_seq (parser, n);
26745 }
26746
26747 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26748 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26749 current value of the PEDANTIC flag, regardless of whether or not
26750 the `__extension__' keyword is present. The caller is responsible
26751 for restoring the value of the PEDANTIC flag. */
26752
26753 static bool
26754 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
26755 {
26756 /* Save the old value of the PEDANTIC flag. */
26757 *saved_pedantic = pedantic;
26758
26759 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
26760 {
26761 /* Consume the `__extension__' token. */
26762 cp_lexer_consume_token (parser->lexer);
26763 /* We're not being pedantic while the `__extension__' keyword is
26764 in effect. */
26765 pedantic = 0;
26766
26767 return true;
26768 }
26769
26770 return false;
26771 }
26772
26773 /* Parse a label declaration.
26774
26775 label-declaration:
26776 __label__ label-declarator-seq ;
26777
26778 label-declarator-seq:
26779 identifier , label-declarator-seq
26780 identifier */
26781
26782 static void
26783 cp_parser_label_declaration (cp_parser* parser)
26784 {
26785 /* Look for the `__label__' keyword. */
26786 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
26787
26788 while (true)
26789 {
26790 tree identifier;
26791
26792 /* Look for an identifier. */
26793 identifier = cp_parser_identifier (parser);
26794 /* If we failed, stop. */
26795 if (identifier == error_mark_node)
26796 break;
26797 /* Declare it as a label. */
26798 finish_label_decl (identifier);
26799 /* If the next token is a `;', stop. */
26800 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26801 break;
26802 /* Look for the `,' separating the label declarations. */
26803 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
26804 }
26805
26806 /* Look for the final `;'. */
26807 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26808 }
26809
26810 // -------------------------------------------------------------------------- //
26811 // Requires Clause
26812
26813 // Parse a requires clause.
26814 //
26815 // requires-clause:
26816 // 'requires' logical-or-expression
26817 //
26818 // The required logical-or-expression must be a constant expression. Note
26819 // that we don't check that the expression is constepxr here. We defer until
26820 // we analyze constraints and then, we only check atomic constraints.
26821 static tree
26822 cp_parser_requires_clause (cp_parser *parser)
26823 {
26824 // Parse the requires clause so that it is not automatically folded.
26825 ++processing_template_decl;
26826 tree expr = cp_parser_binary_expression (parser, false, false,
26827 PREC_NOT_OPERATOR, NULL);
26828 if (check_for_bare_parameter_packs (expr))
26829 expr = error_mark_node;
26830 --processing_template_decl;
26831 return expr;
26832 }
26833
26834 // Optionally parse a requires clause:
26835 static tree
26836 cp_parser_requires_clause_opt (cp_parser *parser)
26837 {
26838 cp_token *tok = cp_lexer_peek_token (parser->lexer);
26839 if (tok->keyword != RID_REQUIRES)
26840 {
26841 if (!flag_concepts && tok->type == CPP_NAME
26842 && tok->u.value == ridpointers[RID_REQUIRES])
26843 {
26844 error_at (cp_lexer_peek_token (parser->lexer)->location,
26845 "%<requires%> only available with %<-fconcepts%>");
26846 /* Parse and discard the requires-clause. */
26847 cp_lexer_consume_token (parser->lexer);
26848 cp_parser_requires_clause (parser);
26849 }
26850 return NULL_TREE;
26851 }
26852 cp_lexer_consume_token (parser->lexer);
26853 return cp_parser_requires_clause (parser);
26854 }
26855
26856
26857 /*---------------------------------------------------------------------------
26858 Requires expressions
26859 ---------------------------------------------------------------------------*/
26860
26861 /* Parse a requires expression
26862
26863 requirement-expression:
26864 'requires' requirement-parameter-list [opt] requirement-body */
26865 static tree
26866 cp_parser_requires_expression (cp_parser *parser)
26867 {
26868 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26869 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26870
26871 /* A requires-expression shall appear only within a concept
26872 definition or a requires-clause.
26873
26874 TODO: Implement this diagnostic correctly. */
26875 if (!processing_template_decl)
26876 {
26877 error_at (loc, "a requires expression cannot appear outside a template");
26878 cp_parser_skip_to_end_of_statement (parser);
26879 return error_mark_node;
26880 }
26881
26882 tree parms, reqs;
26883 {
26884 /* Local parameters are delared as variables within the scope
26885 of the expression. They are not visible past the end of
26886 the expression. Expressions within the requires-expression
26887 are unevaluated. */
26888 struct scope_sentinel
26889 {
26890 scope_sentinel ()
26891 {
26892 ++cp_unevaluated_operand;
26893 begin_scope (sk_block, NULL_TREE);
26894 }
26895
26896 ~scope_sentinel ()
26897 {
26898 pop_bindings_and_leave_scope ();
26899 --cp_unevaluated_operand;
26900 }
26901 } s;
26902
26903 /* Parse the optional parameter list. */
26904 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26905 {
26906 parms = cp_parser_requirement_parameter_list (parser);
26907 if (parms == error_mark_node)
26908 return error_mark_node;
26909 }
26910 else
26911 parms = NULL_TREE;
26912
26913 /* Parse the requirement body. */
26914 reqs = cp_parser_requirement_body (parser);
26915 if (reqs == error_mark_node)
26916 return error_mark_node;
26917 }
26918
26919 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26920 the parm chain. */
26921 grokparms (parms, &parms);
26922 return finish_requires_expr (parms, reqs);
26923 }
26924
26925 /* Parse a parameterized requirement.
26926
26927 requirement-parameter-list:
26928 '(' parameter-declaration-clause ')' */
26929 static tree
26930 cp_parser_requirement_parameter_list (cp_parser *parser)
26931 {
26932 matching_parens parens;
26933 if (!parens.require_open (parser))
26934 return error_mark_node;
26935
26936 tree parms
26937 = cp_parser_parameter_declaration_clause (parser, CP_PARSER_FLAGS_NONE);
26938
26939 if (!parens.require_close (parser))
26940 return error_mark_node;
26941
26942 return parms;
26943 }
26944
26945 /* Parse the body of a requirement.
26946
26947 requirement-body:
26948 '{' requirement-list '}' */
26949 static tree
26950 cp_parser_requirement_body (cp_parser *parser)
26951 {
26952 matching_braces braces;
26953 if (!braces.require_open (parser))
26954 return error_mark_node;
26955
26956 tree reqs = cp_parser_requirement_list (parser);
26957
26958 if (!braces.require_close (parser))
26959 return error_mark_node;
26960
26961 return reqs;
26962 }
26963
26964 /* Parse a list of requirements.
26965
26966 requirement-list:
26967 requirement
26968 requirement-list ';' requirement[opt] */
26969 static tree
26970 cp_parser_requirement_list (cp_parser *parser)
26971 {
26972 tree result = NULL_TREE;
26973 while (true)
26974 {
26975 tree req = cp_parser_requirement (parser);
26976 if (req == error_mark_node)
26977 return error_mark_node;
26978
26979 result = tree_cons (NULL_TREE, req, result);
26980
26981 /* If we see a semi-colon, consume it. */
26982 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26983 cp_lexer_consume_token (parser->lexer);
26984
26985 /* Stop processing at the end of the list. */
26986 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26987 break;
26988 }
26989
26990 /* Reverse the order of requirements so they are analyzed in
26991 declaration order. */
26992 return nreverse (result);
26993 }
26994
26995 /* Parse a syntactic requirement or type requirement.
26996
26997 requirement:
26998 simple-requirement
26999 compound-requirement
27000 type-requirement
27001 nested-requirement */
27002 static tree
27003 cp_parser_requirement (cp_parser *parser)
27004 {
27005 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27006 return cp_parser_compound_requirement (parser);
27007 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
27008 return cp_parser_type_requirement (parser);
27009 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
27010 return cp_parser_nested_requirement (parser);
27011 else
27012 return cp_parser_simple_requirement (parser);
27013 }
27014
27015 /* Parse a simple requirement.
27016
27017 simple-requirement:
27018 expression ';' */
27019 static tree
27020 cp_parser_simple_requirement (cp_parser *parser)
27021 {
27022 tree expr = cp_parser_expression (parser, NULL, false, false);
27023 if (!expr || expr == error_mark_node)
27024 return error_mark_node;
27025
27026 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
27027 return error_mark_node;
27028
27029 return finish_simple_requirement (expr);
27030 }
27031
27032 /* Parse a type requirement
27033
27034 type-requirement
27035 nested-name-specifier [opt] required-type-name ';'
27036
27037 required-type-name:
27038 type-name
27039 'template' [opt] simple-template-id */
27040 static tree
27041 cp_parser_type_requirement (cp_parser *parser)
27042 {
27043 cp_lexer_consume_token (parser->lexer);
27044
27045 // Save the scope before parsing name specifiers.
27046 tree saved_scope = parser->scope;
27047 tree saved_object_scope = parser->object_scope;
27048 tree saved_qualifying_scope = parser->qualifying_scope;
27049 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
27050 cp_parser_nested_name_specifier_opt (parser,
27051 /*typename_keyword_p=*/true,
27052 /*check_dependency_p=*/false,
27053 /*type_p=*/true,
27054 /*is_declaration=*/false);
27055
27056 tree type;
27057 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27058 {
27059 cp_lexer_consume_token (parser->lexer);
27060 type = cp_parser_template_id (parser,
27061 /*template_keyword_p=*/true,
27062 /*check_dependency=*/false,
27063 /*tag_type=*/none_type,
27064 /*is_declaration=*/false);
27065 type = make_typename_type (parser->scope, type, typename_type,
27066 /*complain=*/tf_error);
27067 }
27068 else
27069 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
27070
27071 if (TREE_CODE (type) == TYPE_DECL)
27072 type = TREE_TYPE (type);
27073
27074 parser->scope = saved_scope;
27075 parser->object_scope = saved_object_scope;
27076 parser->qualifying_scope = saved_qualifying_scope;
27077
27078 if (type == error_mark_node)
27079 cp_parser_skip_to_end_of_statement (parser);
27080
27081 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
27082 return error_mark_node;
27083 if (type == error_mark_node)
27084 return error_mark_node;
27085
27086 return finish_type_requirement (type);
27087 }
27088
27089 /* Parse a compound requirement
27090
27091 compound-requirement:
27092 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
27093 static tree
27094 cp_parser_compound_requirement (cp_parser *parser)
27095 {
27096 /* Parse an expression enclosed in '{ }'s. */
27097 matching_braces braces;
27098 if (!braces.require_open (parser))
27099 return error_mark_node;
27100
27101 tree expr = cp_parser_expression (parser, NULL, false, false);
27102 if (!expr || expr == error_mark_node)
27103 return error_mark_node;
27104
27105 if (!braces.require_close (parser))
27106 return error_mark_node;
27107
27108 /* Parse the optional noexcept. */
27109 bool noexcept_p = false;
27110 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
27111 {
27112 cp_lexer_consume_token (parser->lexer);
27113 noexcept_p = true;
27114 }
27115
27116 /* Parse the optional trailing return type. */
27117 tree type = NULL_TREE;
27118 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
27119 {
27120 cp_lexer_consume_token (parser->lexer);
27121 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
27122 parser->in_result_type_constraint_p = true;
27123 type = cp_parser_trailing_type_id (parser);
27124 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
27125 if (type == error_mark_node)
27126 return error_mark_node;
27127 }
27128
27129 return finish_compound_requirement (expr, type, noexcept_p);
27130 }
27131
27132 /* Parse a nested requirement. This is the same as a requires clause.
27133
27134 nested-requirement:
27135 requires-clause */
27136 static tree
27137 cp_parser_nested_requirement (cp_parser *parser)
27138 {
27139 cp_lexer_consume_token (parser->lexer);
27140 tree req = cp_parser_requires_clause (parser);
27141 if (req == error_mark_node)
27142 return error_mark_node;
27143 return finish_nested_requirement (req);
27144 }
27145
27146 /* Support Functions */
27147
27148 /* Return the appropriate prefer_type argument for lookup_name_real based on
27149 tag_type and template_mem_access. */
27150
27151 static inline int
27152 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
27153 {
27154 /* DR 141: When looking in the current enclosing context for a template-name
27155 after -> or ., only consider class templates. */
27156 if (template_mem_access)
27157 return 2;
27158 switch (tag_type)
27159 {
27160 case none_type: return 0; // No preference.
27161 case scope_type: return 1; // Type or namespace.
27162 default: return 2; // Type only.
27163 }
27164 }
27165
27166 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
27167 NAME should have one of the representations used for an
27168 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
27169 is returned. If PARSER->SCOPE is a dependent type, then a
27170 SCOPE_REF is returned.
27171
27172 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
27173 returned; the name was already resolved when the TEMPLATE_ID_EXPR
27174 was formed. Abstractly, such entities should not be passed to this
27175 function, because they do not need to be looked up, but it is
27176 simpler to check for this special case here, rather than at the
27177 call-sites.
27178
27179 In cases not explicitly covered above, this function returns a
27180 DECL, OVERLOAD, or baselink representing the result of the lookup.
27181 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
27182 is returned.
27183
27184 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
27185 (e.g., "struct") that was used. In that case bindings that do not
27186 refer to types are ignored.
27187
27188 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
27189 ignored.
27190
27191 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
27192 are ignored.
27193
27194 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
27195 types.
27196
27197 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
27198 TREE_LIST of candidates if name-lookup results in an ambiguity, and
27199 NULL_TREE otherwise. */
27200
27201 static cp_expr
27202 cp_parser_lookup_name (cp_parser *parser, tree name,
27203 enum tag_types tag_type,
27204 bool is_template,
27205 bool is_namespace,
27206 bool check_dependency,
27207 tree *ambiguous_decls,
27208 location_t name_location)
27209 {
27210 tree decl;
27211 tree object_type = parser->context->object_type;
27212
27213 /* Assume that the lookup will be unambiguous. */
27214 if (ambiguous_decls)
27215 *ambiguous_decls = NULL_TREE;
27216
27217 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
27218 no longer valid. Note that if we are parsing tentatively, and
27219 the parse fails, OBJECT_TYPE will be automatically restored. */
27220 parser->context->object_type = NULL_TREE;
27221
27222 if (name == error_mark_node)
27223 return error_mark_node;
27224
27225 /* A template-id has already been resolved; there is no lookup to
27226 do. */
27227 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
27228 return name;
27229 if (BASELINK_P (name))
27230 {
27231 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
27232 == TEMPLATE_ID_EXPR);
27233 return name;
27234 }
27235
27236 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
27237 it should already have been checked to make sure that the name
27238 used matches the type being destroyed. */
27239 if (TREE_CODE (name) == BIT_NOT_EXPR)
27240 {
27241 tree type;
27242
27243 /* Figure out to which type this destructor applies. */
27244 if (parser->scope)
27245 type = parser->scope;
27246 else if (object_type)
27247 type = object_type;
27248 else
27249 type = current_class_type;
27250 /* If that's not a class type, there is no destructor. */
27251 if (!type || !CLASS_TYPE_P (type))
27252 return error_mark_node;
27253
27254 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
27255 lazily_declare_fn (sfk_destructor, type);
27256
27257 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
27258 return dtor;
27259
27260 return error_mark_node;
27261 }
27262
27263 /* By this point, the NAME should be an ordinary identifier. If
27264 the id-expression was a qualified name, the qualifying scope is
27265 stored in PARSER->SCOPE at this point. */
27266 gcc_assert (identifier_p (name));
27267
27268 /* Perform the lookup. */
27269 if (parser->scope)
27270 {
27271 bool dependent_p;
27272
27273 if (parser->scope == error_mark_node)
27274 return error_mark_node;
27275
27276 /* If the SCOPE is dependent, the lookup must be deferred until
27277 the template is instantiated -- unless we are explicitly
27278 looking up names in uninstantiated templates. Even then, we
27279 cannot look up the name if the scope is not a class type; it
27280 might, for example, be a template type parameter. */
27281 dependent_p = (TYPE_P (parser->scope)
27282 && dependent_scope_p (parser->scope));
27283 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
27284 && dependent_p)
27285 /* Defer lookup. */
27286 decl = error_mark_node;
27287 else
27288 {
27289 tree pushed_scope = NULL_TREE;
27290
27291 /* If PARSER->SCOPE is a dependent type, then it must be a
27292 class type, and we must not be checking dependencies;
27293 otherwise, we would have processed this lookup above. So
27294 that PARSER->SCOPE is not considered a dependent base by
27295 lookup_member, we must enter the scope here. */
27296 if (dependent_p)
27297 pushed_scope = push_scope (parser->scope);
27298
27299 /* If the PARSER->SCOPE is a template specialization, it
27300 may be instantiated during name lookup. In that case,
27301 errors may be issued. Even if we rollback the current
27302 tentative parse, those errors are valid. */
27303 decl = lookup_qualified_name (parser->scope, name,
27304 prefer_type_arg (tag_type),
27305 /*complain=*/true);
27306
27307 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
27308 lookup result and the nested-name-specifier nominates a class C:
27309 * if the name specified after the nested-name-specifier, when
27310 looked up in C, is the injected-class-name of C (Clause 9), or
27311 * if the name specified after the nested-name-specifier is the
27312 same as the identifier or the simple-template-id's template-
27313 name in the last component of the nested-name-specifier,
27314 the name is instead considered to name the constructor of
27315 class C. [ Note: for example, the constructor is not an
27316 acceptable lookup result in an elaborated-type-specifier so
27317 the constructor would not be used in place of the
27318 injected-class-name. --end note ] Such a constructor name
27319 shall be used only in the declarator-id of a declaration that
27320 names a constructor or in a using-declaration. */
27321 if (tag_type == none_type
27322 && DECL_SELF_REFERENCE_P (decl)
27323 && same_type_p (DECL_CONTEXT (decl), parser->scope))
27324 decl = lookup_qualified_name (parser->scope, ctor_identifier,
27325 prefer_type_arg (tag_type),
27326 /*complain=*/true);
27327
27328 /* If we have a single function from a using decl, pull it out. */
27329 if (TREE_CODE (decl) == OVERLOAD
27330 && !really_overloaded_fn (decl))
27331 decl = OVL_FUNCTION (decl);
27332
27333 if (pushed_scope)
27334 pop_scope (pushed_scope);
27335 }
27336
27337 /* If the scope is a dependent type and either we deferred lookup or
27338 we did lookup but didn't find the name, rememeber the name. */
27339 if (decl == error_mark_node && TYPE_P (parser->scope)
27340 && dependent_type_p (parser->scope))
27341 {
27342 if (tag_type)
27343 {
27344 tree type;
27345
27346 /* The resolution to Core Issue 180 says that `struct
27347 A::B' should be considered a type-name, even if `A'
27348 is dependent. */
27349 type = make_typename_type (parser->scope, name, tag_type,
27350 /*complain=*/tf_error);
27351 if (type != error_mark_node)
27352 decl = TYPE_NAME (type);
27353 }
27354 else if (is_template
27355 && (cp_parser_next_token_ends_template_argument_p (parser)
27356 || cp_lexer_next_token_is (parser->lexer,
27357 CPP_CLOSE_PAREN)))
27358 decl = make_unbound_class_template (parser->scope,
27359 name, NULL_TREE,
27360 /*complain=*/tf_error);
27361 else
27362 decl = build_qualified_name (/*type=*/NULL_TREE,
27363 parser->scope, name,
27364 is_template);
27365 }
27366 parser->qualifying_scope = parser->scope;
27367 parser->object_scope = NULL_TREE;
27368 }
27369 else if (object_type)
27370 {
27371 /* Look up the name in the scope of the OBJECT_TYPE, unless the
27372 OBJECT_TYPE is not a class. */
27373 if (CLASS_TYPE_P (object_type))
27374 /* If the OBJECT_TYPE is a template specialization, it may
27375 be instantiated during name lookup. In that case, errors
27376 may be issued. Even if we rollback the current tentative
27377 parse, those errors are valid. */
27378 decl = lookup_member (object_type,
27379 name,
27380 /*protect=*/0,
27381 prefer_type_arg (tag_type),
27382 tf_warning_or_error);
27383 else
27384 decl = NULL_TREE;
27385
27386 if (!decl)
27387 /* Look it up in the enclosing context. DR 141: When looking for a
27388 template-name after -> or ., only consider class templates. */
27389 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
27390 /*nonclass=*/0,
27391 /*block_p=*/true, is_namespace, 0);
27392 if (object_type == unknown_type_node)
27393 /* The object is type-dependent, so we can't look anything up; we used
27394 this to get the DR 141 behavior. */
27395 object_type = NULL_TREE;
27396 parser->object_scope = object_type;
27397 parser->qualifying_scope = NULL_TREE;
27398 }
27399 else
27400 {
27401 decl = lookup_name_real (name, prefer_type_arg (tag_type),
27402 /*nonclass=*/0,
27403 /*block_p=*/true, is_namespace, 0);
27404 parser->qualifying_scope = NULL_TREE;
27405 parser->object_scope = NULL_TREE;
27406 }
27407
27408 /* If the lookup failed, let our caller know. */
27409 if (!decl || decl == error_mark_node)
27410 return error_mark_node;
27411
27412 /* Pull out the template from an injected-class-name (or multiple). */
27413 if (is_template)
27414 decl = maybe_get_template_decl_from_type_decl (decl);
27415
27416 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
27417 if (TREE_CODE (decl) == TREE_LIST)
27418 {
27419 if (ambiguous_decls)
27420 *ambiguous_decls = decl;
27421 /* The error message we have to print is too complicated for
27422 cp_parser_error, so we incorporate its actions directly. */
27423 if (!cp_parser_simulate_error (parser))
27424 {
27425 error_at (name_location, "reference to %qD is ambiguous",
27426 name);
27427 print_candidates (decl);
27428 }
27429 return error_mark_node;
27430 }
27431
27432 gcc_assert (DECL_P (decl)
27433 || TREE_CODE (decl) == OVERLOAD
27434 || TREE_CODE (decl) == SCOPE_REF
27435 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
27436 || BASELINK_P (decl));
27437
27438 /* If we have resolved the name of a member declaration, check to
27439 see if the declaration is accessible. When the name resolves to
27440 set of overloaded functions, accessibility is checked when
27441 overload resolution is done.
27442
27443 During an explicit instantiation, access is not checked at all,
27444 as per [temp.explicit]. */
27445 if (DECL_P (decl))
27446 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
27447
27448 maybe_record_typedef_use (decl);
27449
27450 return cp_expr (decl, name_location);
27451 }
27452
27453 /* Like cp_parser_lookup_name, but for use in the typical case where
27454 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
27455 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
27456
27457 static tree
27458 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
27459 {
27460 return cp_parser_lookup_name (parser, name,
27461 none_type,
27462 /*is_template=*/false,
27463 /*is_namespace=*/false,
27464 /*check_dependency=*/true,
27465 /*ambiguous_decls=*/NULL,
27466 location);
27467 }
27468
27469 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
27470 the current context, return the TYPE_DECL. If TAG_NAME_P is
27471 true, the DECL indicates the class being defined in a class-head,
27472 or declared in an elaborated-type-specifier.
27473
27474 Otherwise, return DECL. */
27475
27476 static tree
27477 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
27478 {
27479 /* If the TEMPLATE_DECL is being declared as part of a class-head,
27480 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
27481
27482 struct A {
27483 template <typename T> struct B;
27484 };
27485
27486 template <typename T> struct A::B {};
27487
27488 Similarly, in an elaborated-type-specifier:
27489
27490 namespace N { struct X{}; }
27491
27492 struct A {
27493 template <typename T> friend struct N::X;
27494 };
27495
27496 However, if the DECL refers to a class type, and we are in
27497 the scope of the class, then the name lookup automatically
27498 finds the TYPE_DECL created by build_self_reference rather
27499 than a TEMPLATE_DECL. For example, in:
27500
27501 template <class T> struct S {
27502 S s;
27503 };
27504
27505 there is no need to handle such case. */
27506
27507 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
27508 return DECL_TEMPLATE_RESULT (decl);
27509
27510 return decl;
27511 }
27512
27513 /* If too many, or too few, template-parameter lists apply to the
27514 declarator, issue an error message. Returns TRUE if all went well,
27515 and FALSE otherwise. */
27516
27517 static bool
27518 cp_parser_check_declarator_template_parameters (cp_parser* parser,
27519 cp_declarator *declarator,
27520 location_t declarator_location)
27521 {
27522 switch (declarator->kind)
27523 {
27524 case cdk_id:
27525 {
27526 unsigned num_templates = 0;
27527 tree scope = declarator->u.id.qualifying_scope;
27528 bool template_id_p = false;
27529
27530 if (scope)
27531 num_templates = num_template_headers_for_class (scope);
27532 else if (TREE_CODE (declarator->u.id.unqualified_name)
27533 == TEMPLATE_ID_EXPR)
27534 {
27535 /* If the DECLARATOR has the form `X<y>' then it uses one
27536 additional level of template parameters. */
27537 ++num_templates;
27538 template_id_p = true;
27539 }
27540
27541 return cp_parser_check_template_parameters
27542 (parser, num_templates, template_id_p, declarator_location,
27543 declarator);
27544 }
27545
27546 case cdk_function:
27547 case cdk_array:
27548 case cdk_pointer:
27549 case cdk_reference:
27550 case cdk_ptrmem:
27551 return (cp_parser_check_declarator_template_parameters
27552 (parser, declarator->declarator, declarator_location));
27553
27554 case cdk_decomp:
27555 case cdk_error:
27556 return true;
27557
27558 default:
27559 gcc_unreachable ();
27560 }
27561 return false;
27562 }
27563
27564 /* NUM_TEMPLATES were used in the current declaration. If that is
27565 invalid, return FALSE and issue an error messages. Otherwise,
27566 return TRUE. If DECLARATOR is non-NULL, then we are checking a
27567 declarator and we can print more accurate diagnostics. */
27568
27569 static bool
27570 cp_parser_check_template_parameters (cp_parser* parser,
27571 unsigned num_templates,
27572 bool template_id_p,
27573 location_t location,
27574 cp_declarator *declarator)
27575 {
27576 /* If there are the same number of template classes and parameter
27577 lists, that's OK. */
27578 if (parser->num_template_parameter_lists == num_templates)
27579 return true;
27580 /* If there are more, but only one more, and the name ends in an identifier,
27581 then we are declaring a primary template. That's OK too. */
27582 if (!template_id_p
27583 && parser->num_template_parameter_lists == num_templates + 1)
27584 return true;
27585 /* If there are more template classes than parameter lists, we have
27586 something like:
27587
27588 template <class T> void S<T>::R<T>::f (); */
27589 if (parser->num_template_parameter_lists < num_templates)
27590 {
27591 if (declarator && !current_function_decl)
27592 error_at (location, "specializing member %<%T::%E%> "
27593 "requires %<template<>%> syntax",
27594 declarator->u.id.qualifying_scope,
27595 declarator->u.id.unqualified_name);
27596 else if (declarator)
27597 error_at (location, "invalid declaration of %<%T::%E%>",
27598 declarator->u.id.qualifying_scope,
27599 declarator->u.id.unqualified_name);
27600 else
27601 error_at (location, "too few template-parameter-lists");
27602 return false;
27603 }
27604 /* Otherwise, there are too many template parameter lists. We have
27605 something like:
27606
27607 template <class T> template <class U> void S::f(); */
27608 error_at (location, "too many template-parameter-lists");
27609 return false;
27610 }
27611
27612 /* Parse an optional `::' token indicating that the following name is
27613 from the global namespace. If so, PARSER->SCOPE is set to the
27614 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
27615 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
27616 Returns the new value of PARSER->SCOPE, if the `::' token is
27617 present, and NULL_TREE otherwise. */
27618
27619 static tree
27620 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
27621 {
27622 cp_token *token;
27623
27624 /* Peek at the next token. */
27625 token = cp_lexer_peek_token (parser->lexer);
27626 /* If we're looking at a `::' token then we're starting from the
27627 global namespace, not our current location. */
27628 if (token->type == CPP_SCOPE)
27629 {
27630 /* Consume the `::' token. */
27631 cp_lexer_consume_token (parser->lexer);
27632 /* Set the SCOPE so that we know where to start the lookup. */
27633 parser->scope = global_namespace;
27634 parser->qualifying_scope = global_namespace;
27635 parser->object_scope = NULL_TREE;
27636
27637 return parser->scope;
27638 }
27639 else if (!current_scope_valid_p)
27640 {
27641 parser->scope = NULL_TREE;
27642 parser->qualifying_scope = NULL_TREE;
27643 parser->object_scope = NULL_TREE;
27644 }
27645
27646 return NULL_TREE;
27647 }
27648
27649 /* Returns TRUE if the upcoming token sequence is the start of a
27650 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27651 declarator is preceded by the `friend' specifier. The parser flags FLAGS
27652 is used to control type-specifier parsing. */
27653
27654 static bool
27655 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
27656 bool friend_p)
27657 {
27658 bool constructor_p;
27659 bool outside_class_specifier_p;
27660 tree nested_name_specifier;
27661 cp_token *next_token;
27662
27663 /* The common case is that this is not a constructor declarator, so
27664 try to avoid doing lots of work if at all possible. It's not
27665 valid declare a constructor at function scope. */
27666 if (parser->in_function_body)
27667 return false;
27668 /* And only certain tokens can begin a constructor declarator. */
27669 next_token = cp_lexer_peek_token (parser->lexer);
27670 if (next_token->type != CPP_NAME
27671 && next_token->type != CPP_SCOPE
27672 && next_token->type != CPP_NESTED_NAME_SPECIFIER
27673 && next_token->type != CPP_TEMPLATE_ID)
27674 return false;
27675
27676 /* Parse tentatively; we are going to roll back all of the tokens
27677 consumed here. */
27678 cp_parser_parse_tentatively (parser);
27679 /* Assume that we are looking at a constructor declarator. */
27680 constructor_p = true;
27681
27682 /* Look for the optional `::' operator. */
27683 cp_parser_global_scope_opt (parser,
27684 /*current_scope_valid_p=*/false);
27685 /* Look for the nested-name-specifier. */
27686 nested_name_specifier
27687 = (cp_parser_nested_name_specifier_opt (parser,
27688 /*typename_keyword_p=*/false,
27689 /*check_dependency_p=*/false,
27690 /*type_p=*/false,
27691 /*is_declaration=*/false));
27692
27693 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
27694 if (nested_name_specifier
27695 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
27696 {
27697 tree s = resolve_typename_type (nested_name_specifier,
27698 /*only_current_p=*/false);
27699 if (TREE_CODE (s) != TYPENAME_TYPE)
27700 nested_name_specifier = s;
27701 }
27702
27703 outside_class_specifier_p = (!at_class_scope_p ()
27704 || !TYPE_BEING_DEFINED (current_class_type)
27705 || friend_p);
27706
27707 /* Outside of a class-specifier, there must be a
27708 nested-name-specifier. Except in C++17 mode, where we
27709 might be declaring a guiding declaration. */
27710 if (!nested_name_specifier && outside_class_specifier_p
27711 && cxx_dialect < cxx17)
27712 constructor_p = false;
27713 else if (nested_name_specifier == error_mark_node)
27714 constructor_p = false;
27715
27716 /* If we have a class scope, this is easy; DR 147 says that S::S always
27717 names the constructor, and no other qualified name could. */
27718 if (constructor_p && nested_name_specifier
27719 && CLASS_TYPE_P (nested_name_specifier))
27720 {
27721 tree id = cp_parser_unqualified_id (parser,
27722 /*template_keyword_p=*/false,
27723 /*check_dependency_p=*/false,
27724 /*declarator_p=*/true,
27725 /*optional_p=*/false);
27726 if (is_overloaded_fn (id))
27727 id = DECL_NAME (get_first_fn (id));
27728 if (!constructor_name_p (id, nested_name_specifier))
27729 constructor_p = false;
27730 }
27731 /* If we still think that this might be a constructor-declarator,
27732 look for a class-name. */
27733 else if (constructor_p)
27734 {
27735 /* If we have:
27736
27737 template <typename T> struct S {
27738 S();
27739 };
27740
27741 we must recognize that the nested `S' names a class. */
27742 if (cxx_dialect >= cxx17)
27743 cp_parser_parse_tentatively (parser);
27744
27745 tree type_decl;
27746 type_decl = cp_parser_class_name (parser,
27747 /*typename_keyword_p=*/false,
27748 /*template_keyword_p=*/false,
27749 none_type,
27750 /*check_dependency_p=*/false,
27751 /*class_head_p=*/false,
27752 /*is_declaration=*/false);
27753
27754 if (cxx_dialect >= cxx17
27755 && !cp_parser_parse_definitely (parser))
27756 {
27757 type_decl = NULL_TREE;
27758 tree tmpl = cp_parser_template_name (parser,
27759 /*template_keyword*/false,
27760 /*check_dependency_p*/false,
27761 /*is_declaration*/false,
27762 none_type,
27763 /*is_identifier*/NULL);
27764 if (DECL_CLASS_TEMPLATE_P (tmpl)
27765 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27766 /* It's a deduction guide, return true. */;
27767 else
27768 cp_parser_simulate_error (parser);
27769 }
27770
27771 /* If there was no class-name, then this is not a constructor.
27772 Otherwise, if we are in a class-specifier and we aren't
27773 handling a friend declaration, check that its type matches
27774 current_class_type (c++/38313). Note: error_mark_node
27775 is left alone for error recovery purposes. */
27776 constructor_p = (!cp_parser_error_occurred (parser)
27777 && (outside_class_specifier_p
27778 || type_decl == NULL_TREE
27779 || type_decl == error_mark_node
27780 || same_type_p (current_class_type,
27781 TREE_TYPE (type_decl))));
27782
27783 /* If we're still considering a constructor, we have to see a `(',
27784 to begin the parameter-declaration-clause, followed by either a
27785 `)', an `...', or a decl-specifier. We need to check for a
27786 type-specifier to avoid being fooled into thinking that:
27787
27788 S (f) (int);
27789
27790 is a constructor. (It is actually a function named `f' that
27791 takes one parameter (of type `int') and returns a value of type
27792 `S'. */
27793 if (constructor_p
27794 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27795 constructor_p = false;
27796
27797 if (constructor_p
27798 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
27799 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
27800 /* A parameter declaration begins with a decl-specifier,
27801 which is either the "attribute" keyword, a storage class
27802 specifier, or (usually) a type-specifier. */
27803 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
27804 {
27805 tree type;
27806 tree pushed_scope = NULL_TREE;
27807 unsigned saved_num_template_parameter_lists;
27808
27809 /* Names appearing in the type-specifier should be looked up
27810 in the scope of the class. */
27811 if (current_class_type)
27812 type = NULL_TREE;
27813 else if (type_decl)
27814 {
27815 type = TREE_TYPE (type_decl);
27816 if (TREE_CODE (type) == TYPENAME_TYPE)
27817 {
27818 type = resolve_typename_type (type,
27819 /*only_current_p=*/false);
27820 if (TREE_CODE (type) == TYPENAME_TYPE)
27821 {
27822 cp_parser_abort_tentative_parse (parser);
27823 return false;
27824 }
27825 }
27826 pushed_scope = push_scope (type);
27827 }
27828
27829 /* Inside the constructor parameter list, surrounding
27830 template-parameter-lists do not apply. */
27831 saved_num_template_parameter_lists
27832 = parser->num_template_parameter_lists;
27833 parser->num_template_parameter_lists = 0;
27834
27835 /* Look for the type-specifier. It's not optional, but its typename
27836 might be. Unless this is a friend declaration; we don't want to
27837 treat
27838
27839 friend S (T::fn)(int);
27840
27841 as a constructor, but with P0634, we might assume a type when
27842 looking for the type-specifier. It is actually a function named
27843 `T::fn' that takes one parameter (of type `int') and returns a
27844 value of type `S'. Constructors can be friends, but they must
27845 use a qualified name. */
27846 cp_parser_type_specifier (parser,
27847 (friend_p ? CP_PARSER_FLAGS_NONE
27848 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
27849 /*decl_specs=*/NULL,
27850 /*is_declarator=*/true,
27851 /*declares_class_or_enum=*/NULL,
27852 /*is_cv_qualifier=*/NULL);
27853
27854 parser->num_template_parameter_lists
27855 = saved_num_template_parameter_lists;
27856
27857 /* Leave the scope of the class. */
27858 if (pushed_scope)
27859 pop_scope (pushed_scope);
27860
27861 constructor_p = !cp_parser_error_occurred (parser);
27862 }
27863 }
27864
27865 /* We did not really want to consume any tokens. */
27866 cp_parser_abort_tentative_parse (parser);
27867
27868 return constructor_p;
27869 }
27870
27871 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27872 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27873 they must be performed once we are in the scope of the function.
27874
27875 Returns the function defined. */
27876
27877 static tree
27878 cp_parser_function_definition_from_specifiers_and_declarator
27879 (cp_parser* parser,
27880 cp_decl_specifier_seq *decl_specifiers,
27881 tree attributes,
27882 const cp_declarator *declarator)
27883 {
27884 tree fn;
27885 bool success_p;
27886
27887 /* Begin the function-definition. */
27888 success_p = start_function (decl_specifiers, declarator, attributes);
27889
27890 /* The things we're about to see are not directly qualified by any
27891 template headers we've seen thus far. */
27892 reset_specialization ();
27893
27894 /* If there were names looked up in the decl-specifier-seq that we
27895 did not check, check them now. We must wait until we are in the
27896 scope of the function to perform the checks, since the function
27897 might be a friend. */
27898 perform_deferred_access_checks (tf_warning_or_error);
27899
27900 if (success_p)
27901 {
27902 cp_finalize_omp_declare_simd (parser, current_function_decl);
27903 parser->omp_declare_simd = NULL;
27904 cp_finalize_oacc_routine (parser, current_function_decl, true);
27905 parser->oacc_routine = NULL;
27906 }
27907
27908 if (!success_p)
27909 {
27910 /* Skip the entire function. */
27911 cp_parser_skip_to_end_of_block_or_statement (parser);
27912 fn = error_mark_node;
27913 }
27914 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27915 {
27916 /* Seen already, skip it. An error message has already been output. */
27917 cp_parser_skip_to_end_of_block_or_statement (parser);
27918 fn = current_function_decl;
27919 current_function_decl = NULL_TREE;
27920 /* If this is a function from a class, pop the nested class. */
27921 if (current_class_name)
27922 pop_nested_class ();
27923 }
27924 else
27925 {
27926 timevar_id_t tv;
27927 if (DECL_DECLARED_INLINE_P (current_function_decl))
27928 tv = TV_PARSE_INLINE;
27929 else
27930 tv = TV_PARSE_FUNC;
27931 timevar_push (tv);
27932 fn = cp_parser_function_definition_after_declarator (parser,
27933 /*inline_p=*/false);
27934 timevar_pop (tv);
27935 }
27936
27937 return fn;
27938 }
27939
27940 /* Parse the part of a function-definition that follows the
27941 declarator. INLINE_P is TRUE iff this function is an inline
27942 function defined within a class-specifier.
27943
27944 Returns the function defined. */
27945
27946 static tree
27947 cp_parser_function_definition_after_declarator (cp_parser* parser,
27948 bool inline_p)
27949 {
27950 tree fn;
27951 bool saved_in_unbraced_linkage_specification_p;
27952 bool saved_in_function_body;
27953 unsigned saved_num_template_parameter_lists;
27954 cp_token *token;
27955 bool fully_implicit_function_template_p
27956 = parser->fully_implicit_function_template_p;
27957 parser->fully_implicit_function_template_p = false;
27958 tree implicit_template_parms
27959 = parser->implicit_template_parms;
27960 parser->implicit_template_parms = 0;
27961 cp_binding_level* implicit_template_scope
27962 = parser->implicit_template_scope;
27963 parser->implicit_template_scope = 0;
27964
27965 saved_in_function_body = parser->in_function_body;
27966 parser->in_function_body = true;
27967 /* If the next token is `return', then the code may be trying to
27968 make use of the "named return value" extension that G++ used to
27969 support. */
27970 token = cp_lexer_peek_token (parser->lexer);
27971 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27972 {
27973 /* Consume the `return' keyword. */
27974 cp_lexer_consume_token (parser->lexer);
27975 /* Look for the identifier that indicates what value is to be
27976 returned. */
27977 cp_parser_identifier (parser);
27978 /* Issue an error message. */
27979 error_at (token->location,
27980 "named return values are no longer supported");
27981 /* Skip tokens until we reach the start of the function body. */
27982 while (true)
27983 {
27984 cp_token *token = cp_lexer_peek_token (parser->lexer);
27985 if (token->type == CPP_OPEN_BRACE
27986 || token->type == CPP_EOF
27987 || token->type == CPP_PRAGMA_EOL)
27988 break;
27989 cp_lexer_consume_token (parser->lexer);
27990 }
27991 }
27992 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27993 anything declared inside `f'. */
27994 saved_in_unbraced_linkage_specification_p
27995 = parser->in_unbraced_linkage_specification_p;
27996 parser->in_unbraced_linkage_specification_p = false;
27997 /* Inside the function, surrounding template-parameter-lists do not
27998 apply. */
27999 saved_num_template_parameter_lists
28000 = parser->num_template_parameter_lists;
28001 parser->num_template_parameter_lists = 0;
28002
28003 /* If the next token is `try', `__transaction_atomic', or
28004 `__transaction_relaxed`, then we are looking at either function-try-block
28005 or function-transaction-block. Note that all of these include the
28006 function-body. */
28007 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
28008 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
28009 else if (cp_lexer_next_token_is_keyword (parser->lexer,
28010 RID_TRANSACTION_RELAXED))
28011 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
28012 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28013 cp_parser_function_try_block (parser);
28014 else
28015 cp_parser_ctor_initializer_opt_and_function_body
28016 (parser, /*in_function_try_block=*/false);
28017
28018 /* Finish the function. */
28019 fn = finish_function (inline_p);
28020 /* Generate code for it, if necessary. */
28021 expand_or_defer_fn (fn);
28022 /* Restore the saved values. */
28023 parser->in_unbraced_linkage_specification_p
28024 = saved_in_unbraced_linkage_specification_p;
28025 parser->num_template_parameter_lists
28026 = saved_num_template_parameter_lists;
28027 parser->in_function_body = saved_in_function_body;
28028
28029 parser->fully_implicit_function_template_p
28030 = fully_implicit_function_template_p;
28031 parser->implicit_template_parms
28032 = implicit_template_parms;
28033 parser->implicit_template_scope
28034 = implicit_template_scope;
28035
28036 if (parser->fully_implicit_function_template_p)
28037 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
28038
28039 return fn;
28040 }
28041
28042 /* Parse a template-declaration body (following argument list). */
28043
28044 static void
28045 cp_parser_template_declaration_after_parameters (cp_parser* parser,
28046 tree parameter_list,
28047 bool member_p)
28048 {
28049 tree decl = NULL_TREE;
28050 bool friend_p = false;
28051
28052 /* We just processed one more parameter list. */
28053 ++parser->num_template_parameter_lists;
28054
28055 /* Get the deferred access checks from the parameter list. These
28056 will be checked once we know what is being declared, as for a
28057 member template the checks must be performed in the scope of the
28058 class containing the member. */
28059 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
28060
28061 /* Tentatively parse for a new template parameter list, which can either be
28062 the template keyword or a template introduction. */
28063 if (cp_parser_template_declaration_after_export (parser, member_p))
28064 /* OK */;
28065 else if (cxx_dialect >= cxx11
28066 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
28067 decl = cp_parser_alias_declaration (parser);
28068 else
28069 {
28070 /* There are no access checks when parsing a template, as we do not
28071 know if a specialization will be a friend. */
28072 push_deferring_access_checks (dk_no_check);
28073 cp_token *token = cp_lexer_peek_token (parser->lexer);
28074 decl = cp_parser_single_declaration (parser,
28075 checks,
28076 member_p,
28077 /*explicit_specialization_p=*/false,
28078 &friend_p);
28079 pop_deferring_access_checks ();
28080
28081 /* If this is a member template declaration, let the front
28082 end know. */
28083 if (member_p && !friend_p && decl)
28084 {
28085 if (TREE_CODE (decl) == TYPE_DECL)
28086 cp_parser_check_access_in_redeclaration (decl, token->location);
28087
28088 decl = finish_member_template_decl (decl);
28089 }
28090 else if (friend_p && decl
28091 && DECL_DECLARES_TYPE_P (decl))
28092 make_friend_class (current_class_type, TREE_TYPE (decl),
28093 /*complain=*/true);
28094 }
28095 /* We are done with the current parameter list. */
28096 --parser->num_template_parameter_lists;
28097
28098 pop_deferring_access_checks ();
28099
28100 /* Finish up. */
28101 finish_template_decl (parameter_list);
28102
28103 /* Check the template arguments for a literal operator template. */
28104 if (decl
28105 && DECL_DECLARES_FUNCTION_P (decl)
28106 && UDLIT_OPER_P (DECL_NAME (decl)))
28107 {
28108 bool ok = true;
28109 if (parameter_list == NULL_TREE)
28110 ok = false;
28111 else
28112 {
28113 int num_parms = TREE_VEC_LENGTH (parameter_list);
28114 if (num_parms == 1)
28115 {
28116 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
28117 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
28118 if (CLASS_TYPE_P (TREE_TYPE (parm)))
28119 /* OK, C++20 string literal operator template. We don't need
28120 to warn in lower dialects here because we will have already
28121 warned about the template parameter. */;
28122 else if (TREE_TYPE (parm) != char_type_node
28123 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
28124 ok = false;
28125 }
28126 else if (num_parms == 2 && cxx_dialect >= cxx14)
28127 {
28128 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
28129 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
28130 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
28131 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
28132 if (parm == error_mark_node
28133 || TREE_TYPE (parm) != TREE_TYPE (type)
28134 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
28135 ok = false;
28136 else
28137 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
28138 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
28139 "ISO C++ did not adopt string literal operator templa"
28140 "tes taking an argument pack of characters");
28141 }
28142 else
28143 ok = false;
28144 }
28145 if (!ok)
28146 {
28147 if (cxx_dialect > cxx17)
28148 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
28149 "template %qD has invalid parameter list; expected "
28150 "non-type template parameter pack %<<char...>%> or "
28151 "single non-type parameter of class type",
28152 decl);
28153 else
28154 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
28155 "template %qD has invalid parameter list; expected "
28156 "non-type template parameter pack %<<char...>%>",
28157 decl);
28158 }
28159 }
28160
28161 /* Register member declarations. */
28162 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
28163 finish_member_declaration (decl);
28164 /* If DECL is a function template, we must return to parse it later.
28165 (Even though there is no definition, there might be default
28166 arguments that need handling.) */
28167 if (member_p && decl
28168 && DECL_DECLARES_FUNCTION_P (decl))
28169 vec_safe_push (unparsed_funs_with_definitions, decl);
28170 }
28171
28172 /* Parse a template introduction header for a template-declaration. Returns
28173 false if tentative parse fails. */
28174
28175 static bool
28176 cp_parser_template_introduction (cp_parser* parser, bool member_p)
28177 {
28178 cp_parser_parse_tentatively (parser);
28179
28180 tree saved_scope = parser->scope;
28181 tree saved_object_scope = parser->object_scope;
28182 tree saved_qualifying_scope = parser->qualifying_scope;
28183
28184 /* Look for the optional `::' operator. */
28185 cp_parser_global_scope_opt (parser,
28186 /*current_scope_valid_p=*/false);
28187 /* Look for the nested-name-specifier. */
28188 cp_parser_nested_name_specifier_opt (parser,
28189 /*typename_keyword_p=*/false,
28190 /*check_dependency_p=*/true,
28191 /*type_p=*/false,
28192 /*is_declaration=*/false);
28193
28194 cp_token *token = cp_lexer_peek_token (parser->lexer);
28195 tree concept_name = cp_parser_identifier (parser);
28196
28197 /* Look up the concept for which we will be matching
28198 template parameters. */
28199 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
28200 token->location);
28201 parser->scope = saved_scope;
28202 parser->object_scope = saved_object_scope;
28203 parser->qualifying_scope = saved_qualifying_scope;
28204
28205 if (concept_name == error_mark_node)
28206 cp_parser_simulate_error (parser);
28207
28208 /* Look for opening brace for introduction. */
28209 matching_braces braces;
28210 braces.require_open (parser);
28211
28212 if (!cp_parser_parse_definitely (parser))
28213 return false;
28214
28215 push_deferring_access_checks (dk_deferred);
28216
28217 /* Build vector of placeholder parameters and grab
28218 matching identifiers. */
28219 tree introduction_list = cp_parser_introduction_list (parser);
28220
28221 /* Look for closing brace for introduction. */
28222 if (!braces.require_close (parser))
28223 return true;
28224
28225 /* The introduction-list shall not be empty. */
28226 int nargs = TREE_VEC_LENGTH (introduction_list);
28227 if (nargs == 0)
28228 {
28229 /* In cp_parser_introduction_list we have already issued an error. */
28230 return true;
28231 }
28232
28233 if (tmpl_decl == error_mark_node)
28234 {
28235 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
28236 token->location);
28237 return true;
28238 }
28239
28240 /* Build and associate the constraint. */
28241 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
28242 if (parms && parms != error_mark_node)
28243 {
28244 cp_parser_template_declaration_after_parameters (parser, parms,
28245 member_p);
28246 return true;
28247 }
28248
28249 error_at (token->location, "no matching concept for template-introduction");
28250 return true;
28251 }
28252
28253 /* Parse a normal template-declaration following the template keyword. */
28254
28255 static void
28256 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
28257 {
28258 tree parameter_list;
28259 bool need_lang_pop;
28260 location_t location = input_location;
28261
28262 /* Look for the `<' token. */
28263 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
28264 return;
28265 if (at_class_scope_p () && current_function_decl)
28266 {
28267 /* 14.5.2.2 [temp.mem]
28268
28269 A local class shall not have member templates. */
28270 error_at (location,
28271 "invalid declaration of member template in local class");
28272 cp_parser_skip_to_end_of_block_or_statement (parser);
28273 return;
28274 }
28275 /* [temp]
28276
28277 A template ... shall not have C linkage. */
28278 if (current_lang_name == lang_name_c)
28279 {
28280 error_at (location, "template with C linkage");
28281 maybe_show_extern_c_location ();
28282 /* Give it C++ linkage to avoid confusing other parts of the
28283 front end. */
28284 push_lang_context (lang_name_cplusplus);
28285 need_lang_pop = true;
28286 }
28287 else
28288 need_lang_pop = false;
28289
28290 /* We cannot perform access checks on the template parameter
28291 declarations until we know what is being declared, just as we
28292 cannot check the decl-specifier list. */
28293 push_deferring_access_checks (dk_deferred);
28294
28295 /* If the next token is `>', then we have an invalid
28296 specialization. Rather than complain about an invalid template
28297 parameter, issue an error message here. */
28298 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
28299 {
28300 cp_parser_error (parser, "invalid explicit specialization");
28301 begin_specialization ();
28302 parameter_list = NULL_TREE;
28303 }
28304 else
28305 {
28306 /* Parse the template parameters. */
28307 parameter_list = cp_parser_template_parameter_list (parser);
28308 }
28309
28310 /* Look for the `>'. */
28311 cp_parser_skip_to_end_of_template_parameter_list (parser);
28312
28313 /* Manage template requirements */
28314 if (flag_concepts)
28315 {
28316 tree reqs = get_shorthand_constraints (current_template_parms);
28317 if (tree r = cp_parser_requires_clause_opt (parser))
28318 reqs = conjoin_constraints (reqs, normalize_expression (r));
28319 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
28320 }
28321
28322 cp_parser_template_declaration_after_parameters (parser, parameter_list,
28323 member_p);
28324
28325 /* For the erroneous case of a template with C linkage, we pushed an
28326 implicit C++ linkage scope; exit that scope now. */
28327 if (need_lang_pop)
28328 pop_lang_context ();
28329 }
28330
28331 /* Parse a template-declaration, assuming that the `export' (and
28332 `extern') keywords, if present, has already been scanned. MEMBER_P
28333 is as for cp_parser_template_declaration. */
28334
28335 static bool
28336 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
28337 {
28338 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28339 {
28340 cp_lexer_consume_token (parser->lexer);
28341 cp_parser_explicit_template_declaration (parser, member_p);
28342 return true;
28343 }
28344 else if (flag_concepts)
28345 return cp_parser_template_introduction (parser, member_p);
28346
28347 return false;
28348 }
28349
28350 /* Perform the deferred access checks from a template-parameter-list.
28351 CHECKS is a TREE_LIST of access checks, as returned by
28352 get_deferred_access_checks. */
28353
28354 static void
28355 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
28356 {
28357 ++processing_template_parmlist;
28358 perform_access_checks (checks, tf_warning_or_error);
28359 --processing_template_parmlist;
28360 }
28361
28362 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
28363 `function-definition' sequence that follows a template header.
28364 If MEMBER_P is true, this declaration appears in a class scope.
28365
28366 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
28367 *FRIEND_P is set to TRUE iff the declaration is a friend. */
28368
28369 static tree
28370 cp_parser_single_declaration (cp_parser* parser,
28371 vec<deferred_access_check, va_gc> *checks,
28372 bool member_p,
28373 bool explicit_specialization_p,
28374 bool* friend_p)
28375 {
28376 int declares_class_or_enum;
28377 tree decl = NULL_TREE;
28378 cp_decl_specifier_seq decl_specifiers;
28379 bool function_definition_p = false;
28380 cp_token *decl_spec_token_start;
28381
28382 /* This function is only used when processing a template
28383 declaration. */
28384 gcc_assert (innermost_scope_kind () == sk_template_parms
28385 || innermost_scope_kind () == sk_template_spec);
28386
28387 /* Defer access checks until we know what is being declared. */
28388 push_deferring_access_checks (dk_deferred);
28389
28390 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
28391 alternative. */
28392 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
28393 cp_parser_decl_specifier_seq (parser,
28394 (CP_PARSER_FLAGS_OPTIONAL
28395 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
28396 &decl_specifiers,
28397 &declares_class_or_enum);
28398 if (friend_p)
28399 *friend_p = cp_parser_friend_p (&decl_specifiers);
28400
28401 /* There are no template typedefs. */
28402 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
28403 {
28404 error_at (decl_spec_token_start->location,
28405 "template declaration of %<typedef%>");
28406 decl = error_mark_node;
28407 }
28408
28409 /* Gather up the access checks that occurred the
28410 decl-specifier-seq. */
28411 stop_deferring_access_checks ();
28412
28413 /* Check for the declaration of a template class. */
28414 if (declares_class_or_enum)
28415 {
28416 if (cp_parser_declares_only_class_p (parser)
28417 || (declares_class_or_enum & 2))
28418 {
28419 // If this is a declaration, but not a definition, associate
28420 // any constraints with the type declaration. Constraints
28421 // are associated with definitions in cp_parser_class_specifier.
28422 if (declares_class_or_enum == 1)
28423 associate_classtype_constraints (decl_specifiers.type);
28424
28425 decl = shadow_tag (&decl_specifiers);
28426
28427 /* In this case:
28428
28429 struct C {
28430 friend template <typename T> struct A<T>::B;
28431 };
28432
28433 A<T>::B will be represented by a TYPENAME_TYPE, and
28434 therefore not recognized by shadow_tag. */
28435 if (friend_p && *friend_p
28436 && !decl
28437 && decl_specifiers.type
28438 && TYPE_P (decl_specifiers.type))
28439 decl = decl_specifiers.type;
28440
28441 if (decl && decl != error_mark_node)
28442 decl = TYPE_NAME (decl);
28443 else
28444 decl = error_mark_node;
28445
28446 /* Perform access checks for template parameters. */
28447 cp_parser_perform_template_parameter_access_checks (checks);
28448
28449 /* Give a helpful diagnostic for
28450 template <class T> struct A { } a;
28451 if we aren't already recovering from an error. */
28452 if (!cp_parser_declares_only_class_p (parser)
28453 && !seen_error ())
28454 {
28455 error_at (cp_lexer_peek_token (parser->lexer)->location,
28456 "a class template declaration must not declare "
28457 "anything else");
28458 cp_parser_skip_to_end_of_block_or_statement (parser);
28459 goto out;
28460 }
28461 }
28462 }
28463
28464 /* Complain about missing 'typename' or other invalid type names. */
28465 if (!decl_specifiers.any_type_specifiers_p
28466 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
28467 {
28468 /* cp_parser_parse_and_diagnose_invalid_type_name calls
28469 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
28470 the rest of this declaration. */
28471 decl = error_mark_node;
28472 goto out;
28473 }
28474
28475 /* If it's not a template class, try for a template function. If
28476 the next token is a `;', then this declaration does not declare
28477 anything. But, if there were errors in the decl-specifiers, then
28478 the error might well have come from an attempted class-specifier.
28479 In that case, there's no need to warn about a missing declarator. */
28480 if (!decl
28481 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
28482 || decl_specifiers.type != error_mark_node))
28483 {
28484 decl = cp_parser_init_declarator (parser,
28485 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
28486 &decl_specifiers,
28487 checks,
28488 /*function_definition_allowed_p=*/true,
28489 member_p,
28490 declares_class_or_enum,
28491 &function_definition_p,
28492 NULL, NULL, NULL);
28493
28494 /* 7.1.1-1 [dcl.stc]
28495
28496 A storage-class-specifier shall not be specified in an explicit
28497 specialization... */
28498 if (decl
28499 && explicit_specialization_p
28500 && decl_specifiers.storage_class != sc_none)
28501 {
28502 error_at (decl_spec_token_start->location,
28503 "explicit template specialization cannot have a storage class");
28504 decl = error_mark_node;
28505 }
28506
28507 if (decl && VAR_P (decl))
28508 check_template_variable (decl);
28509 }
28510
28511 /* Look for a trailing `;' after the declaration. */
28512 if (!function_definition_p
28513 && (decl == error_mark_node
28514 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
28515 cp_parser_skip_to_end_of_block_or_statement (parser);
28516
28517 out:
28518 pop_deferring_access_checks ();
28519
28520 /* Clear any current qualification; whatever comes next is the start
28521 of something new. */
28522 parser->scope = NULL_TREE;
28523 parser->qualifying_scope = NULL_TREE;
28524 parser->object_scope = NULL_TREE;
28525
28526 return decl;
28527 }
28528
28529 /* Parse a cast-expression that is not the operand of a unary "&". */
28530
28531 static cp_expr
28532 cp_parser_simple_cast_expression (cp_parser *parser)
28533 {
28534 return cp_parser_cast_expression (parser, /*address_p=*/false,
28535 /*cast_p=*/false, /*decltype*/false, NULL);
28536 }
28537
28538 /* Parse a functional cast to TYPE. Returns an expression
28539 representing the cast. */
28540
28541 static cp_expr
28542 cp_parser_functional_cast (cp_parser* parser, tree type)
28543 {
28544 vec<tree, va_gc> *vec;
28545 tree expression_list;
28546 cp_expr cast;
28547 bool nonconst_p;
28548
28549 location_t start_loc = input_location;
28550
28551 if (!type)
28552 type = error_mark_node;
28553
28554 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28555 {
28556 cp_lexer_set_source_position (parser->lexer);
28557 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28558 expression_list = cp_parser_braced_list (parser, &nonconst_p);
28559 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
28560 if (TREE_CODE (type) == TYPE_DECL)
28561 type = TREE_TYPE (type);
28562
28563 cast = finish_compound_literal (type, expression_list,
28564 tf_warning_or_error, fcl_functional);
28565 /* Create a location of the form:
28566 type_name{i, f}
28567 ^~~~~~~~~~~~~~~
28568 with caret == start at the start of the type name,
28569 finishing at the closing brace. */
28570 location_t finish_loc
28571 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28572 location_t combined_loc = make_location (start_loc, start_loc,
28573 finish_loc);
28574 cast.set_location (combined_loc);
28575 return cast;
28576 }
28577
28578
28579 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
28580 /*cast_p=*/true,
28581 /*allow_expansion_p=*/true,
28582 /*non_constant_p=*/NULL);
28583 if (vec == NULL)
28584 expression_list = error_mark_node;
28585 else
28586 {
28587 expression_list = build_tree_list_vec (vec);
28588 release_tree_vector (vec);
28589 }
28590
28591 cast = build_functional_cast (type, expression_list,
28592 tf_warning_or_error);
28593 /* [expr.const]/1: In an integral constant expression "only type
28594 conversions to integral or enumeration type can be used". */
28595 if (TREE_CODE (type) == TYPE_DECL)
28596 type = TREE_TYPE (type);
28597 if (cast != error_mark_node
28598 && !cast_valid_in_integral_constant_expression_p (type)
28599 && cp_parser_non_integral_constant_expression (parser,
28600 NIC_CONSTRUCTOR))
28601 return error_mark_node;
28602
28603 /* Create a location of the form:
28604 float(i)
28605 ^~~~~~~~
28606 with caret == start at the start of the type name,
28607 finishing at the closing paren. */
28608 location_t finish_loc
28609 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28610 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
28611 cast.set_location (combined_loc);
28612 return cast;
28613 }
28614
28615 /* Save the tokens that make up the body of a member function defined
28616 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
28617 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
28618 specifiers applied to the declaration. Returns the FUNCTION_DECL
28619 for the member function. */
28620
28621 static tree
28622 cp_parser_save_member_function_body (cp_parser* parser,
28623 cp_decl_specifier_seq *decl_specifiers,
28624 cp_declarator *declarator,
28625 tree attributes)
28626 {
28627 cp_token *first;
28628 cp_token *last;
28629 tree fn;
28630 bool function_try_block = false;
28631
28632 /* Create the FUNCTION_DECL. */
28633 fn = grokmethod (decl_specifiers, declarator, attributes);
28634 cp_finalize_omp_declare_simd (parser, fn);
28635 cp_finalize_oacc_routine (parser, fn, true);
28636 /* If something went badly wrong, bail out now. */
28637 if (fn == error_mark_node)
28638 {
28639 /* If there's a function-body, skip it. */
28640 if (cp_parser_token_starts_function_definition_p
28641 (cp_lexer_peek_token (parser->lexer)))
28642 cp_parser_skip_to_end_of_block_or_statement (parser);
28643 return error_mark_node;
28644 }
28645
28646 /* Remember it, if there are default args to post process. */
28647 cp_parser_save_default_args (parser, fn);
28648
28649 /* Save away the tokens that make up the body of the
28650 function. */
28651 first = parser->lexer->next_token;
28652
28653 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
28654 cp_lexer_consume_token (parser->lexer);
28655 else if (cp_lexer_next_token_is_keyword (parser->lexer,
28656 RID_TRANSACTION_ATOMIC))
28657 {
28658 cp_lexer_consume_token (parser->lexer);
28659 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
28660 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
28661 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
28662 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
28663 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28664 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
28665 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
28666 {
28667 cp_lexer_consume_token (parser->lexer);
28668 cp_lexer_consume_token (parser->lexer);
28669 cp_lexer_consume_token (parser->lexer);
28670 cp_lexer_consume_token (parser->lexer);
28671 cp_lexer_consume_token (parser->lexer);
28672 }
28673 else
28674 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
28675 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
28676 {
28677 cp_lexer_consume_token (parser->lexer);
28678 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28679 break;
28680 }
28681 }
28682
28683 /* Handle function try blocks. */
28684 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28685 {
28686 cp_lexer_consume_token (parser->lexer);
28687 function_try_block = true;
28688 }
28689 /* We can have braced-init-list mem-initializers before the fn body. */
28690 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28691 {
28692 cp_lexer_consume_token (parser->lexer);
28693 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
28694 {
28695 /* cache_group will stop after an un-nested { } pair, too. */
28696 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28697 break;
28698
28699 /* variadic mem-inits have ... after the ')'. */
28700 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28701 cp_lexer_consume_token (parser->lexer);
28702 }
28703 }
28704 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28705 /* Handle function try blocks. */
28706 if (function_try_block)
28707 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
28708 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28709 last = parser->lexer->next_token;
28710
28711 /* Save away the inline definition; we will process it when the
28712 class is complete. */
28713 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
28714 DECL_PENDING_INLINE_P (fn) = 1;
28715
28716 /* We need to know that this was defined in the class, so that
28717 friend templates are handled correctly. */
28718 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
28719
28720 /* Add FN to the queue of functions to be parsed later. */
28721 vec_safe_push (unparsed_funs_with_definitions, fn);
28722
28723 return fn;
28724 }
28725
28726 /* Save the tokens that make up the in-class initializer for a non-static
28727 data member. Returns a DEFERRED_PARSE. */
28728
28729 static tree
28730 cp_parser_save_nsdmi (cp_parser* parser)
28731 {
28732 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
28733 }
28734
28735 /* Parse a template-argument-list, as well as the trailing ">" (but
28736 not the opening "<"). See cp_parser_template_argument_list for the
28737 return value. */
28738
28739 static tree
28740 cp_parser_enclosed_template_argument_list (cp_parser* parser)
28741 {
28742 tree arguments;
28743 tree saved_scope;
28744 tree saved_qualifying_scope;
28745 tree saved_object_scope;
28746 bool saved_greater_than_is_operator_p;
28747
28748 /* [temp.names]
28749
28750 When parsing a template-id, the first non-nested `>' is taken as
28751 the end of the template-argument-list rather than a greater-than
28752 operator. */
28753 saved_greater_than_is_operator_p
28754 = parser->greater_than_is_operator_p;
28755 parser->greater_than_is_operator_p = false;
28756 /* Parsing the argument list may modify SCOPE, so we save it
28757 here. */
28758 saved_scope = parser->scope;
28759 saved_qualifying_scope = parser->qualifying_scope;
28760 saved_object_scope = parser->object_scope;
28761 /* We need to evaluate the template arguments, even though this
28762 template-id may be nested within a "sizeof". */
28763 cp_evaluated ev;
28764 /* Parse the template-argument-list itself. */
28765 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
28766 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28767 arguments = NULL_TREE;
28768 else
28769 arguments = cp_parser_template_argument_list (parser);
28770 /* Look for the `>' that ends the template-argument-list. If we find
28771 a '>>' instead, it's probably just a typo. */
28772 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28773 {
28774 if (cxx_dialect != cxx98)
28775 {
28776 /* In C++0x, a `>>' in a template argument list or cast
28777 expression is considered to be two separate `>'
28778 tokens. So, change the current token to a `>', but don't
28779 consume it: it will be consumed later when the outer
28780 template argument list (or cast expression) is parsed.
28781 Note that this replacement of `>' for `>>' is necessary
28782 even if we are parsing tentatively: in the tentative
28783 case, after calling
28784 cp_parser_enclosed_template_argument_list we will always
28785 throw away all of the template arguments and the first
28786 closing `>', either because the template argument list
28787 was erroneous or because we are replacing those tokens
28788 with a CPP_TEMPLATE_ID token. The second `>' (which will
28789 not have been thrown away) is needed either to close an
28790 outer template argument list or to complete a new-style
28791 cast. */
28792 cp_token *token = cp_lexer_peek_token (parser->lexer);
28793 token->type = CPP_GREATER;
28794 }
28795 else if (!saved_greater_than_is_operator_p)
28796 {
28797 /* If we're in a nested template argument list, the '>>' has
28798 to be a typo for '> >'. We emit the error message, but we
28799 continue parsing and we push a '>' as next token, so that
28800 the argument list will be parsed correctly. Note that the
28801 global source location is still on the token before the
28802 '>>', so we need to say explicitly where we want it. */
28803 cp_token *token = cp_lexer_peek_token (parser->lexer);
28804 gcc_rich_location richloc (token->location);
28805 richloc.add_fixit_replace ("> >");
28806 error_at (&richloc, "%<>>%> should be %<> >%> "
28807 "within a nested template argument list");
28808
28809 token->type = CPP_GREATER;
28810 }
28811 else
28812 {
28813 /* If this is not a nested template argument list, the '>>'
28814 is a typo for '>'. Emit an error message and continue.
28815 Same deal about the token location, but here we can get it
28816 right by consuming the '>>' before issuing the diagnostic. */
28817 cp_token *token = cp_lexer_consume_token (parser->lexer);
28818 error_at (token->location,
28819 "spurious %<>>%>, use %<>%> to terminate "
28820 "a template argument list");
28821 }
28822 }
28823 else
28824 cp_parser_skip_to_end_of_template_parameter_list (parser);
28825 /* The `>' token might be a greater-than operator again now. */
28826 parser->greater_than_is_operator_p
28827 = saved_greater_than_is_operator_p;
28828 /* Restore the SAVED_SCOPE. */
28829 parser->scope = saved_scope;
28830 parser->qualifying_scope = saved_qualifying_scope;
28831 parser->object_scope = saved_object_scope;
28832
28833 return arguments;
28834 }
28835
28836 /* MEMBER_FUNCTION is a member function, or a friend. If default
28837 arguments, or the body of the function have not yet been parsed,
28838 parse them now. */
28839
28840 static void
28841 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
28842 {
28843 timevar_push (TV_PARSE_INMETH);
28844 /* If this member is a template, get the underlying
28845 FUNCTION_DECL. */
28846 if (DECL_FUNCTION_TEMPLATE_P (member_function))
28847 member_function = DECL_TEMPLATE_RESULT (member_function);
28848
28849 /* There should not be any class definitions in progress at this
28850 point; the bodies of members are only parsed outside of all class
28851 definitions. */
28852 gcc_assert (parser->num_classes_being_defined == 0);
28853 /* While we're parsing the member functions we might encounter more
28854 classes. We want to handle them right away, but we don't want
28855 them getting mixed up with functions that are currently in the
28856 queue. */
28857 push_unparsed_function_queues (parser);
28858
28859 /* Make sure that any template parameters are in scope. */
28860 maybe_begin_member_template_processing (member_function);
28861
28862 /* If the body of the function has not yet been parsed, parse it
28863 now. */
28864 if (DECL_PENDING_INLINE_P (member_function))
28865 {
28866 tree function_scope;
28867 cp_token_cache *tokens;
28868
28869 /* The function is no longer pending; we are processing it. */
28870 tokens = DECL_PENDING_INLINE_INFO (member_function);
28871 DECL_PENDING_INLINE_INFO (member_function) = NULL;
28872 DECL_PENDING_INLINE_P (member_function) = 0;
28873
28874 /* If this is a local class, enter the scope of the containing
28875 function. */
28876 function_scope = current_function_decl;
28877 if (function_scope)
28878 push_function_context ();
28879
28880 /* Push the body of the function onto the lexer stack. */
28881 cp_parser_push_lexer_for_tokens (parser, tokens);
28882
28883 /* Let the front end know that we going to be defining this
28884 function. */
28885 start_preparsed_function (member_function, NULL_TREE,
28886 SF_PRE_PARSED | SF_INCLASS_INLINE);
28887
28888 /* Don't do access checking if it is a templated function. */
28889 if (processing_template_decl)
28890 push_deferring_access_checks (dk_no_check);
28891
28892 /* #pragma omp declare reduction needs special parsing. */
28893 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28894 {
28895 parser->lexer->in_pragma = true;
28896 cp_parser_omp_declare_reduction_exprs (member_function, parser);
28897 finish_function (/*inline_p=*/true);
28898 cp_check_omp_declare_reduction (member_function);
28899 }
28900 else
28901 /* Now, parse the body of the function. */
28902 cp_parser_function_definition_after_declarator (parser,
28903 /*inline_p=*/true);
28904
28905 if (processing_template_decl)
28906 pop_deferring_access_checks ();
28907
28908 /* Leave the scope of the containing function. */
28909 if (function_scope)
28910 pop_function_context ();
28911 cp_parser_pop_lexer (parser);
28912 }
28913
28914 /* Remove any template parameters from the symbol table. */
28915 maybe_end_member_template_processing ();
28916
28917 /* Restore the queue. */
28918 pop_unparsed_function_queues (parser);
28919 timevar_pop (TV_PARSE_INMETH);
28920 }
28921
28922 /* If DECL contains any default args, remember it on the unparsed
28923 functions queue. */
28924
28925 static void
28926 cp_parser_save_default_args (cp_parser* parser, tree decl)
28927 {
28928 tree probe;
28929
28930 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28931 probe;
28932 probe = TREE_CHAIN (probe))
28933 if (TREE_PURPOSE (probe))
28934 {
28935 cp_default_arg_entry entry = {current_class_type, decl};
28936 vec_safe_push (unparsed_funs_with_default_args, entry);
28937 break;
28938 }
28939
28940 /* Remember if there is a noexcept-specifier to post process. */
28941 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
28942 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
28943 vec_safe_push (unparsed_noexcepts, decl);
28944 }
28945
28946 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28947 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28948 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28949 from the parameter-type-list. */
28950
28951 static tree
28952 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28953 tree default_arg, tree parmtype)
28954 {
28955 cp_token_cache *tokens;
28956 tree parsed_arg;
28957 bool dummy;
28958
28959 if (default_arg == error_mark_node)
28960 return error_mark_node;
28961
28962 /* Push the saved tokens for the default argument onto the parser's
28963 lexer stack. */
28964 tokens = DEFPARSE_TOKENS (default_arg);
28965 cp_parser_push_lexer_for_tokens (parser, tokens);
28966
28967 start_lambda_scope (decl);
28968
28969 /* Parse the default argument. */
28970 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28971 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28972 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28973
28974 finish_lambda_scope ();
28975
28976 if (parsed_arg == error_mark_node)
28977 cp_parser_skip_to_end_of_statement (parser);
28978
28979 if (!processing_template_decl)
28980 {
28981 /* In a non-template class, check conversions now. In a template,
28982 we'll wait and instantiate these as needed. */
28983 if (TREE_CODE (decl) == PARM_DECL)
28984 parsed_arg = check_default_argument (parmtype, parsed_arg,
28985 tf_warning_or_error);
28986 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28987 parsed_arg = error_mark_node;
28988 else
28989 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28990 }
28991
28992 /* If the token stream has not been completely used up, then
28993 there was extra junk after the end of the default
28994 argument. */
28995 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28996 {
28997 if (TREE_CODE (decl) == PARM_DECL)
28998 cp_parser_error (parser, "expected %<,%>");
28999 else
29000 cp_parser_error (parser, "expected %<;%>");
29001 }
29002
29003 /* Revert to the main lexer. */
29004 cp_parser_pop_lexer (parser);
29005
29006 return parsed_arg;
29007 }
29008
29009 /* FIELD is a non-static data member with an initializer which we saved for
29010 later; parse it now. */
29011
29012 static void
29013 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
29014 {
29015 tree def;
29016
29017 maybe_begin_member_template_processing (field);
29018
29019 push_unparsed_function_queues (parser);
29020 def = cp_parser_late_parse_one_default_arg (parser, field,
29021 DECL_INITIAL (field),
29022 NULL_TREE);
29023 pop_unparsed_function_queues (parser);
29024
29025 maybe_end_member_template_processing ();
29026
29027 DECL_INITIAL (field) = def;
29028 }
29029
29030 /* FN is a FUNCTION_DECL which may contains a parameter with an
29031 unparsed DEFERRED_PARSE. Parse the default args now. This function
29032 assumes that the current scope is the scope in which the default
29033 argument should be processed. */
29034
29035 static void
29036 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
29037 {
29038 unsigned char saved_local_variables_forbidden_p;
29039 tree parm, parmdecl;
29040
29041 /* While we're parsing the default args, we might (due to the
29042 statement expression extension) encounter more classes. We want
29043 to handle them right away, but we don't want them getting mixed
29044 up with default args that are currently in the queue. */
29045 push_unparsed_function_queues (parser);
29046
29047 /* Local variable names (and the `this' keyword) may not appear
29048 in a default argument. */
29049 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
29050 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
29051
29052 push_defarg_context (fn);
29053
29054 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
29055 parmdecl = DECL_ARGUMENTS (fn);
29056 parm && parm != void_list_node;
29057 parm = TREE_CHAIN (parm),
29058 parmdecl = DECL_CHAIN (parmdecl))
29059 {
29060 tree default_arg = TREE_PURPOSE (parm);
29061 tree parsed_arg;
29062 vec<tree, va_gc> *insts;
29063 tree copy;
29064 unsigned ix;
29065
29066 if (!default_arg)
29067 continue;
29068
29069 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
29070 /* This can happen for a friend declaration for a function
29071 already declared with default arguments. */
29072 continue;
29073
29074 parsed_arg
29075 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
29076 default_arg,
29077 TREE_VALUE (parm));
29078 TREE_PURPOSE (parm) = parsed_arg;
29079
29080 /* Update any instantiations we've already created. */
29081 for (insts = DEFPARSE_INSTANTIATIONS (default_arg), ix = 0;
29082 vec_safe_iterate (insts, ix, &copy); ix++)
29083 TREE_PURPOSE (copy) = parsed_arg;
29084 }
29085
29086 pop_defarg_context ();
29087
29088 /* Make sure no default arg is missing. */
29089 check_default_args (fn);
29090
29091 /* Restore the state of local_variables_forbidden_p. */
29092 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
29093
29094 /* Restore the queue. */
29095 pop_unparsed_function_queues (parser);
29096 }
29097
29098 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
29099
29100 sizeof ... ( identifier )
29101
29102 where the 'sizeof' token has already been consumed. */
29103
29104 static tree
29105 cp_parser_sizeof_pack (cp_parser *parser)
29106 {
29107 /* Consume the `...'. */
29108 cp_lexer_consume_token (parser->lexer);
29109 maybe_warn_variadic_templates ();
29110
29111 matching_parens parens;
29112 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
29113 if (paren)
29114 parens.consume_open (parser);
29115 else
29116 permerror (cp_lexer_peek_token (parser->lexer)->location,
29117 "%<sizeof...%> argument must be surrounded by parentheses");
29118
29119 cp_token *token = cp_lexer_peek_token (parser->lexer);
29120 tree name = cp_parser_identifier (parser);
29121 if (name == error_mark_node)
29122 return error_mark_node;
29123 /* The name is not qualified. */
29124 parser->scope = NULL_TREE;
29125 parser->qualifying_scope = NULL_TREE;
29126 parser->object_scope = NULL_TREE;
29127 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
29128 if (expr == error_mark_node)
29129 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
29130 token->location);
29131 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
29132 expr = TREE_TYPE (expr);
29133 else if (TREE_CODE (expr) == CONST_DECL)
29134 expr = DECL_INITIAL (expr);
29135 expr = make_pack_expansion (expr);
29136 PACK_EXPANSION_SIZEOF_P (expr) = true;
29137
29138 if (paren)
29139 parens.require_close (parser);
29140
29141 return expr;
29142 }
29143
29144 /* Parse the operand of `sizeof' (or a similar operator). Returns
29145 either a TYPE or an expression, depending on the form of the
29146 input. The KEYWORD indicates which kind of expression we have
29147 encountered. */
29148
29149 static tree
29150 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
29151 {
29152 tree expr = NULL_TREE;
29153 const char *saved_message;
29154 const char *saved_message_arg;
29155 bool saved_integral_constant_expression_p;
29156 bool saved_non_integral_constant_expression_p;
29157
29158 /* If it's a `...', then we are computing the length of a parameter
29159 pack. */
29160 if (keyword == RID_SIZEOF
29161 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29162 return cp_parser_sizeof_pack (parser);
29163
29164 /* Types cannot be defined in a `sizeof' expression. Save away the
29165 old message. */
29166 saved_message = parser->type_definition_forbidden_message;
29167 saved_message_arg = parser->type_definition_forbidden_message_arg;
29168 parser->type_definition_forbidden_message
29169 = G_("types may not be defined in %qs expressions");
29170 parser->type_definition_forbidden_message_arg
29171 = IDENTIFIER_POINTER (ridpointers[keyword]);
29172
29173 /* The restrictions on constant-expressions do not apply inside
29174 sizeof expressions. */
29175 saved_integral_constant_expression_p
29176 = parser->integral_constant_expression_p;
29177 saved_non_integral_constant_expression_p
29178 = parser->non_integral_constant_expression_p;
29179 parser->integral_constant_expression_p = false;
29180
29181 /* Do not actually evaluate the expression. */
29182 ++cp_unevaluated_operand;
29183 ++c_inhibit_evaluation_warnings;
29184 /* If it's a `(', then we might be looking at the type-id
29185 construction. */
29186 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29187 {
29188 tree type = NULL_TREE;
29189
29190 tentative_firewall firewall (parser);
29191
29192 /* We can't be sure yet whether we're looking at a type-id or an
29193 expression. */
29194 cp_parser_parse_tentatively (parser);
29195
29196 matching_parens parens;
29197 parens.consume_open (parser);
29198
29199 /* Note: as a GNU Extension, compound literals are considered
29200 postfix-expressions as they are in C99, so they are valid
29201 arguments to sizeof. See comment in cp_parser_cast_expression
29202 for details. */
29203 if (cp_parser_compound_literal_p (parser))
29204 cp_parser_simulate_error (parser);
29205 else
29206 {
29207 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
29208 parser->in_type_id_in_expr_p = true;
29209 /* Look for the type-id. */
29210 type = cp_parser_type_id (parser);
29211 /* Look for the closing `)'. */
29212 parens.require_close (parser);
29213 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
29214 }
29215
29216 /* If all went well, then we're done. */
29217 if (cp_parser_parse_definitely (parser))
29218 expr = type;
29219 else
29220 {
29221 /* Commit to the tentative_firewall so we get syntax errors. */
29222 cp_parser_commit_to_tentative_parse (parser);
29223
29224 expr = cp_parser_unary_expression (parser);
29225 }
29226 }
29227 else
29228 expr = cp_parser_unary_expression (parser);
29229
29230 /* Go back to evaluating expressions. */
29231 --cp_unevaluated_operand;
29232 --c_inhibit_evaluation_warnings;
29233
29234 /* And restore the old one. */
29235 parser->type_definition_forbidden_message = saved_message;
29236 parser->type_definition_forbidden_message_arg = saved_message_arg;
29237 parser->integral_constant_expression_p
29238 = saved_integral_constant_expression_p;
29239 parser->non_integral_constant_expression_p
29240 = saved_non_integral_constant_expression_p;
29241
29242 return expr;
29243 }
29244
29245 /* If the current declaration has no declarator, return true. */
29246
29247 static bool
29248 cp_parser_declares_only_class_p (cp_parser *parser)
29249 {
29250 /* If the next token is a `;' or a `,' then there is no
29251 declarator. */
29252 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29253 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
29254 }
29255
29256 /* Update the DECL_SPECS to reflect the storage class indicated by
29257 KEYWORD. */
29258
29259 static void
29260 cp_parser_set_storage_class (cp_parser *parser,
29261 cp_decl_specifier_seq *decl_specs,
29262 enum rid keyword,
29263 cp_token *token)
29264 {
29265 cp_storage_class storage_class;
29266
29267 if (parser->in_unbraced_linkage_specification_p)
29268 {
29269 error_at (token->location, "invalid use of %qD in linkage specification",
29270 ridpointers[keyword]);
29271 return;
29272 }
29273 else if (decl_specs->storage_class != sc_none)
29274 {
29275 decl_specs->conflicting_specifiers_p = true;
29276 return;
29277 }
29278
29279 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
29280 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
29281 && decl_specs->gnu_thread_keyword_p)
29282 {
29283 pedwarn (decl_specs->locations[ds_thread], 0,
29284 "%<__thread%> before %qD", ridpointers[keyword]);
29285 }
29286
29287 switch (keyword)
29288 {
29289 case RID_AUTO:
29290 storage_class = sc_auto;
29291 break;
29292 case RID_REGISTER:
29293 storage_class = sc_register;
29294 break;
29295 case RID_STATIC:
29296 storage_class = sc_static;
29297 break;
29298 case RID_EXTERN:
29299 storage_class = sc_extern;
29300 break;
29301 case RID_MUTABLE:
29302 storage_class = sc_mutable;
29303 break;
29304 default:
29305 gcc_unreachable ();
29306 }
29307 decl_specs->storage_class = storage_class;
29308 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
29309
29310 /* A storage class specifier cannot be applied alongside a typedef
29311 specifier. If there is a typedef specifier present then set
29312 conflicting_specifiers_p which will trigger an error later
29313 on in grokdeclarator. */
29314 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
29315 decl_specs->conflicting_specifiers_p = true;
29316 }
29317
29318 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
29319 is true, the type is a class or enum definition. */
29320
29321 static void
29322 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
29323 tree type_spec,
29324 cp_token *token,
29325 bool type_definition_p)
29326 {
29327 decl_specs->any_specifiers_p = true;
29328
29329 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
29330 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
29331 this is what happened. In system headers, we ignore these
29332 declarations so that G++ can work with system headers that are not
29333 C++-safe. */
29334 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
29335 && !type_definition_p
29336 && (type_spec == boolean_type_node
29337 || type_spec == char8_type_node
29338 || type_spec == char16_type_node
29339 || type_spec == char32_type_node
29340 || type_spec == wchar_type_node)
29341 && (decl_specs->type
29342 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
29343 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
29344 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
29345 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
29346 {
29347 decl_specs->redefined_builtin_type = type_spec;
29348 set_and_check_decl_spec_loc (decl_specs,
29349 ds_redefined_builtin_type_spec,
29350 token);
29351 if (!decl_specs->type)
29352 {
29353 decl_specs->type = type_spec;
29354 decl_specs->type_definition_p = false;
29355 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
29356 }
29357 }
29358 else if (decl_specs->type)
29359 decl_specs->multiple_types_p = true;
29360 else
29361 {
29362 decl_specs->type = type_spec;
29363 decl_specs->type_definition_p = type_definition_p;
29364 decl_specs->redefined_builtin_type = NULL_TREE;
29365 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
29366 }
29367 }
29368
29369 /* True iff TOKEN is the GNU keyword __thread. */
29370
29371 static bool
29372 token_is__thread (cp_token *token)
29373 {
29374 gcc_assert (token->keyword == RID_THREAD);
29375 return id_equal (token->u.value, "__thread");
29376 }
29377
29378 /* Set the location for a declarator specifier and check if it is
29379 duplicated.
29380
29381 DECL_SPECS is the sequence of declarator specifiers onto which to
29382 set the location.
29383
29384 DS is the single declarator specifier to set which location is to
29385 be set onto the existing sequence of declarators.
29386
29387 LOCATION is the location for the declarator specifier to
29388 consider. */
29389
29390 static void
29391 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
29392 cp_decl_spec ds, cp_token *token)
29393 {
29394 gcc_assert (ds < ds_last);
29395
29396 if (decl_specs == NULL)
29397 return;
29398
29399 location_t location = token->location;
29400
29401 if (decl_specs->locations[ds] == 0)
29402 {
29403 decl_specs->locations[ds] = location;
29404 if (ds == ds_thread)
29405 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
29406 }
29407 else
29408 {
29409 if (ds == ds_long)
29410 {
29411 if (decl_specs->locations[ds_long_long] != 0)
29412 error_at (location,
29413 "%<long long long%> is too long for GCC");
29414 else
29415 {
29416 decl_specs->locations[ds_long_long] = location;
29417 pedwarn_cxx98 (location,
29418 OPT_Wlong_long,
29419 "ISO C++ 1998 does not support %<long long%>");
29420 }
29421 }
29422 else if (ds == ds_thread)
29423 {
29424 bool gnu = token_is__thread (token);
29425 gcc_rich_location richloc (location);
29426 if (gnu != decl_specs->gnu_thread_keyword_p)
29427 {
29428 richloc.add_range (decl_specs->locations[ds_thread]);
29429 error_at (&richloc,
29430 "both %<__thread%> and %<thread_local%> specified");
29431 }
29432 else
29433 {
29434 richloc.add_fixit_remove ();
29435 error_at (&richloc, "duplicate %qD", token->u.value);
29436 }
29437 }
29438 else
29439 {
29440 static const char *const decl_spec_names[] = {
29441 "signed",
29442 "unsigned",
29443 "short",
29444 "long",
29445 "const",
29446 "volatile",
29447 "restrict",
29448 "inline",
29449 "virtual",
29450 "explicit",
29451 "friend",
29452 "typedef",
29453 "using",
29454 "constexpr",
29455 "__complex"
29456 };
29457 gcc_rich_location richloc (location);
29458 richloc.add_fixit_remove ();
29459 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
29460 }
29461 }
29462 }
29463
29464 /* Return true iff the declarator specifier DS is present in the
29465 sequence of declarator specifiers DECL_SPECS. */
29466
29467 bool
29468 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
29469 cp_decl_spec ds)
29470 {
29471 gcc_assert (ds < ds_last);
29472
29473 if (decl_specs == NULL)
29474 return false;
29475
29476 return decl_specs->locations[ds] != 0;
29477 }
29478
29479 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
29480 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
29481
29482 static bool
29483 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
29484 {
29485 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
29486 }
29487
29488 /* Issue an error message indicating that TOKEN_DESC was expected.
29489 If KEYWORD is true, it indicated this function is called by
29490 cp_parser_require_keword and the required token can only be
29491 a indicated keyword.
29492
29493 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29494 within any error as the location of an "opening" token matching
29495 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29496 RT_CLOSE_PAREN). */
29497
29498 static void
29499 cp_parser_required_error (cp_parser *parser,
29500 required_token token_desc,
29501 bool keyword,
29502 location_t matching_location)
29503 {
29504 if (cp_parser_simulate_error (parser))
29505 return;
29506
29507 const char *gmsgid = NULL;
29508 switch (token_desc)
29509 {
29510 case RT_NEW:
29511 gmsgid = G_("expected %<new%>");
29512 break;
29513 case RT_DELETE:
29514 gmsgid = G_("expected %<delete%>");
29515 break;
29516 case RT_RETURN:
29517 gmsgid = G_("expected %<return%>");
29518 break;
29519 case RT_WHILE:
29520 gmsgid = G_("expected %<while%>");
29521 break;
29522 case RT_EXTERN:
29523 gmsgid = G_("expected %<extern%>");
29524 break;
29525 case RT_STATIC_ASSERT:
29526 gmsgid = G_("expected %<static_assert%>");
29527 break;
29528 case RT_DECLTYPE:
29529 gmsgid = G_("expected %<decltype%>");
29530 break;
29531 case RT_OPERATOR:
29532 gmsgid = G_("expected %<operator%>");
29533 break;
29534 case RT_CLASS:
29535 gmsgid = G_("expected %<class%>");
29536 break;
29537 case RT_TEMPLATE:
29538 gmsgid = G_("expected %<template%>");
29539 break;
29540 case RT_NAMESPACE:
29541 gmsgid = G_("expected %<namespace%>");
29542 break;
29543 case RT_USING:
29544 gmsgid = G_("expected %<using%>");
29545 break;
29546 case RT_ASM:
29547 gmsgid = G_("expected %<asm%>");
29548 break;
29549 case RT_TRY:
29550 gmsgid = G_("expected %<try%>");
29551 break;
29552 case RT_CATCH:
29553 gmsgid = G_("expected %<catch%>");
29554 break;
29555 case RT_THROW:
29556 gmsgid = G_("expected %<throw%>");
29557 break;
29558 case RT_LABEL:
29559 gmsgid = G_("expected %<__label__%>");
29560 break;
29561 case RT_AT_TRY:
29562 gmsgid = G_("expected %<@try%>");
29563 break;
29564 case RT_AT_SYNCHRONIZED:
29565 gmsgid = G_("expected %<@synchronized%>");
29566 break;
29567 case RT_AT_THROW:
29568 gmsgid = G_("expected %<@throw%>");
29569 break;
29570 case RT_TRANSACTION_ATOMIC:
29571 gmsgid = G_("expected %<__transaction_atomic%>");
29572 break;
29573 case RT_TRANSACTION_RELAXED:
29574 gmsgid = G_("expected %<__transaction_relaxed%>");
29575 break;
29576 default:
29577 break;
29578 }
29579
29580 if (!gmsgid && !keyword)
29581 {
29582 switch (token_desc)
29583 {
29584 case RT_SEMICOLON:
29585 gmsgid = G_("expected %<;%>");
29586 break;
29587 case RT_OPEN_PAREN:
29588 gmsgid = G_("expected %<(%>");
29589 break;
29590 case RT_CLOSE_BRACE:
29591 gmsgid = G_("expected %<}%>");
29592 break;
29593 case RT_OPEN_BRACE:
29594 gmsgid = G_("expected %<{%>");
29595 break;
29596 case RT_CLOSE_SQUARE:
29597 gmsgid = G_("expected %<]%>");
29598 break;
29599 case RT_OPEN_SQUARE:
29600 gmsgid = G_("expected %<[%>");
29601 break;
29602 case RT_COMMA:
29603 gmsgid = G_("expected %<,%>");
29604 break;
29605 case RT_SCOPE:
29606 gmsgid = G_("expected %<::%>");
29607 break;
29608 case RT_LESS:
29609 gmsgid = G_("expected %<<%>");
29610 break;
29611 case RT_GREATER:
29612 gmsgid = G_("expected %<>%>");
29613 break;
29614 case RT_EQ:
29615 gmsgid = G_("expected %<=%>");
29616 break;
29617 case RT_ELLIPSIS:
29618 gmsgid = G_("expected %<...%>");
29619 break;
29620 case RT_MULT:
29621 gmsgid = G_("expected %<*%>");
29622 break;
29623 case RT_COMPL:
29624 gmsgid = G_("expected %<~%>");
29625 break;
29626 case RT_COLON:
29627 gmsgid = G_("expected %<:%>");
29628 break;
29629 case RT_COLON_SCOPE:
29630 gmsgid = G_("expected %<:%> or %<::%>");
29631 break;
29632 case RT_CLOSE_PAREN:
29633 gmsgid = G_("expected %<)%>");
29634 break;
29635 case RT_COMMA_CLOSE_PAREN:
29636 gmsgid = G_("expected %<,%> or %<)%>");
29637 break;
29638 case RT_PRAGMA_EOL:
29639 gmsgid = G_("expected end of line");
29640 break;
29641 case RT_NAME:
29642 gmsgid = G_("expected identifier");
29643 break;
29644 case RT_SELECT:
29645 gmsgid = G_("expected selection-statement");
29646 break;
29647 case RT_ITERATION:
29648 gmsgid = G_("expected iteration-statement");
29649 break;
29650 case RT_JUMP:
29651 gmsgid = G_("expected jump-statement");
29652 break;
29653 case RT_CLASS_KEY:
29654 gmsgid = G_("expected class-key");
29655 break;
29656 case RT_CLASS_TYPENAME_TEMPLATE:
29657 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
29658 break;
29659 default:
29660 gcc_unreachable ();
29661 }
29662 }
29663
29664 if (gmsgid)
29665 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
29666 }
29667
29668
29669 /* If the next token is of the indicated TYPE, consume it. Otherwise,
29670 issue an error message indicating that TOKEN_DESC was expected.
29671
29672 Returns the token consumed, if the token had the appropriate type.
29673 Otherwise, returns NULL.
29674
29675 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29676 within any error as the location of an "opening" token matching
29677 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29678 RT_CLOSE_PAREN). */
29679
29680 static cp_token *
29681 cp_parser_require (cp_parser* parser,
29682 enum cpp_ttype type,
29683 required_token token_desc,
29684 location_t matching_location)
29685 {
29686 if (cp_lexer_next_token_is (parser->lexer, type))
29687 return cp_lexer_consume_token (parser->lexer);
29688 else
29689 {
29690 /* Output the MESSAGE -- unless we're parsing tentatively. */
29691 if (!cp_parser_simulate_error (parser))
29692 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
29693 matching_location);
29694 return NULL;
29695 }
29696 }
29697
29698 /* An error message is produced if the next token is not '>'.
29699 All further tokens are skipped until the desired token is
29700 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29701
29702 static void
29703 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
29704 {
29705 /* Current level of '< ... >'. */
29706 unsigned level = 0;
29707 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29708 unsigned nesting_depth = 0;
29709
29710 /* Are we ready, yet? If not, issue error message. */
29711 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
29712 return;
29713
29714 /* Skip tokens until the desired token is found. */
29715 while (true)
29716 {
29717 /* Peek at the next token. */
29718 switch (cp_lexer_peek_token (parser->lexer)->type)
29719 {
29720 case CPP_LESS:
29721 if (!nesting_depth)
29722 ++level;
29723 break;
29724
29725 case CPP_RSHIFT:
29726 if (cxx_dialect == cxx98)
29727 /* C++0x views the `>>' operator as two `>' tokens, but
29728 C++98 does not. */
29729 break;
29730 else if (!nesting_depth && level-- == 0)
29731 {
29732 /* We've hit a `>>' where the first `>' closes the
29733 template argument list, and the second `>' is
29734 spurious. Just consume the `>>' and stop; we've
29735 already produced at least one error. */
29736 cp_lexer_consume_token (parser->lexer);
29737 return;
29738 }
29739 /* Fall through for C++0x, so we handle the second `>' in
29740 the `>>'. */
29741 gcc_fallthrough ();
29742
29743 case CPP_GREATER:
29744 if (!nesting_depth && level-- == 0)
29745 {
29746 /* We've reached the token we want, consume it and stop. */
29747 cp_lexer_consume_token (parser->lexer);
29748 return;
29749 }
29750 break;
29751
29752 case CPP_OPEN_PAREN:
29753 case CPP_OPEN_SQUARE:
29754 ++nesting_depth;
29755 break;
29756
29757 case CPP_CLOSE_PAREN:
29758 case CPP_CLOSE_SQUARE:
29759 if (nesting_depth-- == 0)
29760 return;
29761 break;
29762
29763 case CPP_EOF:
29764 case CPP_PRAGMA_EOL:
29765 case CPP_SEMICOLON:
29766 case CPP_OPEN_BRACE:
29767 case CPP_CLOSE_BRACE:
29768 /* The '>' was probably forgotten, don't look further. */
29769 return;
29770
29771 default:
29772 break;
29773 }
29774
29775 /* Consume this token. */
29776 cp_lexer_consume_token (parser->lexer);
29777 }
29778 }
29779
29780 /* If the next token is the indicated keyword, consume it. Otherwise,
29781 issue an error message indicating that TOKEN_DESC was expected.
29782
29783 Returns the token consumed, if the token had the appropriate type.
29784 Otherwise, returns NULL. */
29785
29786 static cp_token *
29787 cp_parser_require_keyword (cp_parser* parser,
29788 enum rid keyword,
29789 required_token token_desc)
29790 {
29791 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
29792
29793 if (token && token->keyword != keyword)
29794 {
29795 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
29796 UNKNOWN_LOCATION);
29797 return NULL;
29798 }
29799
29800 return token;
29801 }
29802
29803 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29804 function-definition. */
29805
29806 static bool
29807 cp_parser_token_starts_function_definition_p (cp_token* token)
29808 {
29809 return (/* An ordinary function-body begins with an `{'. */
29810 token->type == CPP_OPEN_BRACE
29811 /* A ctor-initializer begins with a `:'. */
29812 || token->type == CPP_COLON
29813 /* A function-try-block begins with `try'. */
29814 || token->keyword == RID_TRY
29815 /* A function-transaction-block begins with `__transaction_atomic'
29816 or `__transaction_relaxed'. */
29817 || token->keyword == RID_TRANSACTION_ATOMIC
29818 || token->keyword == RID_TRANSACTION_RELAXED
29819 /* The named return value extension begins with `return'. */
29820 || token->keyword == RID_RETURN);
29821 }
29822
29823 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29824 definition. */
29825
29826 static bool
29827 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
29828 {
29829 cp_token *token;
29830
29831 token = cp_lexer_peek_token (parser->lexer);
29832 return (token->type == CPP_OPEN_BRACE
29833 || (token->type == CPP_COLON
29834 && !parser->colon_doesnt_start_class_def_p));
29835 }
29836
29837 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29838 C++0x) ending a template-argument. */
29839
29840 static bool
29841 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
29842 {
29843 cp_token *token;
29844
29845 token = cp_lexer_peek_token (parser->lexer);
29846 return (token->type == CPP_COMMA
29847 || token->type == CPP_GREATER
29848 || token->type == CPP_ELLIPSIS
29849 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
29850 }
29851
29852 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29853 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29854
29855 static bool
29856 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
29857 size_t n)
29858 {
29859 cp_token *token;
29860
29861 token = cp_lexer_peek_nth_token (parser->lexer, n);
29862 if (token->type == CPP_LESS)
29863 return true;
29864 /* Check for the sequence `<::' in the original code. It would be lexed as
29865 `[:', where `[' is a digraph, and there is no whitespace before
29866 `:'. */
29867 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
29868 {
29869 cp_token *token2;
29870 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
29871 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
29872 return true;
29873 }
29874 return false;
29875 }
29876
29877 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29878 or none_type otherwise. */
29879
29880 static enum tag_types
29881 cp_parser_token_is_class_key (cp_token* token)
29882 {
29883 switch (token->keyword)
29884 {
29885 case RID_CLASS:
29886 return class_type;
29887 case RID_STRUCT:
29888 return record_type;
29889 case RID_UNION:
29890 return union_type;
29891
29892 default:
29893 return none_type;
29894 }
29895 }
29896
29897 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29898 or none_type otherwise or if the token is null. */
29899
29900 static enum tag_types
29901 cp_parser_token_is_type_parameter_key (cp_token* token)
29902 {
29903 if (!token)
29904 return none_type;
29905
29906 switch (token->keyword)
29907 {
29908 case RID_CLASS:
29909 return class_type;
29910 case RID_TYPENAME:
29911 return typename_type;
29912
29913 default:
29914 return none_type;
29915 }
29916 }
29917
29918 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29919
29920 static void
29921 cp_parser_check_class_key (enum tag_types class_key, tree type)
29922 {
29923 if (type == error_mark_node)
29924 return;
29925 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29926 {
29927 if (permerror (input_location, "%qs tag used in naming %q#T",
29928 class_key == union_type ? "union"
29929 : class_key == record_type ? "struct" : "class",
29930 type))
29931 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29932 "%q#T was previously declared here", type);
29933 }
29934 }
29935
29936 /* Issue an error message if DECL is redeclared with different
29937 access than its original declaration [class.access.spec/3].
29938 This applies to nested classes, nested class templates and
29939 enumerations [class.mem/1]. */
29940
29941 static void
29942 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29943 {
29944 if (!decl
29945 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29946 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29947 return;
29948
29949 if ((TREE_PRIVATE (decl)
29950 != (current_access_specifier == access_private_node))
29951 || (TREE_PROTECTED (decl)
29952 != (current_access_specifier == access_protected_node)))
29953 error_at (location, "%qD redeclared with different access", decl);
29954 }
29955
29956 /* Look for the `template' keyword, as a syntactic disambiguator.
29957 Return TRUE iff it is present, in which case it will be
29958 consumed. */
29959
29960 static bool
29961 cp_parser_optional_template_keyword (cp_parser *parser)
29962 {
29963 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29964 {
29965 /* In C++98 the `template' keyword can only be used within templates;
29966 outside templates the parser can always figure out what is a
29967 template and what is not. In C++11, per the resolution of DR 468,
29968 `template' is allowed in cases where it is not strictly necessary. */
29969 if (!processing_template_decl
29970 && pedantic && cxx_dialect == cxx98)
29971 {
29972 cp_token *token = cp_lexer_peek_token (parser->lexer);
29973 pedwarn (token->location, OPT_Wpedantic,
29974 "in C++98 %<template%> (as a disambiguator) is only "
29975 "allowed within templates");
29976 /* If this part of the token stream is rescanned, the same
29977 error message would be generated. So, we purge the token
29978 from the stream. */
29979 cp_lexer_purge_token (parser->lexer);
29980 return false;
29981 }
29982 else
29983 {
29984 /* Consume the `template' keyword. */
29985 cp_lexer_consume_token (parser->lexer);
29986 return true;
29987 }
29988 }
29989 return false;
29990 }
29991
29992 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29993 set PARSER->SCOPE, and perform other related actions. */
29994
29995 static void
29996 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29997 {
29998 struct tree_check *check_value;
29999
30000 /* Get the stored value. */
30001 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
30002 /* Set the scope from the stored value. */
30003 parser->scope = saved_checks_value (check_value);
30004 parser->qualifying_scope = check_value->qualifying_scope;
30005 parser->object_scope = NULL_TREE;
30006 }
30007
30008 /* Consume tokens up through a non-nested END token. Returns TRUE if we
30009 encounter the end of a block before what we were looking for. */
30010
30011 static bool
30012 cp_parser_cache_group (cp_parser *parser,
30013 enum cpp_ttype end,
30014 unsigned depth)
30015 {
30016 while (true)
30017 {
30018 cp_token *token = cp_lexer_peek_token (parser->lexer);
30019
30020 /* Abort a parenthesized expression if we encounter a semicolon. */
30021 if ((end == CPP_CLOSE_PAREN || depth == 0)
30022 && token->type == CPP_SEMICOLON)
30023 return true;
30024 /* If we've reached the end of the file, stop. */
30025 if (token->type == CPP_EOF
30026 || (end != CPP_PRAGMA_EOL
30027 && token->type == CPP_PRAGMA_EOL))
30028 return true;
30029 if (token->type == CPP_CLOSE_BRACE && depth == 0)
30030 /* We've hit the end of an enclosing block, so there's been some
30031 kind of syntax error. */
30032 return true;
30033
30034 /* Consume the token. */
30035 cp_lexer_consume_token (parser->lexer);
30036 /* See if it starts a new group. */
30037 if (token->type == CPP_OPEN_BRACE)
30038 {
30039 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
30040 /* In theory this should probably check end == '}', but
30041 cp_parser_save_member_function_body needs it to exit
30042 after either '}' or ')' when called with ')'. */
30043 if (depth == 0)
30044 return false;
30045 }
30046 else if (token->type == CPP_OPEN_PAREN)
30047 {
30048 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
30049 if (depth == 0 && end == CPP_CLOSE_PAREN)
30050 return false;
30051 }
30052 else if (token->type == CPP_PRAGMA)
30053 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
30054 else if (token->type == end)
30055 return false;
30056 }
30057 }
30058
30059 /* Like above, for caching a default argument or NSDMI. Both of these are
30060 terminated by a non-nested comma, but it can be unclear whether or not a
30061 comma is nested in a template argument list unless we do more parsing.
30062 In order to handle this ambiguity, when we encounter a ',' after a '<'
30063 we try to parse what follows as a parameter-declaration-list (in the
30064 case of a default argument) or a member-declarator (in the case of an
30065 NSDMI). If that succeeds, then we stop caching. */
30066
30067 static tree
30068 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
30069 {
30070 unsigned depth = 0;
30071 int maybe_template_id = 0;
30072 cp_token *first_token;
30073 cp_token *token;
30074 tree default_argument;
30075
30076 /* Add tokens until we have processed the entire default
30077 argument. We add the range [first_token, token). */
30078 first_token = cp_lexer_peek_token (parser->lexer);
30079 if (first_token->type == CPP_OPEN_BRACE)
30080 {
30081 /* For list-initialization, this is straightforward. */
30082 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
30083 token = cp_lexer_peek_token (parser->lexer);
30084 }
30085 else while (true)
30086 {
30087 bool done = false;
30088
30089 /* Peek at the next token. */
30090 token = cp_lexer_peek_token (parser->lexer);
30091 /* What we do depends on what token we have. */
30092 switch (token->type)
30093 {
30094 /* In valid code, a default argument must be
30095 immediately followed by a `,' `)', or `...'. */
30096 case CPP_COMMA:
30097 if (depth == 0 && maybe_template_id)
30098 {
30099 /* If we've seen a '<', we might be in a
30100 template-argument-list. Until Core issue 325 is
30101 resolved, we don't know how this situation ought
30102 to be handled, so try to DTRT. We check whether
30103 what comes after the comma is a valid parameter
30104 declaration list. If it is, then the comma ends
30105 the default argument; otherwise the default
30106 argument continues. */
30107 bool error = false;
30108 cp_token *peek;
30109
30110 /* Set ITALP so cp_parser_parameter_declaration_list
30111 doesn't decide to commit to this parse. */
30112 bool saved_italp = parser->in_template_argument_list_p;
30113 parser->in_template_argument_list_p = true;
30114
30115 cp_parser_parse_tentatively (parser);
30116
30117 if (nsdmi)
30118 {
30119 /* Parse declarators until we reach a non-comma or
30120 somthing that cannot be an initializer.
30121 Just checking whether we're looking at a single
30122 declarator is insufficient. Consider:
30123 int var = tuple<T,U>::x;
30124 The template parameter 'U' looks exactly like a
30125 declarator. */
30126 do
30127 {
30128 int ctor_dtor_or_conv_p;
30129 cp_lexer_consume_token (parser->lexer);
30130 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30131 CP_PARSER_FLAGS_NONE,
30132 &ctor_dtor_or_conv_p,
30133 /*parenthesized_p=*/NULL,
30134 /*member_p=*/true,
30135 /*friend_p=*/false,
30136 /*static_p=*/false);
30137 peek = cp_lexer_peek_token (parser->lexer);
30138 if (cp_parser_error_occurred (parser))
30139 break;
30140 }
30141 while (peek->type == CPP_COMMA);
30142 /* If we met an '=' or ';' then the original comma
30143 was the end of the NSDMI. Otherwise assume
30144 we're still in the NSDMI. */
30145 error = (peek->type != CPP_EQ
30146 && peek->type != CPP_SEMICOLON);
30147 }
30148 else
30149 {
30150 cp_lexer_consume_token (parser->lexer);
30151 begin_scope (sk_function_parms, NULL_TREE);
30152 tree t = cp_parser_parameter_declaration_list
30153 (parser, CP_PARSER_FLAGS_NONE);
30154 if (t == error_mark_node)
30155 error = true;
30156 pop_bindings_and_leave_scope ();
30157 }
30158 if (!cp_parser_error_occurred (parser) && !error)
30159 done = true;
30160 cp_parser_abort_tentative_parse (parser);
30161
30162 parser->in_template_argument_list_p = saved_italp;
30163 break;
30164 }
30165 /* FALLTHRU */
30166 case CPP_CLOSE_PAREN:
30167 case CPP_ELLIPSIS:
30168 /* If we run into a non-nested `;', `}', or `]',
30169 then the code is invalid -- but the default
30170 argument is certainly over. */
30171 case CPP_SEMICOLON:
30172 case CPP_CLOSE_BRACE:
30173 case CPP_CLOSE_SQUARE:
30174 if (depth == 0
30175 /* Handle correctly int n = sizeof ... ( p ); */
30176 && token->type != CPP_ELLIPSIS)
30177 done = true;
30178 /* Update DEPTH, if necessary. */
30179 else if (token->type == CPP_CLOSE_PAREN
30180 || token->type == CPP_CLOSE_BRACE
30181 || token->type == CPP_CLOSE_SQUARE)
30182 --depth;
30183 break;
30184
30185 case CPP_OPEN_PAREN:
30186 case CPP_OPEN_SQUARE:
30187 case CPP_OPEN_BRACE:
30188 ++depth;
30189 break;
30190
30191 case CPP_LESS:
30192 if (depth == 0)
30193 /* This might be the comparison operator, or it might
30194 start a template argument list. */
30195 ++maybe_template_id;
30196 break;
30197
30198 case CPP_RSHIFT:
30199 if (cxx_dialect == cxx98)
30200 break;
30201 /* Fall through for C++0x, which treats the `>>'
30202 operator like two `>' tokens in certain
30203 cases. */
30204 gcc_fallthrough ();
30205
30206 case CPP_GREATER:
30207 if (depth == 0)
30208 {
30209 /* This might be an operator, or it might close a
30210 template argument list. But if a previous '<'
30211 started a template argument list, this will have
30212 closed it, so we can't be in one anymore. */
30213 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
30214 if (maybe_template_id < 0)
30215 maybe_template_id = 0;
30216 }
30217 break;
30218
30219 /* If we run out of tokens, issue an error message. */
30220 case CPP_EOF:
30221 case CPP_PRAGMA_EOL:
30222 error_at (token->location, "file ends in default argument");
30223 return error_mark_node;
30224
30225 case CPP_NAME:
30226 case CPP_SCOPE:
30227 /* In these cases, we should look for template-ids.
30228 For example, if the default argument is
30229 `X<int, double>()', we need to do name lookup to
30230 figure out whether or not `X' is a template; if
30231 so, the `,' does not end the default argument.
30232
30233 That is not yet done. */
30234 break;
30235
30236 default:
30237 break;
30238 }
30239
30240 /* If we've reached the end, stop. */
30241 if (done)
30242 break;
30243
30244 /* Add the token to the token block. */
30245 token = cp_lexer_consume_token (parser->lexer);
30246 }
30247
30248 /* Create a DEFERRED_PARSE to represent the unparsed default
30249 argument. */
30250 default_argument = make_node (DEFERRED_PARSE);
30251 DEFPARSE_TOKENS (default_argument)
30252 = cp_token_cache_new (first_token, token);
30253 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
30254
30255 return default_argument;
30256 }
30257
30258 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
30259
30260 location_t
30261 defparse_location (tree default_argument)
30262 {
30263 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
30264 location_t start = tokens->first->location;
30265 location_t end = tokens->last->location;
30266 return make_location (start, start, end);
30267 }
30268
30269 /* Begin parsing tentatively. We always save tokens while parsing
30270 tentatively so that if the tentative parsing fails we can restore the
30271 tokens. */
30272
30273 static void
30274 cp_parser_parse_tentatively (cp_parser* parser)
30275 {
30276 /* Enter a new parsing context. */
30277 parser->context = cp_parser_context_new (parser->context);
30278 /* Begin saving tokens. */
30279 cp_lexer_save_tokens (parser->lexer);
30280 /* In order to avoid repetitive access control error messages,
30281 access checks are queued up until we are no longer parsing
30282 tentatively. */
30283 push_deferring_access_checks (dk_deferred);
30284 }
30285
30286 /* Commit to the currently active tentative parse. */
30287
30288 static void
30289 cp_parser_commit_to_tentative_parse (cp_parser* parser)
30290 {
30291 cp_parser_context *context;
30292 cp_lexer *lexer;
30293
30294 /* Mark all of the levels as committed. */
30295 lexer = parser->lexer;
30296 for (context = parser->context; context->next; context = context->next)
30297 {
30298 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
30299 break;
30300 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
30301 while (!cp_lexer_saving_tokens (lexer))
30302 lexer = lexer->next;
30303 cp_lexer_commit_tokens (lexer);
30304 }
30305 }
30306
30307 /* Commit to the topmost currently active tentative parse.
30308
30309 Note that this function shouldn't be called when there are
30310 irreversible side-effects while in a tentative state. For
30311 example, we shouldn't create a permanent entry in the symbol
30312 table, or issue an error message that might not apply if the
30313 tentative parse is aborted. */
30314
30315 static void
30316 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
30317 {
30318 cp_parser_context *context = parser->context;
30319 cp_lexer *lexer = parser->lexer;
30320
30321 if (context)
30322 {
30323 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
30324 return;
30325 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
30326
30327 while (!cp_lexer_saving_tokens (lexer))
30328 lexer = lexer->next;
30329 cp_lexer_commit_tokens (lexer);
30330 }
30331 }
30332
30333 /* Abort the currently active tentative parse. All consumed tokens
30334 will be rolled back, and no diagnostics will be issued. */
30335
30336 static void
30337 cp_parser_abort_tentative_parse (cp_parser* parser)
30338 {
30339 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
30340 || errorcount > 0);
30341 cp_parser_simulate_error (parser);
30342 /* Now, pretend that we want to see if the construct was
30343 successfully parsed. */
30344 cp_parser_parse_definitely (parser);
30345 }
30346
30347 /* Stop parsing tentatively. If a parse error has occurred, restore the
30348 token stream. Otherwise, commit to the tokens we have consumed.
30349 Returns true if no error occurred; false otherwise. */
30350
30351 static bool
30352 cp_parser_parse_definitely (cp_parser* parser)
30353 {
30354 bool error_occurred;
30355 cp_parser_context *context;
30356
30357 /* Remember whether or not an error occurred, since we are about to
30358 destroy that information. */
30359 error_occurred = cp_parser_error_occurred (parser);
30360 /* Remove the topmost context from the stack. */
30361 context = parser->context;
30362 parser->context = context->next;
30363 /* If no parse errors occurred, commit to the tentative parse. */
30364 if (!error_occurred)
30365 {
30366 /* Commit to the tokens read tentatively, unless that was
30367 already done. */
30368 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
30369 cp_lexer_commit_tokens (parser->lexer);
30370
30371 pop_to_parent_deferring_access_checks ();
30372 }
30373 /* Otherwise, if errors occurred, roll back our state so that things
30374 are just as they were before we began the tentative parse. */
30375 else
30376 {
30377 cp_lexer_rollback_tokens (parser->lexer);
30378 pop_deferring_access_checks ();
30379 }
30380 /* Add the context to the front of the free list. */
30381 context->next = cp_parser_context_free_list;
30382 cp_parser_context_free_list = context;
30383
30384 return !error_occurred;
30385 }
30386
30387 /* Returns true if we are parsing tentatively and are not committed to
30388 this tentative parse. */
30389
30390 static bool
30391 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
30392 {
30393 return (cp_parser_parsing_tentatively (parser)
30394 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
30395 }
30396
30397 /* Returns nonzero iff an error has occurred during the most recent
30398 tentative parse. */
30399
30400 static bool
30401 cp_parser_error_occurred (cp_parser* parser)
30402 {
30403 return (cp_parser_parsing_tentatively (parser)
30404 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
30405 }
30406
30407 /* Returns nonzero if GNU extensions are allowed. */
30408
30409 static bool
30410 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
30411 {
30412 return parser->allow_gnu_extensions_p;
30413 }
30414 \f
30415 /* Objective-C++ Productions */
30416
30417
30418 /* Parse an Objective-C expression, which feeds into a primary-expression
30419 above.
30420
30421 objc-expression:
30422 objc-message-expression
30423 objc-string-literal
30424 objc-encode-expression
30425 objc-protocol-expression
30426 objc-selector-expression
30427
30428 Returns a tree representation of the expression. */
30429
30430 static cp_expr
30431 cp_parser_objc_expression (cp_parser* parser)
30432 {
30433 /* Try to figure out what kind of declaration is present. */
30434 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30435
30436 switch (kwd->type)
30437 {
30438 case CPP_OPEN_SQUARE:
30439 return cp_parser_objc_message_expression (parser);
30440
30441 case CPP_OBJC_STRING:
30442 kwd = cp_lexer_consume_token (parser->lexer);
30443 return objc_build_string_object (kwd->u.value);
30444
30445 case CPP_KEYWORD:
30446 switch (kwd->keyword)
30447 {
30448 case RID_AT_ENCODE:
30449 return cp_parser_objc_encode_expression (parser);
30450
30451 case RID_AT_PROTOCOL:
30452 return cp_parser_objc_protocol_expression (parser);
30453
30454 case RID_AT_SELECTOR:
30455 return cp_parser_objc_selector_expression (parser);
30456
30457 default:
30458 break;
30459 }
30460 /* FALLTHRU */
30461 default:
30462 error_at (kwd->location,
30463 "misplaced %<@%D%> Objective-C++ construct",
30464 kwd->u.value);
30465 cp_parser_skip_to_end_of_block_or_statement (parser);
30466 }
30467
30468 return error_mark_node;
30469 }
30470
30471 /* Parse an Objective-C message expression.
30472
30473 objc-message-expression:
30474 [ objc-message-receiver objc-message-args ]
30475
30476 Returns a representation of an Objective-C message. */
30477
30478 static tree
30479 cp_parser_objc_message_expression (cp_parser* parser)
30480 {
30481 tree receiver, messageargs;
30482
30483 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30484 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
30485 receiver = cp_parser_objc_message_receiver (parser);
30486 messageargs = cp_parser_objc_message_args (parser);
30487 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
30488 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
30489
30490 tree result = objc_build_message_expr (receiver, messageargs);
30491
30492 /* Construct a location e.g.
30493 [self func1:5]
30494 ^~~~~~~~~~~~~~
30495 ranging from the '[' to the ']', with the caret at the start. */
30496 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
30497 protected_set_expr_location (result, combined_loc);
30498
30499 return result;
30500 }
30501
30502 /* Parse an objc-message-receiver.
30503
30504 objc-message-receiver:
30505 expression
30506 simple-type-specifier
30507
30508 Returns a representation of the type or expression. */
30509
30510 static tree
30511 cp_parser_objc_message_receiver (cp_parser* parser)
30512 {
30513 tree rcv;
30514
30515 /* An Objective-C message receiver may be either (1) a type
30516 or (2) an expression. */
30517 cp_parser_parse_tentatively (parser);
30518 rcv = cp_parser_expression (parser);
30519
30520 /* If that worked out, fine. */
30521 if (cp_parser_parse_definitely (parser))
30522 return rcv;
30523
30524 cp_parser_parse_tentatively (parser);
30525 rcv = cp_parser_simple_type_specifier (parser,
30526 /*decl_specs=*/NULL,
30527 CP_PARSER_FLAGS_NONE);
30528
30529 if (cp_parser_parse_definitely (parser))
30530 return objc_get_class_reference (rcv);
30531
30532 cp_parser_error (parser, "objective-c++ message receiver expected");
30533 return error_mark_node;
30534 }
30535
30536 /* Parse the arguments and selectors comprising an Objective-C message.
30537
30538 objc-message-args:
30539 objc-selector
30540 objc-selector-args
30541 objc-selector-args , objc-comma-args
30542
30543 objc-selector-args:
30544 objc-selector [opt] : assignment-expression
30545 objc-selector-args objc-selector [opt] : assignment-expression
30546
30547 objc-comma-args:
30548 assignment-expression
30549 objc-comma-args , assignment-expression
30550
30551 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
30552 selector arguments and TREE_VALUE containing a list of comma
30553 arguments. */
30554
30555 static tree
30556 cp_parser_objc_message_args (cp_parser* parser)
30557 {
30558 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
30559 bool maybe_unary_selector_p = true;
30560 cp_token *token = cp_lexer_peek_token (parser->lexer);
30561
30562 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30563 {
30564 tree selector = NULL_TREE, arg;
30565
30566 if (token->type != CPP_COLON)
30567 selector = cp_parser_objc_selector (parser);
30568
30569 /* Detect if we have a unary selector. */
30570 if (maybe_unary_selector_p
30571 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30572 return build_tree_list (selector, NULL_TREE);
30573
30574 maybe_unary_selector_p = false;
30575 cp_parser_require (parser, CPP_COLON, RT_COLON);
30576 arg = cp_parser_assignment_expression (parser);
30577
30578 sel_args
30579 = chainon (sel_args,
30580 build_tree_list (selector, arg));
30581
30582 token = cp_lexer_peek_token (parser->lexer);
30583 }
30584
30585 /* Handle non-selector arguments, if any. */
30586 while (token->type == CPP_COMMA)
30587 {
30588 tree arg;
30589
30590 cp_lexer_consume_token (parser->lexer);
30591 arg = cp_parser_assignment_expression (parser);
30592
30593 addl_args
30594 = chainon (addl_args,
30595 build_tree_list (NULL_TREE, arg));
30596
30597 token = cp_lexer_peek_token (parser->lexer);
30598 }
30599
30600 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
30601 {
30602 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
30603 return build_tree_list (error_mark_node, error_mark_node);
30604 }
30605
30606 return build_tree_list (sel_args, addl_args);
30607 }
30608
30609 /* Parse an Objective-C encode expression.
30610
30611 objc-encode-expression:
30612 @encode objc-typename
30613
30614 Returns an encoded representation of the type argument. */
30615
30616 static cp_expr
30617 cp_parser_objc_encode_expression (cp_parser* parser)
30618 {
30619 tree type;
30620 cp_token *token;
30621 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30622
30623 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
30624 matching_parens parens;
30625 parens.require_open (parser);
30626 token = cp_lexer_peek_token (parser->lexer);
30627 type = complete_type (cp_parser_type_id (parser));
30628 parens.require_close (parser);
30629
30630 if (!type)
30631 {
30632 error_at (token->location,
30633 "%<@encode%> must specify a type as an argument");
30634 return error_mark_node;
30635 }
30636
30637 /* This happens if we find @encode(T) (where T is a template
30638 typename or something dependent on a template typename) when
30639 parsing a template. In that case, we can't compile it
30640 immediately, but we rather create an AT_ENCODE_EXPR which will
30641 need to be instantiated when the template is used.
30642 */
30643 if (dependent_type_p (type))
30644 {
30645 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
30646 TREE_READONLY (value) = 1;
30647 return value;
30648 }
30649
30650
30651 /* Build a location of the form:
30652 @encode(int)
30653 ^~~~~~~~~~~~
30654 with caret==start at the @ token, finishing at the close paren. */
30655 location_t combined_loc
30656 = make_location (start_loc, start_loc,
30657 cp_lexer_previous_token (parser->lexer)->location);
30658
30659 return cp_expr (objc_build_encode_expr (type), combined_loc);
30660 }
30661
30662 /* Parse an Objective-C @defs expression. */
30663
30664 static tree
30665 cp_parser_objc_defs_expression (cp_parser *parser)
30666 {
30667 tree name;
30668
30669 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
30670 matching_parens parens;
30671 parens.require_open (parser);
30672 name = cp_parser_identifier (parser);
30673 parens.require_close (parser);
30674
30675 return objc_get_class_ivars (name);
30676 }
30677
30678 /* Parse an Objective-C protocol expression.
30679
30680 objc-protocol-expression:
30681 @protocol ( identifier )
30682
30683 Returns a representation of the protocol expression. */
30684
30685 static tree
30686 cp_parser_objc_protocol_expression (cp_parser* parser)
30687 {
30688 tree proto;
30689 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30690
30691 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30692 matching_parens parens;
30693 parens.require_open (parser);
30694 proto = cp_parser_identifier (parser);
30695 parens.require_close (parser);
30696
30697 /* Build a location of the form:
30698 @protocol(prot)
30699 ^~~~~~~~~~~~~~~
30700 with caret==start at the @ token, finishing at the close paren. */
30701 location_t combined_loc
30702 = make_location (start_loc, start_loc,
30703 cp_lexer_previous_token (parser->lexer)->location);
30704 tree result = objc_build_protocol_expr (proto);
30705 protected_set_expr_location (result, combined_loc);
30706 return result;
30707 }
30708
30709 /* Parse an Objective-C selector expression.
30710
30711 objc-selector-expression:
30712 @selector ( objc-method-signature )
30713
30714 objc-method-signature:
30715 objc-selector
30716 objc-selector-seq
30717
30718 objc-selector-seq:
30719 objc-selector :
30720 objc-selector-seq objc-selector :
30721
30722 Returns a representation of the method selector. */
30723
30724 static tree
30725 cp_parser_objc_selector_expression (cp_parser* parser)
30726 {
30727 tree sel_seq = NULL_TREE;
30728 bool maybe_unary_selector_p = true;
30729 cp_token *token;
30730 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30731
30732 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
30733 matching_parens parens;
30734 parens.require_open (parser);
30735 token = cp_lexer_peek_token (parser->lexer);
30736
30737 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
30738 || token->type == CPP_SCOPE)
30739 {
30740 tree selector = NULL_TREE;
30741
30742 if (token->type != CPP_COLON
30743 || token->type == CPP_SCOPE)
30744 selector = cp_parser_objc_selector (parser);
30745
30746 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
30747 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
30748 {
30749 /* Detect if we have a unary selector. */
30750 if (maybe_unary_selector_p)
30751 {
30752 sel_seq = selector;
30753 goto finish_selector;
30754 }
30755 else
30756 {
30757 cp_parser_error (parser, "expected %<:%>");
30758 }
30759 }
30760 maybe_unary_selector_p = false;
30761 token = cp_lexer_consume_token (parser->lexer);
30762
30763 if (token->type == CPP_SCOPE)
30764 {
30765 sel_seq
30766 = chainon (sel_seq,
30767 build_tree_list (selector, NULL_TREE));
30768 sel_seq
30769 = chainon (sel_seq,
30770 build_tree_list (NULL_TREE, NULL_TREE));
30771 }
30772 else
30773 sel_seq
30774 = chainon (sel_seq,
30775 build_tree_list (selector, NULL_TREE));
30776
30777 token = cp_lexer_peek_token (parser->lexer);
30778 }
30779
30780 finish_selector:
30781 parens.require_close (parser);
30782
30783
30784 /* Build a location of the form:
30785 @selector(func)
30786 ^~~~~~~~~~~~~~~
30787 with caret==start at the @ token, finishing at the close paren. */
30788 location_t combined_loc
30789 = make_location (loc, loc,
30790 cp_lexer_previous_token (parser->lexer)->location);
30791 tree result = objc_build_selector_expr (combined_loc, sel_seq);
30792 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30793 protected_set_expr_location (result, combined_loc);
30794 return result;
30795 }
30796
30797 /* Parse a list of identifiers.
30798
30799 objc-identifier-list:
30800 identifier
30801 objc-identifier-list , identifier
30802
30803 Returns a TREE_LIST of identifier nodes. */
30804
30805 static tree
30806 cp_parser_objc_identifier_list (cp_parser* parser)
30807 {
30808 tree identifier;
30809 tree list;
30810 cp_token *sep;
30811
30812 identifier = cp_parser_identifier (parser);
30813 if (identifier == error_mark_node)
30814 return error_mark_node;
30815
30816 list = build_tree_list (NULL_TREE, identifier);
30817 sep = cp_lexer_peek_token (parser->lexer);
30818
30819 while (sep->type == CPP_COMMA)
30820 {
30821 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30822 identifier = cp_parser_identifier (parser);
30823 if (identifier == error_mark_node)
30824 return list;
30825
30826 list = chainon (list, build_tree_list (NULL_TREE,
30827 identifier));
30828 sep = cp_lexer_peek_token (parser->lexer);
30829 }
30830
30831 return list;
30832 }
30833
30834 /* Parse an Objective-C alias declaration.
30835
30836 objc-alias-declaration:
30837 @compatibility_alias identifier identifier ;
30838
30839 This function registers the alias mapping with the Objective-C front end.
30840 It returns nothing. */
30841
30842 static void
30843 cp_parser_objc_alias_declaration (cp_parser* parser)
30844 {
30845 tree alias, orig;
30846
30847 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
30848 alias = cp_parser_identifier (parser);
30849 orig = cp_parser_identifier (parser);
30850 objc_declare_alias (alias, orig);
30851 cp_parser_consume_semicolon_at_end_of_statement (parser);
30852 }
30853
30854 /* Parse an Objective-C class forward-declaration.
30855
30856 objc-class-declaration:
30857 @class objc-identifier-list ;
30858
30859 The function registers the forward declarations with the Objective-C
30860 front end. It returns nothing. */
30861
30862 static void
30863 cp_parser_objc_class_declaration (cp_parser* parser)
30864 {
30865 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
30866 while (true)
30867 {
30868 tree id;
30869
30870 id = cp_parser_identifier (parser);
30871 if (id == error_mark_node)
30872 break;
30873
30874 objc_declare_class (id);
30875
30876 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30877 cp_lexer_consume_token (parser->lexer);
30878 else
30879 break;
30880 }
30881 cp_parser_consume_semicolon_at_end_of_statement (parser);
30882 }
30883
30884 /* Parse a list of Objective-C protocol references.
30885
30886 objc-protocol-refs-opt:
30887 objc-protocol-refs [opt]
30888
30889 objc-protocol-refs:
30890 < objc-identifier-list >
30891
30892 Returns a TREE_LIST of identifiers, if any. */
30893
30894 static tree
30895 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30896 {
30897 tree protorefs = NULL_TREE;
30898
30899 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30900 {
30901 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
30902 protorefs = cp_parser_objc_identifier_list (parser);
30903 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30904 }
30905
30906 return protorefs;
30907 }
30908
30909 /* Parse a Objective-C visibility specification. */
30910
30911 static void
30912 cp_parser_objc_visibility_spec (cp_parser* parser)
30913 {
30914 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30915
30916 switch (vis->keyword)
30917 {
30918 case RID_AT_PRIVATE:
30919 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30920 break;
30921 case RID_AT_PROTECTED:
30922 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30923 break;
30924 case RID_AT_PUBLIC:
30925 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30926 break;
30927 case RID_AT_PACKAGE:
30928 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30929 break;
30930 default:
30931 return;
30932 }
30933
30934 /* Eat '@private'/'@protected'/'@public'. */
30935 cp_lexer_consume_token (parser->lexer);
30936 }
30937
30938 /* Parse an Objective-C method type. Return 'true' if it is a class
30939 (+) method, and 'false' if it is an instance (-) method. */
30940
30941 static inline bool
30942 cp_parser_objc_method_type (cp_parser* parser)
30943 {
30944 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30945 return true;
30946 else
30947 return false;
30948 }
30949
30950 /* Parse an Objective-C protocol qualifier. */
30951
30952 static tree
30953 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30954 {
30955 tree quals = NULL_TREE, node;
30956 cp_token *token = cp_lexer_peek_token (parser->lexer);
30957
30958 node = token->u.value;
30959
30960 while (node && identifier_p (node)
30961 && (node == ridpointers [(int) RID_IN]
30962 || node == ridpointers [(int) RID_OUT]
30963 || node == ridpointers [(int) RID_INOUT]
30964 || node == ridpointers [(int) RID_BYCOPY]
30965 || node == ridpointers [(int) RID_BYREF]
30966 || node == ridpointers [(int) RID_ONEWAY]))
30967 {
30968 quals = tree_cons (NULL_TREE, node, quals);
30969 cp_lexer_consume_token (parser->lexer);
30970 token = cp_lexer_peek_token (parser->lexer);
30971 node = token->u.value;
30972 }
30973
30974 return quals;
30975 }
30976
30977 /* Parse an Objective-C typename. */
30978
30979 static tree
30980 cp_parser_objc_typename (cp_parser* parser)
30981 {
30982 tree type_name = NULL_TREE;
30983
30984 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30985 {
30986 tree proto_quals, cp_type = NULL_TREE;
30987
30988 matching_parens parens;
30989 parens.consume_open (parser); /* Eat '('. */
30990 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30991
30992 /* An ObjC type name may consist of just protocol qualifiers, in which
30993 case the type shall default to 'id'. */
30994 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30995 {
30996 cp_type = cp_parser_type_id (parser);
30997
30998 /* If the type could not be parsed, an error has already
30999 been produced. For error recovery, behave as if it had
31000 not been specified, which will use the default type
31001 'id'. */
31002 if (cp_type == error_mark_node)
31003 {
31004 cp_type = NULL_TREE;
31005 /* We need to skip to the closing parenthesis as
31006 cp_parser_type_id() does not seem to do it for
31007 us. */
31008 cp_parser_skip_to_closing_parenthesis (parser,
31009 /*recovering=*/true,
31010 /*or_comma=*/false,
31011 /*consume_paren=*/false);
31012 }
31013 }
31014
31015 parens.require_close (parser);
31016 type_name = build_tree_list (proto_quals, cp_type);
31017 }
31018
31019 return type_name;
31020 }
31021
31022 /* Check to see if TYPE refers to an Objective-C selector name. */
31023
31024 static bool
31025 cp_parser_objc_selector_p (enum cpp_ttype type)
31026 {
31027 return (type == CPP_NAME || type == CPP_KEYWORD
31028 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
31029 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
31030 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
31031 || type == CPP_XOR || type == CPP_XOR_EQ);
31032 }
31033
31034 /* Parse an Objective-C selector. */
31035
31036 static tree
31037 cp_parser_objc_selector (cp_parser* parser)
31038 {
31039 cp_token *token = cp_lexer_consume_token (parser->lexer);
31040
31041 if (!cp_parser_objc_selector_p (token->type))
31042 {
31043 error_at (token->location, "invalid Objective-C++ selector name");
31044 return error_mark_node;
31045 }
31046
31047 /* C++ operator names are allowed to appear in ObjC selectors. */
31048 switch (token->type)
31049 {
31050 case CPP_AND_AND: return get_identifier ("and");
31051 case CPP_AND_EQ: return get_identifier ("and_eq");
31052 case CPP_AND: return get_identifier ("bitand");
31053 case CPP_OR: return get_identifier ("bitor");
31054 case CPP_COMPL: return get_identifier ("compl");
31055 case CPP_NOT: return get_identifier ("not");
31056 case CPP_NOT_EQ: return get_identifier ("not_eq");
31057 case CPP_OR_OR: return get_identifier ("or");
31058 case CPP_OR_EQ: return get_identifier ("or_eq");
31059 case CPP_XOR: return get_identifier ("xor");
31060 case CPP_XOR_EQ: return get_identifier ("xor_eq");
31061 default: return token->u.value;
31062 }
31063 }
31064
31065 /* Parse an Objective-C params list. */
31066
31067 static tree
31068 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
31069 {
31070 tree params = NULL_TREE;
31071 bool maybe_unary_selector_p = true;
31072 cp_token *token = cp_lexer_peek_token (parser->lexer);
31073
31074 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
31075 {
31076 tree selector = NULL_TREE, type_name, identifier;
31077 tree parm_attr = NULL_TREE;
31078
31079 if (token->keyword == RID_ATTRIBUTE)
31080 break;
31081
31082 if (token->type != CPP_COLON)
31083 selector = cp_parser_objc_selector (parser);
31084
31085 /* Detect if we have a unary selector. */
31086 if (maybe_unary_selector_p
31087 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31088 {
31089 params = selector; /* Might be followed by attributes. */
31090 break;
31091 }
31092
31093 maybe_unary_selector_p = false;
31094 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31095 {
31096 /* Something went quite wrong. There should be a colon
31097 here, but there is not. Stop parsing parameters. */
31098 break;
31099 }
31100 type_name = cp_parser_objc_typename (parser);
31101 /* New ObjC allows attributes on parameters too. */
31102 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31103 parm_attr = cp_parser_attributes_opt (parser);
31104 identifier = cp_parser_identifier (parser);
31105
31106 params
31107 = chainon (params,
31108 objc_build_keyword_decl (selector,
31109 type_name,
31110 identifier,
31111 parm_attr));
31112
31113 token = cp_lexer_peek_token (parser->lexer);
31114 }
31115
31116 if (params == NULL_TREE)
31117 {
31118 cp_parser_error (parser, "objective-c++ method declaration is expected");
31119 return error_mark_node;
31120 }
31121
31122 /* We allow tail attributes for the method. */
31123 if (token->keyword == RID_ATTRIBUTE)
31124 {
31125 *attributes = cp_parser_attributes_opt (parser);
31126 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
31127 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31128 return params;
31129 cp_parser_error (parser,
31130 "method attributes must be specified at the end");
31131 return error_mark_node;
31132 }
31133
31134 if (params == NULL_TREE)
31135 {
31136 cp_parser_error (parser, "objective-c++ method declaration is expected");
31137 return error_mark_node;
31138 }
31139 return params;
31140 }
31141
31142 /* Parse the non-keyword Objective-C params. */
31143
31144 static tree
31145 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
31146 tree* attributes)
31147 {
31148 tree params = make_node (TREE_LIST);
31149 cp_token *token = cp_lexer_peek_token (parser->lexer);
31150 *ellipsisp = false; /* Initially, assume no ellipsis. */
31151
31152 while (token->type == CPP_COMMA)
31153 {
31154 cp_parameter_declarator *parmdecl;
31155 tree parm;
31156
31157 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31158 token = cp_lexer_peek_token (parser->lexer);
31159
31160 if (token->type == CPP_ELLIPSIS)
31161 {
31162 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
31163 *ellipsisp = true;
31164 token = cp_lexer_peek_token (parser->lexer);
31165 break;
31166 }
31167
31168 /* TODO: parse attributes for tail parameters. */
31169 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
31170 false, NULL);
31171 parm = grokdeclarator (parmdecl->declarator,
31172 &parmdecl->decl_specifiers,
31173 PARM, /*initialized=*/0,
31174 /*attrlist=*/NULL);
31175
31176 chainon (params, build_tree_list (NULL_TREE, parm));
31177 token = cp_lexer_peek_token (parser->lexer);
31178 }
31179
31180 /* We allow tail attributes for the method. */
31181 if (token->keyword == RID_ATTRIBUTE)
31182 {
31183 if (*attributes == NULL_TREE)
31184 {
31185 *attributes = cp_parser_attributes_opt (parser);
31186 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
31187 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31188 return params;
31189 }
31190 else
31191 /* We have an error, but parse the attributes, so that we can
31192 carry on. */
31193 *attributes = cp_parser_attributes_opt (parser);
31194
31195 cp_parser_error (parser,
31196 "method attributes must be specified at the end");
31197 return error_mark_node;
31198 }
31199
31200 return params;
31201 }
31202
31203 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
31204
31205 static void
31206 cp_parser_objc_interstitial_code (cp_parser* parser)
31207 {
31208 cp_token *token = cp_lexer_peek_token (parser->lexer);
31209
31210 /* If the next token is `extern' and the following token is a string
31211 literal, then we have a linkage specification. */
31212 if (token->keyword == RID_EXTERN
31213 && cp_parser_is_pure_string_literal
31214 (cp_lexer_peek_nth_token (parser->lexer, 2)))
31215 cp_parser_linkage_specification (parser);
31216 /* Handle #pragma, if any. */
31217 else if (token->type == CPP_PRAGMA)
31218 cp_parser_pragma (parser, pragma_objc_icode, NULL);
31219 /* Allow stray semicolons. */
31220 else if (token->type == CPP_SEMICOLON)
31221 cp_lexer_consume_token (parser->lexer);
31222 /* Mark methods as optional or required, when building protocols. */
31223 else if (token->keyword == RID_AT_OPTIONAL)
31224 {
31225 cp_lexer_consume_token (parser->lexer);
31226 objc_set_method_opt (true);
31227 }
31228 else if (token->keyword == RID_AT_REQUIRED)
31229 {
31230 cp_lexer_consume_token (parser->lexer);
31231 objc_set_method_opt (false);
31232 }
31233 else if (token->keyword == RID_NAMESPACE)
31234 cp_parser_namespace_definition (parser);
31235 /* Other stray characters must generate errors. */
31236 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
31237 {
31238 cp_lexer_consume_token (parser->lexer);
31239 error ("stray %qs between Objective-C++ methods",
31240 token->type == CPP_OPEN_BRACE ? "{" : "}");
31241 }
31242 /* Finally, try to parse a block-declaration, or a function-definition. */
31243 else
31244 cp_parser_block_declaration (parser, /*statement_p=*/false);
31245 }
31246
31247 /* Parse a method signature. */
31248
31249 static tree
31250 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
31251 {
31252 tree rettype, kwdparms, optparms;
31253 bool ellipsis = false;
31254 bool is_class_method;
31255
31256 is_class_method = cp_parser_objc_method_type (parser);
31257 rettype = cp_parser_objc_typename (parser);
31258 *attributes = NULL_TREE;
31259 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
31260 if (kwdparms == error_mark_node)
31261 return error_mark_node;
31262 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
31263 if (optparms == error_mark_node)
31264 return error_mark_node;
31265
31266 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
31267 }
31268
31269 static bool
31270 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
31271 {
31272 tree tattr;
31273 cp_lexer_save_tokens (parser->lexer);
31274 tattr = cp_parser_attributes_opt (parser);
31275 gcc_assert (tattr) ;
31276
31277 /* If the attributes are followed by a method introducer, this is not allowed.
31278 Dump the attributes and flag the situation. */
31279 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
31280 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31281 return true;
31282
31283 /* Otherwise, the attributes introduce some interstitial code, possibly so
31284 rewind to allow that check. */
31285 cp_lexer_rollback_tokens (parser->lexer);
31286 return false;
31287 }
31288
31289 /* Parse an Objective-C method prototype list. */
31290
31291 static void
31292 cp_parser_objc_method_prototype_list (cp_parser* parser)
31293 {
31294 cp_token *token = cp_lexer_peek_token (parser->lexer);
31295
31296 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
31297 {
31298 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
31299 {
31300 tree attributes, sig;
31301 bool is_class_method;
31302 if (token->type == CPP_PLUS)
31303 is_class_method = true;
31304 else
31305 is_class_method = false;
31306 sig = cp_parser_objc_method_signature (parser, &attributes);
31307 if (sig == error_mark_node)
31308 {
31309 cp_parser_skip_to_end_of_block_or_statement (parser);
31310 token = cp_lexer_peek_token (parser->lexer);
31311 continue;
31312 }
31313 objc_add_method_declaration (is_class_method, sig, attributes);
31314 cp_parser_consume_semicolon_at_end_of_statement (parser);
31315 }
31316 else if (token->keyword == RID_AT_PROPERTY)
31317 cp_parser_objc_at_property_declaration (parser);
31318 else if (token->keyword == RID_ATTRIBUTE
31319 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
31320 warning_at (cp_lexer_peek_token (parser->lexer)->location,
31321 OPT_Wattributes,
31322 "prefix attributes are ignored for methods");
31323 else
31324 /* Allow for interspersed non-ObjC++ code. */
31325 cp_parser_objc_interstitial_code (parser);
31326
31327 token = cp_lexer_peek_token (parser->lexer);
31328 }
31329
31330 if (token->type != CPP_EOF)
31331 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31332 else
31333 cp_parser_error (parser, "expected %<@end%>");
31334
31335 objc_finish_interface ();
31336 }
31337
31338 /* Parse an Objective-C method definition list. */
31339
31340 static void
31341 cp_parser_objc_method_definition_list (cp_parser* parser)
31342 {
31343 cp_token *token = cp_lexer_peek_token (parser->lexer);
31344
31345 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
31346 {
31347 tree meth;
31348
31349 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
31350 {
31351 cp_token *ptk;
31352 tree sig, attribute;
31353 bool is_class_method;
31354 if (token->type == CPP_PLUS)
31355 is_class_method = true;
31356 else
31357 is_class_method = false;
31358 push_deferring_access_checks (dk_deferred);
31359 sig = cp_parser_objc_method_signature (parser, &attribute);
31360 if (sig == error_mark_node)
31361 {
31362 cp_parser_skip_to_end_of_block_or_statement (parser);
31363 token = cp_lexer_peek_token (parser->lexer);
31364 continue;
31365 }
31366 objc_start_method_definition (is_class_method, sig, attribute,
31367 NULL_TREE);
31368
31369 /* For historical reasons, we accept an optional semicolon. */
31370 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31371 cp_lexer_consume_token (parser->lexer);
31372
31373 ptk = cp_lexer_peek_token (parser->lexer);
31374 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
31375 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
31376 {
31377 perform_deferred_access_checks (tf_warning_or_error);
31378 stop_deferring_access_checks ();
31379 meth = cp_parser_function_definition_after_declarator (parser,
31380 false);
31381 pop_deferring_access_checks ();
31382 objc_finish_method_definition (meth);
31383 }
31384 }
31385 /* The following case will be removed once @synthesize is
31386 completely implemented. */
31387 else if (token->keyword == RID_AT_PROPERTY)
31388 cp_parser_objc_at_property_declaration (parser);
31389 else if (token->keyword == RID_AT_SYNTHESIZE)
31390 cp_parser_objc_at_synthesize_declaration (parser);
31391 else if (token->keyword == RID_AT_DYNAMIC)
31392 cp_parser_objc_at_dynamic_declaration (parser);
31393 else if (token->keyword == RID_ATTRIBUTE
31394 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
31395 warning_at (token->location, OPT_Wattributes,
31396 "prefix attributes are ignored for methods");
31397 else
31398 /* Allow for interspersed non-ObjC++ code. */
31399 cp_parser_objc_interstitial_code (parser);
31400
31401 token = cp_lexer_peek_token (parser->lexer);
31402 }
31403
31404 if (token->type != CPP_EOF)
31405 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31406 else
31407 cp_parser_error (parser, "expected %<@end%>");
31408
31409 objc_finish_implementation ();
31410 }
31411
31412 /* Parse Objective-C ivars. */
31413
31414 static void
31415 cp_parser_objc_class_ivars (cp_parser* parser)
31416 {
31417 cp_token *token = cp_lexer_peek_token (parser->lexer);
31418
31419 if (token->type != CPP_OPEN_BRACE)
31420 return; /* No ivars specified. */
31421
31422 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
31423 token = cp_lexer_peek_token (parser->lexer);
31424
31425 while (token->type != CPP_CLOSE_BRACE
31426 && token->keyword != RID_AT_END && token->type != CPP_EOF)
31427 {
31428 cp_decl_specifier_seq declspecs;
31429 int decl_class_or_enum_p;
31430 tree prefix_attributes;
31431
31432 cp_parser_objc_visibility_spec (parser);
31433
31434 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31435 break;
31436
31437 cp_parser_decl_specifier_seq (parser,
31438 CP_PARSER_FLAGS_OPTIONAL,
31439 &declspecs,
31440 &decl_class_or_enum_p);
31441
31442 /* auto, register, static, extern, mutable. */
31443 if (declspecs.storage_class != sc_none)
31444 {
31445 cp_parser_error (parser, "invalid type for instance variable");
31446 declspecs.storage_class = sc_none;
31447 }
31448
31449 /* thread_local. */
31450 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31451 {
31452 cp_parser_error (parser, "invalid type for instance variable");
31453 declspecs.locations[ds_thread] = 0;
31454 }
31455
31456 /* typedef. */
31457 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31458 {
31459 cp_parser_error (parser, "invalid type for instance variable");
31460 declspecs.locations[ds_typedef] = 0;
31461 }
31462
31463 prefix_attributes = declspecs.attributes;
31464 declspecs.attributes = NULL_TREE;
31465
31466 /* Keep going until we hit the `;' at the end of the
31467 declaration. */
31468 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31469 {
31470 tree width = NULL_TREE, attributes, first_attribute, decl;
31471 cp_declarator *declarator = NULL;
31472 int ctor_dtor_or_conv_p;
31473
31474 /* Check for a (possibly unnamed) bitfield declaration. */
31475 token = cp_lexer_peek_token (parser->lexer);
31476 if (token->type == CPP_COLON)
31477 goto eat_colon;
31478
31479 if (token->type == CPP_NAME
31480 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
31481 == CPP_COLON))
31482 {
31483 /* Get the name of the bitfield. */
31484 declarator = make_id_declarator (NULL_TREE,
31485 cp_parser_identifier (parser),
31486 sfk_none, token->location);
31487
31488 eat_colon:
31489 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31490 /* Get the width of the bitfield. */
31491 width
31492 = cp_parser_constant_expression (parser);
31493 }
31494 else
31495 {
31496 /* Parse the declarator. */
31497 declarator
31498 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31499 CP_PARSER_FLAGS_NONE,
31500 &ctor_dtor_or_conv_p,
31501 /*parenthesized_p=*/NULL,
31502 /*member_p=*/false,
31503 /*friend_p=*/false,
31504 /*static_p=*/false);
31505 }
31506
31507 /* Look for attributes that apply to the ivar. */
31508 attributes = cp_parser_attributes_opt (parser);
31509 /* Remember which attributes are prefix attributes and
31510 which are not. */
31511 first_attribute = attributes;
31512 /* Combine the attributes. */
31513 attributes = attr_chainon (prefix_attributes, attributes);
31514
31515 if (width)
31516 /* Create the bitfield declaration. */
31517 decl = grokbitfield (declarator, &declspecs,
31518 width, NULL_TREE, attributes);
31519 else
31520 decl = grokfield (declarator, &declspecs,
31521 NULL_TREE, /*init_const_expr_p=*/false,
31522 NULL_TREE, attributes);
31523
31524 /* Add the instance variable. */
31525 if (decl != error_mark_node && decl != NULL_TREE)
31526 objc_add_instance_variable (decl);
31527
31528 /* Reset PREFIX_ATTRIBUTES. */
31529 if (attributes != error_mark_node)
31530 {
31531 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31532 attributes = TREE_CHAIN (attributes);
31533 if (attributes)
31534 TREE_CHAIN (attributes) = NULL_TREE;
31535 }
31536
31537 token = cp_lexer_peek_token (parser->lexer);
31538
31539 if (token->type == CPP_COMMA)
31540 {
31541 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31542 continue;
31543 }
31544 break;
31545 }
31546
31547 cp_parser_consume_semicolon_at_end_of_statement (parser);
31548 token = cp_lexer_peek_token (parser->lexer);
31549 }
31550
31551 if (token->keyword == RID_AT_END)
31552 cp_parser_error (parser, "expected %<}%>");
31553
31554 /* Do not consume the RID_AT_END, so it will be read again as terminating
31555 the @interface of @implementation. */
31556 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
31557 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
31558
31559 /* For historical reasons, we accept an optional semicolon. */
31560 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31561 cp_lexer_consume_token (parser->lexer);
31562 }
31563
31564 /* Parse an Objective-C protocol declaration. */
31565
31566 static void
31567 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
31568 {
31569 tree proto, protorefs;
31570 cp_token *tok;
31571
31572 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
31573 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31574 {
31575 tok = cp_lexer_peek_token (parser->lexer);
31576 error_at (tok->location, "identifier expected after %<@protocol%>");
31577 cp_parser_consume_semicolon_at_end_of_statement (parser);
31578 return;
31579 }
31580
31581 /* See if we have a forward declaration or a definition. */
31582 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
31583
31584 /* Try a forward declaration first. */
31585 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
31586 {
31587 while (true)
31588 {
31589 tree id;
31590
31591 id = cp_parser_identifier (parser);
31592 if (id == error_mark_node)
31593 break;
31594
31595 objc_declare_protocol (id, attributes);
31596
31597 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31598 cp_lexer_consume_token (parser->lexer);
31599 else
31600 break;
31601 }
31602 cp_parser_consume_semicolon_at_end_of_statement (parser);
31603 }
31604
31605 /* Ok, we got a full-fledged definition (or at least should). */
31606 else
31607 {
31608 proto = cp_parser_identifier (parser);
31609 protorefs = cp_parser_objc_protocol_refs_opt (parser);
31610 objc_start_protocol (proto, protorefs, attributes);
31611 cp_parser_objc_method_prototype_list (parser);
31612 }
31613 }
31614
31615 /* Parse an Objective-C superclass or category. */
31616
31617 static void
31618 cp_parser_objc_superclass_or_category (cp_parser *parser,
31619 bool iface_p,
31620 tree *super,
31621 tree *categ, bool *is_class_extension)
31622 {
31623 cp_token *next = cp_lexer_peek_token (parser->lexer);
31624
31625 *super = *categ = NULL_TREE;
31626 *is_class_extension = false;
31627 if (next->type == CPP_COLON)
31628 {
31629 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31630 *super = cp_parser_identifier (parser);
31631 }
31632 else if (next->type == CPP_OPEN_PAREN)
31633 {
31634 matching_parens parens;
31635 parens.consume_open (parser); /* Eat '('. */
31636
31637 /* If there is no category name, and this is an @interface, we
31638 have a class extension. */
31639 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31640 {
31641 *categ = NULL_TREE;
31642 *is_class_extension = true;
31643 }
31644 else
31645 *categ = cp_parser_identifier (parser);
31646
31647 parens.require_close (parser);
31648 }
31649 }
31650
31651 /* Parse an Objective-C class interface. */
31652
31653 static void
31654 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
31655 {
31656 tree name, super, categ, protos;
31657 bool is_class_extension;
31658
31659 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
31660 name = cp_parser_identifier (parser);
31661 if (name == error_mark_node)
31662 {
31663 /* It's hard to recover because even if valid @interface stuff
31664 is to follow, we can't compile it (or validate it) if we
31665 don't even know which class it refers to. Let's assume this
31666 was a stray '@interface' token in the stream and skip it.
31667 */
31668 return;
31669 }
31670 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
31671 &is_class_extension);
31672 protos = cp_parser_objc_protocol_refs_opt (parser);
31673
31674 /* We have either a class or a category on our hands. */
31675 if (categ || is_class_extension)
31676 objc_start_category_interface (name, categ, protos, attributes);
31677 else
31678 {
31679 objc_start_class_interface (name, super, protos, attributes);
31680 /* Handle instance variable declarations, if any. */
31681 cp_parser_objc_class_ivars (parser);
31682 objc_continue_interface ();
31683 }
31684
31685 cp_parser_objc_method_prototype_list (parser);
31686 }
31687
31688 /* Parse an Objective-C class implementation. */
31689
31690 static void
31691 cp_parser_objc_class_implementation (cp_parser* parser)
31692 {
31693 tree name, super, categ;
31694 bool is_class_extension;
31695
31696 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
31697 name = cp_parser_identifier (parser);
31698 if (name == error_mark_node)
31699 {
31700 /* It's hard to recover because even if valid @implementation
31701 stuff is to follow, we can't compile it (or validate it) if
31702 we don't even know which class it refers to. Let's assume
31703 this was a stray '@implementation' token in the stream and
31704 skip it.
31705 */
31706 return;
31707 }
31708 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
31709 &is_class_extension);
31710
31711 /* We have either a class or a category on our hands. */
31712 if (categ)
31713 objc_start_category_implementation (name, categ);
31714 else
31715 {
31716 objc_start_class_implementation (name, super);
31717 /* Handle instance variable declarations, if any. */
31718 cp_parser_objc_class_ivars (parser);
31719 objc_continue_implementation ();
31720 }
31721
31722 cp_parser_objc_method_definition_list (parser);
31723 }
31724
31725 /* Consume the @end token and finish off the implementation. */
31726
31727 static void
31728 cp_parser_objc_end_implementation (cp_parser* parser)
31729 {
31730 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31731 objc_finish_implementation ();
31732 }
31733
31734 /* Parse an Objective-C declaration. */
31735
31736 static void
31737 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
31738 {
31739 /* Try to figure out what kind of declaration is present. */
31740 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31741
31742 if (attributes)
31743 switch (kwd->keyword)
31744 {
31745 case RID_AT_ALIAS:
31746 case RID_AT_CLASS:
31747 case RID_AT_END:
31748 error_at (kwd->location, "attributes may not be specified before"
31749 " the %<@%D%> Objective-C++ keyword",
31750 kwd->u.value);
31751 attributes = NULL;
31752 break;
31753 case RID_AT_IMPLEMENTATION:
31754 warning_at (kwd->location, OPT_Wattributes,
31755 "prefix attributes are ignored before %<@%D%>",
31756 kwd->u.value);
31757 attributes = NULL;
31758 default:
31759 break;
31760 }
31761
31762 switch (kwd->keyword)
31763 {
31764 case RID_AT_ALIAS:
31765 cp_parser_objc_alias_declaration (parser);
31766 break;
31767 case RID_AT_CLASS:
31768 cp_parser_objc_class_declaration (parser);
31769 break;
31770 case RID_AT_PROTOCOL:
31771 cp_parser_objc_protocol_declaration (parser, attributes);
31772 break;
31773 case RID_AT_INTERFACE:
31774 cp_parser_objc_class_interface (parser, attributes);
31775 break;
31776 case RID_AT_IMPLEMENTATION:
31777 cp_parser_objc_class_implementation (parser);
31778 break;
31779 case RID_AT_END:
31780 cp_parser_objc_end_implementation (parser);
31781 break;
31782 default:
31783 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31784 kwd->u.value);
31785 cp_parser_skip_to_end_of_block_or_statement (parser);
31786 }
31787 }
31788
31789 /* Parse an Objective-C try-catch-finally statement.
31790
31791 objc-try-catch-finally-stmt:
31792 @try compound-statement objc-catch-clause-seq [opt]
31793 objc-finally-clause [opt]
31794
31795 objc-catch-clause-seq:
31796 objc-catch-clause objc-catch-clause-seq [opt]
31797
31798 objc-catch-clause:
31799 @catch ( objc-exception-declaration ) compound-statement
31800
31801 objc-finally-clause:
31802 @finally compound-statement
31803
31804 objc-exception-declaration:
31805 parameter-declaration
31806 '...'
31807
31808 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31809
31810 Returns NULL_TREE.
31811
31812 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31813 for C. Keep them in sync. */
31814
31815 static tree
31816 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
31817 {
31818 location_t location;
31819 tree stmt;
31820
31821 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
31822 location = cp_lexer_peek_token (parser->lexer)->location;
31823 objc_maybe_warn_exceptions (location);
31824 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31825 node, lest it get absorbed into the surrounding block. */
31826 stmt = push_stmt_list ();
31827 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31828 objc_begin_try_stmt (location, pop_stmt_list (stmt));
31829
31830 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
31831 {
31832 cp_parameter_declarator *parm;
31833 tree parameter_declaration = error_mark_node;
31834 bool seen_open_paren = false;
31835 matching_parens parens;
31836
31837 cp_lexer_consume_token (parser->lexer);
31838 if (parens.require_open (parser))
31839 seen_open_paren = true;
31840 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31841 {
31842 /* We have "@catch (...)" (where the '...' are literally
31843 what is in the code). Skip the '...'.
31844 parameter_declaration is set to NULL_TREE, and
31845 objc_being_catch_clauses() knows that that means
31846 '...'. */
31847 cp_lexer_consume_token (parser->lexer);
31848 parameter_declaration = NULL_TREE;
31849 }
31850 else
31851 {
31852 /* We have "@catch (NSException *exception)" or something
31853 like that. Parse the parameter declaration. */
31854 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
31855 false, NULL);
31856 if (parm == NULL)
31857 parameter_declaration = error_mark_node;
31858 else
31859 parameter_declaration = grokdeclarator (parm->declarator,
31860 &parm->decl_specifiers,
31861 PARM, /*initialized=*/0,
31862 /*attrlist=*/NULL);
31863 }
31864 if (seen_open_paren)
31865 parens.require_close (parser);
31866 else
31867 {
31868 /* If there was no open parenthesis, we are recovering from
31869 an error, and we are trying to figure out what mistake
31870 the user has made. */
31871
31872 /* If there is an immediate closing parenthesis, the user
31873 probably forgot the opening one (ie, they typed "@catch
31874 NSException *e)". Parse the closing parenthesis and keep
31875 going. */
31876 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31877 cp_lexer_consume_token (parser->lexer);
31878
31879 /* If these is no immediate closing parenthesis, the user
31880 probably doesn't know that parenthesis are required at
31881 all (ie, they typed "@catch NSException *e"). So, just
31882 forget about the closing parenthesis and keep going. */
31883 }
31884 objc_begin_catch_clause (parameter_declaration);
31885 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31886 objc_finish_catch_clause ();
31887 }
31888 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
31889 {
31890 cp_lexer_consume_token (parser->lexer);
31891 location = cp_lexer_peek_token (parser->lexer)->location;
31892 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31893 node, lest it get absorbed into the surrounding block. */
31894 stmt = push_stmt_list ();
31895 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31896 objc_build_finally_clause (location, pop_stmt_list (stmt));
31897 }
31898
31899 return objc_finish_try_stmt ();
31900 }
31901
31902 /* Parse an Objective-C synchronized statement.
31903
31904 objc-synchronized-stmt:
31905 @synchronized ( expression ) compound-statement
31906
31907 Returns NULL_TREE. */
31908
31909 static tree
31910 cp_parser_objc_synchronized_statement (cp_parser *parser)
31911 {
31912 location_t location;
31913 tree lock, stmt;
31914
31915 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31916
31917 location = cp_lexer_peek_token (parser->lexer)->location;
31918 objc_maybe_warn_exceptions (location);
31919 matching_parens parens;
31920 parens.require_open (parser);
31921 lock = cp_parser_expression (parser);
31922 parens.require_close (parser);
31923
31924 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31925 node, lest it get absorbed into the surrounding block. */
31926 stmt = push_stmt_list ();
31927 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31928
31929 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31930 }
31931
31932 /* Parse an Objective-C throw statement.
31933
31934 objc-throw-stmt:
31935 @throw assignment-expression [opt] ;
31936
31937 Returns a constructed '@throw' statement. */
31938
31939 static tree
31940 cp_parser_objc_throw_statement (cp_parser *parser)
31941 {
31942 tree expr = NULL_TREE;
31943 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31944
31945 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31946
31947 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31948 expr = cp_parser_expression (parser);
31949
31950 cp_parser_consume_semicolon_at_end_of_statement (parser);
31951
31952 return objc_build_throw_stmt (loc, expr);
31953 }
31954
31955 /* Parse an Objective-C statement. */
31956
31957 static tree
31958 cp_parser_objc_statement (cp_parser * parser)
31959 {
31960 /* Try to figure out what kind of declaration is present. */
31961 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31962
31963 switch (kwd->keyword)
31964 {
31965 case RID_AT_TRY:
31966 return cp_parser_objc_try_catch_finally_statement (parser);
31967 case RID_AT_SYNCHRONIZED:
31968 return cp_parser_objc_synchronized_statement (parser);
31969 case RID_AT_THROW:
31970 return cp_parser_objc_throw_statement (parser);
31971 default:
31972 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31973 kwd->u.value);
31974 cp_parser_skip_to_end_of_block_or_statement (parser);
31975 }
31976
31977 return error_mark_node;
31978 }
31979
31980 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31981 look ahead to see if an objc keyword follows the attributes. This
31982 is to detect the use of prefix attributes on ObjC @interface and
31983 @protocol. */
31984
31985 static bool
31986 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31987 {
31988 cp_lexer_save_tokens (parser->lexer);
31989 *attrib = cp_parser_attributes_opt (parser);
31990 gcc_assert (*attrib);
31991 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31992 {
31993 cp_lexer_commit_tokens (parser->lexer);
31994 return true;
31995 }
31996 cp_lexer_rollback_tokens (parser->lexer);
31997 return false;
31998 }
31999
32000 /* This routine is a minimal replacement for
32001 c_parser_struct_declaration () used when parsing the list of
32002 types/names or ObjC++ properties. For example, when parsing the
32003 code
32004
32005 @property (readonly) int a, b, c;
32006
32007 this function is responsible for parsing "int a, int b, int c" and
32008 returning the declarations as CHAIN of DECLs.
32009
32010 TODO: Share this code with cp_parser_objc_class_ivars. It's very
32011 similar parsing. */
32012 static tree
32013 cp_parser_objc_struct_declaration (cp_parser *parser)
32014 {
32015 tree decls = NULL_TREE;
32016 cp_decl_specifier_seq declspecs;
32017 int decl_class_or_enum_p;
32018 tree prefix_attributes;
32019
32020 cp_parser_decl_specifier_seq (parser,
32021 CP_PARSER_FLAGS_NONE,
32022 &declspecs,
32023 &decl_class_or_enum_p);
32024
32025 if (declspecs.type == error_mark_node)
32026 return error_mark_node;
32027
32028 /* auto, register, static, extern, mutable. */
32029 if (declspecs.storage_class != sc_none)
32030 {
32031 cp_parser_error (parser, "invalid type for property");
32032 declspecs.storage_class = sc_none;
32033 }
32034
32035 /* thread_local. */
32036 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
32037 {
32038 cp_parser_error (parser, "invalid type for property");
32039 declspecs.locations[ds_thread] = 0;
32040 }
32041
32042 /* typedef. */
32043 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
32044 {
32045 cp_parser_error (parser, "invalid type for property");
32046 declspecs.locations[ds_typedef] = 0;
32047 }
32048
32049 prefix_attributes = declspecs.attributes;
32050 declspecs.attributes = NULL_TREE;
32051
32052 /* Keep going until we hit the `;' at the end of the declaration. */
32053 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
32054 {
32055 tree attributes, first_attribute, decl;
32056 cp_declarator *declarator;
32057 cp_token *token;
32058
32059 /* Parse the declarator. */
32060 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
32061 CP_PARSER_FLAGS_NONE,
32062 NULL, NULL, false, false, false);
32063
32064 /* Look for attributes that apply to the ivar. */
32065 attributes = cp_parser_attributes_opt (parser);
32066 /* Remember which attributes are prefix attributes and
32067 which are not. */
32068 first_attribute = attributes;
32069 /* Combine the attributes. */
32070 attributes = attr_chainon (prefix_attributes, attributes);
32071
32072 decl = grokfield (declarator, &declspecs,
32073 NULL_TREE, /*init_const_expr_p=*/false,
32074 NULL_TREE, attributes);
32075
32076 if (decl == error_mark_node || decl == NULL_TREE)
32077 return error_mark_node;
32078
32079 /* Reset PREFIX_ATTRIBUTES. */
32080 if (attributes != error_mark_node)
32081 {
32082 while (attributes && TREE_CHAIN (attributes) != first_attribute)
32083 attributes = TREE_CHAIN (attributes);
32084 if (attributes)
32085 TREE_CHAIN (attributes) = NULL_TREE;
32086 }
32087
32088 DECL_CHAIN (decl) = decls;
32089 decls = decl;
32090
32091 token = cp_lexer_peek_token (parser->lexer);
32092 if (token->type == CPP_COMMA)
32093 {
32094 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
32095 continue;
32096 }
32097 else
32098 break;
32099 }
32100 return decls;
32101 }
32102
32103 /* Parse an Objective-C @property declaration. The syntax is:
32104
32105 objc-property-declaration:
32106 '@property' objc-property-attributes[opt] struct-declaration ;
32107
32108 objc-property-attributes:
32109 '(' objc-property-attribute-list ')'
32110
32111 objc-property-attribute-list:
32112 objc-property-attribute
32113 objc-property-attribute-list, objc-property-attribute
32114
32115 objc-property-attribute
32116 'getter' = identifier
32117 'setter' = identifier
32118 'readonly'
32119 'readwrite'
32120 'assign'
32121 'retain'
32122 'copy'
32123 'nonatomic'
32124
32125 For example:
32126 @property NSString *name;
32127 @property (readonly) id object;
32128 @property (retain, nonatomic, getter=getTheName) id name;
32129 @property int a, b, c;
32130
32131 PS: This function is identical to
32132 c_parser_objc_at_property_declaration for C. Keep them in sync. */
32133 static void
32134 cp_parser_objc_at_property_declaration (cp_parser *parser)
32135 {
32136 /* The following variables hold the attributes of the properties as
32137 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
32138 seen. When we see an attribute, we set them to 'true' (if they
32139 are boolean properties) or to the identifier (if they have an
32140 argument, ie, for getter and setter). Note that here we only
32141 parse the list of attributes, check the syntax and accumulate the
32142 attributes that we find. objc_add_property_declaration() will
32143 then process the information. */
32144 bool property_assign = false;
32145 bool property_copy = false;
32146 tree property_getter_ident = NULL_TREE;
32147 bool property_nonatomic = false;
32148 bool property_readonly = false;
32149 bool property_readwrite = false;
32150 bool property_retain = false;
32151 tree property_setter_ident = NULL_TREE;
32152
32153 /* 'properties' is the list of properties that we read. Usually a
32154 single one, but maybe more (eg, in "@property int a, b, c;" there
32155 are three). */
32156 tree properties;
32157 location_t loc;
32158
32159 loc = cp_lexer_peek_token (parser->lexer)->location;
32160
32161 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
32162
32163 /* Parse the optional attribute list... */
32164 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32165 {
32166 /* Eat the '('. */
32167 matching_parens parens;
32168 parens.consume_open (parser);
32169
32170 while (true)
32171 {
32172 bool syntax_error = false;
32173 cp_token *token = cp_lexer_peek_token (parser->lexer);
32174 enum rid keyword;
32175
32176 if (token->type != CPP_NAME)
32177 {
32178 cp_parser_error (parser, "expected identifier");
32179 break;
32180 }
32181 keyword = C_RID_CODE (token->u.value);
32182 cp_lexer_consume_token (parser->lexer);
32183 switch (keyword)
32184 {
32185 case RID_ASSIGN: property_assign = true; break;
32186 case RID_COPY: property_copy = true; break;
32187 case RID_NONATOMIC: property_nonatomic = true; break;
32188 case RID_READONLY: property_readonly = true; break;
32189 case RID_READWRITE: property_readwrite = true; break;
32190 case RID_RETAIN: property_retain = true; break;
32191
32192 case RID_GETTER:
32193 case RID_SETTER:
32194 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
32195 {
32196 if (keyword == RID_GETTER)
32197 cp_parser_error (parser,
32198 "missing %<=%> (after %<getter%> attribute)");
32199 else
32200 cp_parser_error (parser,
32201 "missing %<=%> (after %<setter%> attribute)");
32202 syntax_error = true;
32203 break;
32204 }
32205 cp_lexer_consume_token (parser->lexer); /* eat the = */
32206 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
32207 {
32208 cp_parser_error (parser, "expected identifier");
32209 syntax_error = true;
32210 break;
32211 }
32212 if (keyword == RID_SETTER)
32213 {
32214 if (property_setter_ident != NULL_TREE)
32215 {
32216 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
32217 cp_lexer_consume_token (parser->lexer);
32218 }
32219 else
32220 property_setter_ident = cp_parser_objc_selector (parser);
32221 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
32222 cp_parser_error (parser, "setter name must terminate with %<:%>");
32223 else
32224 cp_lexer_consume_token (parser->lexer);
32225 }
32226 else
32227 {
32228 if (property_getter_ident != NULL_TREE)
32229 {
32230 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
32231 cp_lexer_consume_token (parser->lexer);
32232 }
32233 else
32234 property_getter_ident = cp_parser_objc_selector (parser);
32235 }
32236 break;
32237 default:
32238 cp_parser_error (parser, "unknown property attribute");
32239 syntax_error = true;
32240 break;
32241 }
32242
32243 if (syntax_error)
32244 break;
32245
32246 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32247 cp_lexer_consume_token (parser->lexer);
32248 else
32249 break;
32250 }
32251
32252 /* FIXME: "@property (setter, assign);" will generate a spurious
32253 "error: expected ‘)’ before ‘,’ token". This is because
32254 cp_parser_require, unlike the C counterpart, will produce an
32255 error even if we are in error recovery. */
32256 if (!parens.require_close (parser))
32257 {
32258 cp_parser_skip_to_closing_parenthesis (parser,
32259 /*recovering=*/true,
32260 /*or_comma=*/false,
32261 /*consume_paren=*/true);
32262 }
32263 }
32264
32265 /* ... and the property declaration(s). */
32266 properties = cp_parser_objc_struct_declaration (parser);
32267
32268 if (properties == error_mark_node)
32269 {
32270 cp_parser_skip_to_end_of_statement (parser);
32271 /* If the next token is now a `;', consume it. */
32272 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
32273 cp_lexer_consume_token (parser->lexer);
32274 return;
32275 }
32276
32277 if (properties == NULL_TREE)
32278 cp_parser_error (parser, "expected identifier");
32279 else
32280 {
32281 /* Comma-separated properties are chained together in
32282 reverse order; add them one by one. */
32283 properties = nreverse (properties);
32284
32285 for (; properties; properties = TREE_CHAIN (properties))
32286 objc_add_property_declaration (loc, copy_node (properties),
32287 property_readonly, property_readwrite,
32288 property_assign, property_retain,
32289 property_copy, property_nonatomic,
32290 property_getter_ident, property_setter_ident);
32291 }
32292
32293 cp_parser_consume_semicolon_at_end_of_statement (parser);
32294 }
32295
32296 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
32297
32298 objc-synthesize-declaration:
32299 @synthesize objc-synthesize-identifier-list ;
32300
32301 objc-synthesize-identifier-list:
32302 objc-synthesize-identifier
32303 objc-synthesize-identifier-list, objc-synthesize-identifier
32304
32305 objc-synthesize-identifier
32306 identifier
32307 identifier = identifier
32308
32309 For example:
32310 @synthesize MyProperty;
32311 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
32312
32313 PS: This function is identical to c_parser_objc_at_synthesize_declaration
32314 for C. Keep them in sync.
32315 */
32316 static void
32317 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
32318 {
32319 tree list = NULL_TREE;
32320 location_t loc;
32321 loc = cp_lexer_peek_token (parser->lexer)->location;
32322
32323 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
32324 while (true)
32325 {
32326 tree property, ivar;
32327 property = cp_parser_identifier (parser);
32328 if (property == error_mark_node)
32329 {
32330 cp_parser_consume_semicolon_at_end_of_statement (parser);
32331 return;
32332 }
32333 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
32334 {
32335 cp_lexer_consume_token (parser->lexer);
32336 ivar = cp_parser_identifier (parser);
32337 if (ivar == error_mark_node)
32338 {
32339 cp_parser_consume_semicolon_at_end_of_statement (parser);
32340 return;
32341 }
32342 }
32343 else
32344 ivar = NULL_TREE;
32345 list = chainon (list, build_tree_list (ivar, property));
32346 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32347 cp_lexer_consume_token (parser->lexer);
32348 else
32349 break;
32350 }
32351 cp_parser_consume_semicolon_at_end_of_statement (parser);
32352 objc_add_synthesize_declaration (loc, list);
32353 }
32354
32355 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
32356
32357 objc-dynamic-declaration:
32358 @dynamic identifier-list ;
32359
32360 For example:
32361 @dynamic MyProperty;
32362 @dynamic MyProperty, AnotherProperty;
32363
32364 PS: This function is identical to c_parser_objc_at_dynamic_declaration
32365 for C. Keep them in sync.
32366 */
32367 static void
32368 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
32369 {
32370 tree list = NULL_TREE;
32371 location_t loc;
32372 loc = cp_lexer_peek_token (parser->lexer)->location;
32373
32374 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
32375 while (true)
32376 {
32377 tree property;
32378 property = cp_parser_identifier (parser);
32379 if (property == error_mark_node)
32380 {
32381 cp_parser_consume_semicolon_at_end_of_statement (parser);
32382 return;
32383 }
32384 list = chainon (list, build_tree_list (NULL, property));
32385 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32386 cp_lexer_consume_token (parser->lexer);
32387 else
32388 break;
32389 }
32390 cp_parser_consume_semicolon_at_end_of_statement (parser);
32391 objc_add_dynamic_declaration (loc, list);
32392 }
32393
32394 \f
32395 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
32396
32397 /* Returns name of the next clause.
32398 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
32399 the token is not consumed. Otherwise appropriate pragma_omp_clause is
32400 returned and the token is consumed. */
32401
32402 static pragma_omp_clause
32403 cp_parser_omp_clause_name (cp_parser *parser)
32404 {
32405 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
32406
32407 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32408 result = PRAGMA_OACC_CLAUSE_AUTO;
32409 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
32410 result = PRAGMA_OMP_CLAUSE_IF;
32411 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
32412 result = PRAGMA_OMP_CLAUSE_DEFAULT;
32413 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
32414 result = PRAGMA_OACC_CLAUSE_DELETE;
32415 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
32416 result = PRAGMA_OMP_CLAUSE_PRIVATE;
32417 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
32418 result = PRAGMA_OMP_CLAUSE_FOR;
32419 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32420 {
32421 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32422 const char *p = IDENTIFIER_POINTER (id);
32423
32424 switch (p[0])
32425 {
32426 case 'a':
32427 if (!strcmp ("aligned", p))
32428 result = PRAGMA_OMP_CLAUSE_ALIGNED;
32429 else if (!strcmp ("async", p))
32430 result = PRAGMA_OACC_CLAUSE_ASYNC;
32431 break;
32432 case 'c':
32433 if (!strcmp ("collapse", p))
32434 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
32435 else if (!strcmp ("copy", p))
32436 result = PRAGMA_OACC_CLAUSE_COPY;
32437 else if (!strcmp ("copyin", p))
32438 result = PRAGMA_OMP_CLAUSE_COPYIN;
32439 else if (!strcmp ("copyout", p))
32440 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32441 else if (!strcmp ("copyprivate", p))
32442 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
32443 else if (!strcmp ("create", p))
32444 result = PRAGMA_OACC_CLAUSE_CREATE;
32445 break;
32446 case 'd':
32447 if (!strcmp ("defaultmap", p))
32448 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
32449 else if (!strcmp ("depend", p))
32450 result = PRAGMA_OMP_CLAUSE_DEPEND;
32451 else if (!strcmp ("device", p))
32452 result = PRAGMA_OMP_CLAUSE_DEVICE;
32453 else if (!strcmp ("deviceptr", p))
32454 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
32455 else if (!strcmp ("device_resident", p))
32456 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
32457 else if (!strcmp ("dist_schedule", p))
32458 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
32459 break;
32460 case 'f':
32461 if (!strcmp ("final", p))
32462 result = PRAGMA_OMP_CLAUSE_FINAL;
32463 else if (!strcmp ("finalize", p))
32464 result = PRAGMA_OACC_CLAUSE_FINALIZE;
32465 else if (!strcmp ("firstprivate", p))
32466 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
32467 else if (!strcmp ("from", p))
32468 result = PRAGMA_OMP_CLAUSE_FROM;
32469 break;
32470 case 'g':
32471 if (!strcmp ("gang", p))
32472 result = PRAGMA_OACC_CLAUSE_GANG;
32473 else if (!strcmp ("grainsize", p))
32474 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
32475 break;
32476 case 'h':
32477 if (!strcmp ("hint", p))
32478 result = PRAGMA_OMP_CLAUSE_HINT;
32479 else if (!strcmp ("host", p))
32480 result = PRAGMA_OACC_CLAUSE_HOST;
32481 break;
32482 case 'i':
32483 if (!strcmp ("if_present", p))
32484 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
32485 else if (!strcmp ("in_reduction", p))
32486 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
32487 else if (!strcmp ("inbranch", p))
32488 result = PRAGMA_OMP_CLAUSE_INBRANCH;
32489 else if (!strcmp ("independent", p))
32490 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
32491 else if (!strcmp ("is_device_ptr", p))
32492 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
32493 break;
32494 case 'l':
32495 if (!strcmp ("lastprivate", p))
32496 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
32497 else if (!strcmp ("linear", p))
32498 result = PRAGMA_OMP_CLAUSE_LINEAR;
32499 else if (!strcmp ("link", p))
32500 result = PRAGMA_OMP_CLAUSE_LINK;
32501 break;
32502 case 'm':
32503 if (!strcmp ("map", p))
32504 result = PRAGMA_OMP_CLAUSE_MAP;
32505 else if (!strcmp ("mergeable", p))
32506 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
32507 break;
32508 case 'n':
32509 if (!strcmp ("nogroup", p))
32510 result = PRAGMA_OMP_CLAUSE_NOGROUP;
32511 else if (!strcmp ("nontemporal", p))
32512 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
32513 else if (!strcmp ("notinbranch", p))
32514 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
32515 else if (!strcmp ("nowait", p))
32516 result = PRAGMA_OMP_CLAUSE_NOWAIT;
32517 else if (!strcmp ("num_gangs", p))
32518 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
32519 else if (!strcmp ("num_tasks", p))
32520 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
32521 else if (!strcmp ("num_teams", p))
32522 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
32523 else if (!strcmp ("num_threads", p))
32524 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
32525 else if (!strcmp ("num_workers", p))
32526 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
32527 break;
32528 case 'o':
32529 if (!strcmp ("ordered", p))
32530 result = PRAGMA_OMP_CLAUSE_ORDERED;
32531 else if (!strcmp ("order", p))
32532 result = PRAGMA_OMP_CLAUSE_ORDER;
32533 break;
32534 case 'p':
32535 if (!strcmp ("parallel", p))
32536 result = PRAGMA_OMP_CLAUSE_PARALLEL;
32537 else if (!strcmp ("present", p))
32538 result = PRAGMA_OACC_CLAUSE_PRESENT;
32539 else if (!strcmp ("present_or_copy", p)
32540 || !strcmp ("pcopy", p))
32541 result = PRAGMA_OACC_CLAUSE_COPY;
32542 else if (!strcmp ("present_or_copyin", p)
32543 || !strcmp ("pcopyin", p))
32544 result = PRAGMA_OACC_CLAUSE_COPYIN;
32545 else if (!strcmp ("present_or_copyout", p)
32546 || !strcmp ("pcopyout", p))
32547 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32548 else if (!strcmp ("present_or_create", p)
32549 || !strcmp ("pcreate", p))
32550 result = PRAGMA_OACC_CLAUSE_CREATE;
32551 else if (!strcmp ("priority", p))
32552 result = PRAGMA_OMP_CLAUSE_PRIORITY;
32553 else if (!strcmp ("proc_bind", p))
32554 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
32555 break;
32556 case 'r':
32557 if (!strcmp ("reduction", p))
32558 result = PRAGMA_OMP_CLAUSE_REDUCTION;
32559 break;
32560 case 's':
32561 if (!strcmp ("safelen", p))
32562 result = PRAGMA_OMP_CLAUSE_SAFELEN;
32563 else if (!strcmp ("schedule", p))
32564 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
32565 else if (!strcmp ("sections", p))
32566 result = PRAGMA_OMP_CLAUSE_SECTIONS;
32567 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
32568 result = PRAGMA_OACC_CLAUSE_HOST;
32569 else if (!strcmp ("seq", p))
32570 result = PRAGMA_OACC_CLAUSE_SEQ;
32571 else if (!strcmp ("shared", p))
32572 result = PRAGMA_OMP_CLAUSE_SHARED;
32573 else if (!strcmp ("simd", p))
32574 result = PRAGMA_OMP_CLAUSE_SIMD;
32575 else if (!strcmp ("simdlen", p))
32576 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
32577 break;
32578 case 't':
32579 if (!strcmp ("task_reduction", p))
32580 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
32581 else if (!strcmp ("taskgroup", p))
32582 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
32583 else if (!strcmp ("thread_limit", p))
32584 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
32585 else if (!strcmp ("threads", p))
32586 result = PRAGMA_OMP_CLAUSE_THREADS;
32587 else if (!strcmp ("tile", p))
32588 result = PRAGMA_OACC_CLAUSE_TILE;
32589 else if (!strcmp ("to", p))
32590 result = PRAGMA_OMP_CLAUSE_TO;
32591 break;
32592 case 'u':
32593 if (!strcmp ("uniform", p))
32594 result = PRAGMA_OMP_CLAUSE_UNIFORM;
32595 else if (!strcmp ("untied", p))
32596 result = PRAGMA_OMP_CLAUSE_UNTIED;
32597 else if (!strcmp ("use_device", p))
32598 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
32599 else if (!strcmp ("use_device_ptr", p))
32600 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
32601 break;
32602 case 'v':
32603 if (!strcmp ("vector", p))
32604 result = PRAGMA_OACC_CLAUSE_VECTOR;
32605 else if (!strcmp ("vector_length", p))
32606 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
32607 break;
32608 case 'w':
32609 if (!strcmp ("wait", p))
32610 result = PRAGMA_OACC_CLAUSE_WAIT;
32611 else if (!strcmp ("worker", p))
32612 result = PRAGMA_OACC_CLAUSE_WORKER;
32613 break;
32614 }
32615 }
32616
32617 if (result != PRAGMA_OMP_CLAUSE_NONE)
32618 cp_lexer_consume_token (parser->lexer);
32619
32620 return result;
32621 }
32622
32623 /* Validate that a clause of the given type does not already exist. */
32624
32625 static void
32626 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
32627 const char *name, location_t location)
32628 {
32629 tree c;
32630
32631 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
32632 if (OMP_CLAUSE_CODE (c) == code)
32633 {
32634 error_at (location, "too many %qs clauses", name);
32635 break;
32636 }
32637 }
32638
32639 /* OpenMP 2.5:
32640 variable-list:
32641 identifier
32642 variable-list , identifier
32643
32644 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
32645 colon). An opening parenthesis will have been consumed by the caller.
32646
32647 If KIND is nonzero, create the appropriate node and install the decl
32648 in OMP_CLAUSE_DECL and add the node to the head of the list.
32649
32650 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
32651 return the list created.
32652
32653 COLON can be NULL if only closing parenthesis should end the list,
32654 or pointer to bool which will receive false if the list is terminated
32655 by closing parenthesis or true if the list is terminated by colon. */
32656
32657 static tree
32658 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
32659 tree list, bool *colon)
32660 {
32661 cp_token *token;
32662 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32663 if (colon)
32664 {
32665 parser->colon_corrects_to_scope_p = false;
32666 *colon = false;
32667 }
32668 while (1)
32669 {
32670 tree name, decl;
32671
32672 if (kind == OMP_CLAUSE_DEPEND)
32673 cp_parser_parse_tentatively (parser);
32674 token = cp_lexer_peek_token (parser->lexer);
32675 if (kind != 0
32676 && current_class_ptr
32677 && cp_parser_is_keyword (token, RID_THIS))
32678 {
32679 decl = finish_this_expr ();
32680 if (TREE_CODE (decl) == NON_LVALUE_EXPR
32681 || CONVERT_EXPR_P (decl))
32682 decl = TREE_OPERAND (decl, 0);
32683 cp_lexer_consume_token (parser->lexer);
32684 }
32685 else
32686 {
32687 name = cp_parser_id_expression (parser, /*template_p=*/false,
32688 /*check_dependency_p=*/true,
32689 /*template_p=*/NULL,
32690 /*declarator_p=*/false,
32691 /*optional_p=*/false);
32692 if (name == error_mark_node)
32693 {
32694 if (kind == OMP_CLAUSE_DEPEND
32695 && cp_parser_simulate_error (parser))
32696 goto depend_lvalue;
32697 goto skip_comma;
32698 }
32699
32700 if (identifier_p (name))
32701 decl = cp_parser_lookup_name_simple (parser, name, token->location);
32702 else
32703 decl = name;
32704 if (decl == error_mark_node)
32705 {
32706 if (kind == OMP_CLAUSE_DEPEND
32707 && cp_parser_simulate_error (parser))
32708 goto depend_lvalue;
32709 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
32710 token->location);
32711 }
32712 }
32713 if (decl == error_mark_node)
32714 ;
32715 else if (kind != 0)
32716 {
32717 switch (kind)
32718 {
32719 case OMP_CLAUSE__CACHE_:
32720 /* The OpenACC cache directive explicitly only allows "array
32721 elements or subarrays". */
32722 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
32723 {
32724 error_at (token->location, "expected %<[%>");
32725 decl = error_mark_node;
32726 break;
32727 }
32728 /* FALLTHROUGH. */
32729 case OMP_CLAUSE_MAP:
32730 case OMP_CLAUSE_FROM:
32731 case OMP_CLAUSE_TO:
32732 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
32733 {
32734 location_t loc
32735 = cp_lexer_peek_token (parser->lexer)->location;
32736 cp_id_kind idk = CP_ID_KIND_NONE;
32737 cp_lexer_consume_token (parser->lexer);
32738 decl = convert_from_reference (decl);
32739 decl
32740 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
32741 decl, false,
32742 &idk, loc);
32743 }
32744 /* FALLTHROUGH. */
32745 case OMP_CLAUSE_DEPEND:
32746 case OMP_CLAUSE_REDUCTION:
32747 case OMP_CLAUSE_IN_REDUCTION:
32748 case OMP_CLAUSE_TASK_REDUCTION:
32749 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
32750 {
32751 tree low_bound = NULL_TREE, length = NULL_TREE;
32752
32753 parser->colon_corrects_to_scope_p = false;
32754 cp_lexer_consume_token (parser->lexer);
32755 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32756 low_bound = cp_parser_expression (parser);
32757 if (!colon)
32758 parser->colon_corrects_to_scope_p
32759 = saved_colon_corrects_to_scope_p;
32760 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
32761 length = integer_one_node;
32762 else
32763 {
32764 /* Look for `:'. */
32765 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32766 {
32767 if (kind == OMP_CLAUSE_DEPEND
32768 && cp_parser_simulate_error (parser))
32769 goto depend_lvalue;
32770 goto skip_comma;
32771 }
32772 if (kind == OMP_CLAUSE_DEPEND)
32773 cp_parser_commit_to_tentative_parse (parser);
32774 if (!cp_lexer_next_token_is (parser->lexer,
32775 CPP_CLOSE_SQUARE))
32776 length = cp_parser_expression (parser);
32777 }
32778 /* Look for the closing `]'. */
32779 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
32780 RT_CLOSE_SQUARE))
32781 {
32782 if (kind == OMP_CLAUSE_DEPEND
32783 && cp_parser_simulate_error (parser))
32784 goto depend_lvalue;
32785 goto skip_comma;
32786 }
32787
32788 decl = tree_cons (low_bound, length, decl);
32789 }
32790 break;
32791 default:
32792 break;
32793 }
32794
32795 if (kind == OMP_CLAUSE_DEPEND)
32796 {
32797 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
32798 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32799 && cp_parser_simulate_error (parser))
32800 {
32801 depend_lvalue:
32802 cp_parser_abort_tentative_parse (parser);
32803 decl = cp_parser_assignment_expression (parser, NULL,
32804 false, false);
32805 }
32806 else
32807 cp_parser_parse_definitely (parser);
32808 }
32809
32810 tree u = build_omp_clause (token->location, kind);
32811 OMP_CLAUSE_DECL (u) = decl;
32812 OMP_CLAUSE_CHAIN (u) = list;
32813 list = u;
32814 }
32815 else
32816 list = tree_cons (decl, NULL_TREE, list);
32817
32818 get_comma:
32819 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32820 break;
32821 cp_lexer_consume_token (parser->lexer);
32822 }
32823
32824 if (colon)
32825 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32826
32827 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32828 {
32829 *colon = true;
32830 cp_parser_require (parser, CPP_COLON, RT_COLON);
32831 return list;
32832 }
32833
32834 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32835 {
32836 int ending;
32837
32838 /* Try to resync to an unnested comma. Copied from
32839 cp_parser_parenthesized_expression_list. */
32840 skip_comma:
32841 if (colon)
32842 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32843 ending = cp_parser_skip_to_closing_parenthesis (parser,
32844 /*recovering=*/true,
32845 /*or_comma=*/true,
32846 /*consume_paren=*/true);
32847 if (ending < 0)
32848 goto get_comma;
32849 }
32850
32851 return list;
32852 }
32853
32854 /* Similarly, but expect leading and trailing parenthesis. This is a very
32855 common case for omp clauses. */
32856
32857 static tree
32858 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
32859 {
32860 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32861 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
32862 return list;
32863 }
32864
32865 /* OpenACC 2.0:
32866 copy ( variable-list )
32867 copyin ( variable-list )
32868 copyout ( variable-list )
32869 create ( variable-list )
32870 delete ( variable-list )
32871 present ( variable-list ) */
32872
32873 static tree
32874 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
32875 tree list)
32876 {
32877 enum gomp_map_kind kind;
32878 switch (c_kind)
32879 {
32880 case PRAGMA_OACC_CLAUSE_COPY:
32881 kind = GOMP_MAP_TOFROM;
32882 break;
32883 case PRAGMA_OACC_CLAUSE_COPYIN:
32884 kind = GOMP_MAP_TO;
32885 break;
32886 case PRAGMA_OACC_CLAUSE_COPYOUT:
32887 kind = GOMP_MAP_FROM;
32888 break;
32889 case PRAGMA_OACC_CLAUSE_CREATE:
32890 kind = GOMP_MAP_ALLOC;
32891 break;
32892 case PRAGMA_OACC_CLAUSE_DELETE:
32893 kind = GOMP_MAP_RELEASE;
32894 break;
32895 case PRAGMA_OACC_CLAUSE_DEVICE:
32896 kind = GOMP_MAP_FORCE_TO;
32897 break;
32898 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32899 kind = GOMP_MAP_DEVICE_RESIDENT;
32900 break;
32901 case PRAGMA_OACC_CLAUSE_HOST:
32902 kind = GOMP_MAP_FORCE_FROM;
32903 break;
32904 case PRAGMA_OACC_CLAUSE_LINK:
32905 kind = GOMP_MAP_LINK;
32906 break;
32907 case PRAGMA_OACC_CLAUSE_PRESENT:
32908 kind = GOMP_MAP_FORCE_PRESENT;
32909 break;
32910 default:
32911 gcc_unreachable ();
32912 }
32913 tree nl, c;
32914 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32915
32916 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32917 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32918
32919 return nl;
32920 }
32921
32922 /* OpenACC 2.0:
32923 deviceptr ( variable-list ) */
32924
32925 static tree
32926 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32927 {
32928 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32929 tree vars, t;
32930
32931 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32932 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32933 variable-list must only allow for pointer variables. */
32934 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32935 for (t = vars; t; t = TREE_CHAIN (t))
32936 {
32937 tree v = TREE_PURPOSE (t);
32938 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32939 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32940 OMP_CLAUSE_DECL (u) = v;
32941 OMP_CLAUSE_CHAIN (u) = list;
32942 list = u;
32943 }
32944
32945 return list;
32946 }
32947
32948 /* OpenACC 2.5:
32949 auto
32950 finalize
32951 independent
32952 nohost
32953 seq */
32954
32955 static tree
32956 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
32957 tree list)
32958 {
32959 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
32960
32961 tree c = build_omp_clause (loc, code);
32962 OMP_CLAUSE_CHAIN (c) = list;
32963
32964 return c;
32965 }
32966
32967 /* OpenACC:
32968 num_gangs ( expression )
32969 num_workers ( expression )
32970 vector_length ( expression ) */
32971
32972 static tree
32973 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32974 const char *str, tree list)
32975 {
32976 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32977
32978 matching_parens parens;
32979 if (!parens.require_open (parser))
32980 return list;
32981
32982 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32983
32984 if (t == error_mark_node
32985 || !parens.require_close (parser))
32986 {
32987 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32988 /*or_comma=*/false,
32989 /*consume_paren=*/true);
32990 return list;
32991 }
32992
32993 check_no_duplicate_clause (list, code, str, loc);
32994
32995 tree c = build_omp_clause (loc, code);
32996 OMP_CLAUSE_OPERAND (c, 0) = t;
32997 OMP_CLAUSE_CHAIN (c) = list;
32998 return c;
32999 }
33000
33001 /* OpenACC:
33002
33003 gang [( gang-arg-list )]
33004 worker [( [num:] int-expr )]
33005 vector [( [length:] int-expr )]
33006
33007 where gang-arg is one of:
33008
33009 [num:] int-expr
33010 static: size-expr
33011
33012 and size-expr may be:
33013
33014 *
33015 int-expr
33016 */
33017
33018 static tree
33019 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
33020 omp_clause_code kind,
33021 const char *str, tree list)
33022 {
33023 const char *id = "num";
33024 cp_lexer *lexer = parser->lexer;
33025 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
33026
33027 if (kind == OMP_CLAUSE_VECTOR)
33028 id = "length";
33029
33030 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
33031 {
33032 matching_parens parens;
33033 parens.consume_open (parser);
33034
33035 do
33036 {
33037 cp_token *next = cp_lexer_peek_token (lexer);
33038 int idx = 0;
33039
33040 /* Gang static argument. */
33041 if (kind == OMP_CLAUSE_GANG
33042 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
33043 {
33044 cp_lexer_consume_token (lexer);
33045
33046 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33047 goto cleanup_error;
33048
33049 idx = 1;
33050 if (ops[idx] != NULL)
33051 {
33052 cp_parser_error (parser, "too many %<static%> arguments");
33053 goto cleanup_error;
33054 }
33055
33056 /* Check for the '*' argument. */
33057 if (cp_lexer_next_token_is (lexer, CPP_MULT)
33058 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
33059 || cp_lexer_nth_token_is (parser->lexer, 2,
33060 CPP_CLOSE_PAREN)))
33061 {
33062 cp_lexer_consume_token (lexer);
33063 ops[idx] = integer_minus_one_node;
33064
33065 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
33066 {
33067 cp_lexer_consume_token (lexer);
33068 continue;
33069 }
33070 else break;
33071 }
33072 }
33073 /* Worker num: argument and vector length: arguments. */
33074 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
33075 && id_equal (next->u.value, id)
33076 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
33077 {
33078 cp_lexer_consume_token (lexer); /* id */
33079 cp_lexer_consume_token (lexer); /* ':' */
33080 }
33081
33082 /* Now collect the actual argument. */
33083 if (ops[idx] != NULL_TREE)
33084 {
33085 cp_parser_error (parser, "unexpected argument");
33086 goto cleanup_error;
33087 }
33088
33089 tree expr = cp_parser_assignment_expression (parser, NULL, false,
33090 false);
33091 if (expr == error_mark_node)
33092 goto cleanup_error;
33093
33094 mark_exp_read (expr);
33095 ops[idx] = expr;
33096
33097 if (kind == OMP_CLAUSE_GANG
33098 && cp_lexer_next_token_is (lexer, CPP_COMMA))
33099 {
33100 cp_lexer_consume_token (lexer);
33101 continue;
33102 }
33103 break;
33104 }
33105 while (1);
33106
33107 if (!parens.require_close (parser))
33108 goto cleanup_error;
33109 }
33110
33111 check_no_duplicate_clause (list, kind, str, loc);
33112
33113 c = build_omp_clause (loc, kind);
33114
33115 if (ops[1])
33116 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
33117
33118 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
33119 OMP_CLAUSE_CHAIN (c) = list;
33120
33121 return c;
33122
33123 cleanup_error:
33124 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33125 return list;
33126 }
33127
33128 /* OpenACC 2.0:
33129 tile ( size-expr-list ) */
33130
33131 static tree
33132 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
33133 {
33134 tree c, expr = error_mark_node;
33135 tree tile = NULL_TREE;
33136
33137 /* Collapse and tile are mutually exclusive. (The spec doesn't say
33138 so, but the spec authors never considered such a case and have
33139 differing opinions on what it might mean, including 'not
33140 allowed'.) */
33141 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
33142 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
33143 clause_loc);
33144
33145 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33146 return list;
33147
33148 do
33149 {
33150 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
33151 return list;
33152
33153 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
33154 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
33155 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
33156 {
33157 cp_lexer_consume_token (parser->lexer);
33158 expr = integer_zero_node;
33159 }
33160 else
33161 expr = cp_parser_constant_expression (parser);
33162
33163 tile = tree_cons (NULL_TREE, expr, tile);
33164 }
33165 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
33166
33167 /* Consume the trailing ')'. */
33168 cp_lexer_consume_token (parser->lexer);
33169
33170 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
33171 tile = nreverse (tile);
33172 OMP_CLAUSE_TILE_LIST (c) = tile;
33173 OMP_CLAUSE_CHAIN (c) = list;
33174 return c;
33175 }
33176
33177 /* OpenACC 2.0
33178 Parse wait clause or directive parameters. */
33179
33180 static tree
33181 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
33182 {
33183 vec<tree, va_gc> *args;
33184 tree t, args_tree;
33185
33186 args = cp_parser_parenthesized_expression_list (parser, non_attr,
33187 /*cast_p=*/false,
33188 /*allow_expansion_p=*/true,
33189 /*non_constant_p=*/NULL);
33190
33191 if (args == NULL || args->length () == 0)
33192 {
33193 if (args != NULL)
33194 {
33195 cp_parser_error (parser, "expected integer expression list");
33196 release_tree_vector (args);
33197 }
33198 return list;
33199 }
33200
33201 args_tree = build_tree_list_vec (args);
33202
33203 release_tree_vector (args);
33204
33205 for (t = args_tree; t; t = TREE_CHAIN (t))
33206 {
33207 tree targ = TREE_VALUE (t);
33208
33209 if (targ != error_mark_node)
33210 {
33211 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
33212 error ("%<wait%> expression must be integral");
33213 else
33214 {
33215 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
33216
33217 targ = mark_rvalue_use (targ);
33218 OMP_CLAUSE_DECL (c) = targ;
33219 OMP_CLAUSE_CHAIN (c) = list;
33220 list = c;
33221 }
33222 }
33223 }
33224
33225 return list;
33226 }
33227
33228 /* OpenACC:
33229 wait [( int-expr-list )] */
33230
33231 static tree
33232 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
33233 {
33234 location_t location = cp_lexer_peek_token (parser->lexer)->location;
33235
33236 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33237 list = cp_parser_oacc_wait_list (parser, location, list);
33238 else
33239 {
33240 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
33241
33242 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33243 OMP_CLAUSE_CHAIN (c) = list;
33244 list = c;
33245 }
33246
33247 return list;
33248 }
33249
33250 /* OpenMP 3.0:
33251 collapse ( constant-expression ) */
33252
33253 static tree
33254 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
33255 {
33256 tree c, num;
33257 location_t loc;
33258 HOST_WIDE_INT n;
33259
33260 loc = cp_lexer_peek_token (parser->lexer)->location;
33261 matching_parens parens;
33262 if (!parens.require_open (parser))
33263 return list;
33264
33265 num = cp_parser_constant_expression (parser);
33266
33267 if (!parens.require_close (parser))
33268 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33269 /*or_comma=*/false,
33270 /*consume_paren=*/true);
33271
33272 if (num == error_mark_node)
33273 return list;
33274 num = fold_non_dependent_expr (num);
33275 if (!tree_fits_shwi_p (num)
33276 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33277 || (n = tree_to_shwi (num)) <= 0
33278 || (int) n != n)
33279 {
33280 error_at (loc, "collapse argument needs positive constant integer expression");
33281 return list;
33282 }
33283
33284 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
33285 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
33286 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
33287 OMP_CLAUSE_CHAIN (c) = list;
33288 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
33289
33290 return c;
33291 }
33292
33293 /* OpenMP 2.5:
33294 default ( none | shared )
33295
33296 OpenACC:
33297 default ( none | present ) */
33298
33299 static tree
33300 cp_parser_omp_clause_default (cp_parser *parser, tree list,
33301 location_t location, bool is_oacc)
33302 {
33303 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
33304 tree c;
33305
33306 matching_parens parens;
33307 if (!parens.require_open (parser))
33308 return list;
33309 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33310 {
33311 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33312 const char *p = IDENTIFIER_POINTER (id);
33313
33314 switch (p[0])
33315 {
33316 case 'n':
33317 if (strcmp ("none", p) != 0)
33318 goto invalid_kind;
33319 kind = OMP_CLAUSE_DEFAULT_NONE;
33320 break;
33321
33322 case 'p':
33323 if (strcmp ("present", p) != 0 || !is_oacc)
33324 goto invalid_kind;
33325 kind = OMP_CLAUSE_DEFAULT_PRESENT;
33326 break;
33327
33328 case 's':
33329 if (strcmp ("shared", p) != 0 || is_oacc)
33330 goto invalid_kind;
33331 kind = OMP_CLAUSE_DEFAULT_SHARED;
33332 break;
33333
33334 default:
33335 goto invalid_kind;
33336 }
33337
33338 cp_lexer_consume_token (parser->lexer);
33339 }
33340 else
33341 {
33342 invalid_kind:
33343 if (is_oacc)
33344 cp_parser_error (parser, "expected %<none%> or %<present%>");
33345 else
33346 cp_parser_error (parser, "expected %<none%> or %<shared%>");
33347 }
33348
33349 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
33350 || !parens.require_close (parser))
33351 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33352 /*or_comma=*/false,
33353 /*consume_paren=*/true);
33354
33355 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
33356 return list;
33357
33358 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
33359 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
33360 OMP_CLAUSE_CHAIN (c) = list;
33361 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
33362
33363 return c;
33364 }
33365
33366 /* OpenMP 3.1:
33367 final ( expression ) */
33368
33369 static tree
33370 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
33371 {
33372 tree t, c;
33373
33374 matching_parens parens;
33375 if (!parens.require_open (parser))
33376 return list;
33377
33378 t = cp_parser_assignment_expression (parser);
33379
33380 if (t == error_mark_node
33381 || !parens.require_close (parser))
33382 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33383 /*or_comma=*/false,
33384 /*consume_paren=*/true);
33385
33386 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
33387
33388 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
33389 OMP_CLAUSE_FINAL_EXPR (c) = t;
33390 OMP_CLAUSE_CHAIN (c) = list;
33391
33392 return c;
33393 }
33394
33395 /* OpenMP 2.5:
33396 if ( expression )
33397
33398 OpenMP 4.5:
33399 if ( directive-name-modifier : expression )
33400
33401 directive-name-modifier:
33402 parallel | task | taskloop | target data | target | target update
33403 | target enter data | target exit data
33404
33405 OpenMP 5.0:
33406 directive-name-modifier:
33407 ... | simd | cancel */
33408
33409 static tree
33410 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
33411 bool is_omp)
33412 {
33413 tree t, c;
33414 enum tree_code if_modifier = ERROR_MARK;
33415
33416 matching_parens parens;
33417 if (!parens.require_open (parser))
33418 return list;
33419
33420 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33421 {
33422 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33423 const char *p = IDENTIFIER_POINTER (id);
33424 int n = 2;
33425
33426 if (strcmp ("cancel", p) == 0)
33427 if_modifier = VOID_CST;
33428 else if (strcmp ("parallel", p) == 0)
33429 if_modifier = OMP_PARALLEL;
33430 else if (strcmp ("simd", p) == 0)
33431 if_modifier = OMP_SIMD;
33432 else if (strcmp ("task", p) == 0)
33433 if_modifier = OMP_TASK;
33434 else if (strcmp ("taskloop", p) == 0)
33435 if_modifier = OMP_TASKLOOP;
33436 else if (strcmp ("target", p) == 0)
33437 {
33438 if_modifier = OMP_TARGET;
33439 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
33440 {
33441 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
33442 p = IDENTIFIER_POINTER (id);
33443 if (strcmp ("data", p) == 0)
33444 if_modifier = OMP_TARGET_DATA;
33445 else if (strcmp ("update", p) == 0)
33446 if_modifier = OMP_TARGET_UPDATE;
33447 else if (strcmp ("enter", p) == 0)
33448 if_modifier = OMP_TARGET_ENTER_DATA;
33449 else if (strcmp ("exit", p) == 0)
33450 if_modifier = OMP_TARGET_EXIT_DATA;
33451 if (if_modifier != OMP_TARGET)
33452 n = 3;
33453 else
33454 {
33455 location_t loc
33456 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
33457 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
33458 "or %<exit%>");
33459 if_modifier = ERROR_MARK;
33460 }
33461 if (if_modifier == OMP_TARGET_ENTER_DATA
33462 || if_modifier == OMP_TARGET_EXIT_DATA)
33463 {
33464 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
33465 {
33466 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
33467 p = IDENTIFIER_POINTER (id);
33468 if (strcmp ("data", p) == 0)
33469 n = 4;
33470 }
33471 if (n != 4)
33472 {
33473 location_t loc
33474 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
33475 error_at (loc, "expected %<data%>");
33476 if_modifier = ERROR_MARK;
33477 }
33478 }
33479 }
33480 }
33481 if (if_modifier != ERROR_MARK)
33482 {
33483 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
33484 {
33485 while (n-- > 0)
33486 cp_lexer_consume_token (parser->lexer);
33487 }
33488 else
33489 {
33490 if (n > 2)
33491 {
33492 location_t loc
33493 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
33494 error_at (loc, "expected %<:%>");
33495 }
33496 if_modifier = ERROR_MARK;
33497 }
33498 }
33499 }
33500
33501 t = cp_parser_assignment_expression (parser);
33502
33503 if (t == error_mark_node
33504 || !parens.require_close (parser))
33505 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33506 /*or_comma=*/false,
33507 /*consume_paren=*/true);
33508
33509 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33510 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
33511 {
33512 if (if_modifier != ERROR_MARK
33513 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33514 {
33515 const char *p = NULL;
33516 switch (if_modifier)
33517 {
33518 case VOID_CST: p = "cancel"; break;
33519 case OMP_PARALLEL: p = "parallel"; break;
33520 case OMP_SIMD: p = "simd"; break;
33521 case OMP_TASK: p = "task"; break;
33522 case OMP_TASKLOOP: p = "taskloop"; break;
33523 case OMP_TARGET_DATA: p = "target data"; break;
33524 case OMP_TARGET: p = "target"; break;
33525 case OMP_TARGET_UPDATE: p = "target update"; break;
33526 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
33527 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
33528 default: gcc_unreachable ();
33529 }
33530 error_at (location, "too many %<if%> clauses with %qs modifier",
33531 p);
33532 return list;
33533 }
33534 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33535 {
33536 if (!is_omp)
33537 error_at (location, "too many %<if%> clauses");
33538 else
33539 error_at (location, "too many %<if%> clauses without modifier");
33540 return list;
33541 }
33542 else if (if_modifier == ERROR_MARK
33543 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
33544 {
33545 error_at (location, "if any %<if%> clause has modifier, then all "
33546 "%<if%> clauses have to use modifier");
33547 return list;
33548 }
33549 }
33550
33551 c = build_omp_clause (location, OMP_CLAUSE_IF);
33552 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
33553 OMP_CLAUSE_IF_EXPR (c) = t;
33554 OMP_CLAUSE_CHAIN (c) = list;
33555
33556 return c;
33557 }
33558
33559 /* OpenMP 3.1:
33560 mergeable */
33561
33562 static tree
33563 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
33564 tree list, location_t location)
33565 {
33566 tree c;
33567
33568 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
33569 location);
33570
33571 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
33572 OMP_CLAUSE_CHAIN (c) = list;
33573 return c;
33574 }
33575
33576 /* OpenMP 2.5:
33577 nowait */
33578
33579 static tree
33580 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
33581 tree list, location_t location)
33582 {
33583 tree c;
33584
33585 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
33586
33587 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
33588 OMP_CLAUSE_CHAIN (c) = list;
33589 return c;
33590 }
33591
33592 /* OpenMP 2.5:
33593 num_threads ( expression ) */
33594
33595 static tree
33596 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
33597 location_t location)
33598 {
33599 tree t, c;
33600
33601 matching_parens parens;
33602 if (!parens.require_open (parser))
33603 return list;
33604
33605 t = cp_parser_assignment_expression (parser);
33606
33607 if (t == error_mark_node
33608 || !parens.require_close (parser))
33609 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33610 /*or_comma=*/false,
33611 /*consume_paren=*/true);
33612
33613 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
33614 "num_threads", location);
33615
33616 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
33617 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
33618 OMP_CLAUSE_CHAIN (c) = list;
33619
33620 return c;
33621 }
33622
33623 /* OpenMP 4.5:
33624 num_tasks ( expression ) */
33625
33626 static tree
33627 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
33628 location_t location)
33629 {
33630 tree t, c;
33631
33632 matching_parens parens;
33633 if (!parens.require_open (parser))
33634 return list;
33635
33636 t = cp_parser_assignment_expression (parser);
33637
33638 if (t == error_mark_node
33639 || !parens.require_close (parser))
33640 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33641 /*or_comma=*/false,
33642 /*consume_paren=*/true);
33643
33644 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
33645 "num_tasks", location);
33646
33647 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
33648 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
33649 OMP_CLAUSE_CHAIN (c) = list;
33650
33651 return c;
33652 }
33653
33654 /* OpenMP 4.5:
33655 grainsize ( expression ) */
33656
33657 static tree
33658 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
33659 location_t location)
33660 {
33661 tree t, c;
33662
33663 matching_parens parens;
33664 if (!parens.require_open (parser))
33665 return list;
33666
33667 t = cp_parser_assignment_expression (parser);
33668
33669 if (t == error_mark_node
33670 || !parens.require_close (parser))
33671 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33672 /*or_comma=*/false,
33673 /*consume_paren=*/true);
33674
33675 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
33676 "grainsize", location);
33677
33678 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
33679 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
33680 OMP_CLAUSE_CHAIN (c) = list;
33681
33682 return c;
33683 }
33684
33685 /* OpenMP 4.5:
33686 priority ( expression ) */
33687
33688 static tree
33689 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
33690 location_t location)
33691 {
33692 tree t, c;
33693
33694 matching_parens parens;
33695 if (!parens.require_open (parser))
33696 return list;
33697
33698 t = cp_parser_assignment_expression (parser);
33699
33700 if (t == error_mark_node
33701 || !parens.require_close (parser))
33702 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33703 /*or_comma=*/false,
33704 /*consume_paren=*/true);
33705
33706 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
33707 "priority", location);
33708
33709 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
33710 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
33711 OMP_CLAUSE_CHAIN (c) = list;
33712
33713 return c;
33714 }
33715
33716 /* OpenMP 4.5:
33717 hint ( expression ) */
33718
33719 static tree
33720 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
33721 {
33722 tree t, c;
33723
33724 matching_parens parens;
33725 if (!parens.require_open (parser))
33726 return list;
33727
33728 t = cp_parser_assignment_expression (parser);
33729
33730 if (t == error_mark_node
33731 || !parens.require_close (parser))
33732 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33733 /*or_comma=*/false,
33734 /*consume_paren=*/true);
33735
33736 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
33737
33738 c = build_omp_clause (location, OMP_CLAUSE_HINT);
33739 OMP_CLAUSE_HINT_EXPR (c) = t;
33740 OMP_CLAUSE_CHAIN (c) = list;
33741
33742 return c;
33743 }
33744
33745 /* OpenMP 4.5:
33746 defaultmap ( tofrom : scalar )
33747
33748 OpenMP 5.0:
33749 defaultmap ( implicit-behavior [ : variable-category ] ) */
33750
33751 static tree
33752 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
33753 location_t location)
33754 {
33755 tree c, id;
33756 const char *p;
33757 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33758 enum omp_clause_defaultmap_kind category
33759 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
33760
33761 matching_parens parens;
33762 if (!parens.require_open (parser))
33763 return list;
33764
33765 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
33766 p = "default";
33767 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33768 {
33769 invalid_behavior:
33770 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
33771 "%<tofrom%>, %<firstprivate%>, %<none%> "
33772 "or %<default%>");
33773 goto out_err;
33774 }
33775 else
33776 {
33777 id = cp_lexer_peek_token (parser->lexer)->u.value;
33778 p = IDENTIFIER_POINTER (id);
33779 }
33780
33781 switch (p[0])
33782 {
33783 case 'a':
33784 if (strcmp ("alloc", p) == 0)
33785 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
33786 else
33787 goto invalid_behavior;
33788 break;
33789
33790 case 'd':
33791 if (strcmp ("default", p) == 0)
33792 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33793 else
33794 goto invalid_behavior;
33795 break;
33796
33797 case 'f':
33798 if (strcmp ("firstprivate", p) == 0)
33799 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
33800 else if (strcmp ("from", p) == 0)
33801 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
33802 else
33803 goto invalid_behavior;
33804 break;
33805
33806 case 'n':
33807 if (strcmp ("none", p) == 0)
33808 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
33809 else
33810 goto invalid_behavior;
33811 break;
33812
33813 case 't':
33814 if (strcmp ("tofrom", p) == 0)
33815 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
33816 else if (strcmp ("to", p) == 0)
33817 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
33818 else
33819 goto invalid_behavior;
33820 break;
33821
33822 default:
33823 goto invalid_behavior;
33824 }
33825 cp_lexer_consume_token (parser->lexer);
33826
33827 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33828 {
33829 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33830 goto out_err;
33831
33832 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33833 {
33834 invalid_category:
33835 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
33836 "%<pointer%>");
33837 goto out_err;
33838 }
33839 id = cp_lexer_peek_token (parser->lexer)->u.value;
33840 p = IDENTIFIER_POINTER (id);
33841
33842 switch (p[0])
33843 {
33844 case 'a':
33845 if (strcmp ("aggregate", p) == 0)
33846 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
33847 else
33848 goto invalid_category;
33849 break;
33850
33851 case 'p':
33852 if (strcmp ("pointer", p) == 0)
33853 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
33854 else
33855 goto invalid_category;
33856 break;
33857
33858 case 's':
33859 if (strcmp ("scalar", p) == 0)
33860 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
33861 else
33862 goto invalid_category;
33863 break;
33864
33865 default:
33866 goto invalid_category;
33867 }
33868
33869 cp_lexer_consume_token (parser->lexer);
33870 }
33871 if (!parens.require_close (parser))
33872 goto out_err;
33873
33874 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33875 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
33876 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33877 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
33878 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
33879 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
33880 {
33881 enum omp_clause_defaultmap_kind cat = category;
33882 location_t loc = OMP_CLAUSE_LOCATION (c);
33883 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
33884 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
33885 p = NULL;
33886 switch (cat)
33887 {
33888 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
33889 p = NULL;
33890 break;
33891 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
33892 p = "aggregate";
33893 break;
33894 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
33895 p = "pointer";
33896 break;
33897 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
33898 p = "scalar";
33899 break;
33900 default:
33901 gcc_unreachable ();
33902 }
33903 if (p)
33904 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
33905 p);
33906 else
33907 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
33908 "category");
33909 break;
33910 }
33911
33912 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33913 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33914 OMP_CLAUSE_CHAIN (c) = list;
33915 return c;
33916
33917 out_err:
33918 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33919 /*or_comma=*/false,
33920 /*consume_paren=*/true);
33921 return list;
33922 }
33923
33924 /* OpenMP 5.0:
33925 order ( concurrent ) */
33926
33927 static tree
33928 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
33929 {
33930 tree c, id;
33931 const char *p;
33932
33933 matching_parens parens;
33934 if (!parens.require_open (parser))
33935 return list;
33936
33937 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33938 {
33939 cp_parser_error (parser, "expected %<concurrent%>");
33940 goto out_err;
33941 }
33942 else
33943 {
33944 id = cp_lexer_peek_token (parser->lexer)->u.value;
33945 p = IDENTIFIER_POINTER (id);
33946 }
33947 if (strcmp (p, "concurrent") != 0)
33948 {
33949 cp_parser_error (parser, "expected %<concurrent%>");
33950 goto out_err;
33951 }
33952 cp_lexer_consume_token (parser->lexer);
33953 if (!parens.require_close (parser))
33954 goto out_err;
33955
33956 /* check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location); */
33957 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
33958 OMP_CLAUSE_CHAIN (c) = list;
33959 return c;
33960
33961 out_err:
33962 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33963 /*or_comma=*/false,
33964 /*consume_paren=*/true);
33965 return list;
33966 }
33967
33968 /* OpenMP 2.5:
33969 ordered
33970
33971 OpenMP 4.5:
33972 ordered ( constant-expression ) */
33973
33974 static tree
33975 cp_parser_omp_clause_ordered (cp_parser *parser,
33976 tree list, location_t location)
33977 {
33978 tree c, num = NULL_TREE;
33979 HOST_WIDE_INT n;
33980
33981 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
33982 "ordered", location);
33983
33984 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33985 {
33986 matching_parens parens;
33987 parens.consume_open (parser);
33988
33989 num = cp_parser_constant_expression (parser);
33990
33991 if (!parens.require_close (parser))
33992 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33993 /*or_comma=*/false,
33994 /*consume_paren=*/true);
33995
33996 if (num == error_mark_node)
33997 return list;
33998 num = fold_non_dependent_expr (num);
33999 if (!tree_fits_shwi_p (num)
34000 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
34001 || (n = tree_to_shwi (num)) <= 0
34002 || (int) n != n)
34003 {
34004 error_at (location,
34005 "ordered argument needs positive constant integer "
34006 "expression");
34007 return list;
34008 }
34009 }
34010
34011 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
34012 OMP_CLAUSE_ORDERED_EXPR (c) = num;
34013 OMP_CLAUSE_CHAIN (c) = list;
34014 return c;
34015 }
34016
34017 /* OpenMP 2.5:
34018 reduction ( reduction-operator : variable-list )
34019
34020 reduction-operator:
34021 One of: + * - & ^ | && ||
34022
34023 OpenMP 3.1:
34024
34025 reduction-operator:
34026 One of: + * - & ^ | && || min max
34027
34028 OpenMP 4.0:
34029
34030 reduction-operator:
34031 One of: + * - & ^ | && ||
34032 id-expression
34033
34034 OpenMP 5.0:
34035 reduction ( reduction-modifier, reduction-operator : variable-list )
34036 in_reduction ( reduction-operator : variable-list )
34037 task_reduction ( reduction-operator : variable-list ) */
34038
34039 static tree
34040 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
34041 bool is_omp, tree list)
34042 {
34043 enum tree_code code = ERROR_MARK;
34044 tree nlist, c, id = NULL_TREE;
34045 bool task = false;
34046 bool inscan = false;
34047
34048 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34049 return list;
34050
34051 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
34052 {
34053 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
34054 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
34055 {
34056 cp_lexer_consume_token (parser->lexer);
34057 cp_lexer_consume_token (parser->lexer);
34058 }
34059 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34060 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
34061 {
34062 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34063 const char *p = IDENTIFIER_POINTER (id);
34064 if (strcmp (p, "task") == 0)
34065 task = true;
34066 else if (strcmp (p, "inscan") == 0)
34067 inscan = true;
34068 if (task || inscan)
34069 {
34070 cp_lexer_consume_token (parser->lexer);
34071 cp_lexer_consume_token (parser->lexer);
34072 }
34073 }
34074 }
34075
34076 switch (cp_lexer_peek_token (parser->lexer)->type)
34077 {
34078 case CPP_PLUS: code = PLUS_EXPR; break;
34079 case CPP_MULT: code = MULT_EXPR; break;
34080 case CPP_MINUS: code = MINUS_EXPR; break;
34081 case CPP_AND: code = BIT_AND_EXPR; break;
34082 case CPP_XOR: code = BIT_XOR_EXPR; break;
34083 case CPP_OR: code = BIT_IOR_EXPR; break;
34084 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
34085 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
34086 default: break;
34087 }
34088
34089 if (code != ERROR_MARK)
34090 cp_lexer_consume_token (parser->lexer);
34091 else
34092 {
34093 bool saved_colon_corrects_to_scope_p;
34094 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
34095 parser->colon_corrects_to_scope_p = false;
34096 id = cp_parser_id_expression (parser, /*template_p=*/false,
34097 /*check_dependency_p=*/true,
34098 /*template_p=*/NULL,
34099 /*declarator_p=*/false,
34100 /*optional_p=*/false);
34101 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34102 if (identifier_p (id))
34103 {
34104 const char *p = IDENTIFIER_POINTER (id);
34105
34106 if (strcmp (p, "min") == 0)
34107 code = MIN_EXPR;
34108 else if (strcmp (p, "max") == 0)
34109 code = MAX_EXPR;
34110 else if (id == ovl_op_identifier (false, PLUS_EXPR))
34111 code = PLUS_EXPR;
34112 else if (id == ovl_op_identifier (false, MULT_EXPR))
34113 code = MULT_EXPR;
34114 else if (id == ovl_op_identifier (false, MINUS_EXPR))
34115 code = MINUS_EXPR;
34116 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
34117 code = BIT_AND_EXPR;
34118 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
34119 code = BIT_IOR_EXPR;
34120 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
34121 code = BIT_XOR_EXPR;
34122 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
34123 code = TRUTH_ANDIF_EXPR;
34124 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
34125 code = TRUTH_ORIF_EXPR;
34126 id = omp_reduction_id (code, id, NULL_TREE);
34127 tree scope = parser->scope;
34128 if (scope)
34129 id = build_qualified_name (NULL_TREE, scope, id, false);
34130 parser->scope = NULL_TREE;
34131 parser->qualifying_scope = NULL_TREE;
34132 parser->object_scope = NULL_TREE;
34133 }
34134 else
34135 {
34136 error ("invalid reduction-identifier");
34137 resync_fail:
34138 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34139 /*or_comma=*/false,
34140 /*consume_paren=*/true);
34141 return list;
34142 }
34143 }
34144
34145 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34146 goto resync_fail;
34147
34148 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
34149 NULL);
34150 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34151 {
34152 OMP_CLAUSE_REDUCTION_CODE (c) = code;
34153 if (task)
34154 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
34155 else if (inscan)
34156 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
34157 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
34158 }
34159
34160 return nlist;
34161 }
34162
34163 /* OpenMP 2.5:
34164 schedule ( schedule-kind )
34165 schedule ( schedule-kind , expression )
34166
34167 schedule-kind:
34168 static | dynamic | guided | runtime | auto
34169
34170 OpenMP 4.5:
34171 schedule ( schedule-modifier : schedule-kind )
34172 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
34173
34174 schedule-modifier:
34175 simd
34176 monotonic
34177 nonmonotonic */
34178
34179 static tree
34180 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
34181 {
34182 tree c, t;
34183 int modifiers = 0, nmodifiers = 0;
34184
34185 matching_parens parens;
34186 if (!parens.require_open (parser))
34187 return list;
34188
34189 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
34190
34191 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34192 {
34193 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34194 const char *p = IDENTIFIER_POINTER (id);
34195 if (strcmp ("simd", p) == 0)
34196 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
34197 else if (strcmp ("monotonic", p) == 0)
34198 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
34199 else if (strcmp ("nonmonotonic", p) == 0)
34200 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
34201 else
34202 break;
34203 cp_lexer_consume_token (parser->lexer);
34204 if (nmodifiers++ == 0
34205 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34206 cp_lexer_consume_token (parser->lexer);
34207 else
34208 {
34209 cp_parser_require (parser, CPP_COLON, RT_COLON);
34210 break;
34211 }
34212 }
34213
34214 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34215 {
34216 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34217 const char *p = IDENTIFIER_POINTER (id);
34218
34219 switch (p[0])
34220 {
34221 case 'd':
34222 if (strcmp ("dynamic", p) != 0)
34223 goto invalid_kind;
34224 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
34225 break;
34226
34227 case 'g':
34228 if (strcmp ("guided", p) != 0)
34229 goto invalid_kind;
34230 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
34231 break;
34232
34233 case 'r':
34234 if (strcmp ("runtime", p) != 0)
34235 goto invalid_kind;
34236 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
34237 break;
34238
34239 default:
34240 goto invalid_kind;
34241 }
34242 }
34243 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34244 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
34245 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
34246 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
34247 else
34248 goto invalid_kind;
34249 cp_lexer_consume_token (parser->lexer);
34250
34251 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
34252 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
34253 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
34254 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
34255 {
34256 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
34257 "specified");
34258 modifiers = 0;
34259 }
34260
34261 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34262 {
34263 cp_token *token;
34264 cp_lexer_consume_token (parser->lexer);
34265
34266 token = cp_lexer_peek_token (parser->lexer);
34267 t = cp_parser_assignment_expression (parser);
34268
34269 if (t == error_mark_node)
34270 goto resync_fail;
34271 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
34272 error_at (token->location, "schedule %<runtime%> does not take "
34273 "a %<chunk_size%> parameter");
34274 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
34275 error_at (token->location, "schedule %<auto%> does not take "
34276 "a %<chunk_size%> parameter");
34277 else
34278 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
34279
34280 if (!parens.require_close (parser))
34281 goto resync_fail;
34282 }
34283 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34284 goto resync_fail;
34285
34286 OMP_CLAUSE_SCHEDULE_KIND (c)
34287 = (enum omp_clause_schedule_kind)
34288 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
34289
34290 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
34291 OMP_CLAUSE_CHAIN (c) = list;
34292 return c;
34293
34294 invalid_kind:
34295 cp_parser_error (parser, "invalid schedule kind");
34296 resync_fail:
34297 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34298 /*or_comma=*/false,
34299 /*consume_paren=*/true);
34300 return list;
34301 }
34302
34303 /* OpenMP 3.0:
34304 untied */
34305
34306 static tree
34307 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
34308 tree list, location_t location)
34309 {
34310 tree c;
34311
34312 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
34313
34314 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
34315 OMP_CLAUSE_CHAIN (c) = list;
34316 return c;
34317 }
34318
34319 /* OpenMP 4.0:
34320 inbranch
34321 notinbranch */
34322
34323 static tree
34324 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
34325 tree list, location_t location)
34326 {
34327 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
34328 tree c = build_omp_clause (location, code);
34329 OMP_CLAUSE_CHAIN (c) = list;
34330 return c;
34331 }
34332
34333 /* OpenMP 4.0:
34334 parallel
34335 for
34336 sections
34337 taskgroup */
34338
34339 static tree
34340 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
34341 enum omp_clause_code code,
34342 tree list, location_t location)
34343 {
34344 tree c = build_omp_clause (location, code);
34345 OMP_CLAUSE_CHAIN (c) = list;
34346 return c;
34347 }
34348
34349 /* OpenMP 4.5:
34350 nogroup */
34351
34352 static tree
34353 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
34354 tree list, location_t location)
34355 {
34356 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
34357 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
34358 OMP_CLAUSE_CHAIN (c) = list;
34359 return c;
34360 }
34361
34362 /* OpenMP 4.5:
34363 simd
34364 threads */
34365
34366 static tree
34367 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
34368 enum omp_clause_code code,
34369 tree list, location_t location)
34370 {
34371 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
34372 tree c = build_omp_clause (location, code);
34373 OMP_CLAUSE_CHAIN (c) = list;
34374 return c;
34375 }
34376
34377 /* OpenMP 4.0:
34378 num_teams ( expression ) */
34379
34380 static tree
34381 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
34382 location_t location)
34383 {
34384 tree t, c;
34385
34386 matching_parens parens;
34387 if (!parens.require_open (parser))
34388 return list;
34389
34390 t = cp_parser_assignment_expression (parser);
34391
34392 if (t == error_mark_node
34393 || !parens.require_close (parser))
34394 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34395 /*or_comma=*/false,
34396 /*consume_paren=*/true);
34397
34398 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
34399 "num_teams", location);
34400
34401 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
34402 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
34403 OMP_CLAUSE_CHAIN (c) = list;
34404
34405 return c;
34406 }
34407
34408 /* OpenMP 4.0:
34409 thread_limit ( expression ) */
34410
34411 static tree
34412 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
34413 location_t location)
34414 {
34415 tree t, c;
34416
34417 matching_parens parens;
34418 if (!parens.require_open (parser))
34419 return list;
34420
34421 t = cp_parser_assignment_expression (parser);
34422
34423 if (t == error_mark_node
34424 || !parens.require_close (parser))
34425 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34426 /*or_comma=*/false,
34427 /*consume_paren=*/true);
34428
34429 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
34430 "thread_limit", location);
34431
34432 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
34433 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
34434 OMP_CLAUSE_CHAIN (c) = list;
34435
34436 return c;
34437 }
34438
34439 /* OpenMP 4.0:
34440 aligned ( variable-list )
34441 aligned ( variable-list : constant-expression ) */
34442
34443 static tree
34444 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
34445 {
34446 tree nlist, c, alignment = NULL_TREE;
34447 bool colon;
34448
34449 matching_parens parens;
34450 if (!parens.require_open (parser))
34451 return list;
34452
34453 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
34454 &colon);
34455
34456 if (colon)
34457 {
34458 alignment = cp_parser_constant_expression (parser);
34459
34460 if (!parens.require_close (parser))
34461 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34462 /*or_comma=*/false,
34463 /*consume_paren=*/true);
34464
34465 if (alignment == error_mark_node)
34466 alignment = NULL_TREE;
34467 }
34468
34469 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34470 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
34471
34472 return nlist;
34473 }
34474
34475 /* OpenMP 2.5:
34476 lastprivate ( variable-list )
34477
34478 OpenMP 5.0:
34479 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
34480
34481 static tree
34482 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
34483 {
34484 bool conditional = false;
34485
34486 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34487 return list;
34488
34489 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34490 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
34491 {
34492 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34493 const char *p = IDENTIFIER_POINTER (id);
34494
34495 if (strcmp ("conditional", p) == 0)
34496 {
34497 conditional = true;
34498 cp_lexer_consume_token (parser->lexer);
34499 cp_lexer_consume_token (parser->lexer);
34500 }
34501 }
34502
34503 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
34504 list, NULL);
34505
34506 if (conditional)
34507 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34508 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
34509 return nlist;
34510 }
34511
34512 /* OpenMP 4.0:
34513 linear ( variable-list )
34514 linear ( variable-list : expression )
34515
34516 OpenMP 4.5:
34517 linear ( modifier ( variable-list ) )
34518 linear ( modifier ( variable-list ) : expression ) */
34519
34520 static tree
34521 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
34522 bool declare_simd)
34523 {
34524 tree nlist, c, step = integer_one_node;
34525 bool colon;
34526 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
34527
34528 matching_parens parens;
34529 if (!parens.require_open (parser))
34530 return list;
34531
34532 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34533 {
34534 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34535 const char *p = IDENTIFIER_POINTER (id);
34536
34537 if (strcmp ("ref", p) == 0)
34538 kind = OMP_CLAUSE_LINEAR_REF;
34539 else if (strcmp ("val", p) == 0)
34540 kind = OMP_CLAUSE_LINEAR_VAL;
34541 else if (strcmp ("uval", p) == 0)
34542 kind = OMP_CLAUSE_LINEAR_UVAL;
34543 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
34544 cp_lexer_consume_token (parser->lexer);
34545 else
34546 kind = OMP_CLAUSE_LINEAR_DEFAULT;
34547 }
34548
34549 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
34550 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
34551 &colon);
34552 else
34553 {
34554 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
34555 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
34556 if (colon)
34557 cp_parser_require (parser, CPP_COLON, RT_COLON);
34558 else if (!parens.require_close (parser))
34559 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34560 /*or_comma=*/false,
34561 /*consume_paren=*/true);
34562 }
34563
34564 if (colon)
34565 {
34566 step = NULL_TREE;
34567 if (declare_simd
34568 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34569 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
34570 {
34571 cp_token *token = cp_lexer_peek_token (parser->lexer);
34572 cp_parser_parse_tentatively (parser);
34573 step = cp_parser_id_expression (parser, /*template_p=*/false,
34574 /*check_dependency_p=*/true,
34575 /*template_p=*/NULL,
34576 /*declarator_p=*/false,
34577 /*optional_p=*/false);
34578 if (step != error_mark_node)
34579 step = cp_parser_lookup_name_simple (parser, step, token->location);
34580 if (step == error_mark_node)
34581 {
34582 step = NULL_TREE;
34583 cp_parser_abort_tentative_parse (parser);
34584 }
34585 else if (!cp_parser_parse_definitely (parser))
34586 step = NULL_TREE;
34587 }
34588 if (!step)
34589 step = cp_parser_assignment_expression (parser);
34590
34591 if (!parens.require_close (parser))
34592 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34593 /*or_comma=*/false,
34594 /*consume_paren=*/true);
34595
34596 if (step == error_mark_node)
34597 return list;
34598 }
34599
34600 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34601 {
34602 OMP_CLAUSE_LINEAR_STEP (c) = step;
34603 OMP_CLAUSE_LINEAR_KIND (c) = kind;
34604 }
34605
34606 return nlist;
34607 }
34608
34609 /* OpenMP 4.0:
34610 safelen ( constant-expression ) */
34611
34612 static tree
34613 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
34614 location_t location)
34615 {
34616 tree t, c;
34617
34618 matching_parens parens;
34619 if (!parens.require_open (parser))
34620 return list;
34621
34622 t = cp_parser_constant_expression (parser);
34623
34624 if (t == error_mark_node
34625 || !parens.require_close (parser))
34626 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34627 /*or_comma=*/false,
34628 /*consume_paren=*/true);
34629
34630 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
34631
34632 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
34633 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
34634 OMP_CLAUSE_CHAIN (c) = list;
34635
34636 return c;
34637 }
34638
34639 /* OpenMP 4.0:
34640 simdlen ( constant-expression ) */
34641
34642 static tree
34643 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
34644 location_t location)
34645 {
34646 tree t, c;
34647
34648 matching_parens parens;
34649 if (!parens.require_open (parser))
34650 return list;
34651
34652 t = cp_parser_constant_expression (parser);
34653
34654 if (t == error_mark_node
34655 || !parens.require_close (parser))
34656 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34657 /*or_comma=*/false,
34658 /*consume_paren=*/true);
34659
34660 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
34661
34662 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
34663 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
34664 OMP_CLAUSE_CHAIN (c) = list;
34665
34666 return c;
34667 }
34668
34669 /* OpenMP 4.5:
34670 vec:
34671 identifier [+/- integer]
34672 vec , identifier [+/- integer]
34673 */
34674
34675 static tree
34676 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
34677 tree list)
34678 {
34679 tree vec = NULL;
34680
34681 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34682 {
34683 cp_parser_error (parser, "expected identifier");
34684 return list;
34685 }
34686
34687 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34688 {
34689 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
34690 tree t, identifier = cp_parser_identifier (parser);
34691 tree addend = NULL;
34692
34693 if (identifier == error_mark_node)
34694 t = error_mark_node;
34695 else
34696 {
34697 t = cp_parser_lookup_name_simple
34698 (parser, identifier,
34699 cp_lexer_peek_token (parser->lexer)->location);
34700 if (t == error_mark_node)
34701 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
34702 id_loc);
34703 }
34704
34705 bool neg = false;
34706 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
34707 neg = true;
34708 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
34709 {
34710 addend = integer_zero_node;
34711 goto add_to_vector;
34712 }
34713 cp_lexer_consume_token (parser->lexer);
34714
34715 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
34716 {
34717 cp_parser_error (parser, "expected integer");
34718 return list;
34719 }
34720
34721 addend = cp_lexer_peek_token (parser->lexer)->u.value;
34722 if (TREE_CODE (addend) != INTEGER_CST)
34723 {
34724 cp_parser_error (parser, "expected integer");
34725 return list;
34726 }
34727 cp_lexer_consume_token (parser->lexer);
34728
34729 add_to_vector:
34730 if (t != error_mark_node)
34731 {
34732 vec = tree_cons (addend, t, vec);
34733 if (neg)
34734 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
34735 }
34736
34737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
34738 break;
34739
34740 cp_lexer_consume_token (parser->lexer);
34741 }
34742
34743 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
34744 {
34745 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
34746 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
34747 OMP_CLAUSE_DECL (u) = nreverse (vec);
34748 OMP_CLAUSE_CHAIN (u) = list;
34749 return u;
34750 }
34751 return list;
34752 }
34753
34754 /* OpenMP 5.0:
34755 iterators ( iterators-definition )
34756
34757 iterators-definition:
34758 iterator-specifier
34759 iterator-specifier , iterators-definition
34760
34761 iterator-specifier:
34762 identifier = range-specification
34763 iterator-type identifier = range-specification
34764
34765 range-specification:
34766 begin : end
34767 begin : end : step */
34768
34769 static tree
34770 cp_parser_omp_iterators (cp_parser *parser)
34771 {
34772 tree ret = NULL_TREE, *last = &ret;
34773 cp_lexer_consume_token (parser->lexer);
34774
34775 matching_parens parens;
34776 if (!parens.require_open (parser))
34777 return error_mark_node;
34778
34779 bool saved_colon_corrects_to_scope_p
34780 = parser->colon_corrects_to_scope_p;
34781 bool saved_colon_doesnt_start_class_def_p
34782 = parser->colon_doesnt_start_class_def_p;
34783
34784 do
34785 {
34786 tree iter_type;
34787 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34788 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
34789 iter_type = integer_type_node;
34790 else
34791 {
34792 const char *saved_message
34793 = parser->type_definition_forbidden_message;
34794 parser->type_definition_forbidden_message
34795 = G_("types may not be defined in iterator type");
34796
34797 iter_type = cp_parser_type_id (parser);
34798
34799 parser->type_definition_forbidden_message = saved_message;
34800 }
34801
34802 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34803 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34804 {
34805 cp_parser_error (parser, "expected identifier");
34806 break;
34807 }
34808
34809 tree id = cp_parser_identifier (parser);
34810 if (id == error_mark_node)
34811 break;
34812
34813 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34814 break;
34815
34816 parser->colon_corrects_to_scope_p = false;
34817 parser->colon_doesnt_start_class_def_p = true;
34818 tree begin = cp_parser_assignment_expression (parser);
34819
34820 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34821 break;
34822
34823 tree end = cp_parser_assignment_expression (parser);
34824
34825 tree step = integer_one_node;
34826 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34827 {
34828 cp_lexer_consume_token (parser->lexer);
34829 step = cp_parser_assignment_expression (parser);
34830 }
34831
34832 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
34833 DECL_ARTIFICIAL (iter_var) = 1;
34834 DECL_CONTEXT (iter_var) = current_function_decl;
34835 pushdecl (iter_var);
34836
34837 *last = make_tree_vec (6);
34838 TREE_VEC_ELT (*last, 0) = iter_var;
34839 TREE_VEC_ELT (*last, 1) = begin;
34840 TREE_VEC_ELT (*last, 2) = end;
34841 TREE_VEC_ELT (*last, 3) = step;
34842 last = &TREE_CHAIN (*last);
34843
34844 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34845 {
34846 cp_lexer_consume_token (parser->lexer);
34847 continue;
34848 }
34849 break;
34850 }
34851 while (1);
34852
34853 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34854 parser->colon_doesnt_start_class_def_p
34855 = saved_colon_doesnt_start_class_def_p;
34856
34857 if (!parens.require_close (parser))
34858 cp_parser_skip_to_closing_parenthesis (parser,
34859 /*recovering=*/true,
34860 /*or_comma=*/false,
34861 /*consume_paren=*/true);
34862
34863 return ret ? ret : error_mark_node;
34864 }
34865
34866 /* OpenMP 4.0:
34867 depend ( depend-kind : variable-list )
34868
34869 depend-kind:
34870 in | out | inout
34871
34872 OpenMP 4.5:
34873 depend ( source )
34874
34875 depend ( sink : vec )
34876
34877 OpenMP 5.0:
34878 depend ( depend-modifier , depend-kind: variable-list )
34879
34880 depend-kind:
34881 in | out | inout | mutexinoutset | depobj
34882
34883 depend-modifier:
34884 iterator ( iterators-definition ) */
34885
34886 static tree
34887 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
34888 {
34889 tree nlist, c, iterators = NULL_TREE;
34890 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
34891
34892 matching_parens parens;
34893 if (!parens.require_open (parser))
34894 return list;
34895
34896 do
34897 {
34898 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34899 goto invalid_kind;
34900
34901 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34902 const char *p = IDENTIFIER_POINTER (id);
34903
34904 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
34905 {
34906 begin_scope (sk_omp, NULL);
34907 iterators = cp_parser_omp_iterators (parser);
34908 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
34909 continue;
34910 }
34911 if (strcmp ("in", p) == 0)
34912 kind = OMP_CLAUSE_DEPEND_IN;
34913 else if (strcmp ("inout", p) == 0)
34914 kind = OMP_CLAUSE_DEPEND_INOUT;
34915 else if (strcmp ("mutexinoutset", p) == 0)
34916 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
34917 else if (strcmp ("out", p) == 0)
34918 kind = OMP_CLAUSE_DEPEND_OUT;
34919 else if (strcmp ("depobj", p) == 0)
34920 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
34921 else if (strcmp ("sink", p) == 0)
34922 kind = OMP_CLAUSE_DEPEND_SINK;
34923 else if (strcmp ("source", p) == 0)
34924 kind = OMP_CLAUSE_DEPEND_SOURCE;
34925 else
34926 goto invalid_kind;
34927 break;
34928 }
34929 while (1);
34930
34931 cp_lexer_consume_token (parser->lexer);
34932
34933 if (iterators
34934 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
34935 {
34936 poplevel (0, 1, 0);
34937 error_at (loc, "%<iterator%> modifier incompatible with %qs",
34938 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
34939 iterators = NULL_TREE;
34940 }
34941
34942 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
34943 {
34944 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
34945 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34946 OMP_CLAUSE_DECL (c) = NULL_TREE;
34947 OMP_CLAUSE_CHAIN (c) = list;
34948 if (!parens.require_close (parser))
34949 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34950 /*or_comma=*/false,
34951 /*consume_paren=*/true);
34952 return c;
34953 }
34954
34955 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34956 goto resync_fail;
34957
34958 if (kind == OMP_CLAUSE_DEPEND_SINK)
34959 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
34960 else
34961 {
34962 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
34963 list, NULL);
34964
34965 if (iterators)
34966 {
34967 tree block = poplevel (1, 1, 0);
34968 if (iterators == error_mark_node)
34969 iterators = NULL_TREE;
34970 else
34971 TREE_VEC_ELT (iterators, 5) = block;
34972 }
34973
34974 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34975 {
34976 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34977 if (iterators)
34978 OMP_CLAUSE_DECL (c)
34979 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
34980 }
34981 }
34982 return nlist;
34983
34984 invalid_kind:
34985 cp_parser_error (parser, "invalid depend kind");
34986 resync_fail:
34987 if (iterators)
34988 poplevel (0, 1, 0);
34989 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34990 /*or_comma=*/false,
34991 /*consume_paren=*/true);
34992 return list;
34993 }
34994
34995 /* OpenMP 4.0:
34996 map ( map-kind : variable-list )
34997 map ( variable-list )
34998
34999 map-kind:
35000 alloc | to | from | tofrom
35001
35002 OpenMP 4.5:
35003 map-kind:
35004 alloc | to | from | tofrom | release | delete
35005
35006 map ( always [,] map-kind: variable-list ) */
35007
35008 static tree
35009 cp_parser_omp_clause_map (cp_parser *parser, tree list)
35010 {
35011 tree nlist, c;
35012 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
35013 bool always = false;
35014
35015 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35016 return list;
35017
35018 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35019 {
35020 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35021 const char *p = IDENTIFIER_POINTER (id);
35022
35023 if (strcmp ("always", p) == 0)
35024 {
35025 int nth = 2;
35026 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
35027 nth++;
35028 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
35029 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
35030 == RID_DELETE))
35031 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
35032 == CPP_COLON))
35033 {
35034 always = true;
35035 cp_lexer_consume_token (parser->lexer);
35036 if (nth == 3)
35037 cp_lexer_consume_token (parser->lexer);
35038 }
35039 }
35040 }
35041
35042 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
35043 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
35044 {
35045 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35046 const char *p = IDENTIFIER_POINTER (id);
35047
35048 if (strcmp ("alloc", p) == 0)
35049 kind = GOMP_MAP_ALLOC;
35050 else if (strcmp ("to", p) == 0)
35051 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
35052 else if (strcmp ("from", p) == 0)
35053 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
35054 else if (strcmp ("tofrom", p) == 0)
35055 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
35056 else if (strcmp ("release", p) == 0)
35057 kind = GOMP_MAP_RELEASE;
35058 else
35059 {
35060 cp_parser_error (parser, "invalid map kind");
35061 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35062 /*or_comma=*/false,
35063 /*consume_paren=*/true);
35064 return list;
35065 }
35066 cp_lexer_consume_token (parser->lexer);
35067 cp_lexer_consume_token (parser->lexer);
35068 }
35069 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
35070 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
35071 {
35072 kind = GOMP_MAP_DELETE;
35073 cp_lexer_consume_token (parser->lexer);
35074 cp_lexer_consume_token (parser->lexer);
35075 }
35076
35077 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
35078 NULL);
35079
35080 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
35081 OMP_CLAUSE_SET_MAP_KIND (c, kind);
35082
35083 return nlist;
35084 }
35085
35086 /* OpenMP 4.0:
35087 device ( expression ) */
35088
35089 static tree
35090 cp_parser_omp_clause_device (cp_parser *parser, tree list,
35091 location_t location)
35092 {
35093 tree t, c;
35094
35095 matching_parens parens;
35096 if (!parens.require_open (parser))
35097 return list;
35098
35099 t = cp_parser_assignment_expression (parser);
35100
35101 if (t == error_mark_node
35102 || !parens.require_close (parser))
35103 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35104 /*or_comma=*/false,
35105 /*consume_paren=*/true);
35106
35107 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
35108 "device", location);
35109
35110 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
35111 OMP_CLAUSE_DEVICE_ID (c) = t;
35112 OMP_CLAUSE_CHAIN (c) = list;
35113
35114 return c;
35115 }
35116
35117 /* OpenMP 4.0:
35118 dist_schedule ( static )
35119 dist_schedule ( static , expression ) */
35120
35121 static tree
35122 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
35123 location_t location)
35124 {
35125 tree c, t;
35126
35127 matching_parens parens;
35128 if (!parens.require_open (parser))
35129 return list;
35130
35131 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
35132
35133 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
35134 goto invalid_kind;
35135 cp_lexer_consume_token (parser->lexer);
35136
35137 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35138 {
35139 cp_lexer_consume_token (parser->lexer);
35140
35141 t = cp_parser_assignment_expression (parser);
35142
35143 if (t == error_mark_node)
35144 goto resync_fail;
35145 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
35146
35147 if (!parens.require_close (parser))
35148 goto resync_fail;
35149 }
35150 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
35151 goto resync_fail;
35152
35153 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
35154 location);
35155 OMP_CLAUSE_CHAIN (c) = list;
35156 return c;
35157
35158 invalid_kind:
35159 cp_parser_error (parser, "invalid dist_schedule kind");
35160 resync_fail:
35161 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35162 /*or_comma=*/false,
35163 /*consume_paren=*/true);
35164 return list;
35165 }
35166
35167 /* OpenMP 4.0:
35168 proc_bind ( proc-bind-kind )
35169
35170 proc-bind-kind:
35171 master | close | spread */
35172
35173 static tree
35174 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
35175 location_t location)
35176 {
35177 tree c;
35178 enum omp_clause_proc_bind_kind kind;
35179
35180 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35181 return list;
35182
35183 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35184 {
35185 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35186 const char *p = IDENTIFIER_POINTER (id);
35187
35188 if (strcmp ("master", p) == 0)
35189 kind = OMP_CLAUSE_PROC_BIND_MASTER;
35190 else if (strcmp ("close", p) == 0)
35191 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
35192 else if (strcmp ("spread", p) == 0)
35193 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
35194 else
35195 goto invalid_kind;
35196 }
35197 else
35198 goto invalid_kind;
35199
35200 cp_lexer_consume_token (parser->lexer);
35201 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
35202 goto resync_fail;
35203
35204 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
35205 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
35206 location);
35207 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
35208 OMP_CLAUSE_CHAIN (c) = list;
35209 return c;
35210
35211 invalid_kind:
35212 cp_parser_error (parser, "invalid depend kind");
35213 resync_fail:
35214 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35215 /*or_comma=*/false,
35216 /*consume_paren=*/true);
35217 return list;
35218 }
35219
35220 /* OpenACC:
35221 async [( int-expr )] */
35222
35223 static tree
35224 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
35225 {
35226 tree c, t;
35227 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35228
35229 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
35230
35231 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35232 {
35233 matching_parens parens;
35234 parens.consume_open (parser);
35235
35236 t = cp_parser_expression (parser);
35237 if (t == error_mark_node
35238 || !parens.require_close (parser))
35239 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35240 /*or_comma=*/false,
35241 /*consume_paren=*/true);
35242 }
35243
35244 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
35245
35246 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
35247 OMP_CLAUSE_ASYNC_EXPR (c) = t;
35248 OMP_CLAUSE_CHAIN (c) = list;
35249 list = c;
35250
35251 return list;
35252 }
35253
35254 /* Parse all OpenACC clauses. The set clauses allowed by the directive
35255 is a bitmask in MASK. Return the list of clauses found. */
35256
35257 static tree
35258 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
35259 const char *where, cp_token *pragma_tok,
35260 bool finish_p = true)
35261 {
35262 tree clauses = NULL;
35263 bool first = true;
35264
35265 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35266 {
35267 location_t here;
35268 pragma_omp_clause c_kind;
35269 omp_clause_code code;
35270 const char *c_name;
35271 tree prev = clauses;
35272
35273 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35274 cp_lexer_consume_token (parser->lexer);
35275
35276 here = cp_lexer_peek_token (parser->lexer)->location;
35277 c_kind = cp_parser_omp_clause_name (parser);
35278
35279 switch (c_kind)
35280 {
35281 case PRAGMA_OACC_CLAUSE_ASYNC:
35282 clauses = cp_parser_oacc_clause_async (parser, clauses);
35283 c_name = "async";
35284 break;
35285 case PRAGMA_OACC_CLAUSE_AUTO:
35286 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
35287 clauses);
35288 c_name = "auto";
35289 break;
35290 case PRAGMA_OACC_CLAUSE_COLLAPSE:
35291 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
35292 c_name = "collapse";
35293 break;
35294 case PRAGMA_OACC_CLAUSE_COPY:
35295 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35296 c_name = "copy";
35297 break;
35298 case PRAGMA_OACC_CLAUSE_COPYIN:
35299 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35300 c_name = "copyin";
35301 break;
35302 case PRAGMA_OACC_CLAUSE_COPYOUT:
35303 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35304 c_name = "copyout";
35305 break;
35306 case PRAGMA_OACC_CLAUSE_CREATE:
35307 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35308 c_name = "create";
35309 break;
35310 case PRAGMA_OACC_CLAUSE_DELETE:
35311 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35312 c_name = "delete";
35313 break;
35314 case PRAGMA_OMP_CLAUSE_DEFAULT:
35315 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
35316 c_name = "default";
35317 break;
35318 case PRAGMA_OACC_CLAUSE_DEVICE:
35319 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35320 c_name = "device";
35321 break;
35322 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
35323 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
35324 c_name = "deviceptr";
35325 break;
35326 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
35327 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35328 c_name = "device_resident";
35329 break;
35330 case PRAGMA_OACC_CLAUSE_FINALIZE:
35331 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
35332 clauses);
35333 c_name = "finalize";
35334 break;
35335 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
35336 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
35337 clauses);
35338 c_name = "firstprivate";
35339 break;
35340 case PRAGMA_OACC_CLAUSE_GANG:
35341 c_name = "gang";
35342 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
35343 c_name, clauses);
35344 break;
35345 case PRAGMA_OACC_CLAUSE_HOST:
35346 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35347 c_name = "host";
35348 break;
35349 case PRAGMA_OACC_CLAUSE_IF:
35350 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
35351 c_name = "if";
35352 break;
35353 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
35354 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
35355 clauses);
35356 c_name = "if_present";
35357 break;
35358 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
35359 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
35360 clauses);
35361 c_name = "independent";
35362 break;
35363 case PRAGMA_OACC_CLAUSE_LINK:
35364 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35365 c_name = "link";
35366 break;
35367 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
35368 code = OMP_CLAUSE_NUM_GANGS;
35369 c_name = "num_gangs";
35370 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35371 clauses);
35372 break;
35373 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
35374 c_name = "num_workers";
35375 code = OMP_CLAUSE_NUM_WORKERS;
35376 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35377 clauses);
35378 break;
35379 case PRAGMA_OACC_CLAUSE_PRESENT:
35380 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35381 c_name = "present";
35382 break;
35383 case PRAGMA_OACC_CLAUSE_PRIVATE:
35384 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35385 clauses);
35386 c_name = "private";
35387 break;
35388 case PRAGMA_OACC_CLAUSE_REDUCTION:
35389 clauses
35390 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35391 false, clauses);
35392 c_name = "reduction";
35393 break;
35394 case PRAGMA_OACC_CLAUSE_SEQ:
35395 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
35396 clauses);
35397 c_name = "seq";
35398 break;
35399 case PRAGMA_OACC_CLAUSE_TILE:
35400 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
35401 c_name = "tile";
35402 break;
35403 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
35404 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35405 clauses);
35406 c_name = "use_device";
35407 break;
35408 case PRAGMA_OACC_CLAUSE_VECTOR:
35409 c_name = "vector";
35410 clauses = cp_parser_oacc_shape_clause (parser, here,
35411 OMP_CLAUSE_VECTOR,
35412 c_name, clauses);
35413 break;
35414 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
35415 c_name = "vector_length";
35416 code = OMP_CLAUSE_VECTOR_LENGTH;
35417 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35418 clauses);
35419 break;
35420 case PRAGMA_OACC_CLAUSE_WAIT:
35421 clauses = cp_parser_oacc_clause_wait (parser, clauses);
35422 c_name = "wait";
35423 break;
35424 case PRAGMA_OACC_CLAUSE_WORKER:
35425 c_name = "worker";
35426 clauses = cp_parser_oacc_shape_clause (parser, here,
35427 OMP_CLAUSE_WORKER,
35428 c_name, clauses);
35429 break;
35430 default:
35431 cp_parser_error (parser, "expected %<#pragma acc%> clause");
35432 goto saw_error;
35433 }
35434
35435 first = false;
35436
35437 if (((mask >> c_kind) & 1) == 0)
35438 {
35439 /* Remove the invalid clause(s) from the list to avoid
35440 confusing the rest of the compiler. */
35441 clauses = prev;
35442 error_at (here, "%qs is not valid for %qs", c_name, where);
35443 }
35444 }
35445
35446 saw_error:
35447 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35448
35449 if (finish_p)
35450 return finish_omp_clauses (clauses, C_ORT_ACC);
35451
35452 return clauses;
35453 }
35454
35455 /* Parse all OpenMP clauses. The set clauses allowed by the directive
35456 is a bitmask in MASK. Return the list of clauses found; the result
35457 of clause default goes in *pdefault. */
35458
35459 static tree
35460 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
35461 const char *where, cp_token *pragma_tok,
35462 bool finish_p = true)
35463 {
35464 tree clauses = NULL;
35465 bool first = true;
35466 cp_token *token = NULL;
35467
35468 /* Don't create location wrapper nodes within OpenMP clauses. */
35469 auto_suppress_location_wrappers sentinel;
35470
35471 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35472 {
35473 pragma_omp_clause c_kind;
35474 const char *c_name;
35475 tree prev = clauses;
35476
35477 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35478 cp_lexer_consume_token (parser->lexer);
35479
35480 token = cp_lexer_peek_token (parser->lexer);
35481 c_kind = cp_parser_omp_clause_name (parser);
35482
35483 switch (c_kind)
35484 {
35485 case PRAGMA_OMP_CLAUSE_COLLAPSE:
35486 clauses = cp_parser_omp_clause_collapse (parser, clauses,
35487 token->location);
35488 c_name = "collapse";
35489 break;
35490 case PRAGMA_OMP_CLAUSE_COPYIN:
35491 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
35492 c_name = "copyin";
35493 break;
35494 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
35495 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
35496 clauses);
35497 c_name = "copyprivate";
35498 break;
35499 case PRAGMA_OMP_CLAUSE_DEFAULT:
35500 clauses = cp_parser_omp_clause_default (parser, clauses,
35501 token->location, false);
35502 c_name = "default";
35503 break;
35504 case PRAGMA_OMP_CLAUSE_FINAL:
35505 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
35506 c_name = "final";
35507 break;
35508 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
35509 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
35510 clauses);
35511 c_name = "firstprivate";
35512 break;
35513 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
35514 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
35515 token->location);
35516 c_name = "grainsize";
35517 break;
35518 case PRAGMA_OMP_CLAUSE_HINT:
35519 clauses = cp_parser_omp_clause_hint (parser, clauses,
35520 token->location);
35521 c_name = "hint";
35522 break;
35523 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
35524 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
35525 token->location);
35526 c_name = "defaultmap";
35527 break;
35528 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
35529 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35530 clauses);
35531 c_name = "use_device_ptr";
35532 break;
35533 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
35534 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
35535 clauses);
35536 c_name = "is_device_ptr";
35537 break;
35538 case PRAGMA_OMP_CLAUSE_IF:
35539 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
35540 true);
35541 c_name = "if";
35542 break;
35543 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
35544 clauses
35545 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
35546 true, clauses);
35547 c_name = "in_reduction";
35548 break;
35549 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
35550 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
35551 c_name = "lastprivate";
35552 break;
35553 case PRAGMA_OMP_CLAUSE_MERGEABLE:
35554 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
35555 token->location);
35556 c_name = "mergeable";
35557 break;
35558 case PRAGMA_OMP_CLAUSE_NOWAIT:
35559 clauses = cp_parser_omp_clause_nowait (parser, clauses,
35560 token->location);
35561 c_name = "nowait";
35562 break;
35563 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
35564 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
35565 token->location);
35566 c_name = "num_tasks";
35567 break;
35568 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
35569 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
35570 token->location);
35571 c_name = "num_threads";
35572 break;
35573 case PRAGMA_OMP_CLAUSE_ORDER:
35574 clauses = cp_parser_omp_clause_order (parser, clauses,
35575 token->location);
35576 c_name = "order";
35577 break;
35578 case PRAGMA_OMP_CLAUSE_ORDERED:
35579 clauses = cp_parser_omp_clause_ordered (parser, clauses,
35580 token->location);
35581 c_name = "ordered";
35582 break;
35583 case PRAGMA_OMP_CLAUSE_PRIORITY:
35584 clauses = cp_parser_omp_clause_priority (parser, clauses,
35585 token->location);
35586 c_name = "priority";
35587 break;
35588 case PRAGMA_OMP_CLAUSE_PRIVATE:
35589 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35590 clauses);
35591 c_name = "private";
35592 break;
35593 case PRAGMA_OMP_CLAUSE_REDUCTION:
35594 clauses
35595 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35596 true, clauses);
35597 c_name = "reduction";
35598 break;
35599 case PRAGMA_OMP_CLAUSE_SCHEDULE:
35600 clauses = cp_parser_omp_clause_schedule (parser, clauses,
35601 token->location);
35602 c_name = "schedule";
35603 break;
35604 case PRAGMA_OMP_CLAUSE_SHARED:
35605 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
35606 clauses);
35607 c_name = "shared";
35608 break;
35609 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
35610 clauses
35611 = cp_parser_omp_clause_reduction (parser,
35612 OMP_CLAUSE_TASK_REDUCTION,
35613 true, clauses);
35614 c_name = "task_reduction";
35615 break;
35616 case PRAGMA_OMP_CLAUSE_UNTIED:
35617 clauses = cp_parser_omp_clause_untied (parser, clauses,
35618 token->location);
35619 c_name = "untied";
35620 break;
35621 case PRAGMA_OMP_CLAUSE_INBRANCH:
35622 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
35623 clauses, token->location);
35624 c_name = "inbranch";
35625 break;
35626 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
35627 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
35628 clauses);
35629 c_name = "nontemporal";
35630 break;
35631 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
35632 clauses = cp_parser_omp_clause_branch (parser,
35633 OMP_CLAUSE_NOTINBRANCH,
35634 clauses, token->location);
35635 c_name = "notinbranch";
35636 break;
35637 case PRAGMA_OMP_CLAUSE_PARALLEL:
35638 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
35639 clauses, token->location);
35640 c_name = "parallel";
35641 if (!first)
35642 {
35643 clause_not_first:
35644 error_at (token->location, "%qs must be the first clause of %qs",
35645 c_name, where);
35646 clauses = prev;
35647 }
35648 break;
35649 case PRAGMA_OMP_CLAUSE_FOR:
35650 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
35651 clauses, token->location);
35652 c_name = "for";
35653 if (!first)
35654 goto clause_not_first;
35655 break;
35656 case PRAGMA_OMP_CLAUSE_SECTIONS:
35657 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
35658 clauses, token->location);
35659 c_name = "sections";
35660 if (!first)
35661 goto clause_not_first;
35662 break;
35663 case PRAGMA_OMP_CLAUSE_TASKGROUP:
35664 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
35665 clauses, token->location);
35666 c_name = "taskgroup";
35667 if (!first)
35668 goto clause_not_first;
35669 break;
35670 case PRAGMA_OMP_CLAUSE_LINK:
35671 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
35672 c_name = "to";
35673 break;
35674 case PRAGMA_OMP_CLAUSE_TO:
35675 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
35676 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35677 clauses);
35678 else
35679 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
35680 c_name = "to";
35681 break;
35682 case PRAGMA_OMP_CLAUSE_FROM:
35683 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
35684 c_name = "from";
35685 break;
35686 case PRAGMA_OMP_CLAUSE_UNIFORM:
35687 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
35688 clauses);
35689 c_name = "uniform";
35690 break;
35691 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
35692 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
35693 token->location);
35694 c_name = "num_teams";
35695 break;
35696 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
35697 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
35698 token->location);
35699 c_name = "thread_limit";
35700 break;
35701 case PRAGMA_OMP_CLAUSE_ALIGNED:
35702 clauses = cp_parser_omp_clause_aligned (parser, clauses);
35703 c_name = "aligned";
35704 break;
35705 case PRAGMA_OMP_CLAUSE_LINEAR:
35706 {
35707 bool declare_simd = false;
35708 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
35709 declare_simd = true;
35710 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
35711 }
35712 c_name = "linear";
35713 break;
35714 case PRAGMA_OMP_CLAUSE_DEPEND:
35715 clauses = cp_parser_omp_clause_depend (parser, clauses,
35716 token->location);
35717 c_name = "depend";
35718 break;
35719 case PRAGMA_OMP_CLAUSE_MAP:
35720 clauses = cp_parser_omp_clause_map (parser, clauses);
35721 c_name = "map";
35722 break;
35723 case PRAGMA_OMP_CLAUSE_DEVICE:
35724 clauses = cp_parser_omp_clause_device (parser, clauses,
35725 token->location);
35726 c_name = "device";
35727 break;
35728 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
35729 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
35730 token->location);
35731 c_name = "dist_schedule";
35732 break;
35733 case PRAGMA_OMP_CLAUSE_PROC_BIND:
35734 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
35735 token->location);
35736 c_name = "proc_bind";
35737 break;
35738 case PRAGMA_OMP_CLAUSE_SAFELEN:
35739 clauses = cp_parser_omp_clause_safelen (parser, clauses,
35740 token->location);
35741 c_name = "safelen";
35742 break;
35743 case PRAGMA_OMP_CLAUSE_SIMDLEN:
35744 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
35745 token->location);
35746 c_name = "simdlen";
35747 break;
35748 case PRAGMA_OMP_CLAUSE_NOGROUP:
35749 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
35750 token->location);
35751 c_name = "nogroup";
35752 break;
35753 case PRAGMA_OMP_CLAUSE_THREADS:
35754 clauses
35755 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
35756 clauses, token->location);
35757 c_name = "threads";
35758 break;
35759 case PRAGMA_OMP_CLAUSE_SIMD:
35760 clauses
35761 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
35762 clauses, token->location);
35763 c_name = "simd";
35764 break;
35765 default:
35766 cp_parser_error (parser, "expected %<#pragma omp%> clause");
35767 goto saw_error;
35768 }
35769
35770 first = false;
35771
35772 if (((mask >> c_kind) & 1) == 0)
35773 {
35774 /* Remove the invalid clause(s) from the list to avoid
35775 confusing the rest of the compiler. */
35776 clauses = prev;
35777 error_at (token->location, "%qs is not valid for %qs", c_name, where);
35778 }
35779 }
35780 saw_error:
35781 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35782 if (finish_p)
35783 {
35784 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
35785 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
35786 else
35787 return finish_omp_clauses (clauses, C_ORT_OMP);
35788 }
35789 return clauses;
35790 }
35791
35792 /* OpenMP 2.5:
35793 structured-block:
35794 statement
35795
35796 In practice, we're also interested in adding the statement to an
35797 outer node. So it is convenient if we work around the fact that
35798 cp_parser_statement calls add_stmt. */
35799
35800 static unsigned
35801 cp_parser_begin_omp_structured_block (cp_parser *parser)
35802 {
35803 unsigned save = parser->in_statement;
35804
35805 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35806 This preserves the "not within loop or switch" style error messages
35807 for nonsense cases like
35808 void foo() {
35809 #pragma omp single
35810 break;
35811 }
35812 */
35813 if (parser->in_statement)
35814 parser->in_statement = IN_OMP_BLOCK;
35815
35816 return save;
35817 }
35818
35819 static void
35820 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
35821 {
35822 parser->in_statement = save;
35823 }
35824
35825 static tree
35826 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
35827 {
35828 tree stmt = begin_omp_structured_block ();
35829 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35830
35831 cp_parser_statement (parser, NULL_TREE, false, if_p);
35832
35833 cp_parser_end_omp_structured_block (parser, save);
35834 return finish_omp_structured_block (stmt);
35835 }
35836
35837 /* OpenMP 2.5:
35838 # pragma omp atomic new-line
35839 expression-stmt
35840
35841 expression-stmt:
35842 x binop= expr | x++ | ++x | x-- | --x
35843 binop:
35844 +, *, -, /, &, ^, |, <<, >>
35845
35846 where x is an lvalue expression with scalar type.
35847
35848 OpenMP 3.1:
35849 # pragma omp atomic new-line
35850 update-stmt
35851
35852 # pragma omp atomic read new-line
35853 read-stmt
35854
35855 # pragma omp atomic write new-line
35856 write-stmt
35857
35858 # pragma omp atomic update new-line
35859 update-stmt
35860
35861 # pragma omp atomic capture new-line
35862 capture-stmt
35863
35864 # pragma omp atomic capture new-line
35865 capture-block
35866
35867 read-stmt:
35868 v = x
35869 write-stmt:
35870 x = expr
35871 update-stmt:
35872 expression-stmt | x = x binop expr
35873 capture-stmt:
35874 v = expression-stmt
35875 capture-block:
35876 { v = x; update-stmt; } | { update-stmt; v = x; }
35877
35878 OpenMP 4.0:
35879 update-stmt:
35880 expression-stmt | x = x binop expr | x = expr binop x
35881 capture-stmt:
35882 v = update-stmt
35883 capture-block:
35884 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35885
35886 where x and v are lvalue expressions with scalar type. */
35887
35888 static void
35889 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
35890 {
35891 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
35892 tree rhs1 = NULL_TREE, orig_lhs;
35893 location_t loc = pragma_tok->location;
35894 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
35895 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
35896 bool structured_block = false;
35897 bool first = true;
35898 tree clauses = NULL_TREE;
35899
35900 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35901 {
35902 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35903 cp_lexer_consume_token (parser->lexer);
35904
35905 first = false;
35906
35907 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35908 {
35909 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35910 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
35911 const char *p = IDENTIFIER_POINTER (id);
35912 enum tree_code new_code = ERROR_MARK;
35913 enum omp_memory_order new_memory_order
35914 = OMP_MEMORY_ORDER_UNSPECIFIED;
35915
35916 if (!strcmp (p, "read"))
35917 new_code = OMP_ATOMIC_READ;
35918 else if (!strcmp (p, "write"))
35919 new_code = NOP_EXPR;
35920 else if (!strcmp (p, "update"))
35921 new_code = OMP_ATOMIC;
35922 else if (!strcmp (p, "capture"))
35923 new_code = OMP_ATOMIC_CAPTURE_NEW;
35924 else if (!strcmp (p, "seq_cst"))
35925 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35926 else if (!strcmp (p, "acq_rel"))
35927 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35928 else if (!strcmp (p, "release"))
35929 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
35930 else if (!strcmp (p, "acquire"))
35931 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35932 else if (!strcmp (p, "relaxed"))
35933 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
35934 else if (!strcmp (p, "hint"))
35935 {
35936 cp_lexer_consume_token (parser->lexer);
35937 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
35938 continue;
35939 }
35940 else
35941 {
35942 p = NULL;
35943 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
35944 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35945 "%<release%>, %<relaxed%> or %<hint%> clause");
35946 }
35947 if (p)
35948 {
35949 if (new_code != ERROR_MARK)
35950 {
35951 if (code != ERROR_MARK)
35952 error_at (cloc, "too many atomic clauses");
35953 else
35954 code = new_code;
35955 }
35956 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35957 {
35958 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35959 error_at (cloc, "too many memory order clauses");
35960 else
35961 memory_order = new_memory_order;
35962 }
35963 cp_lexer_consume_token (parser->lexer);
35964 continue;
35965 }
35966 }
35967 break;
35968 }
35969 cp_parser_require_pragma_eol (parser, pragma_tok);
35970
35971 if (code == ERROR_MARK)
35972 code = OMP_ATOMIC;
35973 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
35974 {
35975 omp_requires_mask
35976 = (enum omp_requires) (omp_requires_mask
35977 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
35978 switch ((enum omp_memory_order)
35979 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
35980 {
35981 case OMP_MEMORY_ORDER_UNSPECIFIED:
35982 case OMP_MEMORY_ORDER_RELAXED:
35983 memory_order = OMP_MEMORY_ORDER_RELAXED;
35984 break;
35985 case OMP_MEMORY_ORDER_SEQ_CST:
35986 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35987 break;
35988 case OMP_MEMORY_ORDER_ACQ_REL:
35989 switch (code)
35990 {
35991 case OMP_ATOMIC_READ:
35992 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35993 break;
35994 case NOP_EXPR: /* atomic write */
35995 case OMP_ATOMIC:
35996 memory_order = OMP_MEMORY_ORDER_RELEASE;
35997 break;
35998 default:
35999 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
36000 break;
36001 }
36002 break;
36003 default:
36004 gcc_unreachable ();
36005 }
36006 }
36007 else
36008 switch (code)
36009 {
36010 case OMP_ATOMIC_READ:
36011 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
36012 || memory_order == OMP_MEMORY_ORDER_RELEASE)
36013 {
36014 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
36015 "%<acq_rel%> or %<release%> clauses");
36016 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
36017 }
36018 break;
36019 case NOP_EXPR: /* atomic write */
36020 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
36021 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
36022 {
36023 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
36024 "%<acq_rel%> or %<acquire%> clauses");
36025 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
36026 }
36027 break;
36028 case OMP_ATOMIC:
36029 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
36030 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
36031 {
36032 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
36033 "%<acq_rel%> or %<acquire%> clauses");
36034 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
36035 }
36036 break;
36037 default:
36038 break;
36039 }
36040
36041 switch (code)
36042 {
36043 case OMP_ATOMIC_READ:
36044 case NOP_EXPR: /* atomic write */
36045 v = cp_parser_unary_expression (parser);
36046 if (v == error_mark_node)
36047 goto saw_error;
36048 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
36049 goto saw_error;
36050 if (code == NOP_EXPR)
36051 lhs = cp_parser_expression (parser);
36052 else
36053 lhs = cp_parser_unary_expression (parser);
36054 if (lhs == error_mark_node)
36055 goto saw_error;
36056 if (code == NOP_EXPR)
36057 {
36058 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
36059 opcode. */
36060 code = OMP_ATOMIC;
36061 rhs = lhs;
36062 lhs = v;
36063 v = NULL_TREE;
36064 }
36065 goto done;
36066 case OMP_ATOMIC_CAPTURE_NEW:
36067 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36068 {
36069 cp_lexer_consume_token (parser->lexer);
36070 structured_block = true;
36071 }
36072 else
36073 {
36074 v = cp_parser_unary_expression (parser);
36075 if (v == error_mark_node)
36076 goto saw_error;
36077 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
36078 goto saw_error;
36079 }
36080 default:
36081 break;
36082 }
36083
36084 restart:
36085 lhs = cp_parser_unary_expression (parser);
36086 orig_lhs = lhs;
36087 switch (TREE_CODE (lhs))
36088 {
36089 case ERROR_MARK:
36090 goto saw_error;
36091
36092 case POSTINCREMENT_EXPR:
36093 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
36094 code = OMP_ATOMIC_CAPTURE_OLD;
36095 /* FALLTHROUGH */
36096 case PREINCREMENT_EXPR:
36097 lhs = TREE_OPERAND (lhs, 0);
36098 opcode = PLUS_EXPR;
36099 rhs = integer_one_node;
36100 break;
36101
36102 case POSTDECREMENT_EXPR:
36103 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
36104 code = OMP_ATOMIC_CAPTURE_OLD;
36105 /* FALLTHROUGH */
36106 case PREDECREMENT_EXPR:
36107 lhs = TREE_OPERAND (lhs, 0);
36108 opcode = MINUS_EXPR;
36109 rhs = integer_one_node;
36110 break;
36111
36112 case COMPOUND_EXPR:
36113 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
36114 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
36115 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
36116 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
36117 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
36118 (TREE_OPERAND (lhs, 1), 0), 0)))
36119 == BOOLEAN_TYPE)
36120 /* Undo effects of boolean_increment for post {in,de}crement. */
36121 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
36122 /* FALLTHRU */
36123 case MODIFY_EXPR:
36124 if (TREE_CODE (lhs) == MODIFY_EXPR
36125 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
36126 {
36127 /* Undo effects of boolean_increment. */
36128 if (integer_onep (TREE_OPERAND (lhs, 1)))
36129 {
36130 /* This is pre or post increment. */
36131 rhs = TREE_OPERAND (lhs, 1);
36132 lhs = TREE_OPERAND (lhs, 0);
36133 opcode = NOP_EXPR;
36134 if (code == OMP_ATOMIC_CAPTURE_NEW
36135 && !structured_block
36136 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
36137 code = OMP_ATOMIC_CAPTURE_OLD;
36138 break;
36139 }
36140 }
36141 /* FALLTHRU */
36142 default:
36143 switch (cp_lexer_peek_token (parser->lexer)->type)
36144 {
36145 case CPP_MULT_EQ:
36146 opcode = MULT_EXPR;
36147 break;
36148 case CPP_DIV_EQ:
36149 opcode = TRUNC_DIV_EXPR;
36150 break;
36151 case CPP_PLUS_EQ:
36152 opcode = PLUS_EXPR;
36153 break;
36154 case CPP_MINUS_EQ:
36155 opcode = MINUS_EXPR;
36156 break;
36157 case CPP_LSHIFT_EQ:
36158 opcode = LSHIFT_EXPR;
36159 break;
36160 case CPP_RSHIFT_EQ:
36161 opcode = RSHIFT_EXPR;
36162 break;
36163 case CPP_AND_EQ:
36164 opcode = BIT_AND_EXPR;
36165 break;
36166 case CPP_OR_EQ:
36167 opcode = BIT_IOR_EXPR;
36168 break;
36169 case CPP_XOR_EQ:
36170 opcode = BIT_XOR_EXPR;
36171 break;
36172 case CPP_EQ:
36173 enum cp_parser_prec oprec;
36174 cp_token *token;
36175 cp_lexer_consume_token (parser->lexer);
36176 cp_parser_parse_tentatively (parser);
36177 rhs1 = cp_parser_simple_cast_expression (parser);
36178 if (rhs1 == error_mark_node)
36179 {
36180 cp_parser_abort_tentative_parse (parser);
36181 cp_parser_simple_cast_expression (parser);
36182 goto saw_error;
36183 }
36184 token = cp_lexer_peek_token (parser->lexer);
36185 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
36186 {
36187 cp_parser_abort_tentative_parse (parser);
36188 cp_parser_parse_tentatively (parser);
36189 rhs = cp_parser_binary_expression (parser, false, true,
36190 PREC_NOT_OPERATOR, NULL);
36191 if (rhs == error_mark_node)
36192 {
36193 cp_parser_abort_tentative_parse (parser);
36194 cp_parser_binary_expression (parser, false, true,
36195 PREC_NOT_OPERATOR, NULL);
36196 goto saw_error;
36197 }
36198 switch (TREE_CODE (rhs))
36199 {
36200 case MULT_EXPR:
36201 case TRUNC_DIV_EXPR:
36202 case RDIV_EXPR:
36203 case PLUS_EXPR:
36204 case MINUS_EXPR:
36205 case LSHIFT_EXPR:
36206 case RSHIFT_EXPR:
36207 case BIT_AND_EXPR:
36208 case BIT_IOR_EXPR:
36209 case BIT_XOR_EXPR:
36210 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
36211 {
36212 if (cp_parser_parse_definitely (parser))
36213 {
36214 opcode = TREE_CODE (rhs);
36215 rhs1 = TREE_OPERAND (rhs, 0);
36216 rhs = TREE_OPERAND (rhs, 1);
36217 goto stmt_done;
36218 }
36219 else
36220 goto saw_error;
36221 }
36222 break;
36223 default:
36224 break;
36225 }
36226 cp_parser_abort_tentative_parse (parser);
36227 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
36228 {
36229 rhs = cp_parser_expression (parser);
36230 if (rhs == error_mark_node)
36231 goto saw_error;
36232 opcode = NOP_EXPR;
36233 rhs1 = NULL_TREE;
36234 goto stmt_done;
36235 }
36236 cp_parser_error (parser,
36237 "invalid form of %<#pragma omp atomic%>");
36238 goto saw_error;
36239 }
36240 if (!cp_parser_parse_definitely (parser))
36241 goto saw_error;
36242 switch (token->type)
36243 {
36244 case CPP_SEMICOLON:
36245 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
36246 {
36247 code = OMP_ATOMIC_CAPTURE_OLD;
36248 v = lhs;
36249 lhs = NULL_TREE;
36250 lhs1 = rhs1;
36251 rhs1 = NULL_TREE;
36252 cp_lexer_consume_token (parser->lexer);
36253 goto restart;
36254 }
36255 else if (structured_block)
36256 {
36257 opcode = NOP_EXPR;
36258 rhs = rhs1;
36259 rhs1 = NULL_TREE;
36260 goto stmt_done;
36261 }
36262 cp_parser_error (parser,
36263 "invalid form of %<#pragma omp atomic%>");
36264 goto saw_error;
36265 case CPP_MULT:
36266 opcode = MULT_EXPR;
36267 break;
36268 case CPP_DIV:
36269 opcode = TRUNC_DIV_EXPR;
36270 break;
36271 case CPP_PLUS:
36272 opcode = PLUS_EXPR;
36273 break;
36274 case CPP_MINUS:
36275 opcode = MINUS_EXPR;
36276 break;
36277 case CPP_LSHIFT:
36278 opcode = LSHIFT_EXPR;
36279 break;
36280 case CPP_RSHIFT:
36281 opcode = RSHIFT_EXPR;
36282 break;
36283 case CPP_AND:
36284 opcode = BIT_AND_EXPR;
36285 break;
36286 case CPP_OR:
36287 opcode = BIT_IOR_EXPR;
36288 break;
36289 case CPP_XOR:
36290 opcode = BIT_XOR_EXPR;
36291 break;
36292 default:
36293 cp_parser_error (parser,
36294 "invalid operator for %<#pragma omp atomic%>");
36295 goto saw_error;
36296 }
36297 oprec = TOKEN_PRECEDENCE (token);
36298 gcc_assert (oprec != PREC_NOT_OPERATOR);
36299 if (commutative_tree_code (opcode))
36300 oprec = (enum cp_parser_prec) (oprec - 1);
36301 cp_lexer_consume_token (parser->lexer);
36302 rhs = cp_parser_binary_expression (parser, false, false,
36303 oprec, NULL);
36304 if (rhs == error_mark_node)
36305 goto saw_error;
36306 goto stmt_done;
36307 /* FALLTHROUGH */
36308 default:
36309 cp_parser_error (parser,
36310 "invalid operator for %<#pragma omp atomic%>");
36311 goto saw_error;
36312 }
36313 cp_lexer_consume_token (parser->lexer);
36314
36315 rhs = cp_parser_expression (parser);
36316 if (rhs == error_mark_node)
36317 goto saw_error;
36318 break;
36319 }
36320 stmt_done:
36321 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
36322 {
36323 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
36324 goto saw_error;
36325 v = cp_parser_unary_expression (parser);
36326 if (v == error_mark_node)
36327 goto saw_error;
36328 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
36329 goto saw_error;
36330 lhs1 = cp_parser_unary_expression (parser);
36331 if (lhs1 == error_mark_node)
36332 goto saw_error;
36333 }
36334 if (structured_block)
36335 {
36336 cp_parser_consume_semicolon_at_end_of_statement (parser);
36337 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
36338 }
36339 done:
36340 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36341 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
36342 rhs1, clauses, memory_order);
36343 if (!structured_block)
36344 cp_parser_consume_semicolon_at_end_of_statement (parser);
36345 return;
36346
36347 saw_error:
36348 cp_parser_skip_to_end_of_block_or_statement (parser);
36349 if (structured_block)
36350 {
36351 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36352 cp_lexer_consume_token (parser->lexer);
36353 else if (code == OMP_ATOMIC_CAPTURE_NEW)
36354 {
36355 cp_parser_skip_to_end_of_block_or_statement (parser);
36356 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36357 cp_lexer_consume_token (parser->lexer);
36358 }
36359 }
36360 }
36361
36362
36363 /* OpenMP 2.5:
36364 # pragma omp barrier new-line */
36365
36366 static void
36367 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
36368 {
36369 cp_parser_require_pragma_eol (parser, pragma_tok);
36370 finish_omp_barrier ();
36371 }
36372
36373 /* OpenMP 2.5:
36374 # pragma omp critical [(name)] new-line
36375 structured-block
36376
36377 OpenMP 4.5:
36378 # pragma omp critical [(name) [hint(expression)]] new-line
36379 structured-block */
36380
36381 #define OMP_CRITICAL_CLAUSE_MASK \
36382 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
36383
36384 static tree
36385 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36386 {
36387 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
36388
36389 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36390 {
36391 matching_parens parens;
36392 parens.consume_open (parser);
36393
36394 name = cp_parser_identifier (parser);
36395
36396 if (name == error_mark_node
36397 || !parens.require_close (parser))
36398 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36399 /*or_comma=*/false,
36400 /*consume_paren=*/true);
36401 if (name == error_mark_node)
36402 name = NULL;
36403
36404 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
36405 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
36406 cp_lexer_consume_token (parser->lexer);
36407
36408 clauses = cp_parser_omp_all_clauses (parser,
36409 OMP_CRITICAL_CLAUSE_MASK,
36410 "#pragma omp critical", pragma_tok);
36411 }
36412 else
36413 cp_parser_require_pragma_eol (parser, pragma_tok);
36414
36415 stmt = cp_parser_omp_structured_block (parser, if_p);
36416 return c_finish_omp_critical (input_location, stmt, name, clauses);
36417 }
36418
36419 /* OpenMP 5.0:
36420 # pragma omp depobj ( depobj ) depobj-clause new-line
36421
36422 depobj-clause:
36423 depend (dependence-type : locator)
36424 destroy
36425 update (dependence-type)
36426
36427 dependence-type:
36428 in
36429 out
36430 inout
36431 mutexinout */
36432
36433 static void
36434 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
36435 {
36436 location_t loc = pragma_tok->location;
36437 matching_parens parens;
36438 if (!parens.require_open (parser))
36439 {
36440 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36441 return;
36442 }
36443
36444 tree depobj = cp_parser_assignment_expression (parser);
36445
36446 if (!parens.require_close (parser))
36447 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36448 /*or_comma=*/false,
36449 /*consume_paren=*/true);
36450
36451 tree clause = NULL_TREE;
36452 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
36453 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
36454 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36455 {
36456 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36457 const char *p = IDENTIFIER_POINTER (id);
36458
36459 cp_lexer_consume_token (parser->lexer);
36460 if (!strcmp ("depend", p))
36461 {
36462 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
36463 if (clause)
36464 clause = finish_omp_clauses (clause, C_ORT_OMP);
36465 if (!clause)
36466 clause = error_mark_node;
36467 }
36468 else if (!strcmp ("destroy", p))
36469 kind = OMP_CLAUSE_DEPEND_LAST;
36470 else if (!strcmp ("update", p))
36471 {
36472 matching_parens c_parens;
36473 if (c_parens.require_open (parser))
36474 {
36475 location_t c2_loc
36476 = cp_lexer_peek_token (parser->lexer)->location;
36477 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36478 {
36479 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
36480 const char *p2 = IDENTIFIER_POINTER (id2);
36481
36482 cp_lexer_consume_token (parser->lexer);
36483 if (!strcmp ("in", p2))
36484 kind = OMP_CLAUSE_DEPEND_IN;
36485 else if (!strcmp ("out", p2))
36486 kind = OMP_CLAUSE_DEPEND_OUT;
36487 else if (!strcmp ("inout", p2))
36488 kind = OMP_CLAUSE_DEPEND_INOUT;
36489 else if (!strcmp ("mutexinoutset", p2))
36490 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
36491 }
36492 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
36493 {
36494 clause = error_mark_node;
36495 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
36496 "%<mutexinoutset%>");
36497 }
36498 if (!c_parens.require_close (parser))
36499 cp_parser_skip_to_closing_parenthesis (parser,
36500 /*recovering=*/true,
36501 /*or_comma=*/false,
36502 /*consume_paren=*/true);
36503 }
36504 else
36505 clause = error_mark_node;
36506 }
36507 }
36508 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
36509 {
36510 clause = error_mark_node;
36511 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
36512 }
36513 cp_parser_require_pragma_eol (parser, pragma_tok);
36514
36515 finish_omp_depobj (loc, depobj, kind, clause);
36516 }
36517
36518
36519 /* OpenMP 2.5:
36520 # pragma omp flush flush-vars[opt] new-line
36521
36522 flush-vars:
36523 ( variable-list )
36524
36525 OpenMP 5.0:
36526 # pragma omp flush memory-order-clause new-line */
36527
36528 static void
36529 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
36530 {
36531 enum memmodel mo = MEMMODEL_LAST;
36532 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36533 {
36534 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36535 const char *p = IDENTIFIER_POINTER (id);
36536 if (!strcmp (p, "acq_rel"))
36537 mo = MEMMODEL_ACQ_REL;
36538 else if (!strcmp (p, "release"))
36539 mo = MEMMODEL_RELEASE;
36540 else if (!strcmp (p, "acquire"))
36541 mo = MEMMODEL_ACQUIRE;
36542 else
36543 error_at (cp_lexer_peek_token (parser->lexer)->location,
36544 "expected %<acq_rel%>, %<release%> or %<acquire%>");
36545 cp_lexer_consume_token (parser->lexer);
36546 }
36547 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36548 {
36549 if (mo != MEMMODEL_LAST)
36550 error_at (cp_lexer_peek_token (parser->lexer)->location,
36551 "%<flush%> list specified together with memory order "
36552 "clause");
36553 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36554 }
36555 cp_parser_require_pragma_eol (parser, pragma_tok);
36556
36557 finish_omp_flush (mo);
36558 }
36559
36560 /* Helper function, to parse omp for increment expression. */
36561
36562 static tree
36563 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
36564 {
36565 tree cond = cp_parser_binary_expression (parser, false, true,
36566 PREC_NOT_OPERATOR, NULL);
36567 if (cond == error_mark_node
36568 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36569 {
36570 cp_parser_skip_to_end_of_statement (parser);
36571 return error_mark_node;
36572 }
36573
36574 switch (TREE_CODE (cond))
36575 {
36576 case GT_EXPR:
36577 case GE_EXPR:
36578 case LT_EXPR:
36579 case LE_EXPR:
36580 break;
36581 case NE_EXPR:
36582 if (code != OACC_LOOP)
36583 break;
36584 gcc_fallthrough ();
36585 default:
36586 return error_mark_node;
36587 }
36588
36589 /* If decl is an iterator, preserve LHS and RHS of the relational
36590 expr until finish_omp_for. */
36591 if (decl
36592 && (type_dependent_expression_p (decl)
36593 || CLASS_TYPE_P (TREE_TYPE (decl))))
36594 return cond;
36595
36596 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
36597 TREE_CODE (cond),
36598 TREE_OPERAND (cond, 0), ERROR_MARK,
36599 TREE_OPERAND (cond, 1), ERROR_MARK,
36600 /*overload=*/NULL, tf_warning_or_error);
36601 }
36602
36603 /* Helper function, to parse omp for increment expression. */
36604
36605 static tree
36606 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
36607 {
36608 cp_token *token = cp_lexer_peek_token (parser->lexer);
36609 enum tree_code op;
36610 tree lhs, rhs;
36611 cp_id_kind idk;
36612 bool decl_first;
36613
36614 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36615 {
36616 op = (token->type == CPP_PLUS_PLUS
36617 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
36618 cp_lexer_consume_token (parser->lexer);
36619 lhs = cp_parser_simple_cast_expression (parser);
36620 if (lhs != decl
36621 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36622 return error_mark_node;
36623 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36624 }
36625
36626 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
36627 if (lhs != decl
36628 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36629 return error_mark_node;
36630
36631 token = cp_lexer_peek_token (parser->lexer);
36632 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36633 {
36634 op = (token->type == CPP_PLUS_PLUS
36635 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
36636 cp_lexer_consume_token (parser->lexer);
36637 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36638 }
36639
36640 op = cp_parser_assignment_operator_opt (parser);
36641 if (op == ERROR_MARK)
36642 return error_mark_node;
36643
36644 if (op != NOP_EXPR)
36645 {
36646 rhs = cp_parser_assignment_expression (parser);
36647 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
36648 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36649 }
36650
36651 lhs = cp_parser_binary_expression (parser, false, false,
36652 PREC_ADDITIVE_EXPRESSION, NULL);
36653 token = cp_lexer_peek_token (parser->lexer);
36654 decl_first = (lhs == decl
36655 || (processing_template_decl && cp_tree_equal (lhs, decl)));
36656 if (decl_first)
36657 lhs = NULL_TREE;
36658 if (token->type != CPP_PLUS
36659 && token->type != CPP_MINUS)
36660 return error_mark_node;
36661
36662 do
36663 {
36664 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
36665 cp_lexer_consume_token (parser->lexer);
36666 rhs = cp_parser_binary_expression (parser, false, false,
36667 PREC_ADDITIVE_EXPRESSION, NULL);
36668 token = cp_lexer_peek_token (parser->lexer);
36669 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
36670 {
36671 if (lhs == NULL_TREE)
36672 {
36673 if (op == PLUS_EXPR)
36674 lhs = rhs;
36675 else
36676 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
36677 tf_warning_or_error);
36678 }
36679 else
36680 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
36681 ERROR_MARK, NULL, tf_warning_or_error);
36682 }
36683 }
36684 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
36685
36686 if (!decl_first)
36687 {
36688 if ((rhs != decl
36689 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
36690 || op == MINUS_EXPR)
36691 return error_mark_node;
36692 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
36693 }
36694 else
36695 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
36696
36697 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36698 }
36699
36700 /* Parse the initialization statement of an OpenMP for loop.
36701
36702 Return true if the resulting construct should have an
36703 OMP_CLAUSE_PRIVATE added to it. */
36704
36705 static tree
36706 cp_parser_omp_for_loop_init (cp_parser *parser,
36707 tree &this_pre_body,
36708 releasing_vec &for_block,
36709 tree &init,
36710 tree &orig_init,
36711 tree &decl,
36712 tree &real_decl)
36713 {
36714 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36715 return NULL_TREE;
36716
36717 tree add_private_clause = NULL_TREE;
36718
36719 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
36720
36721 init-expr:
36722 var = lb
36723 integer-type var = lb
36724 random-access-iterator-type var = lb
36725 pointer-type var = lb
36726 */
36727 cp_decl_specifier_seq type_specifiers;
36728
36729 /* First, try to parse as an initialized declaration. See
36730 cp_parser_condition, from whence the bulk of this is copied. */
36731
36732 cp_parser_parse_tentatively (parser);
36733 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
36734 /*is_declaration=*/true,
36735 /*is_trailing_return=*/false,
36736 &type_specifiers);
36737 if (cp_parser_parse_definitely (parser))
36738 {
36739 /* If parsing a type specifier seq succeeded, then this
36740 MUST be a initialized declaration. */
36741 tree asm_specification, attributes;
36742 cp_declarator *declarator;
36743
36744 declarator = cp_parser_declarator (parser,
36745 CP_PARSER_DECLARATOR_NAMED,
36746 CP_PARSER_FLAGS_NONE,
36747 /*ctor_dtor_or_conv_p=*/NULL,
36748 /*parenthesized_p=*/NULL,
36749 /*member_p=*/false,
36750 /*friend_p=*/false,
36751 /*static_p=*/false);
36752 attributes = cp_parser_attributes_opt (parser);
36753 asm_specification = cp_parser_asm_specification_opt (parser);
36754
36755 if (declarator == cp_error_declarator)
36756 cp_parser_skip_to_end_of_statement (parser);
36757
36758 else
36759 {
36760 tree pushed_scope, auto_node;
36761
36762 decl = start_decl (declarator, &type_specifiers,
36763 SD_INITIALIZED, attributes,
36764 /*prefix_attributes=*/NULL_TREE,
36765 &pushed_scope);
36766
36767 auto_node = type_uses_auto (TREE_TYPE (decl));
36768 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36769 {
36770 if (cp_lexer_next_token_is (parser->lexer,
36771 CPP_OPEN_PAREN))
36772 error ("parenthesized initialization is not allowed in "
36773 "OpenMP %<for%> loop");
36774 else
36775 /* Trigger an error. */
36776 cp_parser_require (parser, CPP_EQ, RT_EQ);
36777
36778 init = error_mark_node;
36779 cp_parser_skip_to_end_of_statement (parser);
36780 }
36781 else if (CLASS_TYPE_P (TREE_TYPE (decl))
36782 || type_dependent_expression_p (decl)
36783 || auto_node)
36784 {
36785 bool is_direct_init, is_non_constant_init;
36786
36787 init = cp_parser_initializer (parser,
36788 &is_direct_init,
36789 &is_non_constant_init);
36790
36791 if (auto_node)
36792 {
36793 TREE_TYPE (decl)
36794 = do_auto_deduction (TREE_TYPE (decl), init,
36795 auto_node);
36796
36797 if (!CLASS_TYPE_P (TREE_TYPE (decl))
36798 && !type_dependent_expression_p (decl))
36799 goto non_class;
36800 }
36801
36802 cp_finish_decl (decl, init, !is_non_constant_init,
36803 asm_specification,
36804 LOOKUP_ONLYCONVERTING);
36805 orig_init = init;
36806 if (CLASS_TYPE_P (TREE_TYPE (decl)))
36807 {
36808 vec_safe_push (for_block, this_pre_body);
36809 init = NULL_TREE;
36810 }
36811 else
36812 {
36813 init = pop_stmt_list (this_pre_body);
36814 if (init && TREE_CODE (init) == STATEMENT_LIST)
36815 {
36816 tree_stmt_iterator i = tsi_start (init);
36817 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36818 while (!tsi_end_p (i))
36819 {
36820 tree t = tsi_stmt (i);
36821 if (TREE_CODE (t) == DECL_EXPR
36822 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
36823 {
36824 tsi_delink (&i);
36825 vec_safe_push (for_block, t);
36826 continue;
36827 }
36828 break;
36829 }
36830 if (tsi_one_before_end_p (i))
36831 {
36832 tree t = tsi_stmt (i);
36833 tsi_delink (&i);
36834 free_stmt_list (init);
36835 init = t;
36836 }
36837 }
36838 }
36839 this_pre_body = NULL_TREE;
36840 }
36841 else
36842 {
36843 /* Consume '='. */
36844 cp_lexer_consume_token (parser->lexer);
36845 init = cp_parser_assignment_expression (parser);
36846
36847 non_class:
36848 if (TYPE_REF_P (TREE_TYPE (decl)))
36849 init = error_mark_node;
36850 else
36851 cp_finish_decl (decl, NULL_TREE,
36852 /*init_const_expr_p=*/false,
36853 asm_specification,
36854 LOOKUP_ONLYCONVERTING);
36855 }
36856
36857 if (pushed_scope)
36858 pop_scope (pushed_scope);
36859 }
36860 }
36861 else
36862 {
36863 cp_id_kind idk;
36864 /* If parsing a type specifier sequence failed, then
36865 this MUST be a simple expression. */
36866 cp_parser_parse_tentatively (parser);
36867 decl = cp_parser_primary_expression (parser, false, false,
36868 false, &idk);
36869 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
36870 if (!cp_parser_error_occurred (parser)
36871 && decl
36872 && (TREE_CODE (decl) == COMPONENT_REF
36873 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
36874 {
36875 cp_parser_abort_tentative_parse (parser);
36876 cp_parser_parse_tentatively (parser);
36877 cp_token *token = cp_lexer_peek_token (parser->lexer);
36878 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
36879 /*check_dependency_p=*/true,
36880 /*template_p=*/NULL,
36881 /*declarator_p=*/false,
36882 /*optional_p=*/false);
36883 if (name != error_mark_node
36884 && last_tok == cp_lexer_peek_token (parser->lexer))
36885 {
36886 decl = cp_parser_lookup_name_simple (parser, name,
36887 token->location);
36888 if (TREE_CODE (decl) == FIELD_DECL)
36889 add_private_clause = omp_privatize_field (decl, false);
36890 }
36891 cp_parser_abort_tentative_parse (parser);
36892 cp_parser_parse_tentatively (parser);
36893 decl = cp_parser_primary_expression (parser, false, false,
36894 false, &idk);
36895 }
36896 if (!cp_parser_error_occurred (parser)
36897 && decl
36898 && DECL_P (decl)
36899 && CLASS_TYPE_P (TREE_TYPE (decl)))
36900 {
36901 tree rhs;
36902
36903 cp_parser_parse_definitely (parser);
36904 cp_parser_require (parser, CPP_EQ, RT_EQ);
36905 rhs = cp_parser_assignment_expression (parser);
36906 orig_init = rhs;
36907 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
36908 decl, NOP_EXPR,
36909 rhs,
36910 tf_warning_or_error));
36911 if (!add_private_clause)
36912 add_private_clause = decl;
36913 }
36914 else
36915 {
36916 decl = NULL;
36917 cp_parser_abort_tentative_parse (parser);
36918 init = cp_parser_expression (parser);
36919 if (init)
36920 {
36921 if (TREE_CODE (init) == MODIFY_EXPR
36922 || TREE_CODE (init) == MODOP_EXPR)
36923 real_decl = TREE_OPERAND (init, 0);
36924 }
36925 }
36926 }
36927 return add_private_clause;
36928 }
36929
36930 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36931
36932 void
36933 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
36934 tree &decl, tree &orig_decl, tree &init,
36935 tree &orig_init, tree &cond, tree &incr)
36936 {
36937 tree begin, end, range_temp_decl = NULL_TREE;
36938 tree iter_type, begin_expr, end_expr;
36939
36940 if (processing_template_decl)
36941 {
36942 if (check_for_bare_parameter_packs (init))
36943 init = error_mark_node;
36944 if (!type_dependent_expression_p (init)
36945 /* do_auto_deduction doesn't mess with template init-lists. */
36946 && !BRACE_ENCLOSED_INITIALIZER_P (init))
36947 {
36948 tree d = decl;
36949 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
36950 {
36951 tree v = DECL_VALUE_EXPR (decl);
36952 if (TREE_CODE (v) == ARRAY_REF
36953 && VAR_P (TREE_OPERAND (v, 0))
36954 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36955 d = TREE_OPERAND (v, 0);
36956 }
36957 do_range_for_auto_deduction (d, init);
36958 }
36959 cond = global_namespace;
36960 incr = NULL_TREE;
36961 orig_init = init;
36962 if (this_pre_body)
36963 this_pre_body = pop_stmt_list (this_pre_body);
36964 return;
36965 }
36966
36967 init = mark_lvalue_use (init);
36968
36969 if (decl == error_mark_node || init == error_mark_node)
36970 /* If an error happened previously do nothing or else a lot of
36971 unhelpful errors would be issued. */
36972 begin_expr = end_expr = iter_type = error_mark_node;
36973 else
36974 {
36975 tree range_temp;
36976
36977 if (VAR_P (init)
36978 && array_of_runtime_bound_p (TREE_TYPE (init)))
36979 /* Can't bind a reference to an array of runtime bound. */
36980 range_temp = init;
36981 else
36982 {
36983 range_temp = build_range_temp (init);
36984 DECL_NAME (range_temp) = NULL_TREE;
36985 pushdecl (range_temp);
36986 cp_finish_decl (range_temp, init,
36987 /*is_constant_init*/false, NULL_TREE,
36988 LOOKUP_ONLYCONVERTING);
36989 range_temp_decl = range_temp;
36990 range_temp = convert_from_reference (range_temp);
36991 }
36992 iter_type = cp_parser_perform_range_for_lookup (range_temp,
36993 &begin_expr, &end_expr);
36994 }
36995
36996 tree end_iter_type = iter_type;
36997 if (cxx_dialect >= cxx17)
36998 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
36999 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
37000 TREE_USED (end) = 1;
37001 DECL_ARTIFICIAL (end) = 1;
37002 pushdecl (end);
37003 cp_finish_decl (end, end_expr,
37004 /*is_constant_init*/false, NULL_TREE,
37005 LOOKUP_ONLYCONVERTING);
37006
37007 /* The new for initialization statement. */
37008 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
37009 TREE_USED (begin) = 1;
37010 DECL_ARTIFICIAL (begin) = 1;
37011 pushdecl (begin);
37012 orig_init = init;
37013 if (CLASS_TYPE_P (iter_type))
37014 init = NULL_TREE;
37015 else
37016 {
37017 init = begin_expr;
37018 begin_expr = NULL_TREE;
37019 }
37020 cp_finish_decl (begin, begin_expr,
37021 /*is_constant_init*/false, NULL_TREE,
37022 LOOKUP_ONLYCONVERTING);
37023
37024 /* The new for condition. */
37025 if (CLASS_TYPE_P (iter_type))
37026 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
37027 else
37028 cond = build_x_binary_op (input_location, NE_EXPR,
37029 begin, ERROR_MARK,
37030 end, ERROR_MARK,
37031 NULL, tf_warning_or_error);
37032
37033 /* The new increment expression. */
37034 if (CLASS_TYPE_P (iter_type))
37035 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
37036 else
37037 incr = finish_unary_op_expr (input_location,
37038 PREINCREMENT_EXPR, begin,
37039 tf_warning_or_error);
37040
37041 orig_decl = decl;
37042 decl = begin;
37043 if (for_block)
37044 {
37045 vec_safe_push (for_block, this_pre_body);
37046 this_pre_body = NULL_TREE;
37047 }
37048
37049 tree decomp_first_name = NULL_TREE;
37050 unsigned decomp_cnt = 0;
37051 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
37052 {
37053 tree v = DECL_VALUE_EXPR (orig_decl);
37054 if (TREE_CODE (v) == ARRAY_REF
37055 && VAR_P (TREE_OPERAND (v, 0))
37056 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
37057 {
37058 tree d = orig_decl;
37059 orig_decl = TREE_OPERAND (v, 0);
37060 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
37061 decomp_first_name = d;
37062 }
37063 }
37064
37065 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
37066 if (auto_node)
37067 {
37068 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
37069 tf_none);
37070 if (!error_operand_p (t))
37071 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
37072 t, auto_node);
37073 }
37074
37075 tree v = make_tree_vec (decomp_cnt + 3);
37076 TREE_VEC_ELT (v, 0) = range_temp_decl;
37077 TREE_VEC_ELT (v, 1) = end;
37078 TREE_VEC_ELT (v, 2) = orig_decl;
37079 for (unsigned i = 0; i < decomp_cnt; i++)
37080 {
37081 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
37082 decomp_first_name = DECL_CHAIN (decomp_first_name);
37083 }
37084 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
37085 }
37086
37087 /* Helper for cp_parser_omp_for_loop, finalize part of range for
37088 inside of the collapsed body. */
37089
37090 void
37091 cp_finish_omp_range_for (tree orig, tree begin)
37092 {
37093 gcc_assert (TREE_CODE (orig) == TREE_LIST
37094 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
37095 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
37096 tree decomp_first_name = NULL_TREE;
37097 unsigned int decomp_cnt = 0;
37098
37099 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
37100 {
37101 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
37102 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
37103 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
37104 }
37105
37106 /* The declaration is initialized with *__begin inside the loop body. */
37107 cp_finish_decl (decl,
37108 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
37109 tf_warning_or_error),
37110 /*is_constant_init*/false, NULL_TREE,
37111 LOOKUP_ONLYCONVERTING);
37112 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
37113 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
37114 }
37115
37116 /* OpenMP 5.0:
37117
37118 scan-loop-body:
37119 { structured-block scan-directive structured-block } */
37120
37121 static void
37122 cp_parser_omp_scan_loop_body (cp_parser *parser)
37123 {
37124 tree substmt, clauses = NULL_TREE;
37125
37126 matching_braces braces;
37127 if (!braces.require_open (parser))
37128 return;
37129
37130 substmt = cp_parser_omp_structured_block (parser, NULL);
37131 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
37132 add_stmt (substmt);
37133
37134 cp_token *tok = cp_lexer_peek_token (parser->lexer);
37135 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
37136 {
37137 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
37138
37139 cp_lexer_consume_token (parser->lexer);
37140
37141 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37142 {
37143 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37144 const char *p = IDENTIFIER_POINTER (id);
37145 if (strcmp (p, "inclusive") == 0)
37146 clause = OMP_CLAUSE_INCLUSIVE;
37147 else if (strcmp (p, "exclusive") == 0)
37148 clause = OMP_CLAUSE_EXCLUSIVE;
37149 }
37150 if (clause != OMP_CLAUSE_ERROR)
37151 {
37152 cp_lexer_consume_token (parser->lexer);
37153 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
37154 }
37155 else
37156 cp_parser_error (parser, "expected %<inclusive%> or "
37157 "%<exclusive%> clause");
37158
37159 cp_parser_require_pragma_eol (parser, tok);
37160 }
37161 else
37162 error ("expected %<#pragma omp scan%>");
37163
37164 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37165 substmt = cp_parser_omp_structured_block (parser, NULL);
37166 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
37167 clauses);
37168 add_stmt (substmt);
37169
37170 braces.require_close (parser);
37171 }
37172
37173 /* Parse the restricted form of the for statement allowed by OpenMP. */
37174
37175 static tree
37176 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
37177 tree *cclauses, bool *if_p)
37178 {
37179 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
37180 tree orig_decl;
37181 tree real_decl, initv, condv, incrv, declv, orig_declv;
37182 tree this_pre_body, cl, ordered_cl = NULL_TREE;
37183 location_t loc_first;
37184 bool collapse_err = false;
37185 int i, collapse = 1, ordered = 0, count, nbraces = 0;
37186 releasing_vec for_block;
37187 auto_vec<tree, 4> orig_inits;
37188 bool tiling = false;
37189 bool inscan = false;
37190
37191 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
37192 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
37193 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
37194 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
37195 {
37196 tiling = true;
37197 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
37198 }
37199 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
37200 && OMP_CLAUSE_ORDERED_EXPR (cl))
37201 {
37202 ordered_cl = cl;
37203 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
37204 }
37205 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
37206 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
37207 && (code == OMP_SIMD || code == OMP_FOR))
37208 inscan = true;
37209
37210 if (ordered && ordered < collapse)
37211 {
37212 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
37213 "%<ordered%> clause parameter is less than %<collapse%>");
37214 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
37215 = build_int_cst (NULL_TREE, collapse);
37216 ordered = collapse;
37217 }
37218 if (ordered)
37219 {
37220 for (tree *pc = &clauses; *pc; )
37221 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
37222 {
37223 error_at (OMP_CLAUSE_LOCATION (*pc),
37224 "%<linear%> clause may not be specified together "
37225 "with %<ordered%> clause with a parameter");
37226 *pc = OMP_CLAUSE_CHAIN (*pc);
37227 }
37228 else
37229 pc = &OMP_CLAUSE_CHAIN (*pc);
37230 }
37231
37232 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
37233 count = ordered ? ordered : collapse;
37234
37235 declv = make_tree_vec (count);
37236 initv = make_tree_vec (count);
37237 condv = make_tree_vec (count);
37238 incrv = make_tree_vec (count);
37239 orig_declv = NULL_TREE;
37240
37241 loc_first = cp_lexer_peek_token (parser->lexer)->location;
37242
37243 for (i = 0; i < count; i++)
37244 {
37245 int bracecount = 0;
37246 tree add_private_clause = NULL_TREE;
37247 location_t loc;
37248
37249 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37250 {
37251 if (!collapse_err)
37252 cp_parser_error (parser, "for statement expected");
37253 return NULL;
37254 }
37255 loc = cp_lexer_consume_token (parser->lexer)->location;
37256
37257 /* Don't create location wrapper nodes within an OpenMP "for"
37258 statement. */
37259 auto_suppress_location_wrappers sentinel;
37260
37261 matching_parens parens;
37262 if (!parens.require_open (parser))
37263 return NULL;
37264
37265 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
37266 this_pre_body = push_stmt_list ();
37267
37268 if (code != OACC_LOOP && cxx_dialect >= cxx11)
37269 {
37270 /* Save tokens so that we can put them back. */
37271 cp_lexer_save_tokens (parser->lexer);
37272
37273 /* Look for ':' that is not nested in () or {}. */
37274 bool is_range_for
37275 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
37276 /*recovering=*/false,
37277 CPP_COLON,
37278 /*consume_paren=*/
37279 false) == -1);
37280
37281 /* Roll back the tokens we skipped. */
37282 cp_lexer_rollback_tokens (parser->lexer);
37283
37284 if (is_range_for)
37285 {
37286 bool saved_colon_corrects_to_scope_p
37287 = parser->colon_corrects_to_scope_p;
37288
37289 /* A colon is used in range-based for. */
37290 parser->colon_corrects_to_scope_p = false;
37291
37292 /* Parse the declaration. */
37293 cp_parser_simple_declaration (parser,
37294 /*function_definition_allowed_p=*/
37295 false, &decl);
37296 parser->colon_corrects_to_scope_p
37297 = saved_colon_corrects_to_scope_p;
37298
37299 cp_parser_require (parser, CPP_COLON, RT_COLON);
37300
37301 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
37302 false, 0, true);
37303
37304 cp_convert_omp_range_for (this_pre_body, for_block, decl,
37305 orig_decl, init, orig_init,
37306 cond, incr);
37307 if (this_pre_body)
37308 {
37309 if (pre_body)
37310 {
37311 tree t = pre_body;
37312 pre_body = push_stmt_list ();
37313 add_stmt (t);
37314 add_stmt (this_pre_body);
37315 pre_body = pop_stmt_list (pre_body);
37316 }
37317 else
37318 pre_body = this_pre_body;
37319 }
37320
37321 if (ordered_cl)
37322 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
37323 "%<ordered%> clause with parameter on "
37324 "range-based %<for%> loop");
37325
37326 goto parse_close_paren;
37327 }
37328 }
37329
37330 add_private_clause
37331 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
37332 init, orig_init, decl, real_decl);
37333
37334 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37335 if (this_pre_body)
37336 {
37337 this_pre_body = pop_stmt_list (this_pre_body);
37338 if (pre_body)
37339 {
37340 tree t = pre_body;
37341 pre_body = push_stmt_list ();
37342 add_stmt (t);
37343 add_stmt (this_pre_body);
37344 pre_body = pop_stmt_list (pre_body);
37345 }
37346 else
37347 pre_body = this_pre_body;
37348 }
37349
37350 if (decl)
37351 real_decl = decl;
37352 if (cclauses != NULL
37353 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
37354 && real_decl != NULL_TREE)
37355 {
37356 tree *c;
37357 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
37358 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
37359 && OMP_CLAUSE_DECL (*c) == real_decl)
37360 {
37361 error_at (loc, "iteration variable %qD"
37362 " should not be firstprivate", real_decl);
37363 *c = OMP_CLAUSE_CHAIN (*c);
37364 }
37365 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
37366 && OMP_CLAUSE_DECL (*c) == real_decl)
37367 {
37368 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
37369 tree l = *c;
37370 *c = OMP_CLAUSE_CHAIN (*c);
37371 if (code == OMP_SIMD)
37372 {
37373 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37374 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
37375 }
37376 else
37377 {
37378 OMP_CLAUSE_CHAIN (l) = clauses;
37379 clauses = l;
37380 }
37381 add_private_clause = NULL_TREE;
37382 }
37383 else
37384 {
37385 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
37386 && OMP_CLAUSE_DECL (*c) == real_decl)
37387 add_private_clause = NULL_TREE;
37388 c = &OMP_CLAUSE_CHAIN (*c);
37389 }
37390 }
37391
37392 if (add_private_clause)
37393 {
37394 tree c;
37395 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
37396 {
37397 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
37398 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
37399 && OMP_CLAUSE_DECL (c) == decl)
37400 break;
37401 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
37402 && OMP_CLAUSE_DECL (c) == decl)
37403 error_at (loc, "iteration variable %qD "
37404 "should not be firstprivate",
37405 decl);
37406 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
37407 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
37408 && OMP_CLAUSE_DECL (c) == decl)
37409 error_at (loc, "iteration variable %qD should not be reduction",
37410 decl);
37411 }
37412 if (c == NULL)
37413 {
37414 if (code != OMP_SIMD)
37415 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
37416 else if (collapse == 1)
37417 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37418 else
37419 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
37420 OMP_CLAUSE_DECL (c) = add_private_clause;
37421 c = finish_omp_clauses (c, C_ORT_OMP);
37422 if (c)
37423 {
37424 OMP_CLAUSE_CHAIN (c) = clauses;
37425 clauses = c;
37426 /* For linear, signal that we need to fill up
37427 the so far unknown linear step. */
37428 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
37429 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
37430 }
37431 }
37432 }
37433
37434 cond = NULL;
37435 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37436 cond = cp_parser_omp_for_cond (parser, decl, code);
37437 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37438
37439 incr = NULL;
37440 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
37441 {
37442 /* If decl is an iterator, preserve the operator on decl
37443 until finish_omp_for. */
37444 if (real_decl
37445 && ((processing_template_decl
37446 && (TREE_TYPE (real_decl) == NULL_TREE
37447 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
37448 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
37449 incr = cp_parser_omp_for_incr (parser, real_decl);
37450 else
37451 incr = cp_parser_expression (parser);
37452 if (!EXPR_HAS_LOCATION (incr))
37453 protected_set_expr_location (incr, input_location);
37454 }
37455
37456 parse_close_paren:
37457 if (!parens.require_close (parser))
37458 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37459 /*or_comma=*/false,
37460 /*consume_paren=*/true);
37461
37462 TREE_VEC_ELT (declv, i) = decl;
37463 TREE_VEC_ELT (initv, i) = init;
37464 TREE_VEC_ELT (condv, i) = cond;
37465 TREE_VEC_ELT (incrv, i) = incr;
37466 if (orig_init)
37467 {
37468 orig_inits.safe_grow_cleared (i + 1);
37469 orig_inits[i] = orig_init;
37470 }
37471 if (orig_decl)
37472 {
37473 if (!orig_declv)
37474 orig_declv = copy_node (declv);
37475 TREE_VEC_ELT (orig_declv, i) = orig_decl;
37476 }
37477 else if (orig_declv)
37478 TREE_VEC_ELT (orig_declv, i) = decl;
37479
37480 if (i == count - 1)
37481 break;
37482
37483 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
37484 in between the collapsed for loops to be still considered perfectly
37485 nested. Hopefully the final version clarifies this.
37486 For now handle (multiple) {'s and empty statements. */
37487 cp_parser_parse_tentatively (parser);
37488 for (;;)
37489 {
37490 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37491 break;
37492 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
37493 {
37494 cp_lexer_consume_token (parser->lexer);
37495 bracecount++;
37496 }
37497 else if (bracecount
37498 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37499 cp_lexer_consume_token (parser->lexer);
37500 else
37501 {
37502 loc = cp_lexer_peek_token (parser->lexer)->location;
37503 error_at (loc, "not enough for loops to collapse");
37504 collapse_err = true;
37505 cp_parser_abort_tentative_parse (parser);
37506 declv = NULL_TREE;
37507 break;
37508 }
37509 }
37510
37511 if (declv)
37512 {
37513 cp_parser_parse_definitely (parser);
37514 nbraces += bracecount;
37515 }
37516 }
37517
37518 if (nbraces)
37519 if_p = NULL;
37520
37521 /* Note that we saved the original contents of this flag when we entered
37522 the structured block, and so we don't need to re-save it here. */
37523 parser->in_statement = IN_OMP_FOR;
37524
37525 /* Note that the grammar doesn't call for a structured block here,
37526 though the loop as a whole is a structured block. */
37527 if (orig_declv)
37528 {
37529 body = begin_omp_structured_block ();
37530 for (i = 0; i < count; i++)
37531 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
37532 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
37533 TREE_VEC_ELT (declv, i));
37534 }
37535 else
37536 body = push_stmt_list ();
37537 if (inscan)
37538 cp_parser_omp_scan_loop_body (parser);
37539 else
37540 cp_parser_statement (parser, NULL_TREE, false, if_p);
37541 if (orig_declv)
37542 body = finish_omp_structured_block (body);
37543 else
37544 body = pop_stmt_list (body);
37545
37546 if (declv == NULL_TREE)
37547 ret = NULL_TREE;
37548 else
37549 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
37550 incrv, body, pre_body, &orig_inits, clauses);
37551
37552 while (nbraces)
37553 {
37554 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
37555 {
37556 cp_lexer_consume_token (parser->lexer);
37557 nbraces--;
37558 }
37559 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37560 cp_lexer_consume_token (parser->lexer);
37561 else
37562 {
37563 if (!collapse_err)
37564 {
37565 error_at (cp_lexer_peek_token (parser->lexer)->location,
37566 "collapsed loops not perfectly nested");
37567 }
37568 collapse_err = true;
37569 cp_parser_statement_seq_opt (parser, NULL);
37570 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
37571 break;
37572 }
37573 }
37574
37575 while (!for_block->is_empty ())
37576 {
37577 tree t = for_block->pop ();
37578 if (TREE_CODE (t) == STATEMENT_LIST)
37579 add_stmt (pop_stmt_list (t));
37580 else
37581 add_stmt (t);
37582 }
37583
37584 return ret;
37585 }
37586
37587 /* Helper function for OpenMP parsing, split clauses and call
37588 finish_omp_clauses on each of the set of clauses afterwards. */
37589
37590 static void
37591 cp_omp_split_clauses (location_t loc, enum tree_code code,
37592 omp_clause_mask mask, tree clauses, tree *cclauses)
37593 {
37594 int i;
37595 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
37596 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
37597 if (cclauses[i])
37598 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
37599 }
37600
37601 /* OpenMP 4.0:
37602 #pragma omp simd simd-clause[optseq] new-line
37603 for-loop */
37604
37605 #define OMP_SIMD_CLAUSE_MASK \
37606 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
37607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
37616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
37617
37618 static tree
37619 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
37620 char *p_name, omp_clause_mask mask, tree *cclauses,
37621 bool *if_p)
37622 {
37623 tree clauses, sb, ret;
37624 unsigned int save;
37625 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37626
37627 strcat (p_name, " simd");
37628 mask |= OMP_SIMD_CLAUSE_MASK;
37629
37630 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37631 cclauses == NULL);
37632 if (cclauses)
37633 {
37634 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
37635 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
37636 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
37637 OMP_CLAUSE_ORDERED);
37638 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
37639 {
37640 error_at (OMP_CLAUSE_LOCATION (c),
37641 "%<ordered%> clause with parameter may not be specified "
37642 "on %qs construct", p_name);
37643 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
37644 }
37645 }
37646
37647 keep_next_level (true);
37648 sb = begin_omp_structured_block ();
37649 save = cp_parser_begin_omp_structured_block (parser);
37650
37651 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
37652
37653 cp_parser_end_omp_structured_block (parser, save);
37654 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37655
37656 return ret;
37657 }
37658
37659 /* OpenMP 2.5:
37660 #pragma omp for for-clause[optseq] new-line
37661 for-loop
37662
37663 OpenMP 4.0:
37664 #pragma omp for simd for-simd-clause[optseq] new-line
37665 for-loop */
37666
37667 #define OMP_FOR_CLAUSE_MASK \
37668 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
37674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
37675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
37678
37679 static tree
37680 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
37681 char *p_name, omp_clause_mask mask, tree *cclauses,
37682 bool *if_p)
37683 {
37684 tree clauses, sb, ret;
37685 unsigned int save;
37686 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37687
37688 strcat (p_name, " for");
37689 mask |= OMP_FOR_CLAUSE_MASK;
37690 /* parallel for{, simd} disallows nowait clause, but for
37691 target {teams distribute ,}parallel for{, simd} it should be accepted. */
37692 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
37693 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37694 /* Composite distribute parallel for{, simd} disallows ordered clause. */
37695 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37696 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
37697
37698 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37699 {
37700 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37701 const char *p = IDENTIFIER_POINTER (id);
37702
37703 if (strcmp (p, "simd") == 0)
37704 {
37705 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37706 if (cclauses == NULL)
37707 cclauses = cclauses_buf;
37708
37709 cp_lexer_consume_token (parser->lexer);
37710 if (!flag_openmp) /* flag_openmp_simd */
37711 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37712 cclauses, if_p);
37713 sb = begin_omp_structured_block ();
37714 save = cp_parser_begin_omp_structured_block (parser);
37715 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37716 cclauses, if_p);
37717 cp_parser_end_omp_structured_block (parser, save);
37718 tree body = finish_omp_structured_block (sb);
37719 if (ret == NULL)
37720 return ret;
37721 ret = make_node (OMP_FOR);
37722 TREE_TYPE (ret) = void_type_node;
37723 OMP_FOR_BODY (ret) = body;
37724 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37725 SET_EXPR_LOCATION (ret, loc);
37726 add_stmt (ret);
37727 return ret;
37728 }
37729 }
37730 if (!flag_openmp) /* flag_openmp_simd */
37731 {
37732 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37733 return NULL_TREE;
37734 }
37735
37736 /* Composite distribute parallel for disallows linear clause. */
37737 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37738 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
37739
37740 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37741 cclauses == NULL);
37742 if (cclauses)
37743 {
37744 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
37745 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37746 }
37747
37748 keep_next_level (true);
37749 sb = begin_omp_structured_block ();
37750 save = cp_parser_begin_omp_structured_block (parser);
37751
37752 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
37753
37754 cp_parser_end_omp_structured_block (parser, save);
37755 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37756
37757 return ret;
37758 }
37759
37760 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
37761 omp_clause_mask, tree *, bool *);
37762
37763 /* OpenMP 2.5:
37764 # pragma omp master new-line
37765 structured-block */
37766
37767 static tree
37768 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
37769 char *p_name, omp_clause_mask mask, tree *cclauses,
37770 bool *if_p)
37771 {
37772 tree clauses, sb, ret;
37773 unsigned int save;
37774 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37775
37776 strcat (p_name, " master");
37777
37778 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37779 {
37780 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37781 const char *p = IDENTIFIER_POINTER (id);
37782
37783 if (strcmp (p, "taskloop") == 0)
37784 {
37785 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37786 if (cclauses == NULL)
37787 cclauses = cclauses_buf;
37788
37789 cp_lexer_consume_token (parser->lexer);
37790 if (!flag_openmp) /* flag_openmp_simd */
37791 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37792 cclauses, if_p);
37793 sb = begin_omp_structured_block ();
37794 save = cp_parser_begin_omp_structured_block (parser);
37795 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37796 cclauses, if_p);
37797 cp_parser_end_omp_structured_block (parser, save);
37798 tree body = finish_omp_structured_block (sb);
37799 if (ret == NULL)
37800 return ret;
37801 return c_finish_omp_master (loc, body);
37802 }
37803 }
37804 if (!flag_openmp) /* flag_openmp_simd */
37805 {
37806 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37807 return NULL_TREE;
37808 }
37809
37810 if (cclauses)
37811 {
37812 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37813 false);
37814 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
37815 }
37816 else
37817 cp_parser_require_pragma_eol (parser, pragma_tok);
37818
37819 return c_finish_omp_master (loc,
37820 cp_parser_omp_structured_block (parser, if_p));
37821 }
37822
37823 /* OpenMP 2.5:
37824 # pragma omp ordered new-line
37825 structured-block
37826
37827 OpenMP 4.5:
37828 # pragma omp ordered ordered-clauses new-line
37829 structured-block */
37830
37831 #define OMP_ORDERED_CLAUSE_MASK \
37832 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
37833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37834
37835 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
37836 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37837
37838 static bool
37839 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
37840 enum pragma_context context, bool *if_p)
37841 {
37842 location_t loc = pragma_tok->location;
37843
37844 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37845 {
37846 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37847 const char *p = IDENTIFIER_POINTER (id);
37848
37849 if (strcmp (p, "depend") == 0)
37850 {
37851 if (!flag_openmp) /* flag_openmp_simd */
37852 {
37853 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37854 return false;
37855 }
37856 if (context == pragma_stmt)
37857 {
37858 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
37859 "%<depend%> clause may only be used in compound "
37860 "statements");
37861 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37862 return false;
37863 }
37864 tree clauses
37865 = cp_parser_omp_all_clauses (parser,
37866 OMP_ORDERED_DEPEND_CLAUSE_MASK,
37867 "#pragma omp ordered", pragma_tok);
37868 c_finish_omp_ordered (loc, clauses, NULL_TREE);
37869 return false;
37870 }
37871 }
37872
37873 tree clauses
37874 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
37875 "#pragma omp ordered", pragma_tok);
37876
37877 if (!flag_openmp /* flag_openmp_simd */
37878 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
37879 return false;
37880
37881 c_finish_omp_ordered (loc, clauses,
37882 cp_parser_omp_structured_block (parser, if_p));
37883 return true;
37884 }
37885
37886 /* OpenMP 2.5:
37887
37888 section-scope:
37889 { section-sequence }
37890
37891 section-sequence:
37892 section-directive[opt] structured-block
37893 section-sequence section-directive structured-block */
37894
37895 static tree
37896 cp_parser_omp_sections_scope (cp_parser *parser)
37897 {
37898 tree stmt, substmt;
37899 bool error_suppress = false;
37900 cp_token *tok;
37901
37902 matching_braces braces;
37903 if (!braces.require_open (parser))
37904 return NULL_TREE;
37905
37906 stmt = push_stmt_list ();
37907
37908 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
37909 != PRAGMA_OMP_SECTION)
37910 {
37911 substmt = cp_parser_omp_structured_block (parser, NULL);
37912 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37913 add_stmt (substmt);
37914 }
37915
37916 while (1)
37917 {
37918 tok = cp_lexer_peek_token (parser->lexer);
37919 if (tok->type == CPP_CLOSE_BRACE)
37920 break;
37921 if (tok->type == CPP_EOF)
37922 break;
37923
37924 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
37925 {
37926 cp_lexer_consume_token (parser->lexer);
37927 cp_parser_require_pragma_eol (parser, tok);
37928 error_suppress = false;
37929 }
37930 else if (!error_suppress)
37931 {
37932 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
37933 error_suppress = true;
37934 }
37935
37936 substmt = cp_parser_omp_structured_block (parser, NULL);
37937 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37938 add_stmt (substmt);
37939 }
37940 braces.require_close (parser);
37941
37942 substmt = pop_stmt_list (stmt);
37943
37944 stmt = make_node (OMP_SECTIONS);
37945 TREE_TYPE (stmt) = void_type_node;
37946 OMP_SECTIONS_BODY (stmt) = substmt;
37947
37948 add_stmt (stmt);
37949 return stmt;
37950 }
37951
37952 /* OpenMP 2.5:
37953 # pragma omp sections sections-clause[optseq] newline
37954 sections-scope */
37955
37956 #define OMP_SECTIONS_CLAUSE_MASK \
37957 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37962
37963 static tree
37964 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
37965 char *p_name, omp_clause_mask mask, tree *cclauses)
37966 {
37967 tree clauses, ret;
37968 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37969
37970 strcat (p_name, " sections");
37971 mask |= OMP_SECTIONS_CLAUSE_MASK;
37972 if (cclauses)
37973 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37974
37975 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37976 cclauses == NULL);
37977 if (cclauses)
37978 {
37979 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
37980 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
37981 }
37982
37983 ret = cp_parser_omp_sections_scope (parser);
37984 if (ret)
37985 OMP_SECTIONS_CLAUSES (ret) = clauses;
37986
37987 return ret;
37988 }
37989
37990 /* OpenMP 2.5:
37991 # pragma omp parallel parallel-clause[optseq] new-line
37992 structured-block
37993 # pragma omp parallel for parallel-for-clause[optseq] new-line
37994 structured-block
37995 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37996 structured-block
37997
37998 OpenMP 4.0:
37999 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
38000 structured-block */
38001
38002 #define OMP_PARALLEL_CLAUSE_MASK \
38003 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
38007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
38008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
38009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
38011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
38012
38013 static tree
38014 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
38015 char *p_name, omp_clause_mask mask, tree *cclauses,
38016 bool *if_p)
38017 {
38018 tree stmt, clauses, block;
38019 unsigned int save;
38020 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38021
38022 strcat (p_name, " parallel");
38023 mask |= OMP_PARALLEL_CLAUSE_MASK;
38024 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
38025 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
38026 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
38027 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
38028
38029 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
38030 {
38031 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38032 if (cclauses == NULL)
38033 cclauses = cclauses_buf;
38034
38035 cp_lexer_consume_token (parser->lexer);
38036 if (!flag_openmp) /* flag_openmp_simd */
38037 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
38038 if_p);
38039 block = begin_omp_parallel ();
38040 save = cp_parser_begin_omp_structured_block (parser);
38041 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
38042 if_p);
38043 cp_parser_end_omp_structured_block (parser, save);
38044 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
38045 block);
38046 if (ret == NULL_TREE)
38047 return ret;
38048 OMP_PARALLEL_COMBINED (stmt) = 1;
38049 return stmt;
38050 }
38051 /* When combined with distribute, parallel has to be followed by for.
38052 #pragma omp target parallel is allowed though. */
38053 else if (cclauses
38054 && (mask & (OMP_CLAUSE_MASK_1
38055 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
38056 {
38057 error_at (loc, "expected %<for%> after %qs", p_name);
38058 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38059 return NULL_TREE;
38060 }
38061 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38062 {
38063 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38064 const char *p = IDENTIFIER_POINTER (id);
38065 if (strcmp (p, "master") == 0)
38066 {
38067 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38068 cclauses = cclauses_buf;
38069
38070 cp_lexer_consume_token (parser->lexer);
38071 block = begin_omp_parallel ();
38072 save = cp_parser_begin_omp_structured_block (parser);
38073 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
38074 cclauses, if_p);
38075 cp_parser_end_omp_structured_block (parser, save);
38076 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
38077 block);
38078 OMP_PARALLEL_COMBINED (stmt) = 1;
38079 if (ret == NULL_TREE)
38080 return ret;
38081 return stmt;
38082 }
38083 else if (!flag_openmp) /* flag_openmp_simd */
38084 {
38085 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38086 return NULL_TREE;
38087 }
38088 else if (strcmp (p, "sections") == 0)
38089 {
38090 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38091 cclauses = cclauses_buf;
38092
38093 cp_lexer_consume_token (parser->lexer);
38094 block = begin_omp_parallel ();
38095 save = cp_parser_begin_omp_structured_block (parser);
38096 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
38097 cp_parser_end_omp_structured_block (parser, save);
38098 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
38099 block);
38100 OMP_PARALLEL_COMBINED (stmt) = 1;
38101 return stmt;
38102 }
38103 }
38104 else if (!flag_openmp) /* flag_openmp_simd */
38105 {
38106 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38107 return NULL_TREE;
38108 }
38109
38110 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38111 cclauses == NULL);
38112 if (cclauses)
38113 {
38114 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
38115 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
38116 }
38117
38118 block = begin_omp_parallel ();
38119 save = cp_parser_begin_omp_structured_block (parser);
38120 cp_parser_statement (parser, NULL_TREE, false, if_p);
38121 cp_parser_end_omp_structured_block (parser, save);
38122 stmt = finish_omp_parallel (clauses, block);
38123 return stmt;
38124 }
38125
38126 /* OpenMP 2.5:
38127 # pragma omp single single-clause[optseq] new-line
38128 structured-block */
38129
38130 #define OMP_SINGLE_CLAUSE_MASK \
38131 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
38134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38135
38136 static tree
38137 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38138 {
38139 tree stmt = make_node (OMP_SINGLE);
38140 TREE_TYPE (stmt) = void_type_node;
38141 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38142
38143 OMP_SINGLE_CLAUSES (stmt)
38144 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
38145 "#pragma omp single", pragma_tok);
38146 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38147
38148 return add_stmt (stmt);
38149 }
38150
38151 /* OpenMP 3.0:
38152 # pragma omp task task-clause[optseq] new-line
38153 structured-block */
38154
38155 #define OMP_TASK_CLAUSE_MASK \
38156 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
38158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
38159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
38162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
38163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
38164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
38166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
38167
38168 static tree
38169 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38170 {
38171 tree clauses, block;
38172 unsigned int save;
38173
38174 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
38175 "#pragma omp task", pragma_tok);
38176 block = begin_omp_task ();
38177 save = cp_parser_begin_omp_structured_block (parser);
38178 cp_parser_statement (parser, NULL_TREE, false, if_p);
38179 cp_parser_end_omp_structured_block (parser, save);
38180 return finish_omp_task (clauses, block);
38181 }
38182
38183 /* OpenMP 3.0:
38184 # pragma omp taskwait new-line
38185
38186 OpenMP 5.0:
38187 # pragma omp taskwait taskwait-clause[opt] new-line */
38188
38189 #define OMP_TASKWAIT_CLAUSE_MASK \
38190 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
38191
38192 static void
38193 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
38194 {
38195 tree clauses
38196 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
38197 "#pragma omp taskwait", pragma_tok);
38198
38199 if (clauses)
38200 {
38201 tree stmt = make_node (OMP_TASK);
38202 TREE_TYPE (stmt) = void_node;
38203 OMP_TASK_CLAUSES (stmt) = clauses;
38204 OMP_TASK_BODY (stmt) = NULL_TREE;
38205 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38206 add_stmt (stmt);
38207 }
38208 else
38209 finish_omp_taskwait ();
38210 }
38211
38212 /* OpenMP 3.1:
38213 # pragma omp taskyield new-line */
38214
38215 static void
38216 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
38217 {
38218 cp_parser_require_pragma_eol (parser, pragma_tok);
38219 finish_omp_taskyield ();
38220 }
38221
38222 /* OpenMP 4.0:
38223 # pragma omp taskgroup new-line
38224 structured-block
38225
38226 OpenMP 5.0:
38227 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
38228
38229 #define OMP_TASKGROUP_CLAUSE_MASK \
38230 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
38231
38232 static tree
38233 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38234 {
38235 tree clauses
38236 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
38237 "#pragma omp taskgroup", pragma_tok);
38238 return c_finish_omp_taskgroup (input_location,
38239 cp_parser_omp_structured_block (parser,
38240 if_p),
38241 clauses);
38242 }
38243
38244
38245 /* OpenMP 2.5:
38246 # pragma omp threadprivate (variable-list) */
38247
38248 static void
38249 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
38250 {
38251 tree vars;
38252
38253 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
38254 cp_parser_require_pragma_eol (parser, pragma_tok);
38255
38256 finish_omp_threadprivate (vars);
38257 }
38258
38259 /* OpenMP 4.0:
38260 # pragma omp cancel cancel-clause[optseq] new-line */
38261
38262 #define OMP_CANCEL_CLAUSE_MASK \
38263 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
38264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
38265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
38266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
38267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
38268
38269 static void
38270 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
38271 {
38272 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
38273 "#pragma omp cancel", pragma_tok);
38274 finish_omp_cancel (clauses);
38275 }
38276
38277 /* OpenMP 4.0:
38278 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
38279
38280 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
38281 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
38282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
38283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
38284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
38285
38286 static void
38287 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
38288 enum pragma_context context)
38289 {
38290 tree clauses;
38291 bool point_seen = false;
38292
38293 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38294 {
38295 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38296 const char *p = IDENTIFIER_POINTER (id);
38297
38298 if (strcmp (p, "point") == 0)
38299 {
38300 cp_lexer_consume_token (parser->lexer);
38301 point_seen = true;
38302 }
38303 }
38304 if (!point_seen)
38305 {
38306 cp_parser_error (parser, "expected %<point%>");
38307 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38308 return;
38309 }
38310
38311 if (context != pragma_compound)
38312 {
38313 if (context == pragma_stmt)
38314 error_at (pragma_tok->location,
38315 "%<#pragma %s%> may only be used in compound statements",
38316 "omp cancellation point");
38317 else
38318 cp_parser_error (parser, "expected declaration specifiers");
38319 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38320 return;
38321 }
38322
38323 clauses = cp_parser_omp_all_clauses (parser,
38324 OMP_CANCELLATION_POINT_CLAUSE_MASK,
38325 "#pragma omp cancellation point",
38326 pragma_tok);
38327 finish_omp_cancellation_point (clauses);
38328 }
38329
38330 /* OpenMP 4.0:
38331 #pragma omp distribute distribute-clause[optseq] new-line
38332 for-loop */
38333
38334 #define OMP_DISTRIBUTE_CLAUSE_MASK \
38335 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
38338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
38339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
38340
38341 static tree
38342 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
38343 char *p_name, omp_clause_mask mask, tree *cclauses,
38344 bool *if_p)
38345 {
38346 tree clauses, sb, ret;
38347 unsigned int save;
38348 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38349
38350 strcat (p_name, " distribute");
38351 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
38352
38353 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38354 {
38355 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38356 const char *p = IDENTIFIER_POINTER (id);
38357 bool simd = false;
38358 bool parallel = false;
38359
38360 if (strcmp (p, "simd") == 0)
38361 simd = true;
38362 else
38363 parallel = strcmp (p, "parallel") == 0;
38364 if (parallel || simd)
38365 {
38366 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38367 if (cclauses == NULL)
38368 cclauses = cclauses_buf;
38369 cp_lexer_consume_token (parser->lexer);
38370 if (!flag_openmp) /* flag_openmp_simd */
38371 {
38372 if (simd)
38373 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38374 cclauses, if_p);
38375 else
38376 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
38377 cclauses, if_p);
38378 }
38379 sb = begin_omp_structured_block ();
38380 save = cp_parser_begin_omp_structured_block (parser);
38381 if (simd)
38382 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38383 cclauses, if_p);
38384 else
38385 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
38386 cclauses, if_p);
38387 cp_parser_end_omp_structured_block (parser, save);
38388 tree body = finish_omp_structured_block (sb);
38389 if (ret == NULL)
38390 return ret;
38391 ret = make_node (OMP_DISTRIBUTE);
38392 TREE_TYPE (ret) = void_type_node;
38393 OMP_FOR_BODY (ret) = body;
38394 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
38395 SET_EXPR_LOCATION (ret, loc);
38396 add_stmt (ret);
38397 return ret;
38398 }
38399 }
38400 if (!flag_openmp) /* flag_openmp_simd */
38401 {
38402 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38403 return NULL_TREE;
38404 }
38405
38406 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38407 cclauses == NULL);
38408 if (cclauses)
38409 {
38410 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
38411 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
38412 }
38413
38414 keep_next_level (true);
38415 sb = begin_omp_structured_block ();
38416 save = cp_parser_begin_omp_structured_block (parser);
38417
38418 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
38419
38420 cp_parser_end_omp_structured_block (parser, save);
38421 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
38422
38423 return ret;
38424 }
38425
38426 /* OpenMP 4.0:
38427 # pragma omp teams teams-clause[optseq] new-line
38428 structured-block */
38429
38430 #define OMP_TEAMS_CLAUSE_MASK \
38431 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
38434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
38436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
38437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
38438
38439 static tree
38440 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
38441 char *p_name, omp_clause_mask mask, tree *cclauses,
38442 bool *if_p)
38443 {
38444 tree clauses, sb, ret;
38445 unsigned int save;
38446 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38447
38448 strcat (p_name, " teams");
38449 mask |= OMP_TEAMS_CLAUSE_MASK;
38450
38451 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38452 {
38453 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38454 const char *p = IDENTIFIER_POINTER (id);
38455 if (strcmp (p, "distribute") == 0)
38456 {
38457 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38458 if (cclauses == NULL)
38459 cclauses = cclauses_buf;
38460
38461 cp_lexer_consume_token (parser->lexer);
38462 if (!flag_openmp) /* flag_openmp_simd */
38463 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
38464 cclauses, if_p);
38465 keep_next_level (true);
38466 sb = begin_omp_structured_block ();
38467 save = cp_parser_begin_omp_structured_block (parser);
38468 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
38469 cclauses, if_p);
38470 cp_parser_end_omp_structured_block (parser, save);
38471 tree body = finish_omp_structured_block (sb);
38472 if (ret == NULL)
38473 return ret;
38474 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38475 ret = make_node (OMP_TEAMS);
38476 TREE_TYPE (ret) = void_type_node;
38477 OMP_TEAMS_CLAUSES (ret) = clauses;
38478 OMP_TEAMS_BODY (ret) = body;
38479 OMP_TEAMS_COMBINED (ret) = 1;
38480 SET_EXPR_LOCATION (ret, loc);
38481 return add_stmt (ret);
38482 }
38483 }
38484 if (!flag_openmp) /* flag_openmp_simd */
38485 {
38486 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38487 return NULL_TREE;
38488 }
38489
38490 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38491 cclauses == NULL);
38492 if (cclauses)
38493 {
38494 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
38495 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38496 }
38497
38498 tree stmt = make_node (OMP_TEAMS);
38499 TREE_TYPE (stmt) = void_type_node;
38500 OMP_TEAMS_CLAUSES (stmt) = clauses;
38501 keep_next_level (true);
38502 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38503 SET_EXPR_LOCATION (stmt, loc);
38504
38505 return add_stmt (stmt);
38506 }
38507
38508 /* OpenMP 4.0:
38509 # pragma omp target data target-data-clause[optseq] new-line
38510 structured-block */
38511
38512 #define OMP_TARGET_DATA_CLAUSE_MASK \
38513 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
38517
38518 static tree
38519 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38520 {
38521 tree clauses
38522 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
38523 "#pragma omp target data", pragma_tok);
38524 int map_seen = 0;
38525 for (tree *pc = &clauses; *pc;)
38526 {
38527 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38528 switch (OMP_CLAUSE_MAP_KIND (*pc))
38529 {
38530 case GOMP_MAP_TO:
38531 case GOMP_MAP_ALWAYS_TO:
38532 case GOMP_MAP_FROM:
38533 case GOMP_MAP_ALWAYS_FROM:
38534 case GOMP_MAP_TOFROM:
38535 case GOMP_MAP_ALWAYS_TOFROM:
38536 case GOMP_MAP_ALLOC:
38537 map_seen = 3;
38538 break;
38539 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38540 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38541 case GOMP_MAP_ALWAYS_POINTER:
38542 break;
38543 default:
38544 map_seen |= 1;
38545 error_at (OMP_CLAUSE_LOCATION (*pc),
38546 "%<#pragma omp target data%> with map-type other "
38547 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38548 "on %<map%> clause");
38549 *pc = OMP_CLAUSE_CHAIN (*pc);
38550 continue;
38551 }
38552 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
38553 map_seen = 3;
38554 pc = &OMP_CLAUSE_CHAIN (*pc);
38555 }
38556
38557 if (map_seen != 3)
38558 {
38559 if (map_seen == 0)
38560 error_at (pragma_tok->location,
38561 "%<#pragma omp target data%> must contain at least "
38562 "one %<map%> or %<use_device_ptr%> clause");
38563 return NULL_TREE;
38564 }
38565
38566 tree stmt = make_node (OMP_TARGET_DATA);
38567 TREE_TYPE (stmt) = void_type_node;
38568 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
38569
38570 keep_next_level (true);
38571 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38572
38573 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38574 return add_stmt (stmt);
38575 }
38576
38577 /* OpenMP 4.5:
38578 # pragma omp target enter data target-enter-data-clause[optseq] new-line
38579 structured-block */
38580
38581 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
38582 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38587
38588 static tree
38589 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
38590 enum pragma_context context)
38591 {
38592 bool data_seen = false;
38593 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38594 {
38595 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38596 const char *p = IDENTIFIER_POINTER (id);
38597
38598 if (strcmp (p, "data") == 0)
38599 {
38600 cp_lexer_consume_token (parser->lexer);
38601 data_seen = true;
38602 }
38603 }
38604 if (!data_seen)
38605 {
38606 cp_parser_error (parser, "expected %<data%>");
38607 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38608 return NULL_TREE;
38609 }
38610
38611 if (context == pragma_stmt)
38612 {
38613 error_at (pragma_tok->location,
38614 "%<#pragma %s%> may only be used in compound statements",
38615 "omp target enter data");
38616 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38617 return NULL_TREE;
38618 }
38619
38620 tree clauses
38621 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
38622 "#pragma omp target enter data", pragma_tok);
38623 int map_seen = 0;
38624 for (tree *pc = &clauses; *pc;)
38625 {
38626 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38627 switch (OMP_CLAUSE_MAP_KIND (*pc))
38628 {
38629 case GOMP_MAP_TO:
38630 case GOMP_MAP_ALWAYS_TO:
38631 case GOMP_MAP_ALLOC:
38632 map_seen = 3;
38633 break;
38634 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38635 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38636 case GOMP_MAP_ALWAYS_POINTER:
38637 break;
38638 default:
38639 map_seen |= 1;
38640 error_at (OMP_CLAUSE_LOCATION (*pc),
38641 "%<#pragma omp target enter data%> with map-type other "
38642 "than %<to%> or %<alloc%> on %<map%> clause");
38643 *pc = OMP_CLAUSE_CHAIN (*pc);
38644 continue;
38645 }
38646 pc = &OMP_CLAUSE_CHAIN (*pc);
38647 }
38648
38649 if (map_seen != 3)
38650 {
38651 if (map_seen == 0)
38652 error_at (pragma_tok->location,
38653 "%<#pragma omp target enter data%> must contain at least "
38654 "one %<map%> clause");
38655 return NULL_TREE;
38656 }
38657
38658 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
38659 TREE_TYPE (stmt) = void_type_node;
38660 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
38661 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38662 return add_stmt (stmt);
38663 }
38664
38665 /* OpenMP 4.5:
38666 # pragma omp target exit data target-enter-data-clause[optseq] new-line
38667 structured-block */
38668
38669 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
38670 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38675
38676 static tree
38677 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
38678 enum pragma_context context)
38679 {
38680 bool data_seen = false;
38681 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38682 {
38683 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38684 const char *p = IDENTIFIER_POINTER (id);
38685
38686 if (strcmp (p, "data") == 0)
38687 {
38688 cp_lexer_consume_token (parser->lexer);
38689 data_seen = true;
38690 }
38691 }
38692 if (!data_seen)
38693 {
38694 cp_parser_error (parser, "expected %<data%>");
38695 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38696 return NULL_TREE;
38697 }
38698
38699 if (context == pragma_stmt)
38700 {
38701 error_at (pragma_tok->location,
38702 "%<#pragma %s%> may only be used in compound statements",
38703 "omp target exit data");
38704 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38705 return NULL_TREE;
38706 }
38707
38708 tree clauses
38709 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
38710 "#pragma omp target exit data", pragma_tok);
38711 int map_seen = 0;
38712 for (tree *pc = &clauses; *pc;)
38713 {
38714 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38715 switch (OMP_CLAUSE_MAP_KIND (*pc))
38716 {
38717 case GOMP_MAP_FROM:
38718 case GOMP_MAP_ALWAYS_FROM:
38719 case GOMP_MAP_RELEASE:
38720 case GOMP_MAP_DELETE:
38721 map_seen = 3;
38722 break;
38723 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38724 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38725 case GOMP_MAP_ALWAYS_POINTER:
38726 break;
38727 default:
38728 map_seen |= 1;
38729 error_at (OMP_CLAUSE_LOCATION (*pc),
38730 "%<#pragma omp target exit data%> with map-type other "
38731 "than %<from%>, %<release%> or %<delete%> on %<map%>"
38732 " clause");
38733 *pc = OMP_CLAUSE_CHAIN (*pc);
38734 continue;
38735 }
38736 pc = &OMP_CLAUSE_CHAIN (*pc);
38737 }
38738
38739 if (map_seen != 3)
38740 {
38741 if (map_seen == 0)
38742 error_at (pragma_tok->location,
38743 "%<#pragma omp target exit data%> must contain at least "
38744 "one %<map%> clause");
38745 return NULL_TREE;
38746 }
38747
38748 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
38749 TREE_TYPE (stmt) = void_type_node;
38750 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
38751 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38752 return add_stmt (stmt);
38753 }
38754
38755 /* OpenMP 4.0:
38756 # pragma omp target update target-update-clause[optseq] new-line */
38757
38758 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
38759 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
38760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38765
38766 static bool
38767 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
38768 enum pragma_context context)
38769 {
38770 if (context == pragma_stmt)
38771 {
38772 error_at (pragma_tok->location,
38773 "%<#pragma %s%> may only be used in compound statements",
38774 "omp target update");
38775 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38776 return false;
38777 }
38778
38779 tree clauses
38780 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
38781 "#pragma omp target update", pragma_tok);
38782 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
38783 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
38784 {
38785 error_at (pragma_tok->location,
38786 "%<#pragma omp target update%> must contain at least one "
38787 "%<from%> or %<to%> clauses");
38788 return false;
38789 }
38790
38791 tree stmt = make_node (OMP_TARGET_UPDATE);
38792 TREE_TYPE (stmt) = void_type_node;
38793 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
38794 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38795 add_stmt (stmt);
38796 return false;
38797 }
38798
38799 /* OpenMP 4.0:
38800 # pragma omp target target-clause[optseq] new-line
38801 structured-block */
38802
38803 #define OMP_TARGET_CLAUSE_MASK \
38804 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
38812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
38813
38814 static bool
38815 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
38816 enum pragma_context context, bool *if_p)
38817 {
38818 tree *pc = NULL, stmt;
38819
38820 if (flag_openmp)
38821 omp_requires_mask
38822 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
38823
38824 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38825 {
38826 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38827 const char *p = IDENTIFIER_POINTER (id);
38828 enum tree_code ccode = ERROR_MARK;
38829
38830 if (strcmp (p, "teams") == 0)
38831 ccode = OMP_TEAMS;
38832 else if (strcmp (p, "parallel") == 0)
38833 ccode = OMP_PARALLEL;
38834 else if (strcmp (p, "simd") == 0)
38835 ccode = OMP_SIMD;
38836 if (ccode != ERROR_MARK)
38837 {
38838 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
38839 char p_name[sizeof ("#pragma omp target teams distribute "
38840 "parallel for simd")];
38841
38842 cp_lexer_consume_token (parser->lexer);
38843 strcpy (p_name, "#pragma omp target");
38844 if (!flag_openmp) /* flag_openmp_simd */
38845 {
38846 tree stmt;
38847 switch (ccode)
38848 {
38849 case OMP_TEAMS:
38850 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
38851 OMP_TARGET_CLAUSE_MASK,
38852 cclauses, if_p);
38853 break;
38854 case OMP_PARALLEL:
38855 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38856 OMP_TARGET_CLAUSE_MASK,
38857 cclauses, if_p);
38858 break;
38859 case OMP_SIMD:
38860 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
38861 OMP_TARGET_CLAUSE_MASK,
38862 cclauses, if_p);
38863 break;
38864 default:
38865 gcc_unreachable ();
38866 }
38867 return stmt != NULL_TREE;
38868 }
38869 keep_next_level (true);
38870 tree sb = begin_omp_structured_block (), ret;
38871 unsigned save = cp_parser_begin_omp_structured_block (parser);
38872 switch (ccode)
38873 {
38874 case OMP_TEAMS:
38875 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
38876 OMP_TARGET_CLAUSE_MASK, cclauses,
38877 if_p);
38878 break;
38879 case OMP_PARALLEL:
38880 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38881 OMP_TARGET_CLAUSE_MASK, cclauses,
38882 if_p);
38883 break;
38884 case OMP_SIMD:
38885 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
38886 OMP_TARGET_CLAUSE_MASK, cclauses,
38887 if_p);
38888 break;
38889 default:
38890 gcc_unreachable ();
38891 }
38892 cp_parser_end_omp_structured_block (parser, save);
38893 tree body = finish_omp_structured_block (sb);
38894 if (ret == NULL_TREE)
38895 return false;
38896 if (ccode == OMP_TEAMS && !processing_template_decl)
38897 {
38898 /* For combined target teams, ensure the num_teams and
38899 thread_limit clause expressions are evaluated on the host,
38900 before entering the target construct. */
38901 tree c;
38902 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38903 c; c = OMP_CLAUSE_CHAIN (c))
38904 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
38905 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
38906 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
38907 {
38908 tree expr = OMP_CLAUSE_OPERAND (c, 0);
38909 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
38910 if (expr == error_mark_node)
38911 continue;
38912 tree tmp = TARGET_EXPR_SLOT (expr);
38913 add_stmt (expr);
38914 OMP_CLAUSE_OPERAND (c, 0) = expr;
38915 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
38916 OMP_CLAUSE_FIRSTPRIVATE);
38917 OMP_CLAUSE_DECL (tc) = tmp;
38918 OMP_CLAUSE_CHAIN (tc)
38919 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38920 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
38921 }
38922 }
38923 tree stmt = make_node (OMP_TARGET);
38924 TREE_TYPE (stmt) = void_type_node;
38925 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38926 OMP_TARGET_BODY (stmt) = body;
38927 OMP_TARGET_COMBINED (stmt) = 1;
38928 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38929 add_stmt (stmt);
38930 pc = &OMP_TARGET_CLAUSES (stmt);
38931 goto check_clauses;
38932 }
38933 else if (!flag_openmp) /* flag_openmp_simd */
38934 {
38935 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38936 return false;
38937 }
38938 else if (strcmp (p, "data") == 0)
38939 {
38940 cp_lexer_consume_token (parser->lexer);
38941 cp_parser_omp_target_data (parser, pragma_tok, if_p);
38942 return true;
38943 }
38944 else if (strcmp (p, "enter") == 0)
38945 {
38946 cp_lexer_consume_token (parser->lexer);
38947 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
38948 return false;
38949 }
38950 else if (strcmp (p, "exit") == 0)
38951 {
38952 cp_lexer_consume_token (parser->lexer);
38953 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
38954 return false;
38955 }
38956 else if (strcmp (p, "update") == 0)
38957 {
38958 cp_lexer_consume_token (parser->lexer);
38959 return cp_parser_omp_target_update (parser, pragma_tok, context);
38960 }
38961 }
38962 if (!flag_openmp) /* flag_openmp_simd */
38963 {
38964 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38965 return false;
38966 }
38967
38968 stmt = make_node (OMP_TARGET);
38969 TREE_TYPE (stmt) = void_type_node;
38970
38971 OMP_TARGET_CLAUSES (stmt)
38972 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
38973 "#pragma omp target", pragma_tok);
38974 pc = &OMP_TARGET_CLAUSES (stmt);
38975 keep_next_level (true);
38976 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38977
38978 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38979 add_stmt (stmt);
38980
38981 check_clauses:
38982 while (*pc)
38983 {
38984 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38985 switch (OMP_CLAUSE_MAP_KIND (*pc))
38986 {
38987 case GOMP_MAP_TO:
38988 case GOMP_MAP_ALWAYS_TO:
38989 case GOMP_MAP_FROM:
38990 case GOMP_MAP_ALWAYS_FROM:
38991 case GOMP_MAP_TOFROM:
38992 case GOMP_MAP_ALWAYS_TOFROM:
38993 case GOMP_MAP_ALLOC:
38994 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38995 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38996 case GOMP_MAP_ALWAYS_POINTER:
38997 break;
38998 default:
38999 error_at (OMP_CLAUSE_LOCATION (*pc),
39000 "%<#pragma omp target%> with map-type other "
39001 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
39002 "on %<map%> clause");
39003 *pc = OMP_CLAUSE_CHAIN (*pc);
39004 continue;
39005 }
39006 pc = &OMP_CLAUSE_CHAIN (*pc);
39007 }
39008 return true;
39009 }
39010
39011 /* OpenACC 2.0:
39012 # pragma acc cache (variable-list) new-line
39013 */
39014
39015 static tree
39016 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
39017 {
39018 tree stmt, clauses;
39019
39020 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
39021 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
39022
39023 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
39024
39025 stmt = make_node (OACC_CACHE);
39026 TREE_TYPE (stmt) = void_type_node;
39027 OACC_CACHE_CLAUSES (stmt) = clauses;
39028 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39029 add_stmt (stmt);
39030
39031 return stmt;
39032 }
39033
39034 /* OpenACC 2.0:
39035 # pragma acc data oacc-data-clause[optseq] new-line
39036 structured-block */
39037
39038 #define OACC_DATA_CLAUSE_MASK \
39039 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
39046
39047 static tree
39048 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39049 {
39050 tree stmt, clauses, block;
39051 unsigned int save;
39052
39053 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
39054 "#pragma acc data", pragma_tok);
39055
39056 block = begin_omp_parallel ();
39057 save = cp_parser_begin_omp_structured_block (parser);
39058 cp_parser_statement (parser, NULL_TREE, false, if_p);
39059 cp_parser_end_omp_structured_block (parser, save);
39060 stmt = finish_oacc_data (clauses, block);
39061 return stmt;
39062 }
39063
39064 /* OpenACC 2.0:
39065 # pragma acc host_data <clauses> new-line
39066 structured-block */
39067
39068 #define OACC_HOST_DATA_CLAUSE_MASK \
39069 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
39070
39071 static tree
39072 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39073 {
39074 tree stmt, clauses, block;
39075 unsigned int save;
39076
39077 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
39078 "#pragma acc host_data", pragma_tok);
39079
39080 block = begin_omp_parallel ();
39081 save = cp_parser_begin_omp_structured_block (parser);
39082 cp_parser_statement (parser, NULL_TREE, false, if_p);
39083 cp_parser_end_omp_structured_block (parser, save);
39084 stmt = finish_oacc_host_data (clauses, block);
39085 return stmt;
39086 }
39087
39088 /* OpenACC 2.0:
39089 # pragma acc declare oacc-data-clause[optseq] new-line
39090 */
39091
39092 #define OACC_DECLARE_CLAUSE_MASK \
39093 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
39099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
39100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
39101
39102 static tree
39103 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
39104 {
39105 tree clauses, stmt;
39106 bool error = false;
39107
39108 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
39109 "#pragma acc declare", pragma_tok, true);
39110
39111
39112 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
39113 {
39114 error_at (pragma_tok->location,
39115 "no valid clauses specified in %<#pragma acc declare%>");
39116 return NULL_TREE;
39117 }
39118
39119 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
39120 {
39121 location_t loc = OMP_CLAUSE_LOCATION (t);
39122 tree decl = OMP_CLAUSE_DECL (t);
39123 if (!DECL_P (decl))
39124 {
39125 error_at (loc, "array section in %<#pragma acc declare%>");
39126 error = true;
39127 continue;
39128 }
39129 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
39130 switch (OMP_CLAUSE_MAP_KIND (t))
39131 {
39132 case GOMP_MAP_FIRSTPRIVATE_POINTER:
39133 case GOMP_MAP_ALLOC:
39134 case GOMP_MAP_TO:
39135 case GOMP_MAP_FORCE_DEVICEPTR:
39136 case GOMP_MAP_DEVICE_RESIDENT:
39137 break;
39138
39139 case GOMP_MAP_LINK:
39140 if (!global_bindings_p ()
39141 && (TREE_STATIC (decl)
39142 || !DECL_EXTERNAL (decl)))
39143 {
39144 error_at (loc,
39145 "%qD must be a global variable in "
39146 "%<#pragma acc declare link%>",
39147 decl);
39148 error = true;
39149 continue;
39150 }
39151 break;
39152
39153 default:
39154 if (global_bindings_p ())
39155 {
39156 error_at (loc, "invalid OpenACC clause at file scope");
39157 error = true;
39158 continue;
39159 }
39160 if (DECL_EXTERNAL (decl))
39161 {
39162 error_at (loc,
39163 "invalid use of %<extern%> variable %qD "
39164 "in %<#pragma acc declare%>", decl);
39165 error = true;
39166 continue;
39167 }
39168 else if (TREE_PUBLIC (decl))
39169 {
39170 error_at (loc,
39171 "invalid use of %<global%> variable %qD "
39172 "in %<#pragma acc declare%>", decl);
39173 error = true;
39174 continue;
39175 }
39176 break;
39177 }
39178
39179 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
39180 || lookup_attribute ("omp declare target link",
39181 DECL_ATTRIBUTES (decl)))
39182 {
39183 error_at (loc, "variable %qD used more than once with "
39184 "%<#pragma acc declare%>", decl);
39185 error = true;
39186 continue;
39187 }
39188
39189 if (!error)
39190 {
39191 tree id;
39192
39193 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
39194 id = get_identifier ("omp declare target link");
39195 else
39196 id = get_identifier ("omp declare target");
39197
39198 DECL_ATTRIBUTES (decl)
39199 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
39200 if (global_bindings_p ())
39201 {
39202 symtab_node *node = symtab_node::get (decl);
39203 if (node != NULL)
39204 {
39205 node->offloadable = 1;
39206 if (ENABLE_OFFLOADING)
39207 {
39208 g->have_offload = true;
39209 if (is_a <varpool_node *> (node))
39210 vec_safe_push (offload_vars, decl);
39211 }
39212 }
39213 }
39214 }
39215 }
39216
39217 if (error || global_bindings_p ())
39218 return NULL_TREE;
39219
39220 stmt = make_node (OACC_DECLARE);
39221 TREE_TYPE (stmt) = void_type_node;
39222 OACC_DECLARE_CLAUSES (stmt) = clauses;
39223 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39224
39225 add_stmt (stmt);
39226
39227 return NULL_TREE;
39228 }
39229
39230 /* OpenACC 2.0:
39231 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
39232
39233 or
39234
39235 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
39236
39237 LOC is the location of the #pragma token.
39238 */
39239
39240 #define OACC_ENTER_DATA_CLAUSE_MASK \
39241 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39246
39247 #define OACC_EXIT_DATA_CLAUSE_MASK \
39248 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39249 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
39252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
39253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39254
39255 static tree
39256 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
39257 bool enter)
39258 {
39259 location_t loc = pragma_tok->location;
39260 tree stmt, clauses;
39261 const char *p = "";
39262
39263 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39264 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
39265
39266 if (strcmp (p, "data") != 0)
39267 {
39268 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
39269 enter ? "enter" : "exit");
39270 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39271 return NULL_TREE;
39272 }
39273
39274 cp_lexer_consume_token (parser->lexer);
39275
39276 if (enter)
39277 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
39278 "#pragma acc enter data", pragma_tok);
39279 else
39280 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
39281 "#pragma acc exit data", pragma_tok);
39282
39283 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
39284 {
39285 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
39286 enter ? "enter" : "exit");
39287 return NULL_TREE;
39288 }
39289
39290 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
39291 TREE_TYPE (stmt) = void_type_node;
39292 OMP_STANDALONE_CLAUSES (stmt) = clauses;
39293 SET_EXPR_LOCATION (stmt, loc);
39294 add_stmt (stmt);
39295 return stmt;
39296 }
39297
39298 /* OpenACC 2.0:
39299 # pragma acc loop oacc-loop-clause[optseq] new-line
39300 structured-block */
39301
39302 #define OACC_LOOP_CLAUSE_MASK \
39303 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
39304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
39305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
39306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39307 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
39310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
39311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
39312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
39313
39314 static tree
39315 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
39316 omp_clause_mask mask, tree *cclauses, bool *if_p)
39317 {
39318 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
39319
39320 strcat (p_name, " loop");
39321 mask |= OACC_LOOP_CLAUSE_MASK;
39322
39323 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
39324 cclauses == NULL);
39325 if (cclauses)
39326 {
39327 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
39328 if (*cclauses)
39329 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
39330 if (clauses)
39331 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
39332 }
39333
39334 tree block = begin_omp_structured_block ();
39335 int save = cp_parser_begin_omp_structured_block (parser);
39336 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
39337 cp_parser_end_omp_structured_block (parser, save);
39338 add_stmt (finish_omp_structured_block (block));
39339
39340 return stmt;
39341 }
39342
39343 /* OpenACC 2.0:
39344 # pragma acc kernels oacc-kernels-clause[optseq] new-line
39345 structured-block
39346
39347 or
39348
39349 # pragma acc parallel oacc-parallel-clause[optseq] new-line
39350 structured-block
39351 */
39352
39353 #define OACC_KERNELS_CLAUSE_MASK \
39354 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
39360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
39363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
39364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
39365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
39366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39367
39368 #define OACC_PARALLEL_CLAUSE_MASK \
39369 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
39375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
39377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
39379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
39380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
39381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
39382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
39383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
39384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39385
39386 static tree
39387 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
39388 char *p_name, bool *if_p)
39389 {
39390 omp_clause_mask mask;
39391 enum tree_code code;
39392 switch (cp_parser_pragma_kind (pragma_tok))
39393 {
39394 case PRAGMA_OACC_KERNELS:
39395 strcat (p_name, " kernels");
39396 mask = OACC_KERNELS_CLAUSE_MASK;
39397 code = OACC_KERNELS;
39398 break;
39399 case PRAGMA_OACC_PARALLEL:
39400 strcat (p_name, " parallel");
39401 mask = OACC_PARALLEL_CLAUSE_MASK;
39402 code = OACC_PARALLEL;
39403 break;
39404 default:
39405 gcc_unreachable ();
39406 }
39407
39408 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39409 {
39410 const char *p
39411 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
39412 if (strcmp (p, "loop") == 0)
39413 {
39414 cp_lexer_consume_token (parser->lexer);
39415 tree block = begin_omp_parallel ();
39416 tree clauses;
39417 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
39418 &clauses, if_p);
39419 protected_set_expr_location (stmt, pragma_tok->location);
39420 return finish_omp_construct (code, block, clauses);
39421 }
39422 }
39423
39424 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
39425
39426 tree block = begin_omp_parallel ();
39427 unsigned int save = cp_parser_begin_omp_structured_block (parser);
39428 cp_parser_statement (parser, NULL_TREE, false, if_p);
39429 cp_parser_end_omp_structured_block (parser, save);
39430 return finish_omp_construct (code, block, clauses);
39431 }
39432
39433 /* OpenACC 2.0:
39434 # pragma acc update oacc-update-clause[optseq] new-line
39435 */
39436
39437 #define OACC_UPDATE_CLAUSE_MASK \
39438 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
39440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
39441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
39443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
39444
39445 static tree
39446 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
39447 {
39448 tree stmt, clauses;
39449
39450 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
39451 "#pragma acc update", pragma_tok);
39452
39453 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
39454 {
39455 error_at (pragma_tok->location,
39456 "%<#pragma acc update%> must contain at least one "
39457 "%<device%> or %<host%> or %<self%> clause");
39458 return NULL_TREE;
39459 }
39460
39461 stmt = make_node (OACC_UPDATE);
39462 TREE_TYPE (stmt) = void_type_node;
39463 OACC_UPDATE_CLAUSES (stmt) = clauses;
39464 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39465 add_stmt (stmt);
39466 return stmt;
39467 }
39468
39469 /* OpenACC 2.0:
39470 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
39471
39472 LOC is the location of the #pragma token.
39473 */
39474
39475 #define OACC_WAIT_CLAUSE_MASK \
39476 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
39477
39478 static tree
39479 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
39480 {
39481 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
39482 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39483
39484 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39485 list = cp_parser_oacc_wait_list (parser, loc, list);
39486
39487 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
39488 "#pragma acc wait", pragma_tok);
39489
39490 stmt = c_finish_oacc_wait (loc, list, clauses);
39491 stmt = finish_expr_stmt (stmt);
39492
39493 return stmt;
39494 }
39495
39496 /* OpenMP 4.0:
39497 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
39498
39499 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
39500 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
39501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
39503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
39504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
39505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
39506
39507 static void
39508 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
39509 enum pragma_context context)
39510 {
39511 bool first_p = parser->omp_declare_simd == NULL;
39512 cp_omp_declare_simd_data data;
39513 if (first_p)
39514 {
39515 data.error_seen = false;
39516 data.fndecl_seen = false;
39517 data.tokens = vNULL;
39518 data.clauses = NULL_TREE;
39519 /* It is safe to take the address of a local variable; it will only be
39520 used while this scope is live. */
39521 parser->omp_declare_simd = &data;
39522 }
39523
39524 /* Store away all pragma tokens. */
39525 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39526 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39527 cp_lexer_consume_token (parser->lexer);
39528 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39529 parser->omp_declare_simd->error_seen = true;
39530 cp_parser_require_pragma_eol (parser, pragma_tok);
39531 struct cp_token_cache *cp
39532 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39533 parser->omp_declare_simd->tokens.safe_push (cp);
39534
39535 if (first_p)
39536 {
39537 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39538 cp_parser_pragma (parser, context, NULL);
39539 switch (context)
39540 {
39541 case pragma_external:
39542 cp_parser_declaration (parser);
39543 break;
39544 case pragma_member:
39545 cp_parser_member_declaration (parser);
39546 break;
39547 case pragma_objc_icode:
39548 cp_parser_block_declaration (parser, /*statement_p=*/false);
39549 break;
39550 default:
39551 cp_parser_declaration_statement (parser);
39552 break;
39553 }
39554 if (parser->omp_declare_simd
39555 && !parser->omp_declare_simd->error_seen
39556 && !parser->omp_declare_simd->fndecl_seen)
39557 error_at (pragma_tok->location,
39558 "%<#pragma omp declare simd%> not immediately followed by "
39559 "function declaration or definition");
39560 data.tokens.release ();
39561 parser->omp_declare_simd = NULL;
39562 }
39563 }
39564
39565 /* Finalize #pragma omp declare simd clauses after direct declarator has
39566 been parsed, and put that into "omp declare simd" attribute. */
39567
39568 static tree
39569 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
39570 {
39571 struct cp_token_cache *ce;
39572 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
39573 int i;
39574
39575 if (!data->error_seen && data->fndecl_seen)
39576 {
39577 error ("%<#pragma omp declare simd%> not immediately followed by "
39578 "a single function declaration or definition");
39579 data->error_seen = true;
39580 }
39581 if (data->error_seen)
39582 return attrs;
39583
39584 FOR_EACH_VEC_ELT (data->tokens, i, ce)
39585 {
39586 tree c, cl;
39587
39588 cp_parser_push_lexer_for_tokens (parser, ce);
39589 parser->lexer->in_pragma = true;
39590 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39591 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39592 cp_lexer_consume_token (parser->lexer);
39593 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
39594 "#pragma omp declare simd", pragma_tok);
39595 cp_parser_pop_lexer (parser);
39596 if (cl)
39597 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
39598 c = build_tree_list (get_identifier ("omp declare simd"), cl);
39599 TREE_CHAIN (c) = attrs;
39600 if (processing_template_decl)
39601 ATTR_IS_DEPENDENT (c) = 1;
39602 attrs = c;
39603 }
39604
39605 data->fndecl_seen = true;
39606 return attrs;
39607 }
39608
39609
39610 /* OpenMP 4.0:
39611 # pragma omp declare target new-line
39612 declarations and definitions
39613 # pragma omp end declare target new-line
39614
39615 OpenMP 4.5:
39616 # pragma omp declare target ( extended-list ) new-line
39617
39618 # pragma omp declare target declare-target-clauses[seq] new-line */
39619
39620 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
39621 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
39622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
39623
39624 static void
39625 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
39626 {
39627 tree clauses = NULL_TREE;
39628 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39629 clauses
39630 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
39631 "#pragma omp declare target", pragma_tok);
39632 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39633 {
39634 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
39635 clauses);
39636 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
39637 cp_parser_require_pragma_eol (parser, pragma_tok);
39638 }
39639 else
39640 {
39641 cp_parser_require_pragma_eol (parser, pragma_tok);
39642 scope_chain->omp_declare_target_attribute++;
39643 return;
39644 }
39645 if (scope_chain->omp_declare_target_attribute)
39646 error_at (pragma_tok->location,
39647 "%<#pragma omp declare target%> with clauses in between "
39648 "%<#pragma omp declare target%> without clauses and "
39649 "%<#pragma omp end declare target%>");
39650 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
39651 {
39652 tree t = OMP_CLAUSE_DECL (c), id;
39653 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
39654 tree at2 = lookup_attribute ("omp declare target link",
39655 DECL_ATTRIBUTES (t));
39656 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
39657 {
39658 id = get_identifier ("omp declare target link");
39659 std::swap (at1, at2);
39660 }
39661 else
39662 id = get_identifier ("omp declare target");
39663 if (at2)
39664 {
39665 error_at (OMP_CLAUSE_LOCATION (c),
39666 "%qD specified both in declare target %<link%> and %<to%>"
39667 " clauses", t);
39668 continue;
39669 }
39670 if (!at1)
39671 {
39672 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
39673 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
39674 continue;
39675
39676 symtab_node *node = symtab_node::get (t);
39677 if (node != NULL)
39678 {
39679 node->offloadable = 1;
39680 if (ENABLE_OFFLOADING)
39681 {
39682 g->have_offload = true;
39683 if (is_a <varpool_node *> (node))
39684 vec_safe_push (offload_vars, t);
39685 }
39686 }
39687 }
39688 }
39689 }
39690
39691 static void
39692 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
39693 {
39694 const char *p = "";
39695 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39696 {
39697 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39698 p = IDENTIFIER_POINTER (id);
39699 }
39700 if (strcmp (p, "declare") == 0)
39701 {
39702 cp_lexer_consume_token (parser->lexer);
39703 p = "";
39704 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39705 {
39706 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39707 p = IDENTIFIER_POINTER (id);
39708 }
39709 if (strcmp (p, "target") == 0)
39710 cp_lexer_consume_token (parser->lexer);
39711 else
39712 {
39713 cp_parser_error (parser, "expected %<target%>");
39714 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39715 return;
39716 }
39717 }
39718 else
39719 {
39720 cp_parser_error (parser, "expected %<declare%>");
39721 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39722 return;
39723 }
39724 cp_parser_require_pragma_eol (parser, pragma_tok);
39725 if (!scope_chain->omp_declare_target_attribute)
39726 error_at (pragma_tok->location,
39727 "%<#pragma omp end declare target%> without corresponding "
39728 "%<#pragma omp declare target%>");
39729 else
39730 scope_chain->omp_declare_target_attribute--;
39731 }
39732
39733 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
39734 expression and optional initializer clause of
39735 #pragma omp declare reduction. We store the expression(s) as
39736 either 3, 6 or 7 special statements inside of the artificial function's
39737 body. The first two statements are DECL_EXPRs for the artificial
39738 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
39739 expression that uses those variables.
39740 If there was any INITIALIZER clause, this is followed by further statements,
39741 the fourth and fifth statements are DECL_EXPRs for the artificial
39742 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
39743 constructor variant (first token after open paren is not omp_priv),
39744 then the sixth statement is a statement with the function call expression
39745 that uses the OMP_PRIV and optionally OMP_ORIG variable.
39746 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
39747 to initialize the OMP_PRIV artificial variable and there is seventh
39748 statement, a DECL_EXPR of the OMP_PRIV statement again. */
39749
39750 static bool
39751 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
39752 {
39753 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
39754 gcc_assert (TYPE_REF_P (type));
39755 type = TREE_TYPE (type);
39756 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
39757 DECL_ARTIFICIAL (omp_out) = 1;
39758 pushdecl (omp_out);
39759 add_decl_expr (omp_out);
39760 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
39761 DECL_ARTIFICIAL (omp_in) = 1;
39762 pushdecl (omp_in);
39763 add_decl_expr (omp_in);
39764 tree combiner;
39765 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
39766
39767 keep_next_level (true);
39768 tree block = begin_omp_structured_block ();
39769 combiner = cp_parser_expression (parser);
39770 finish_expr_stmt (combiner);
39771 block = finish_omp_structured_block (block);
39772 add_stmt (block);
39773
39774 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
39775 return false;
39776
39777 const char *p = "";
39778 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39779 {
39780 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39781 p = IDENTIFIER_POINTER (id);
39782 }
39783
39784 if (strcmp (p, "initializer") == 0)
39785 {
39786 cp_lexer_consume_token (parser->lexer);
39787 matching_parens parens;
39788 if (!parens.require_open (parser))
39789 return false;
39790
39791 p = "";
39792 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39793 {
39794 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39795 p = IDENTIFIER_POINTER (id);
39796 }
39797
39798 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
39799 DECL_ARTIFICIAL (omp_priv) = 1;
39800 pushdecl (omp_priv);
39801 add_decl_expr (omp_priv);
39802 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
39803 DECL_ARTIFICIAL (omp_orig) = 1;
39804 pushdecl (omp_orig);
39805 add_decl_expr (omp_orig);
39806
39807 keep_next_level (true);
39808 block = begin_omp_structured_block ();
39809
39810 bool ctor = false;
39811 if (strcmp (p, "omp_priv") == 0)
39812 {
39813 bool is_direct_init, is_non_constant_init;
39814 ctor = true;
39815 cp_lexer_consume_token (parser->lexer);
39816 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
39817 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
39818 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39819 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
39820 == CPP_CLOSE_PAREN
39821 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
39822 == CPP_CLOSE_PAREN))
39823 {
39824 finish_omp_structured_block (block);
39825 error ("invalid initializer clause");
39826 return false;
39827 }
39828 initializer = cp_parser_initializer (parser, &is_direct_init,
39829 &is_non_constant_init);
39830 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
39831 NULL_TREE, LOOKUP_ONLYCONVERTING);
39832 }
39833 else
39834 {
39835 cp_parser_parse_tentatively (parser);
39836 /* Don't create location wrapper nodes here. */
39837 auto_suppress_location_wrappers sentinel;
39838 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
39839 /*check_dependency_p=*/true,
39840 /*template_p=*/NULL,
39841 /*declarator_p=*/false,
39842 /*optional_p=*/false);
39843 vec<tree, va_gc> *args;
39844 if (fn_name == error_mark_node
39845 || cp_parser_error_occurred (parser)
39846 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39847 || ((args = cp_parser_parenthesized_expression_list
39848 (parser, non_attr, /*cast_p=*/false,
39849 /*allow_expansion_p=*/true,
39850 /*non_constant_p=*/NULL)),
39851 cp_parser_error_occurred (parser)))
39852 {
39853 finish_omp_structured_block (block);
39854 cp_parser_abort_tentative_parse (parser);
39855 cp_parser_error (parser, "expected id-expression (arguments)");
39856 return false;
39857 }
39858 unsigned int i;
39859 tree arg;
39860 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
39861 if (arg == omp_priv
39862 || (TREE_CODE (arg) == ADDR_EXPR
39863 && TREE_OPERAND (arg, 0) == omp_priv))
39864 break;
39865 cp_parser_abort_tentative_parse (parser);
39866 if (arg == NULL_TREE)
39867 error ("one of the initializer call arguments should be %<omp_priv%>"
39868 " or %<&omp_priv%>");
39869 initializer = cp_parser_postfix_expression (parser, false, false, false,
39870 false, NULL);
39871 finish_expr_stmt (initializer);
39872 }
39873
39874 block = finish_omp_structured_block (block);
39875 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
39876 add_stmt (block);
39877
39878 if (ctor)
39879 add_decl_expr (omp_orig);
39880
39881 if (!parens.require_close (parser))
39882 return false;
39883 }
39884
39885 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
39886 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
39887 UNKNOWN_LOCATION);
39888
39889 return true;
39890 }
39891
39892 /* OpenMP 4.0
39893 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39894 initializer-clause[opt] new-line
39895
39896 initializer-clause:
39897 initializer (omp_priv initializer)
39898 initializer (function-name (argument-list)) */
39899
39900 static void
39901 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
39902 enum pragma_context)
39903 {
39904 auto_vec<tree> types;
39905 enum tree_code reduc_code = ERROR_MARK;
39906 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
39907 unsigned int i;
39908 cp_token *first_token;
39909 cp_token_cache *cp;
39910 int errs;
39911 void *p;
39912
39913 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39914 p = obstack_alloc (&declarator_obstack, 0);
39915
39916 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39917 goto fail;
39918
39919 switch (cp_lexer_peek_token (parser->lexer)->type)
39920 {
39921 case CPP_PLUS:
39922 reduc_code = PLUS_EXPR;
39923 break;
39924 case CPP_MULT:
39925 reduc_code = MULT_EXPR;
39926 break;
39927 case CPP_MINUS:
39928 reduc_code = MINUS_EXPR;
39929 break;
39930 case CPP_AND:
39931 reduc_code = BIT_AND_EXPR;
39932 break;
39933 case CPP_XOR:
39934 reduc_code = BIT_XOR_EXPR;
39935 break;
39936 case CPP_OR:
39937 reduc_code = BIT_IOR_EXPR;
39938 break;
39939 case CPP_AND_AND:
39940 reduc_code = TRUTH_ANDIF_EXPR;
39941 break;
39942 case CPP_OR_OR:
39943 reduc_code = TRUTH_ORIF_EXPR;
39944 break;
39945 case CPP_NAME:
39946 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
39947 break;
39948 default:
39949 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39950 "%<|%>, %<&&%>, %<||%> or identifier");
39951 goto fail;
39952 }
39953
39954 if (reduc_code != ERROR_MARK)
39955 cp_lexer_consume_token (parser->lexer);
39956
39957 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
39958 if (reduc_id == error_mark_node)
39959 goto fail;
39960
39961 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39962 goto fail;
39963
39964 /* Types may not be defined in declare reduction type list. */
39965 const char *saved_message;
39966 saved_message = parser->type_definition_forbidden_message;
39967 parser->type_definition_forbidden_message
39968 = G_("types may not be defined in declare reduction type list");
39969 bool saved_colon_corrects_to_scope_p;
39970 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39971 parser->colon_corrects_to_scope_p = false;
39972 bool saved_colon_doesnt_start_class_def_p;
39973 saved_colon_doesnt_start_class_def_p
39974 = parser->colon_doesnt_start_class_def_p;
39975 parser->colon_doesnt_start_class_def_p = true;
39976
39977 while (true)
39978 {
39979 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39980 type = cp_parser_type_id (parser);
39981 if (type == error_mark_node)
39982 ;
39983 else if (ARITHMETIC_TYPE_P (type)
39984 && (orig_reduc_id == NULL_TREE
39985 || (TREE_CODE (type) != COMPLEX_TYPE
39986 && (id_equal (orig_reduc_id, "min")
39987 || id_equal (orig_reduc_id, "max")))))
39988 error_at (loc, "predeclared arithmetic type %qT in "
39989 "%<#pragma omp declare reduction%>", type);
39990 else if (FUNC_OR_METHOD_TYPE_P (type)
39991 || TREE_CODE (type) == ARRAY_TYPE)
39992 error_at (loc, "function or array type %qT in "
39993 "%<#pragma omp declare reduction%>", type);
39994 else if (TYPE_REF_P (type))
39995 error_at (loc, "reference type %qT in "
39996 "%<#pragma omp declare reduction%>", type);
39997 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
39998 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
39999 "type %qT in %<#pragma omp declare reduction%>", type);
40000 else
40001 types.safe_push (type);
40002
40003 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40004 cp_lexer_consume_token (parser->lexer);
40005 else
40006 break;
40007 }
40008
40009 /* Restore the saved message. */
40010 parser->type_definition_forbidden_message = saved_message;
40011 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40012 parser->colon_doesnt_start_class_def_p
40013 = saved_colon_doesnt_start_class_def_p;
40014
40015 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
40016 || types.is_empty ())
40017 {
40018 fail:
40019 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40020 goto done;
40021 }
40022
40023 first_token = cp_lexer_peek_token (parser->lexer);
40024 cp = NULL;
40025 errs = errorcount;
40026 FOR_EACH_VEC_ELT (types, i, type)
40027 {
40028 tree fntype
40029 = build_function_type_list (void_type_node,
40030 cp_build_reference_type (type, false),
40031 NULL_TREE);
40032 tree this_reduc_id = reduc_id;
40033 if (!dependent_type_p (type))
40034 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
40035 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
40036 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
40037 DECL_ARTIFICIAL (fndecl) = 1;
40038 DECL_EXTERNAL (fndecl) = 1;
40039 DECL_DECLARED_INLINE_P (fndecl) = 1;
40040 DECL_IGNORED_P (fndecl) = 1;
40041 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
40042 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
40043 DECL_ATTRIBUTES (fndecl)
40044 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
40045 DECL_ATTRIBUTES (fndecl));
40046 if (processing_template_decl)
40047 fndecl = push_template_decl (fndecl);
40048 bool block_scope = false;
40049 tree block = NULL_TREE;
40050 if (current_function_decl)
40051 {
40052 block_scope = true;
40053 DECL_CONTEXT (fndecl) = global_namespace;
40054 if (!processing_template_decl)
40055 pushdecl (fndecl);
40056 }
40057 else if (current_class_type)
40058 {
40059 if (cp == NULL)
40060 {
40061 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
40062 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
40063 cp_lexer_consume_token (parser->lexer);
40064 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40065 goto fail;
40066 cp = cp_token_cache_new (first_token,
40067 cp_lexer_peek_nth_token (parser->lexer,
40068 2));
40069 }
40070 DECL_STATIC_FUNCTION_P (fndecl) = 1;
40071 finish_member_declaration (fndecl);
40072 DECL_PENDING_INLINE_INFO (fndecl) = cp;
40073 DECL_PENDING_INLINE_P (fndecl) = 1;
40074 vec_safe_push (unparsed_funs_with_definitions, fndecl);
40075 continue;
40076 }
40077 else
40078 {
40079 DECL_CONTEXT (fndecl) = current_namespace;
40080 pushdecl (fndecl);
40081 }
40082 if (!block_scope)
40083 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
40084 else
40085 block = begin_omp_structured_block ();
40086 if (cp)
40087 {
40088 cp_parser_push_lexer_for_tokens (parser, cp);
40089 parser->lexer->in_pragma = true;
40090 }
40091 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
40092 {
40093 if (!block_scope)
40094 finish_function (/*inline_p=*/false);
40095 else
40096 DECL_CONTEXT (fndecl) = current_function_decl;
40097 if (cp)
40098 cp_parser_pop_lexer (parser);
40099 goto fail;
40100 }
40101 if (cp)
40102 cp_parser_pop_lexer (parser);
40103 if (!block_scope)
40104 finish_function (/*inline_p=*/false);
40105 else
40106 {
40107 DECL_CONTEXT (fndecl) = current_function_decl;
40108 block = finish_omp_structured_block (block);
40109 if (TREE_CODE (block) == BIND_EXPR)
40110 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
40111 else if (TREE_CODE (block) == STATEMENT_LIST)
40112 DECL_SAVED_TREE (fndecl) = block;
40113 if (processing_template_decl)
40114 add_decl_expr (fndecl);
40115 }
40116 cp_check_omp_declare_reduction (fndecl);
40117 if (cp == NULL && types.length () > 1)
40118 cp = cp_token_cache_new (first_token,
40119 cp_lexer_peek_nth_token (parser->lexer, 2));
40120 if (errs != errorcount)
40121 break;
40122 }
40123
40124 cp_parser_require_pragma_eol (parser, pragma_tok);
40125
40126 done:
40127 /* Free any declarators allocated. */
40128 obstack_free (&declarator_obstack, p);
40129 }
40130
40131 /* OpenMP 4.0
40132 #pragma omp declare simd declare-simd-clauses[optseq] new-line
40133 #pragma omp declare reduction (reduction-id : typename-list : expression) \
40134 initializer-clause[opt] new-line
40135 #pragma omp declare target new-line */
40136
40137 static bool
40138 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
40139 enum pragma_context context)
40140 {
40141 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40142 {
40143 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40144 const char *p = IDENTIFIER_POINTER (id);
40145
40146 if (strcmp (p, "simd") == 0)
40147 {
40148 cp_lexer_consume_token (parser->lexer);
40149 cp_parser_omp_declare_simd (parser, pragma_tok,
40150 context);
40151 return true;
40152 }
40153 cp_ensure_no_omp_declare_simd (parser);
40154 if (strcmp (p, "reduction") == 0)
40155 {
40156 cp_lexer_consume_token (parser->lexer);
40157 cp_parser_omp_declare_reduction (parser, pragma_tok,
40158 context);
40159 return false;
40160 }
40161 if (!flag_openmp) /* flag_openmp_simd */
40162 {
40163 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40164 return false;
40165 }
40166 if (strcmp (p, "target") == 0)
40167 {
40168 cp_lexer_consume_token (parser->lexer);
40169 cp_parser_omp_declare_target (parser, pragma_tok);
40170 return false;
40171 }
40172 }
40173 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
40174 "or %<target%>");
40175 cp_parser_require_pragma_eol (parser, pragma_tok);
40176 return false;
40177 }
40178
40179 /* OpenMP 5.0
40180 #pragma omp requires clauses[optseq] new-line */
40181
40182 static bool
40183 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
40184 {
40185 bool first = true;
40186 enum omp_requires new_req = (enum omp_requires) 0;
40187
40188 location_t loc = pragma_tok->location;
40189 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40190 {
40191 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40192 cp_lexer_consume_token (parser->lexer);
40193
40194 first = false;
40195
40196 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40197 {
40198 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40199 const char *p = IDENTIFIER_POINTER (id);
40200 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
40201 enum omp_requires this_req = (enum omp_requires) 0;
40202
40203 if (!strcmp (p, "unified_address"))
40204 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
40205 else if (!strcmp (p, "unified_shared_memory"))
40206 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
40207 else if (!strcmp (p, "dynamic_allocators"))
40208 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
40209 else if (!strcmp (p, "reverse_offload"))
40210 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
40211 else if (!strcmp (p, "atomic_default_mem_order"))
40212 {
40213 cp_lexer_consume_token (parser->lexer);
40214
40215 matching_parens parens;
40216 if (parens.require_open (parser))
40217 {
40218 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40219 {
40220 id = cp_lexer_peek_token (parser->lexer)->u.value;
40221 p = IDENTIFIER_POINTER (id);
40222
40223 if (!strcmp (p, "seq_cst"))
40224 this_req
40225 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
40226 else if (!strcmp (p, "relaxed"))
40227 this_req
40228 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
40229 else if (!strcmp (p, "acq_rel"))
40230 this_req
40231 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
40232 }
40233 if (this_req == 0)
40234 {
40235 error_at (cp_lexer_peek_token (parser->lexer)->location,
40236 "expected %<seq_cst%>, %<relaxed%> or "
40237 "%<acq_rel%>");
40238 if (cp_lexer_nth_token_is (parser->lexer, 2,
40239 CPP_CLOSE_PAREN))
40240 cp_lexer_consume_token (parser->lexer);
40241 }
40242 else
40243 cp_lexer_consume_token (parser->lexer);
40244
40245 if (!parens.require_close (parser))
40246 cp_parser_skip_to_closing_parenthesis (parser,
40247 /*recovering=*/true,
40248 /*or_comma=*/false,
40249 /*consume_paren=*/
40250 true);
40251
40252 if (this_req == 0)
40253 {
40254 cp_parser_require_pragma_eol (parser, pragma_tok);
40255 return false;
40256 }
40257 }
40258 p = NULL;
40259 }
40260 else
40261 {
40262 error_at (cloc, "expected %<unified_address%>, "
40263 "%<unified_shared_memory%>, "
40264 "%<dynamic_allocators%>, "
40265 "%<reverse_offload%> "
40266 "or %<atomic_default_mem_order%> clause");
40267 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40268 return false;
40269 }
40270 if (p)
40271 sorry_at (cloc, "%qs clause on %<requires%> directive not "
40272 "supported yet", p);
40273 if (p)
40274 cp_lexer_consume_token (parser->lexer);
40275 if (this_req)
40276 {
40277 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
40278 {
40279 if ((this_req & new_req) != 0)
40280 error_at (cloc, "too many %qs clauses", p);
40281 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
40282 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
40283 error_at (cloc, "%qs clause used lexically after first "
40284 "target construct or offloading API", p);
40285 }
40286 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
40287 {
40288 error_at (cloc, "too many %qs clauses",
40289 "atomic_default_mem_order");
40290 this_req = (enum omp_requires) 0;
40291 }
40292 else if ((omp_requires_mask
40293 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
40294 {
40295 error_at (cloc, "more than one %<atomic_default_mem_order%>"
40296 " clause in a single compilation unit");
40297 this_req
40298 = (enum omp_requires)
40299 (omp_requires_mask
40300 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
40301 }
40302 else if ((omp_requires_mask
40303 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
40304 error_at (cloc, "%<atomic_default_mem_order%> clause used "
40305 "lexically after first %<atomic%> construct "
40306 "without memory order clause");
40307 new_req = (enum omp_requires) (new_req | this_req);
40308 omp_requires_mask
40309 = (enum omp_requires) (omp_requires_mask | this_req);
40310 continue;
40311 }
40312 }
40313 break;
40314 }
40315 cp_parser_require_pragma_eol (parser, pragma_tok);
40316
40317 if (new_req == 0)
40318 error_at (loc, "%<pragma omp requires%> requires at least one clause");
40319 return false;
40320 }
40321
40322
40323 /* OpenMP 4.5:
40324 #pragma omp taskloop taskloop-clause[optseq] new-line
40325 for-loop
40326
40327 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
40328 for-loop */
40329
40330 #define OMP_TASKLOOP_CLAUSE_MASK \
40331 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
40332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
40336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
40337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
40338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
40339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
40340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
40342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
40343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
40344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
40345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
40347
40348 static tree
40349 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
40350 char *p_name, omp_clause_mask mask, tree *cclauses,
40351 bool *if_p)
40352 {
40353 tree clauses, sb, ret;
40354 unsigned int save;
40355 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40356
40357 strcat (p_name, " taskloop");
40358 mask |= OMP_TASKLOOP_CLAUSE_MASK;
40359 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
40360 clause. */
40361 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
40362 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
40363
40364 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40365 {
40366 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40367 const char *p = IDENTIFIER_POINTER (id);
40368
40369 if (strcmp (p, "simd") == 0)
40370 {
40371 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40372 if (cclauses == NULL)
40373 cclauses = cclauses_buf;
40374
40375 cp_lexer_consume_token (parser->lexer);
40376 if (!flag_openmp) /* flag_openmp_simd */
40377 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40378 cclauses, if_p);
40379 sb = begin_omp_structured_block ();
40380 save = cp_parser_begin_omp_structured_block (parser);
40381 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40382 cclauses, if_p);
40383 cp_parser_end_omp_structured_block (parser, save);
40384 tree body = finish_omp_structured_block (sb);
40385 if (ret == NULL)
40386 return ret;
40387 ret = make_node (OMP_TASKLOOP);
40388 TREE_TYPE (ret) = void_type_node;
40389 OMP_FOR_BODY (ret) = body;
40390 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
40391 SET_EXPR_LOCATION (ret, loc);
40392 add_stmt (ret);
40393 return ret;
40394 }
40395 }
40396 if (!flag_openmp) /* flag_openmp_simd */
40397 {
40398 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40399 return NULL_TREE;
40400 }
40401
40402 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40403 cclauses == NULL);
40404 if (cclauses)
40405 {
40406 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
40407 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
40408 }
40409
40410 keep_next_level (true);
40411 sb = begin_omp_structured_block ();
40412 save = cp_parser_begin_omp_structured_block (parser);
40413
40414 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
40415 if_p);
40416
40417 cp_parser_end_omp_structured_block (parser, save);
40418 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
40419
40420 return ret;
40421 }
40422
40423
40424 /* OpenACC 2.0:
40425 # pragma acc routine oacc-routine-clause[optseq] new-line
40426 function-definition
40427
40428 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
40429 */
40430
40431 #define OACC_ROUTINE_CLAUSE_MASK \
40432 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
40433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
40434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
40435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
40436
40437
40438 /* Parse the OpenACC routine pragma. This has an optional '( name )'
40439 component, which must resolve to a declared namespace-scope
40440 function. The clauses are either processed directly (for a named
40441 function), or defered until the immediatley following declaration
40442 is parsed. */
40443
40444 static void
40445 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
40446 enum pragma_context context)
40447 {
40448 gcc_checking_assert (context == pragma_external);
40449 /* The checking for "another pragma following this one" in the "no optional
40450 '( name )'" case makes sure that we dont re-enter. */
40451 gcc_checking_assert (parser->oacc_routine == NULL);
40452
40453 cp_oacc_routine_data data;
40454 data.error_seen = false;
40455 data.fndecl_seen = false;
40456 data.tokens = vNULL;
40457 data.clauses = NULL_TREE;
40458 data.loc = pragma_tok->location;
40459 /* It is safe to take the address of a local variable; it will only be
40460 used while this scope is live. */
40461 parser->oacc_routine = &data;
40462
40463 /* Look for optional '( name )'. */
40464 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
40465 {
40466 matching_parens parens;
40467 parens.consume_open (parser); /* '(' */
40468
40469 /* We parse the name as an id-expression. If it resolves to
40470 anything other than a non-overloaded function at namespace
40471 scope, it's an error. */
40472 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
40473 tree name = cp_parser_id_expression (parser,
40474 /*template_keyword_p=*/false,
40475 /*check_dependency_p=*/false,
40476 /*template_p=*/NULL,
40477 /*declarator_p=*/false,
40478 /*optional_p=*/false);
40479 tree decl = (identifier_p (name)
40480 ? cp_parser_lookup_name_simple (parser, name, name_loc)
40481 : name);
40482 if (name != error_mark_node && decl == error_mark_node)
40483 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
40484
40485 if (decl == error_mark_node
40486 || !parens.require_close (parser))
40487 {
40488 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40489 parser->oacc_routine = NULL;
40490 return;
40491 }
40492
40493 data.clauses
40494 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40495 "#pragma acc routine",
40496 cp_lexer_peek_token (parser->lexer));
40497 /* The clauses are in reverse order; fix that to make later diagnostic
40498 emission easier. */
40499 data.clauses = nreverse (data.clauses);
40500
40501 if (decl && is_overloaded_fn (decl)
40502 && (TREE_CODE (decl) != FUNCTION_DECL
40503 || DECL_FUNCTION_TEMPLATE_P (decl)))
40504 {
40505 error_at (name_loc,
40506 "%<#pragma acc routine%> names a set of overloads");
40507 parser->oacc_routine = NULL;
40508 return;
40509 }
40510
40511 /* Perhaps we should use the same rule as declarations in different
40512 namespaces? */
40513 if (!DECL_NAMESPACE_SCOPE_P (decl))
40514 {
40515 error_at (name_loc,
40516 "%qD does not refer to a namespace scope function", decl);
40517 parser->oacc_routine = NULL;
40518 return;
40519 }
40520
40521 if (TREE_CODE (decl) != FUNCTION_DECL)
40522 {
40523 error_at (name_loc, "%qD does not refer to a function", decl);
40524 parser->oacc_routine = NULL;
40525 return;
40526 }
40527
40528 cp_finalize_oacc_routine (parser, decl, false);
40529 parser->oacc_routine = NULL;
40530 }
40531 else /* No optional '( name )'. */
40532 {
40533 /* Store away all pragma tokens. */
40534 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
40535 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
40536 cp_lexer_consume_token (parser->lexer);
40537 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40538 parser->oacc_routine->error_seen = true;
40539 cp_parser_require_pragma_eol (parser, pragma_tok);
40540 struct cp_token_cache *cp
40541 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
40542 parser->oacc_routine->tokens.safe_push (cp);
40543
40544 /* Emit a helpful diagnostic if there's another pragma following this
40545 one. */
40546 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
40547 {
40548 cp_ensure_no_oacc_routine (parser);
40549 data.tokens.release ();
40550 /* ..., and then just keep going. */
40551 return;
40552 }
40553
40554 /* We only have to consider the pragma_external case here. */
40555 cp_parser_declaration (parser);
40556 if (parser->oacc_routine
40557 && !parser->oacc_routine->fndecl_seen)
40558 cp_ensure_no_oacc_routine (parser);
40559 else
40560 parser->oacc_routine = NULL;
40561 data.tokens.release ();
40562 }
40563 }
40564
40565 /* Finalize #pragma acc routine clauses after direct declarator has
40566 been parsed. */
40567
40568 static tree
40569 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
40570 {
40571 struct cp_token_cache *ce;
40572 cp_oacc_routine_data *data = parser->oacc_routine;
40573
40574 if (!data->error_seen && data->fndecl_seen)
40575 {
40576 error_at (data->loc,
40577 "%<#pragma acc routine%> not immediately followed by "
40578 "a single function declaration or definition");
40579 data->error_seen = true;
40580 }
40581 if (data->error_seen)
40582 return attrs;
40583
40584 gcc_checking_assert (data->tokens.length () == 1);
40585 ce = data->tokens[0];
40586
40587 cp_parser_push_lexer_for_tokens (parser, ce);
40588 parser->lexer->in_pragma = true;
40589 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
40590
40591 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
40592 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
40593 parser->oacc_routine->clauses
40594 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40595 "#pragma acc routine", pragma_tok);
40596 /* The clauses are in reverse order; fix that to make later diagnostic
40597 emission easier. */
40598 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
40599 cp_parser_pop_lexer (parser);
40600 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
40601 fndecl_seen. */
40602
40603 return attrs;
40604 }
40605
40606 /* Apply any saved OpenACC routine clauses to a just-parsed
40607 declaration. */
40608
40609 static void
40610 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
40611 {
40612 if (__builtin_expect (parser->oacc_routine != NULL, 0))
40613 {
40614 /* Keep going if we're in error reporting mode. */
40615 if (parser->oacc_routine->error_seen
40616 || fndecl == error_mark_node)
40617 return;
40618
40619 if (parser->oacc_routine->fndecl_seen)
40620 {
40621 error_at (parser->oacc_routine->loc,
40622 "%<#pragma acc routine%> not immediately followed by"
40623 " a single function declaration or definition");
40624 parser->oacc_routine = NULL;
40625 return;
40626 }
40627 if (TREE_CODE (fndecl) != FUNCTION_DECL)
40628 {
40629 cp_ensure_no_oacc_routine (parser);
40630 return;
40631 }
40632
40633 int compatible
40634 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
40635 parser->oacc_routine->loc,
40636 "#pragma acc routine");
40637 if (compatible < 0)
40638 {
40639 parser->oacc_routine = NULL;
40640 return;
40641 }
40642 if (compatible > 0)
40643 {
40644 }
40645 else
40646 {
40647 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
40648 {
40649 error_at (parser->oacc_routine->loc,
40650 TREE_USED (fndecl)
40651 ? G_("%<#pragma acc routine%> must be applied before"
40652 " use")
40653 : G_("%<#pragma acc routine%> must be applied before"
40654 " definition"));
40655 parser->oacc_routine = NULL;
40656 return;
40657 }
40658
40659 /* Set the routine's level of parallelism. */
40660 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
40661 oacc_replace_fn_attrib (fndecl, dims);
40662
40663 /* Add an "omp declare target" attribute. */
40664 DECL_ATTRIBUTES (fndecl)
40665 = tree_cons (get_identifier ("omp declare target"),
40666 parser->oacc_routine->clauses,
40667 DECL_ATTRIBUTES (fndecl));
40668 }
40669
40670 /* Don't unset parser->oacc_routine here: we may still need it to
40671 diagnose wrong usage. But, remember that we've used this "#pragma acc
40672 routine". */
40673 parser->oacc_routine->fndecl_seen = true;
40674 }
40675 }
40676
40677 /* Main entry point to OpenMP statement pragmas. */
40678
40679 static void
40680 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40681 {
40682 tree stmt;
40683 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
40684 omp_clause_mask mask (0);
40685
40686 switch (cp_parser_pragma_kind (pragma_tok))
40687 {
40688 case PRAGMA_OACC_ATOMIC:
40689 cp_parser_omp_atomic (parser, pragma_tok);
40690 return;
40691 case PRAGMA_OACC_CACHE:
40692 stmt = cp_parser_oacc_cache (parser, pragma_tok);
40693 break;
40694 case PRAGMA_OACC_DATA:
40695 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
40696 break;
40697 case PRAGMA_OACC_ENTER_DATA:
40698 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
40699 break;
40700 case PRAGMA_OACC_EXIT_DATA:
40701 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
40702 break;
40703 case PRAGMA_OACC_HOST_DATA:
40704 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
40705 break;
40706 case PRAGMA_OACC_KERNELS:
40707 case PRAGMA_OACC_PARALLEL:
40708 strcpy (p_name, "#pragma acc");
40709 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
40710 if_p);
40711 break;
40712 case PRAGMA_OACC_LOOP:
40713 strcpy (p_name, "#pragma acc");
40714 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
40715 if_p);
40716 break;
40717 case PRAGMA_OACC_UPDATE:
40718 stmt = cp_parser_oacc_update (parser, pragma_tok);
40719 break;
40720 case PRAGMA_OACC_WAIT:
40721 stmt = cp_parser_oacc_wait (parser, pragma_tok);
40722 break;
40723 case PRAGMA_OMP_ATOMIC:
40724 cp_parser_omp_atomic (parser, pragma_tok);
40725 return;
40726 case PRAGMA_OMP_CRITICAL:
40727 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
40728 break;
40729 case PRAGMA_OMP_DISTRIBUTE:
40730 strcpy (p_name, "#pragma omp");
40731 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
40732 if_p);
40733 break;
40734 case PRAGMA_OMP_FOR:
40735 strcpy (p_name, "#pragma omp");
40736 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
40737 if_p);
40738 break;
40739 case PRAGMA_OMP_MASTER:
40740 strcpy (p_name, "#pragma omp");
40741 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
40742 if_p);
40743 break;
40744 case PRAGMA_OMP_PARALLEL:
40745 strcpy (p_name, "#pragma omp");
40746 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
40747 if_p);
40748 break;
40749 case PRAGMA_OMP_SECTIONS:
40750 strcpy (p_name, "#pragma omp");
40751 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
40752 break;
40753 case PRAGMA_OMP_SIMD:
40754 strcpy (p_name, "#pragma omp");
40755 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
40756 if_p);
40757 break;
40758 case PRAGMA_OMP_SINGLE:
40759 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
40760 break;
40761 case PRAGMA_OMP_TASK:
40762 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
40763 break;
40764 case PRAGMA_OMP_TASKGROUP:
40765 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
40766 break;
40767 case PRAGMA_OMP_TASKLOOP:
40768 strcpy (p_name, "#pragma omp");
40769 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
40770 if_p);
40771 break;
40772 case PRAGMA_OMP_TEAMS:
40773 strcpy (p_name, "#pragma omp");
40774 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
40775 if_p);
40776 break;
40777 default:
40778 gcc_unreachable ();
40779 }
40780
40781 protected_set_expr_location (stmt, pragma_tok->location);
40782 }
40783 \f
40784 /* Transactional Memory parsing routines. */
40785
40786 /* Parse a transaction attribute.
40787
40788 txn-attribute:
40789 attribute
40790 [ [ identifier ] ]
40791
40792 We use this instead of cp_parser_attributes_opt for transactions to avoid
40793 the pedwarn in C++98 mode. */
40794
40795 static tree
40796 cp_parser_txn_attribute_opt (cp_parser *parser)
40797 {
40798 cp_token *token;
40799 tree attr_name, attr = NULL;
40800
40801 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
40802 return cp_parser_attributes_opt (parser);
40803
40804 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
40805 return NULL_TREE;
40806 cp_lexer_consume_token (parser->lexer);
40807 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
40808 goto error1;
40809
40810 token = cp_lexer_peek_token (parser->lexer);
40811 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
40812 {
40813 token = cp_lexer_consume_token (parser->lexer);
40814
40815 attr_name = (token->type == CPP_KEYWORD
40816 /* For keywords, use the canonical spelling,
40817 not the parsed identifier. */
40818 ? ridpointers[(int) token->keyword]
40819 : token->u.value);
40820 attr = build_tree_list (attr_name, NULL_TREE);
40821 }
40822 else
40823 cp_parser_error (parser, "expected identifier");
40824
40825 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40826 error1:
40827 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40828 return attr;
40829 }
40830
40831 /* Parse a __transaction_atomic or __transaction_relaxed statement.
40832
40833 transaction-statement:
40834 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40835 compound-statement
40836 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40837 */
40838
40839 static tree
40840 cp_parser_transaction (cp_parser *parser, cp_token *token)
40841 {
40842 unsigned char old_in = parser->in_transaction;
40843 unsigned char this_in = 1, new_in;
40844 enum rid keyword = token->keyword;
40845 tree stmt, attrs, noex;
40846
40847 cp_lexer_consume_token (parser->lexer);
40848
40849 if (keyword == RID_TRANSACTION_RELAXED
40850 || keyword == RID_SYNCHRONIZED)
40851 this_in |= TM_STMT_ATTR_RELAXED;
40852 else
40853 {
40854 attrs = cp_parser_txn_attribute_opt (parser);
40855 if (attrs)
40856 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40857 }
40858
40859 /* Parse a noexcept specification. */
40860 if (keyword == RID_ATOMIC_NOEXCEPT)
40861 noex = boolean_true_node;
40862 else if (keyword == RID_ATOMIC_CANCEL)
40863 {
40864 /* cancel-and-throw is unimplemented. */
40865 sorry ("%<atomic_cancel%>");
40866 noex = NULL_TREE;
40867 }
40868 else
40869 noex = cp_parser_noexcept_specification_opt (parser,
40870 /*require_constexpr=*/true,
40871 /*consumed_expr=*/NULL,
40872 /*return_cond=*/true,
40873 /*friend_p=*/false);
40874
40875 /* Keep track if we're in the lexical scope of an outer transaction. */
40876 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
40877
40878 stmt = begin_transaction_stmt (token->location, NULL, this_in);
40879
40880 parser->in_transaction = new_in;
40881 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
40882 parser->in_transaction = old_in;
40883
40884 finish_transaction_stmt (stmt, NULL, this_in, noex);
40885
40886 return stmt;
40887 }
40888
40889 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40890
40891 transaction-expression:
40892 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40893 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40894 */
40895
40896 static tree
40897 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
40898 {
40899 unsigned char old_in = parser->in_transaction;
40900 unsigned char this_in = 1;
40901 cp_token *token;
40902 tree expr, noex;
40903 bool noex_expr;
40904 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40905
40906 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40907 || keyword == RID_TRANSACTION_RELAXED);
40908
40909 if (!flag_tm)
40910 error_at (loc,
40911 keyword == RID_TRANSACTION_RELAXED
40912 ? G_("%<__transaction_relaxed%> without transactional memory "
40913 "support enabled")
40914 : G_("%<__transaction_atomic%> without transactional memory "
40915 "support enabled"));
40916
40917 token = cp_parser_require_keyword (parser, keyword,
40918 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40919 : RT_TRANSACTION_RELAXED));
40920 gcc_assert (token != NULL);
40921
40922 if (keyword == RID_TRANSACTION_RELAXED)
40923 this_in |= TM_STMT_ATTR_RELAXED;
40924
40925 /* Set this early. This might mean that we allow transaction_cancel in
40926 an expression that we find out later actually has to be a constexpr.
40927 However, we expect that cxx_constant_value will be able to deal with
40928 this; also, if the noexcept has no constexpr, then what we parse next
40929 really is a transaction's body. */
40930 parser->in_transaction = this_in;
40931
40932 /* Parse a noexcept specification. */
40933 noex = cp_parser_noexcept_specification_opt (parser,
40934 /*require_constexpr=*/false,
40935 &noex_expr,
40936 /*return_cond=*/true,
40937 /*friend_p=*/false);
40938
40939 if (!noex || !noex_expr
40940 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
40941 {
40942 matching_parens parens;
40943 parens.require_open (parser);
40944
40945 expr = cp_parser_expression (parser);
40946 expr = finish_parenthesized_expr (expr);
40947
40948 parens.require_close (parser);
40949 }
40950 else
40951 {
40952 /* The only expression that is available got parsed for the noexcept
40953 already. noexcept is true then. */
40954 expr = noex;
40955 noex = boolean_true_node;
40956 }
40957
40958 expr = build_transaction_expr (token->location, expr, this_in, noex);
40959 parser->in_transaction = old_in;
40960
40961 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
40962 return error_mark_node;
40963
40964 return (flag_tm ? expr : error_mark_node);
40965 }
40966
40967 /* Parse a function-transaction-block.
40968
40969 function-transaction-block:
40970 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40971 function-body
40972 __transaction_atomic txn-attribute[opt] function-try-block
40973 __transaction_relaxed ctor-initializer[opt] function-body
40974 __transaction_relaxed function-try-block
40975 */
40976
40977 static void
40978 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
40979 {
40980 unsigned char old_in = parser->in_transaction;
40981 unsigned char new_in = 1;
40982 tree compound_stmt, stmt, attrs;
40983 cp_token *token;
40984
40985 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40986 || keyword == RID_TRANSACTION_RELAXED);
40987 token = cp_parser_require_keyword (parser, keyword,
40988 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40989 : RT_TRANSACTION_RELAXED));
40990 gcc_assert (token != NULL);
40991
40992 if (keyword == RID_TRANSACTION_RELAXED)
40993 new_in |= TM_STMT_ATTR_RELAXED;
40994 else
40995 {
40996 attrs = cp_parser_txn_attribute_opt (parser);
40997 if (attrs)
40998 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40999 }
41000
41001 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
41002
41003 parser->in_transaction = new_in;
41004
41005 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
41006 cp_parser_function_try_block (parser);
41007 else
41008 cp_parser_ctor_initializer_opt_and_function_body
41009 (parser, /*in_function_try_block=*/false);
41010
41011 parser->in_transaction = old_in;
41012
41013 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
41014 }
41015
41016 /* Parse a __transaction_cancel statement.
41017
41018 cancel-statement:
41019 __transaction_cancel txn-attribute[opt] ;
41020 __transaction_cancel txn-attribute[opt] throw-expression ;
41021
41022 ??? Cancel and throw is not yet implemented. */
41023
41024 static tree
41025 cp_parser_transaction_cancel (cp_parser *parser)
41026 {
41027 cp_token *token;
41028 bool is_outer = false;
41029 tree stmt, attrs;
41030
41031 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
41032 RT_TRANSACTION_CANCEL);
41033 gcc_assert (token != NULL);
41034
41035 attrs = cp_parser_txn_attribute_opt (parser);
41036 if (attrs)
41037 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
41038
41039 /* ??? Parse cancel-and-throw here. */
41040
41041 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
41042
41043 if (!flag_tm)
41044 {
41045 error_at (token->location, "%<__transaction_cancel%> without "
41046 "transactional memory support enabled");
41047 return error_mark_node;
41048 }
41049 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
41050 {
41051 error_at (token->location, "%<__transaction_cancel%> within a "
41052 "%<__transaction_relaxed%>");
41053 return error_mark_node;
41054 }
41055 else if (is_outer)
41056 {
41057 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
41058 && !is_tm_may_cancel_outer (current_function_decl))
41059 {
41060 error_at (token->location, "outer %<__transaction_cancel%> not "
41061 "within outer %<__transaction_atomic%>");
41062 error_at (token->location,
41063 " or a %<transaction_may_cancel_outer%> function");
41064 return error_mark_node;
41065 }
41066 }
41067 else if (parser->in_transaction == 0)
41068 {
41069 error_at (token->location, "%<__transaction_cancel%> not within "
41070 "%<__transaction_atomic%>");
41071 return error_mark_node;
41072 }
41073
41074 stmt = build_tm_abort_call (token->location, is_outer);
41075 add_stmt (stmt);
41076
41077 return stmt;
41078 }
41079 \f
41080 /* The parser. */
41081
41082 static GTY (()) cp_parser *the_parser;
41083
41084 \f
41085 /* Special handling for the first token or line in the file. The first
41086 thing in the file might be #pragma GCC pch_preprocess, which loads a
41087 PCH file, which is a GC collection point. So we need to handle this
41088 first pragma without benefit of an existing lexer structure.
41089
41090 Always returns one token to the caller in *FIRST_TOKEN. This is
41091 either the true first token of the file, or the first token after
41092 the initial pragma. */
41093
41094 static void
41095 cp_parser_initial_pragma (cp_token *first_token)
41096 {
41097 tree name = NULL;
41098
41099 cp_lexer_get_preprocessor_token (NULL, first_token);
41100 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
41101 return;
41102
41103 cp_lexer_get_preprocessor_token (NULL, first_token);
41104 if (first_token->type == CPP_STRING)
41105 {
41106 name = first_token->u.value;
41107
41108 cp_lexer_get_preprocessor_token (NULL, first_token);
41109 if (first_token->type != CPP_PRAGMA_EOL)
41110 error_at (first_token->location,
41111 "junk at end of %<#pragma GCC pch_preprocess%>");
41112 }
41113 else
41114 error_at (first_token->location, "expected string literal");
41115
41116 /* Skip to the end of the pragma. */
41117 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
41118 cp_lexer_get_preprocessor_token (NULL, first_token);
41119
41120 /* Now actually load the PCH file. */
41121 if (name)
41122 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
41123
41124 /* Read one more token to return to our caller. We have to do this
41125 after reading the PCH file in, since its pointers have to be
41126 live. */
41127 cp_lexer_get_preprocessor_token (NULL, first_token);
41128 }
41129
41130 /* Parse a pragma GCC ivdep. */
41131
41132 static bool
41133 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
41134 {
41135 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41136 return true;
41137 }
41138
41139 /* Parse a pragma GCC unroll. */
41140
41141 static unsigned short
41142 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
41143 {
41144 location_t location = cp_lexer_peek_token (parser->lexer)->location;
41145 tree expr = cp_parser_constant_expression (parser);
41146 unsigned short unroll;
41147 expr = maybe_constant_value (expr);
41148 HOST_WIDE_INT lunroll = 0;
41149 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
41150 || TREE_CODE (expr) != INTEGER_CST
41151 || (lunroll = tree_to_shwi (expr)) < 0
41152 || lunroll >= USHRT_MAX)
41153 {
41154 error_at (location, "%<#pragma GCC unroll%> requires an"
41155 " assignment-expression that evaluates to a non-negative"
41156 " integral constant less than %u", USHRT_MAX);
41157 unroll = 0;
41158 }
41159 else
41160 {
41161 unroll = (unsigned short)lunroll;
41162 if (unroll == 0)
41163 unroll = 1;
41164 }
41165 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41166 return unroll;
41167 }
41168
41169 /* Normal parsing of a pragma token. Here we can (and must) use the
41170 regular lexer. */
41171
41172 static bool
41173 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
41174 {
41175 cp_token *pragma_tok;
41176 unsigned int id;
41177 tree stmt;
41178 bool ret;
41179
41180 pragma_tok = cp_lexer_consume_token (parser->lexer);
41181 gcc_assert (pragma_tok->type == CPP_PRAGMA);
41182 parser->lexer->in_pragma = true;
41183
41184 id = cp_parser_pragma_kind (pragma_tok);
41185 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
41186 cp_ensure_no_omp_declare_simd (parser);
41187 switch (id)
41188 {
41189 case PRAGMA_GCC_PCH_PREPROCESS:
41190 error_at (pragma_tok->location,
41191 "%<#pragma GCC pch_preprocess%> must be first");
41192 break;
41193
41194 case PRAGMA_OMP_BARRIER:
41195 switch (context)
41196 {
41197 case pragma_compound:
41198 cp_parser_omp_barrier (parser, pragma_tok);
41199 return false;
41200 case pragma_stmt:
41201 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
41202 "used in compound statements", "omp barrier");
41203 break;
41204 default:
41205 goto bad_stmt;
41206 }
41207 break;
41208
41209 case PRAGMA_OMP_DEPOBJ:
41210 switch (context)
41211 {
41212 case pragma_compound:
41213 cp_parser_omp_depobj (parser, pragma_tok);
41214 return false;
41215 case pragma_stmt:
41216 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
41217 "used in compound statements", "omp depobj");
41218 break;
41219 default:
41220 goto bad_stmt;
41221 }
41222 break;
41223
41224 case PRAGMA_OMP_FLUSH:
41225 switch (context)
41226 {
41227 case pragma_compound:
41228 cp_parser_omp_flush (parser, pragma_tok);
41229 return false;
41230 case pragma_stmt:
41231 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
41232 "used in compound statements", "omp flush");
41233 break;
41234 default:
41235 goto bad_stmt;
41236 }
41237 break;
41238
41239 case PRAGMA_OMP_TASKWAIT:
41240 switch (context)
41241 {
41242 case pragma_compound:
41243 cp_parser_omp_taskwait (parser, pragma_tok);
41244 return false;
41245 case pragma_stmt:
41246 error_at (pragma_tok->location,
41247 "%<#pragma %s%> may only be used in compound statements",
41248 "omp taskwait");
41249 break;
41250 default:
41251 goto bad_stmt;
41252 }
41253 break;
41254
41255 case PRAGMA_OMP_TASKYIELD:
41256 switch (context)
41257 {
41258 case pragma_compound:
41259 cp_parser_omp_taskyield (parser, pragma_tok);
41260 return false;
41261 case pragma_stmt:
41262 error_at (pragma_tok->location,
41263 "%<#pragma %s%> may only be used in compound statements",
41264 "omp taskyield");
41265 break;
41266 default:
41267 goto bad_stmt;
41268 }
41269 break;
41270
41271 case PRAGMA_OMP_CANCEL:
41272 switch (context)
41273 {
41274 case pragma_compound:
41275 cp_parser_omp_cancel (parser, pragma_tok);
41276 return false;
41277 case pragma_stmt:
41278 error_at (pragma_tok->location,
41279 "%<#pragma %s%> may only be used in compound statements",
41280 "omp cancel");
41281 break;
41282 default:
41283 goto bad_stmt;
41284 }
41285 break;
41286
41287 case PRAGMA_OMP_CANCELLATION_POINT:
41288 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
41289 return false;
41290
41291 case PRAGMA_OMP_THREADPRIVATE:
41292 cp_parser_omp_threadprivate (parser, pragma_tok);
41293 return false;
41294
41295 case PRAGMA_OMP_DECLARE:
41296 return cp_parser_omp_declare (parser, pragma_tok, context);
41297
41298 case PRAGMA_OACC_DECLARE:
41299 cp_parser_oacc_declare (parser, pragma_tok);
41300 return false;
41301
41302 case PRAGMA_OACC_ENTER_DATA:
41303 if (context == pragma_stmt)
41304 {
41305 error_at (pragma_tok->location,
41306 "%<#pragma %s%> may only be used in compound statements",
41307 "acc enter data");
41308 break;
41309 }
41310 else if (context != pragma_compound)
41311 goto bad_stmt;
41312 cp_parser_omp_construct (parser, pragma_tok, if_p);
41313 return true;
41314
41315 case PRAGMA_OACC_EXIT_DATA:
41316 if (context == pragma_stmt)
41317 {
41318 error_at (pragma_tok->location,
41319 "%<#pragma %s%> may only be used in compound statements",
41320 "acc exit data");
41321 break;
41322 }
41323 else if (context != pragma_compound)
41324 goto bad_stmt;
41325 cp_parser_omp_construct (parser, pragma_tok, if_p);
41326 return true;
41327
41328 case PRAGMA_OACC_ROUTINE:
41329 if (context != pragma_external)
41330 {
41331 error_at (pragma_tok->location,
41332 "%<#pragma acc routine%> must be at file scope");
41333 break;
41334 }
41335 cp_parser_oacc_routine (parser, pragma_tok, context);
41336 return false;
41337
41338 case PRAGMA_OACC_UPDATE:
41339 if (context == pragma_stmt)
41340 {
41341 error_at (pragma_tok->location,
41342 "%<#pragma %s%> may only be used in compound statements",
41343 "acc update");
41344 break;
41345 }
41346 else if (context != pragma_compound)
41347 goto bad_stmt;
41348 cp_parser_omp_construct (parser, pragma_tok, if_p);
41349 return true;
41350
41351 case PRAGMA_OACC_WAIT:
41352 if (context == pragma_stmt)
41353 {
41354 error_at (pragma_tok->location,
41355 "%<#pragma %s%> may only be used in compound statements",
41356 "acc wait");
41357 break;
41358 }
41359 else if (context != pragma_compound)
41360 goto bad_stmt;
41361 cp_parser_omp_construct (parser, pragma_tok, if_p);
41362 return true;
41363
41364 case PRAGMA_OACC_ATOMIC:
41365 case PRAGMA_OACC_CACHE:
41366 case PRAGMA_OACC_DATA:
41367 case PRAGMA_OACC_HOST_DATA:
41368 case PRAGMA_OACC_KERNELS:
41369 case PRAGMA_OACC_PARALLEL:
41370 case PRAGMA_OACC_LOOP:
41371 case PRAGMA_OMP_ATOMIC:
41372 case PRAGMA_OMP_CRITICAL:
41373 case PRAGMA_OMP_DISTRIBUTE:
41374 case PRAGMA_OMP_FOR:
41375 case PRAGMA_OMP_MASTER:
41376 case PRAGMA_OMP_PARALLEL:
41377 case PRAGMA_OMP_SECTIONS:
41378 case PRAGMA_OMP_SIMD:
41379 case PRAGMA_OMP_SINGLE:
41380 case PRAGMA_OMP_TASK:
41381 case PRAGMA_OMP_TASKGROUP:
41382 case PRAGMA_OMP_TASKLOOP:
41383 case PRAGMA_OMP_TEAMS:
41384 if (context != pragma_stmt && context != pragma_compound)
41385 goto bad_stmt;
41386 stmt = push_omp_privatization_clauses (false);
41387 cp_parser_omp_construct (parser, pragma_tok, if_p);
41388 pop_omp_privatization_clauses (stmt);
41389 return true;
41390
41391 case PRAGMA_OMP_REQUIRES:
41392 return cp_parser_omp_requires (parser, pragma_tok);
41393
41394 case PRAGMA_OMP_ORDERED:
41395 if (context != pragma_stmt && context != pragma_compound)
41396 goto bad_stmt;
41397 stmt = push_omp_privatization_clauses (false);
41398 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
41399 pop_omp_privatization_clauses (stmt);
41400 return ret;
41401
41402 case PRAGMA_OMP_TARGET:
41403 if (context != pragma_stmt && context != pragma_compound)
41404 goto bad_stmt;
41405 stmt = push_omp_privatization_clauses (false);
41406 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
41407 pop_omp_privatization_clauses (stmt);
41408 return ret;
41409
41410 case PRAGMA_OMP_END_DECLARE_TARGET:
41411 cp_parser_omp_end_declare_target (parser, pragma_tok);
41412 return false;
41413
41414 case PRAGMA_OMP_SCAN:
41415 error_at (pragma_tok->location,
41416 "%<#pragma omp scan%> may only be used in "
41417 "a loop construct with %<inscan%> %<reduction%> clause");
41418 break;
41419
41420 case PRAGMA_OMP_SECTION:
41421 error_at (pragma_tok->location,
41422 "%<#pragma omp section%> may only be used in "
41423 "%<#pragma omp sections%> construct");
41424 break;
41425
41426 case PRAGMA_IVDEP:
41427 {
41428 if (context == pragma_external)
41429 {
41430 error_at (pragma_tok->location,
41431 "%<#pragma GCC ivdep%> must be inside a function");
41432 break;
41433 }
41434 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
41435 unsigned short unroll;
41436 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41437 if (tok->type == CPP_PRAGMA
41438 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
41439 {
41440 tok = cp_lexer_consume_token (parser->lexer);
41441 unroll = cp_parser_pragma_unroll (parser, tok);
41442 tok = cp_lexer_peek_token (the_parser->lexer);
41443 }
41444 else
41445 unroll = 0;
41446 if (tok->type != CPP_KEYWORD
41447 || (tok->keyword != RID_FOR
41448 && tok->keyword != RID_WHILE
41449 && tok->keyword != RID_DO))
41450 {
41451 cp_parser_error (parser, "for, while or do statement expected");
41452 return false;
41453 }
41454 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
41455 return true;
41456 }
41457
41458 case PRAGMA_UNROLL:
41459 {
41460 if (context == pragma_external)
41461 {
41462 error_at (pragma_tok->location,
41463 "%<#pragma GCC unroll%> must be inside a function");
41464 break;
41465 }
41466 const unsigned short unroll
41467 = cp_parser_pragma_unroll (parser, pragma_tok);
41468 bool ivdep;
41469 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41470 if (tok->type == CPP_PRAGMA
41471 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
41472 {
41473 tok = cp_lexer_consume_token (parser->lexer);
41474 ivdep = cp_parser_pragma_ivdep (parser, tok);
41475 tok = cp_lexer_peek_token (the_parser->lexer);
41476 }
41477 else
41478 ivdep = false;
41479 if (tok->type != CPP_KEYWORD
41480 || (tok->keyword != RID_FOR
41481 && tok->keyword != RID_WHILE
41482 && tok->keyword != RID_DO))
41483 {
41484 cp_parser_error (parser, "for, while or do statement expected");
41485 return false;
41486 }
41487 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
41488 return true;
41489 }
41490
41491 default:
41492 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
41493 c_invoke_pragma_handler (id);
41494 break;
41495
41496 bad_stmt:
41497 cp_parser_error (parser, "expected declaration specifiers");
41498 break;
41499 }
41500
41501 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41502 return false;
41503 }
41504
41505 /* The interface the pragma parsers have to the lexer. */
41506
41507 enum cpp_ttype
41508 pragma_lex (tree *value, location_t *loc)
41509 {
41510 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41511 enum cpp_ttype ret = tok->type;
41512
41513 *value = tok->u.value;
41514 if (loc)
41515 *loc = tok->location;
41516
41517 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
41518 ret = CPP_EOF;
41519 else if (ret == CPP_STRING)
41520 *value = cp_parser_string_literal (the_parser, false, false);
41521 else
41522 {
41523 if (ret == CPP_KEYWORD)
41524 ret = CPP_NAME;
41525 cp_lexer_consume_token (the_parser->lexer);
41526 }
41527
41528 return ret;
41529 }
41530
41531 \f
41532 /* External interface. */
41533
41534 /* Parse one entire translation unit. */
41535
41536 void
41537 c_parse_file (void)
41538 {
41539 static bool already_called = false;
41540
41541 if (already_called)
41542 fatal_error (input_location,
41543 "inter-module optimizations not implemented for C++");
41544 already_called = true;
41545
41546 the_parser = cp_parser_new ();
41547 push_deferring_access_checks (flag_access_control
41548 ? dk_no_deferred : dk_no_check);
41549 cp_parser_translation_unit (the_parser);
41550 the_parser = NULL;
41551
41552 finish_translation_unit ();
41553 }
41554
41555 /* Create an identifier for a generic parameter type (a synthesized
41556 template parameter implied by `auto' or a concept identifier). */
41557
41558 static GTY(()) int generic_parm_count;
41559 static tree
41560 make_generic_type_name ()
41561 {
41562 char buf[32];
41563 sprintf (buf, "auto:%d", ++generic_parm_count);
41564 return get_identifier (buf);
41565 }
41566
41567 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
41568 (creating a new template parameter list if necessary). Returns the newly
41569 created template type parm. */
41570
41571 static tree
41572 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
41573 {
41574 gcc_assert (current_binding_level->kind == sk_function_parms);
41575
41576 /* Before committing to modifying any scope, if we're in an
41577 implicit template scope, and we're trying to synthesize a
41578 constrained parameter, try to find a previous parameter with
41579 the same name. This is the same-type rule for abbreviated
41580 function templates.
41581
41582 NOTE: We can generate implicit parameters when tentatively
41583 parsing a nested name specifier, only to reject that parse
41584 later. However, matching the same template-id as part of a
41585 direct-declarator should generate an identical template
41586 parameter, so this rule will merge them. */
41587 if (parser->implicit_template_scope && constr)
41588 {
41589 tree t = parser->implicit_template_parms;
41590 while (t)
41591 {
41592 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
41593 {
41594 tree d = TREE_VALUE (t);
41595 if (TREE_CODE (d) == PARM_DECL)
41596 /* Return the TEMPLATE_PARM_INDEX. */
41597 d = DECL_INITIAL (d);
41598 return d;
41599 }
41600 t = TREE_CHAIN (t);
41601 }
41602 }
41603
41604 /* We are either continuing a function template that already contains implicit
41605 template parameters, creating a new fully-implicit function template, or
41606 extending an existing explicit function template with implicit template
41607 parameters. */
41608
41609 cp_binding_level *const entry_scope = current_binding_level;
41610
41611 bool become_template = false;
41612 cp_binding_level *parent_scope = 0;
41613
41614 if (parser->implicit_template_scope)
41615 {
41616 gcc_assert (parser->implicit_template_parms);
41617
41618 current_binding_level = parser->implicit_template_scope;
41619 }
41620 else
41621 {
41622 /* Roll back to the existing template parameter scope (in the case of
41623 extending an explicit function template) or introduce a new template
41624 parameter scope ahead of the function parameter scope (or class scope
41625 in the case of out-of-line member definitions). The function scope is
41626 added back after template parameter synthesis below. */
41627
41628 cp_binding_level *scope = entry_scope;
41629
41630 while (scope->kind == sk_function_parms)
41631 {
41632 parent_scope = scope;
41633 scope = scope->level_chain;
41634 }
41635 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
41636 {
41637 /* If not defining a class, then any class scope is a scope level in
41638 an out-of-line member definition. In this case simply wind back
41639 beyond the first such scope to inject the template parameter list.
41640 Otherwise wind back to the class being defined. The latter can
41641 occur in class member friend declarations such as:
41642
41643 class A {
41644 void foo (auto);
41645 };
41646 class B {
41647 friend void A::foo (auto);
41648 };
41649
41650 The template parameter list synthesized for the friend declaration
41651 must be injected in the scope of 'B'. This can also occur in
41652 erroneous cases such as:
41653
41654 struct A {
41655 struct B {
41656 void foo (auto);
41657 };
41658 void B::foo (auto) {}
41659 };
41660
41661 Here the attempted definition of 'B::foo' within 'A' is ill-formed
41662 but, nevertheless, the template parameter list synthesized for the
41663 declarator should be injected into the scope of 'A' as if the
41664 ill-formed template was specified explicitly. */
41665
41666 while (scope->kind == sk_class && !scope->defining_class_p)
41667 {
41668 parent_scope = scope;
41669 scope = scope->level_chain;
41670 }
41671 }
41672
41673 current_binding_level = scope;
41674
41675 if (scope->kind != sk_template_parms
41676 || !function_being_declared_is_template_p (parser))
41677 {
41678 /* Introduce a new template parameter list for implicit template
41679 parameters. */
41680
41681 become_template = true;
41682
41683 parser->implicit_template_scope
41684 = begin_scope (sk_template_parms, NULL);
41685
41686 ++processing_template_decl;
41687
41688 parser->fully_implicit_function_template_p = true;
41689 ++parser->num_template_parameter_lists;
41690 }
41691 else
41692 {
41693 /* Synthesize implicit template parameters at the end of the explicit
41694 template parameter list. */
41695
41696 gcc_assert (current_template_parms);
41697
41698 parser->implicit_template_scope = scope;
41699
41700 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41701 parser->implicit_template_parms
41702 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
41703 }
41704 }
41705
41706 /* Synthesize a new template parameter and track the current template
41707 parameter chain with implicit_template_parms. */
41708
41709 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
41710 tree synth_id = make_generic_type_name ();
41711 tree synth_tmpl_parm;
41712 bool non_type = false;
41713
41714 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
41715 synth_tmpl_parm
41716 = finish_template_type_parm (class_type_node, synth_id);
41717 else if (TREE_CODE (proto) == TEMPLATE_DECL)
41718 synth_tmpl_parm
41719 = finish_constrained_template_template_parm (proto, synth_id);
41720 else
41721 {
41722 synth_tmpl_parm = copy_decl (proto);
41723 DECL_NAME (synth_tmpl_parm) = synth_id;
41724 non_type = true;
41725 }
41726
41727 // Attach the constraint to the parm before processing.
41728 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
41729 TREE_TYPE (node) = constr;
41730 tree new_parm
41731 = process_template_parm (parser->implicit_template_parms,
41732 input_location,
41733 node,
41734 /*non_type=*/non_type,
41735 /*param_pack=*/false);
41736
41737 // Chain the new parameter to the list of implicit parameters.
41738 if (parser->implicit_template_parms)
41739 parser->implicit_template_parms
41740 = TREE_CHAIN (parser->implicit_template_parms);
41741 else
41742 parser->implicit_template_parms = new_parm;
41743
41744 tree new_decl = get_local_decls ();
41745 if (non_type)
41746 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
41747 new_decl = DECL_INITIAL (new_decl);
41748
41749 /* If creating a fully implicit function template, start the new implicit
41750 template parameter list with this synthesized type, otherwise grow the
41751 current template parameter list. */
41752
41753 if (become_template)
41754 {
41755 parent_scope->level_chain = current_binding_level;
41756
41757 tree new_parms = make_tree_vec (1);
41758 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
41759 current_template_parms = tree_cons (size_int (processing_template_decl),
41760 new_parms, current_template_parms);
41761 }
41762 else
41763 {
41764 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41765 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
41766 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
41767 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
41768 }
41769
41770 // If the new parameter was constrained, we need to add that to the
41771 // constraints in the template parameter list.
41772 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
41773 {
41774 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
41775 reqs = conjoin_constraints (reqs, req);
41776 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
41777 }
41778
41779 current_binding_level = entry_scope;
41780
41781 return new_decl;
41782 }
41783
41784 /* Finish the declaration of a fully implicit function template. Such a
41785 template has no explicit template parameter list so has not been through the
41786 normal template head and tail processing. synthesize_implicit_template_parm
41787 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
41788 provided if the declaration is a class member such that its template
41789 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
41790 form is returned. Otherwise NULL_TREE is returned. */
41791
41792 static tree
41793 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
41794 {
41795 gcc_assert (parser->fully_implicit_function_template_p);
41796
41797 if (member_decl_opt && member_decl_opt != error_mark_node
41798 && DECL_VIRTUAL_P (member_decl_opt))
41799 {
41800 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
41801 "implicit templates may not be %<virtual%>");
41802 DECL_VIRTUAL_P (member_decl_opt) = false;
41803 }
41804
41805 if (member_decl_opt)
41806 member_decl_opt = finish_member_template_decl (member_decl_opt);
41807 end_template_decl ();
41808
41809 parser->fully_implicit_function_template_p = false;
41810 parser->implicit_template_parms = 0;
41811 parser->implicit_template_scope = 0;
41812 --parser->num_template_parameter_lists;
41813
41814 return member_decl_opt;
41815 }
41816
41817 /* Like finish_fully_implicit_template, but to be used in error
41818 recovery, rearranging scopes so that we restore the state we had
41819 before synthesize_implicit_template_parm inserted the implement
41820 template parms scope. */
41821
41822 static void
41823 abort_fully_implicit_template (cp_parser *parser)
41824 {
41825 cp_binding_level *return_to_scope = current_binding_level;
41826
41827 if (parser->implicit_template_scope
41828 && return_to_scope != parser->implicit_template_scope)
41829 {
41830 cp_binding_level *child = return_to_scope;
41831 for (cp_binding_level *scope = child->level_chain;
41832 scope != parser->implicit_template_scope;
41833 scope = child->level_chain)
41834 child = scope;
41835 child->level_chain = parser->implicit_template_scope->level_chain;
41836 parser->implicit_template_scope->level_chain = return_to_scope;
41837 current_binding_level = parser->implicit_template_scope;
41838 }
41839 else
41840 return_to_scope = return_to_scope->level_chain;
41841
41842 finish_fully_implicit_template (parser, NULL);
41843
41844 gcc_assert (current_binding_level == return_to_scope);
41845 }
41846
41847 /* Helper function for diagnostics that have complained about things
41848 being used with 'extern "C"' linkage.
41849
41850 Attempt to issue a note showing where the 'extern "C"' linkage began. */
41851
41852 void
41853 maybe_show_extern_c_location (void)
41854 {
41855 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
41856 inform (the_parser->innermost_linkage_specification_location,
41857 "%<extern \"C\"%> linkage started here");
41858 }
41859
41860 #include "gt-cp-parser.h"